Search in sources :

Example 11 with CosmosContainerResponse

use of com.azure.cosmos.models.CosmosContainerResponse in project testcontainers-java by testcontainers.

the class CosmosDBEmulatorContainerTest method testWithCosmosClient.

// }
@Test
public void testWithCosmosClient() throws Exception {
    // buildAndSaveNewKeyStore {
    Path keyStoreFile = tempFolder.newFile("azure-cosmos-emulator.keystore").toPath();
    KeyStore keyStore = emulator.buildNewKeyStore();
    keyStore.store(new FileOutputStream(keyStoreFile.toFile()), emulator.getEmulatorKey().toCharArray());
    // }
    // setSystemTrustStoreParameters {
    System.setProperty("javax.net.ssl.trustStore", keyStoreFile.toString());
    System.setProperty("javax.net.ssl.trustStorePassword", emulator.getEmulatorKey());
    System.setProperty("javax.net.ssl.trustStoreType", "PKCS12");
    // }
    // buildClient {
    CosmosAsyncClient client = new CosmosClientBuilder().gatewayMode().endpointDiscoveryEnabled(false).endpoint(emulator.getEmulatorEndpoint()).key(emulator.getEmulatorKey()).buildAsyncClient();
    // }
    // testWithClientAgainstEmulatorContainer {
    CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists("Azure").block();
    Assertions.assertThat(databaseResponse.getStatusCode()).isEqualTo(201);
    CosmosContainerResponse containerResponse = client.getDatabase("Azure").createContainerIfNotExists("ServiceContainer", "/name").block();
    Assertions.assertThat(containerResponse.getStatusCode()).isEqualTo(201);
// }
}
Also used : Path(java.nio.file.Path) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) FileOutputStream(java.io.FileOutputStream) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) KeyStore(java.security.KeyStore) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) Test(org.junit.Test)

Example 12 with CosmosContainerResponse

use of com.azure.cosmos.models.CosmosContainerResponse in project DataSpaceConnector by eclipse-dataspaceconnector.

the class CosmosContractDefinitionStoreIntegrationTest method setUp.

