Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

CosmosDB unit tests optional (if emulator is installed). Temporary. #1026

Merged
merged 1 commit into from
Mar 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.microsoft.azure.documentdb.DocumentClientException;
import com.microsoft.bot.builder.Storage;
import com.microsoft.bot.builder.StorageBaseTests;
import java.io.File;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
Expand Down Expand Up @@ -47,20 +48,24 @@ public class CosmosDbPartitionStorageTests extends StorageBaseTests {

@BeforeClass
public static void allTestsInit() throws IOException, InterruptedException, DocumentClientException {
Process p = Runtime.getRuntime().exec
("cmd /C \"" + System.getenv("ProgramFiles") + "\\Azure Cosmos DB Emulator\\CosmosDB.Emulator.exe\" /GetStatus");

int result = p.waitFor();
if (result == 2) {
emulatorIsRunning = true;

DocumentClient client = new DocumentClient(
CosmosServiceEndpoint,
CosmosAuthKey,
ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);

createDatabaseIfNeeded(client);
File emulator = new File(System.getenv("ProgramFiles") + "\\Azure Cosmos DB Emulator\\CosmosDB.Emulator.exe");
if (emulator.exists()) {
Process p = Runtime.getRuntime().exec
("cmd /C \"" + emulator.getAbsolutePath() + " /GetStatus");

int result = p.waitFor();
if (result == 2) {
emulatorIsRunning = true;

DocumentClient client = new DocumentClient(
CosmosServiceEndpoint,
CosmosAuthKey,
ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session
);

createDatabaseIfNeeded(client);
}
}
}

Expand All @@ -85,12 +90,14 @@ public static void allTestCleanup() throws DocumentClientException {

@Before
public void testInit() {
storage = new CosmosDbPartitionedStorage(new CosmosDbPartitionedStorageOptions() {{
setAuthKey(CosmosAuthKey);
setContainerId(CosmosCollectionName);
setCosmosDbEndpoint(CosmosServiceEndpoint);
setDatabaseId(CosmosDatabaseName);
}});
if (emulatorIsRunning) {
storage = new CosmosDbPartitionedStorage(new CosmosDbPartitionedStorageOptions() {{
setAuthKey(CosmosAuthKey);
setContainerId(CosmosCollectionName);
setCosmosDbEndpoint(CosmosServiceEndpoint);
setDatabaseId(CosmosDatabaseName);
}});
}
}

@After
Expand Down Expand Up @@ -186,65 +193,83 @@ public void constructorShouldThrowOnInvalidOptions() {
// NOTE: THESE TESTS REQUIRE THAT THE COSMOS DB EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
@Test
public void createObjectCosmosDBPartitionTest() {
assertEmulator();
super.createObjectTest(storage);
if (runIfEmulator()) {
super.createObjectTest(storage);
}
}

// NOTE: THESE TESTS REQUIRE THAT THE COSMOS DB EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
@Test
public void readUnknownCosmosDBPartitionTest() {
assertEmulator();
super.readUnknownTest(storage);
if (runIfEmulator()) {
super.readUnknownTest(storage);
}
}

// NOTE: THESE TESTS REQUIRE THAT THE COSMOS DB EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
@Test
public void updateObjectCosmosDBPartitionTest() {
assertEmulator();
super.updateObjectTest(storage);
if (runIfEmulator()) {
super.updateObjectTest(storage);
}
}

// NOTE: THESE TESTS REQUIRE THAT THE COSMOS DB EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
@Test
public void deleteObjectCosmosDBPartitionTest() {
assertEmulator();
super.deleteObjectTest(storage);
if (runIfEmulator()) {
super.deleteObjectTest(storage);
}
}

// NOTE: THESE TESTS REQUIRE THAT THE COSMOS DB EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
@Test
public void deleteUnknownObjectCosmosDBPartitionTest() {
assertEmulator();
storage.delete(new String[] {"unknown_delete"});
if (runIfEmulator()) {
storage.delete(new String[]{"unknown_delete"});
}
}

// NOTE: THESE TESTS REQUIRE THAT THE COSMOS DB EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
@Test
public void handleCrazyKeysCosmosDBPartition() {
assertEmulator();
super.handleCrazyKeys(storage);
if (runIfEmulator()) {
super.handleCrazyKeys(storage);
}
}

@Test
public void readingEmptyKeysReturnsEmptyDictionary() {
Map<String, Object> state = storage.read(new String[] {}).join();
Assert.assertNotNull(state);
Assert.assertEquals(0, state.size());
if (runIfEmulator()) {
Map<String, Object> state = storage.read(new String[]{}).join();
Assert.assertNotNull(state);
Assert.assertEquals(0, state.size());
}
}

@Test(expected = IllegalArgumentException.class)
public void readingNullKeysThrowException() {
storage.read(null).join();
if (runIfEmulator()) {
storage.read(null).join();
} else {
throw new IllegalArgumentException("bogus exception");
}
}

@Test(expected = IllegalArgumentException.class)
public void writingNullStoreItemsThrowException() {
storage.write(null);
if (runIfEmulator()) {
storage.write(null);
} else {
throw new IllegalArgumentException("bogus exception");
}
}

@Test
public void writingNoStoreItemsDoesntThrow() {
storage.write(new HashMap<>());
if (runIfEmulator()) {
storage.write(new HashMap<>());
}
}

private static void createDatabaseIfNeeded(DocumentClient client) throws DocumentClientException {
Expand All @@ -264,9 +289,12 @@ private static void createDatabaseIfNeeded(DocumentClient client) throws Documen
}
}

private void assertEmulator() {
private boolean runIfEmulator() {
if (!emulatorIsRunning) {
Assert.fail(NO_EMULATOR_MESSAGE);
System.out.println(NO_EMULATOR_MESSAGE);
return false;
}

return true;
}
}