@BeforeEach
void setUp() {
    var containerName = CONTAINER_PREFIX + UUID.randomUUID();
    CosmosContainerResponse containerIfNotExists = database.createContainerIfNotExists(containerName, "/partitionKey");
    container = database.getContainer(containerIfNotExists.getProperties().getId());
    assertThat(database).describedAs("CosmosDB database is null - did something go wrong during initialization?").isNotNull();
    var cosmosDbApi = new CosmosDbApiImpl(container, true);
    store = new CosmosContractDefinitionStore(cosmosDbApi, typeManager, new RetryPolicy<>().withMaxRetries(3).withBackoff(1, 5, ChronoUnit.SECONDS), TEST_PARTITION_KEY);
}
Also used : CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) RetryPolicy(net.jodah.failsafe.RetryPolicy) CosmosDbApiImpl(org.eclipse.dataspaceconnector.azure.cosmos.CosmosDbApiImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 13 with CosmosContainerResponse

use of com.azure.cosmos.models.CosmosContainerResponse in project DataSpaceConnector by eclipse-dataspaceconnector.

the class CosmosPolicyStoreIntegrationTest method setUp.

@BeforeEach
void setUp() {
    var containerName = CONTAINER_PREFIX + UUID.randomUUID();
    CosmosContainerResponse containerIfNotExists = database.createContainerIfNotExists(containerName, "/partitionKey");
    container = database.getContainer(containerIfNotExists.getProperties().getId());
    assertThat(database).describedAs("CosmosDB database is null - did something go wrong during initialization?").isNotNull();
    var cosmosDbApi = new CosmosDbApiImpl(container, true);
    store = new CosmosPolicyStore(cosmosDbApi, typeManager, new RetryPolicy<>().withMaxRetries(3).withBackoff(1, 5, ChronoUnit.SECONDS), TEST_PARTITION_KEY);
}
Also used : CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) RetryPolicy(net.jodah.failsafe.RetryPolicy) CosmosDbApiImpl(org.eclipse.dataspaceconnector.azure.cosmos.CosmosDbApiImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with CosmosContainerResponse

use of com.azure.cosmos.models.CosmosContainerResponse in project scalardb by scalar-labs.

the class CosmosAdminTest method createIndex_ShouldCreateIndexProperly.

@Test
public void createIndex_ShouldCreateIndexProperly() throws ExecutionException {
    // Arrange
    String namespace = "ns";
    String table = "sample_table";
    when(client.getDatabase(namespace)).thenReturn(database);
    CosmosContainerResponse response = mock(CosmosContainerResponse.class);
    when(database.createContainerIfNotExists(table, "/concatenatedPartitionKey")).thenReturn(response);
    CosmosContainerProperties properties = mock(CosmosContainerProperties.class);
    when(response.getProperties()).thenReturn(properties);
    when(database.getContainer(table)).thenReturn(container);
    // for metadata table
    CosmosDatabase metadataDatabase = mock(CosmosDatabase.class);
    CosmosContainer metadataContainer = mock(CosmosContainer.class);
    when(client.getDatabase(CosmosAdmin.METADATA_DATABASE)).thenReturn(metadataDatabase);
    when(metadataDatabase.getContainer(CosmosAdmin.METADATA_CONTAINER)).thenReturn(metadataContainer);
    @SuppressWarnings("unchecked") CosmosItemResponse<CosmosTableMetadata> itemResponse = mock(CosmosItemResponse.class);
    when(metadataContainer.readItem(anyString(), any(PartitionKey.class), ArgumentMatchers.<Class<CosmosTableMetadata>>any())).thenReturn(itemResponse);
    CosmosTableMetadata cosmosTableMetadata = new CosmosTableMetadata();
    cosmosTableMetadata.setId(getFullTableName(namespace, table));
    cosmosTableMetadata.setColumns(ImmutableMap.of("c1", "int", "c2", "text", "c3", "bigint"));
    cosmosTableMetadata.setPartitionKeyNames(Collections.singletonList("c1"));
    cosmosTableMetadata.setClusteringKeyNames(Collections.emptyList());
    cosmosTableMetadata.setClusteringOrders(Collections.emptyMap());
    cosmosTableMetadata.setSecondaryIndexNames(ImmutableSet.of("c2"));
    when(itemResponse.getItem()).thenReturn(cosmosTableMetadata);
    // Act
    admin.createIndex(namespace, table, "c3");
    // Assert
    verify(database).createContainerIfNotExists(table, "/concatenatedPartitionKey");
    ArgumentCaptor<IndexingPolicy> indexingPolicyCaptor = ArgumentCaptor.forClass(IndexingPolicy.class);
    verify(properties).setIndexingPolicy(indexingPolicyCaptor.capture());
    IndexingPolicy indexingPolicy = indexingPolicyCaptor.getValue();
    assertThat(indexingPolicy.getIncludedPaths().size()).isEqualTo(3);
    assertThat(indexingPolicy.getIncludedPaths().get(0).getPath()).isEqualTo("/concatenatedPartitionKey/?");
    assertThat(indexingPolicy.getIncludedPaths().get(1).getPath()).isEqualTo("/values/c3/?");
    assertThat(indexingPolicy.getIncludedPaths().get(2).getPath()).isEqualTo("/values/c2/?");
    assertThat(indexingPolicy.getExcludedPaths().size()).isEqualTo(1);
    assertThat(indexingPolicy.getExcludedPaths().get(0).getPath()).isEqualTo("/*");
    assertThat(indexingPolicy.getCompositeIndexes()).isEmpty();
    verify(container).replace(properties);
    // for metadata table
    CosmosTableMetadata expected = new CosmosTableMetadata();
    expected.setId(getFullTableName(namespace, table));
    expected.setColumns(ImmutableMap.of("c1", "int", "c2", "text", "c3", "bigint"));
    expected.setPartitionKeyNames(Collections.singletonList("c1"));
    expected.setClusteringKeyNames(Collections.emptyList());
    expected.setClusteringOrders(Collections.emptyMap());
    expected.setSecondaryIndexNames(ImmutableSet.of("c2", "c3"));
    verify(metadataContainer).upsertItem(expected);
}
Also used : CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) PartitionKey(com.azure.cosmos.models.PartitionKey) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) Test(org.junit.jupiter.api.Test)

Example 15 with CosmosContainerResponse

use of com.azure.cosmos.models.CosmosContainerResponse in project scalardb by scalar-labs.

the class CosmosAdminTest method dropIndex_ShouldDropIndexProperly.

@Test
public void dropIndex_ShouldDropIndexProperly() throws ExecutionException {
    // Arrange
    String namespace = "ns";
    String table = "sample_table";
    when(client.getDatabase(namespace)).thenReturn(database);
    CosmosContainerResponse response = mock(CosmosContainerResponse.class);
    when(database.createContainerIfNotExists(table, "/concatenatedPartitionKey")).thenReturn(response);
    CosmosContainerProperties properties = mock(CosmosContainerProperties.class);
    when(response.getProperties()).thenReturn(properties);
    when(database.getContainer(table)).thenReturn(container);
    // for metadata table
    CosmosDatabase metadataDatabase = mock(CosmosDatabase.class);
    CosmosContainer metadataContainer = mock(CosmosContainer.class);
    when(client.getDatabase(CosmosAdmin.METADATA_DATABASE)).thenReturn(metadataDatabase);
    when(metadataDatabase.getContainer(CosmosAdmin.METADATA_CONTAINER)).thenReturn(metadataContainer);
    @SuppressWarnings("unchecked") CosmosItemResponse<CosmosTableMetadata> itemResponse = mock(CosmosItemResponse.class);
    when(metadataContainer.readItem(anyString(), any(PartitionKey.class), ArgumentMatchers.<Class<CosmosTableMetadata>>any())).thenReturn(itemResponse);
    CosmosTableMetadata cosmosTableMetadata = new CosmosTableMetadata();
    cosmosTableMetadata.setId(getFullTableName(namespace, table));
    cosmosTableMetadata.setColumns(ImmutableMap.of("c1", "int", "c2", "text", "c3", "bigint"));
    cosmosTableMetadata.setPartitionKeyNames(Collections.singletonList("c1"));
    cosmosTableMetadata.setClusteringKeyNames(Collections.emptyList());
    cosmosTableMetadata.setClusteringOrders(Collections.emptyMap());
    cosmosTableMetadata.setSecondaryIndexNames(ImmutableSet.of("c2", "c3"));
    when(itemResponse.getItem()).thenReturn(cosmosTableMetadata);
    // Act
    admin.dropIndex(namespace, table, "c2");
    // Assert
    verify(database).createContainerIfNotExists(table, "/concatenatedPartitionKey");
    ArgumentCaptor<IndexingPolicy> indexingPolicyCaptor = ArgumentCaptor.forClass(IndexingPolicy.class);
    verify(properties).setIndexingPolicy(indexingPolicyCaptor.capture());
    IndexingPolicy indexingPolicy = indexingPolicyCaptor.getValue();
    assertThat(indexingPolicy.getIncludedPaths().size()).isEqualTo(2);
    assertThat(indexingPolicy.getIncludedPaths().get(0).getPath()).isEqualTo("/concatenatedPartitionKey/?");
    assertThat(indexingPolicy.getIncludedPaths().get(1).getPath()).isEqualTo("/values/c3/?");
    assertThat(indexingPolicy.getExcludedPaths().size()).isEqualTo(1);
    assertThat(indexingPolicy.getExcludedPaths().get(0).getPath()).isEqualTo("/*");
    assertThat(indexingPolicy.getCompositeIndexes()).isEmpty();
    verify(container).replace(properties);
    // for metadata table
    CosmosTableMetadata expected = new CosmosTableMetadata();
    expected.setId(getFullTableName(namespace, table));
    expected.setColumns(ImmutableMap.of("c1", "int", "c2", "text", "c3", "bigint"));
    expected.setPartitionKeyNames(Collections.singletonList("c1"));
    expected.setClusteringKeyNames(Collections.emptyList());
    expected.setClusteringOrders(Collections.emptyMap());
    expected.setSecondaryIndexNames(ImmutableSet.of("c3"));
    verify(metadataContainer).upsertItem(expected);
}
Also used : CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) PartitionKey(com.azure.cosmos.models.PartitionKey) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) Test(org.junit.jupiter.api.Test)

Aggregations

CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)25 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)19 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)15 CosmosContainerRequestOptions (com.azure.cosmos.models.CosmosContainerRequestOptions)7 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)6 CosmosDatabaseResponse (com.azure.cosmos.models.CosmosDatabaseResponse)6 ArrayList (java.util.ArrayList)6 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)5 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)5 CosmosException (com.azure.cosmos.CosmosException)5 PartitionKey (com.azure.cosmos.models.PartitionKey)5 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)4 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)4 CosmosContainer (com.azure.cosmos.CosmosContainer)4 CosmosDatabase (com.azure.cosmos.CosmosDatabase)4 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)4 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)4 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)4 Duration (java.time.Duration)4 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)3