From f0a55aebf1461b1214dc75786da64a5c7335baa6 Mon Sep 17 00:00:00 2001 From: Kushagra Thapar Date: Tue, 11 Aug 2020 22:53:14 -0700 Subject: [PATCH] Updated README for azure-spring-data-cosmos-core and other cosmos spring modules (#13985) * Updated indexing policy tests based on V4 SDK mode * Updated readme files for azure-spring-data-cosmos-core, azure-spring-data-2-2-cosmos and azure-spring-data-2-3-cosmos * Fixed broken links * Fixed readme links * Fixed some links and code review comments --- .../azure-spring-data-2-2-cosmos/README.md | 68 ++- .../azure-spring-data-2-3-cosmos/README.md | 68 ++- .../azure-spring-data-cosmos-core/README.md | 428 ++++++++++-------- .../CosmosIndexingPolicyCodeSnippet.java | 3 +- .../com/azure/cosmos/SampleApplication.java | 2 - .../java/com/azure/cosmos/UserRepository.java | 4 +- .../repository/CosmosAnnotationUnitTest.java | 6 - .../data/cosmos/common/TestConstants.java | 40 +- .../azure/spring/data/cosmos/domain/Role.java | 12 +- .../integration/CosmosAnnotationIT.java | 22 +- .../src/test/resources/logback-test.xml | 2 +- 11 files changed, 401 insertions(+), 254 deletions(-) diff --git a/sdk/cosmos/azure-spring-data-2-2-cosmos/README.md b/sdk/cosmos/azure-spring-data-2-2-cosmos/README.md index 4e4ff1f0eb43b..f7585e1a3a483 100644 --- a/sdk/cosmos/azure-spring-data-2-2-cosmos/README.md +++ b/sdk/cosmos/azure-spring-data-2-2-cosmos/README.md @@ -1,9 +1,73 @@ # Azure Spring Data 2.2 Cosmos client library for Java -## Key concepts +**Azure Spring Data Cosmos** provides Spring Data 2.2.x support for Azure Cosmos DB using the [SQL API][sql_api_query], based on Spring Data framework. +[Azure Cosmos DB][cosmos_introduction] is a globally-distributed database service which allows developers to work with data using a variety of standard APIs, such as SQL, MongoDB, Cassandra, Graph, and Table. + +[Source code][source_code] | [Package (Maven)][azure_spring_data_2_2_cosmos_maven] | [API reference documentation][api_documentation] | [Product documentation][azure_spring_data_cosmos_docs] | +[Samples][samples] + +## Spring data version support +This project supports [spring-data-commons 2.2.x][spring_data_2_2_commons] version. + ## Getting started +### Include the package +If you are using Maven, add the following dependency. + +[//]: # ({x-version-update-start;com.azure:azure-spring-data-2-2-cosmos;current}) +```xml + + com.azure + azure-spring-data-2-2-cosmos + 3.0.0-beta.1 + +``` +[//]: # ({x-version-update-end}) + ## Key concepts -## Examples +Please refer to [Azure Spring Data Cosmos Core module][azure_spring_data_cosmos_core_readme_key_concepts] for key concepts. + ## Troubleshooting +Please refer to [Azure Spring Data Cosmos Core module][azure_spring_data_cosmos_core_readme_troubleshooting] for troubleshooting guide. + +## Examples +- Please refer to [sample project here][samples]. + ## Next steps +- Read more about [azure spring data cosmos][azure_spring_data_cosmos_docs]. +- Read more about [azure cosmosdb service][cosmos_docs]. + ## Contributing + +This project welcomes contributions and suggestions. Most contributions require you to agree to a +[Contributor License Agreement (CLA)][cla] declaring that you have the right to, and actually do, grant us the rights +to use your contribution. + +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate +the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to +do this once across all repos using our CLA. + +This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For more information see the [Code of Conduct FAQ][coc_faq] +or contact [opencode@microsoft.com][coc_contact] with any additional questions or comments. + + +[source_code]: https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-cosmos-core +[cosmos_introduction]: https://docs.microsoft.com/en-us/azure/cosmos-db/ +[cosmos_docs]: https://docs.microsoft.com/en-us/azure/cosmos-db/introduction +[jdk]: https://docs.microsoft.com/java/azure/java-supported-jdk-runtime?view=azure-java-stable +[maven]: https://maven.apache.org/ +[cla]: https://cla.microsoft.com +[coc]: https://opensource.microsoft.com/codeofconduct/ +[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ +[coc_contact]: mailto:opencode@microsoft.com +[samples]: https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos +[sql_api_query]: https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sql-query +[spring_data_2_2_commons]: https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons/2.2.0.RELEASE +[api_documentation]: https://azure.github.io/azure-sdk-for-java/cosmos.html#azure-spring-data-2-2-cosmos +[maven]: https://maven.apache.org/ +[azure_spring_data_cosmos_core_readme_key_concepts]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/cosmos/azure-spring-data-cosmos-core/README.md#key-concepts +[azure_spring_data_cosmos_core_readme_troubleshooting]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/cosmos/azure-spring-data-cosmos-core/README.md#troubleshooting +[azure_spring_data_cosmos_docs]: https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-java-spring +[azure_spring_data_2_2_cosmos_maven]: https://search.maven.org/artifact/com.azure/azure-spring-data-2-2-cosmos +[azure_spring_data_2_2_cosmos_maven_svg]: https://img.shields.io/maven-central/v/com.azure/azure-spring-data-2-2-cosmos.svg + +![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-java%2Fsdk%2Fcosmos%2FREADME.png) diff --git a/sdk/cosmos/azure-spring-data-2-3-cosmos/README.md b/sdk/cosmos/azure-spring-data-2-3-cosmos/README.md index 0c4d9e3a3c66b..a7486d53cbcfa 100644 --- a/sdk/cosmos/azure-spring-data-2-3-cosmos/README.md +++ b/sdk/cosmos/azure-spring-data-2-3-cosmos/README.md @@ -1,9 +1,73 @@ # Azure Spring Data 2.3 Cosmos client library for Java -## Key concepts +**Azure Spring Data Cosmos** provides Spring Data 2.3.x support for Azure Cosmos DB using the [SQL API][sql_api_query], based on Spring Data framework. +[Azure Cosmos DB][cosmos_introduction] is a globally-distributed database service which allows developers to work with data using a variety of standard APIs, such as SQL, MongoDB, Cassandra, Graph, and Table. + +[Source code][source_code] | [Package (Maven)][azure_spring_data_2_3_cosmos_maven] | [API reference documentation][api_documentation] | [Product documentation][azure_spring_data_cosmos_docs] | +[Samples][samples] + +## Spring data version support +This project supports [spring-data-commons 2.3.x][spring_data_2_3_commons] version. + ## Getting started +### Include the package +If you are using Maven, add the following dependency. + +[//]: # ({x-version-update-start;com.azure:azure-spring-data-2-3-cosmos;current}) +```xml + + com.azure + azure-spring-data-2-3-cosmos + 3.0.0-beta.1 + +``` +[//]: # ({x-version-update-end}) + ## Key concepts -## Examples +Please refer to [Azure Spring Data Cosmos Core module][azure_spring_data_cosmos_core_readme_key_concepts] for key concepts. + ## Troubleshooting +Please refer to [Azure Spring Data Cosmos Core module][azure_spring_data_cosmos_core_readme_troubleshooting] for troubleshooting guide. + +## Examples +- Please refer to [sample project here][samples]. + ## Next steps +- Read more about [azure spring data cosmos][azure_spring_data_cosmos_docs]. +- Read more about [azure cosmosdb service][cosmos_docs]. + ## Contributing + +This project welcomes contributions and suggestions. Most contributions require you to agree to a +[Contributor License Agreement (CLA)][cla] declaring that you have the right to, and actually do, grant us the rights +to use your contribution. + +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate +the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to +do this once across all repos using our CLA. + +This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For more information see the [Code of Conduct FAQ][coc_faq] +or contact [opencode@microsoft.com][coc_contact] with any additional questions or comments. + + +[source_code]: https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-cosmos-core +[cosmos_introduction]: https://docs.microsoft.com/en-us/azure/cosmos-db/ +[cosmos_docs]: https://docs.microsoft.com/en-us/azure/cosmos-db/introduction +[jdk]: https://docs.microsoft.com/java/azure/java-supported-jdk-runtime?view=azure-java-stable +[maven]: https://maven.apache.org/ +[cla]: https://cla.microsoft.com +[coc]: https://opensource.microsoft.com/codeofconduct/ +[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ +[coc_contact]: mailto:opencode@microsoft.com +[samples]: https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos +[sql_api_query]: https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sql-query +[spring_data_2_3_commons]: https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons/2.3.0.RELEASE +[api_documentation]: https://azure.github.io/azure-sdk-for-java/cosmos.html#azure-spring-data-2-3-cosmos +[maven]: https://maven.apache.org/ +[azure_spring_data_cosmos_core_readme_key_concepts]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/cosmos/azure-spring-data-cosmos-core/README.md#key-concepts +[azure_spring_data_cosmos_core_readme_troubleshooting]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/cosmos/azure-spring-data-cosmos-core/README.md#troubleshooting +[azure_spring_data_cosmos_docs]: https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-java-spring +[azure_spring_data_2_3_cosmos_maven]: https://search.maven.org/artifact/com.azure/azure-spring-data-2-3-cosmos +[azure_spring_data_2_3_cosmos_maven_svg]: https://img.shields.io/maven-central/v/com.azure/azure-spring-data-2-3-cosmos.svg + +![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-java%2Fsdk%2Fcosmos%2FREADME.png) diff --git a/sdk/cosmos/azure-spring-data-cosmos-core/README.md b/sdk/cosmos/azure-spring-data-cosmos-core/README.md index 751b5fbfc26b8..5bc6b977ed811 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-core/README.md +++ b/sdk/cosmos/azure-spring-data-cosmos-core/README.md @@ -1,161 +1,30 @@ -#Azure Cosmos DB client library for Java +# Azure Spring Data Cosmos Core client library for Java -## Getting started -[Azure Cosmos DB](https://docs.microsoft.com/azure/cosmos-db/introduction) is a globally-distributed database service that allows developers to work with data using a variety of standard APIs, such as SQL, MongoDB, Cassandra, Graph, and Table. - -**Spring Data Azure Cosmos DB** provides initial Spring Data support for Azure Cosmos DB using the [SQL API](https://docs.microsoft.com/azure/cosmos-db/sql-api-introduction), based on Spring Data framework. Currently it only supports SQL API, the other APIs are in the plan. - -## TOC - -* [Examples](#Examples) -* [Spring data version support](#spring-data-version-support) -* [Feature List](#feature-list) -* [Quick Start](#quick-start) -* [Beta version package](#Beta version package) -* [Troubleshooting](#Troubleshooting) -* [Contributing](#Contributing) -* [Code of Conduct](#code-of-conduct) -* [Key concepts](#Key concepts) -* [Next steps](#Next steps) - -## Examples -Please refer to [sample project here](./samplecode). - -## Feature List -- Spring Data ReactiveCrudRepository CrudRepository basic CRUD functionality - - save - - findAll - - findOne by Id - - deleteAll - - delete by Id - - delete entity -- Spring Data [@Id](https://github.com/spring-projects/spring-data-commons/blob/db62390de90c93a78743c97cc2cc9ccd964994a5/src/main/java/org/springframework/data/annotation/Id.java) annotation. - There're 2 ways to map a field in domain class to `id` field of Azure Cosmos DB Item. - - annotate a field in domain class with `@Id`, this field will be mapped to Item `id` in Cosmos DB. - - set name of this field to `id`, this field will be mapped to Item `id` in Azure Cosmos DB. -- Custom container Name. - By default, container name will be class name of user domain class. To customize it, add the `@Container(containerName="myCustomContainerName")` annotation to the domain class. The container field also supports SpEL expressions (eg. `container = "${dynamic.container.name}"` or `container = "#{@someBean.getContainerName()}"`) in order to provide container names programmatically/via configuration properties. -- Custom IndexingPolicy - By default, IndexingPolicy will be set by azure service. To customize it add annotation `@CosmosIndexingPolicy` to domain class. This annotation has 4 attributes to customize, see following: - -```java -// Indicate if indexing policy use automatic or not -boolean automatic() default Constants.DEFAULT_INDEXING_POLICY_AUTOMATIC; - -// Indexing policy mode, option Consistent|Lazy|None. -IndexingMode mode() default IndexingMode.CONSISTENT; - -// Included paths for indexing -String[] includePaths() default {}; - -// Excluded paths for indexing -String[] excludePaths() default {}; -``` - -- Supports Optimistic Locking for specific containers, which means upserts/deletes by Item will fail with an exception in case the Item was modified by another process in the meanwhile. To enable Optimistic Locking for a container, just create a string `_etag` field and mark it with the `@Version` annotation. See the following: - - -```java -@Container(containerName = "myContainer") -public class MyItem { - String id; - String data; - @Version - String _etag; -} -``` -- Supports [Azure Cosmos DB partition](https://docs.microsoft.com/azure/cosmos-db/partition-data). To specify a field of domain class to be partition key field, just annotate it with `@PartitionKey`. When you do CRUD operation, pls specify your partition value. For more sample on partition CRUD, pls refer to [test here](./src/test/java/com/azure/spring/data/cosmos/repository/integration/AddressRepositoryIT.java) -- Supports [Spring Data custom query](https://docs.spring.io/spring-data/commons/docs/current/reference/html/#repositories.query-methods.details) find operation, e.g., `findByAFieldAndBField` -- Supports [Spring Data pagable and sort](https://docs.spring.io/spring-data/commons/docs/current/reference/html/#repositories.special-parameters). - - Based on available RUs on the database account, cosmosDB can return items less than or equal to the requested size. - - Due to this variable number of returned items in every iteration, user should not rely on the totalPageSize, and instead iterating over pageable should be done in this way. - -```java -private List findAllWithPageSize(int pageSize) { - - final CosmosPageRequest pageRequest = new CosmosPageRequest(0, pageSize, null); - Page page = repository.findAll(pageRequest); - List pageContent = page.getContent(); - while (page.hasNext()) { - Pageable nextPageable = page.nextPageable(); - page = repository.findAll(nextPageable); - pageContent = page.getContent(); - } - return pageContent; -} -``` -- Supports [spring-boot-starter-data-rest](https://projects.spring.io/spring-data-rest/). -- Supports List and nested type in domain class. -- Configurable ObjectMapper bean with unique name `cosmosObjectMapper`, only configure customized ObjectMapper if you really need to. e.g., - -```java -@Bean(name = "cosmosObjectMapper") -public ObjectMapper objectMapper() { - return new ObjectMapper(); // Do configuration to the ObjectMapper if required -} -``` -- Supports Audit fields on database entities using the standard spring-data annotations. This feature is enabled by adding -the `@EnableCosmosAuditing` annotation to your application configuration. Entities can annotate fields using `@CreatedBy` -`@CreatedDate` `@LastModifiedBy` and `@LastModifiedDate`. These fields will be updated automatically. - -```java -@Container(containerName = "myContainer") -public class AuditableUser { - private String id; - private String firstName; - @CreatedBy - private String createdBy; - @CreatedDate - private OffsetDateTime createdDate; - @LastModifiedBy - private String lastModifiedBy; - @LastModifiedDate - private OffsetDateTime lastModifiedByDate; -} -``` -- Supports auto generation of string type UUIDs using the @GeneratedValue annotation. The id field of an entity with a string -type id can be annotated with @GeneratedValue to automatically generate a random UUID prior to insertion. - -```java -public class GeneratedIdEntity { - - @Id - @GeneratedValue - private String id; - -} -``` - -## Quick Start - -### Add the dependency -`azure-spring-data-cosmos-core` is published on Maven Central Repository. -If you are using Maven, add the following dependency. +**Azure Spring Data Cosmos** provides core Spring Data support for Azure Cosmos DB using the [SQL API][sql_api_query], based on Spring Data framework. +[Azure Cosmos DB][cosmos_introduction] is a globally-distributed database service which allows developers to work with data using a variety of standard APIs, such as SQL, MongoDB, Cassandra, Graph, and Table. -[//]: # "{x-version-update-start;com.azure:azure-spring-data-cosmos-core;current}" -```xml - - com.azure - azure-spring-data-cosmos-core - 3.0.0-beta.1 - -``` +## Spring data version support +This project supports both [spring-data-commons 2.2.x][azure_spring_data_2_2_cosmos] and [spring-data-commons 2.3.x][azure_spring_data_2_3_cosmos] versions. -### Setup Configuration -Setup configuration class. +## Getting started -CosmosKeyCredential feature provides capability to rotate keys on the fly. You can switch keys using switchToSecondaryKey(). -For more information on this, see the Sample Application code. +### Include the package +Please refer to [azure-spring-data-2-2-cosmos readme][azure_spring_data_2_2_cosmos_readme] or [azure-spring-data-2-3-cosmos readme][azure_spring_data_2_3_cosmos_readme] on how to include the individual packages. -### Sync and Reactive Repository support -2.2.x supports both sync and reactive repository support. +### Prerequisites -Use `@EnableCosmosRepositories` to enable sync repository support. +- Java Development Kit 8 +- An active Azure account. If you don't have one, you can sign up for a [free account][azure_subscription]. Alternatively, you can use the [Azure Cosmos DB Emulator][local_emulator] for development and testing. As emulator https certificate is self signed, you need to import its certificate to java trusted cert store, [explained here][local_emulator_export_ssl_certificates] +- (Optional) SLF4J is a logging facade. +- (Optional) [SLF4J binding](http://www.slf4j.org/manual.html) is used to associate a specific logging framework with SLF4J. +- (Optional) Maven -For reactive repository support, use `@EnableReactiveCosmosRepositories` +SLF4J is only needed if you plan to use logging, please also download an SLF4J binding which will link the SLF4J API with the logging implementation of your choice. See the [SLF4J user manual](http://www.slf4j.org/manual.html) for more information. + +### Setup Configuration Class +- In order to set up configuration class, you'll need to extend `AbstractCosmosConfiguration` -### Response Diagnostics String and Query Metrics -2.2.x supports Response Diagnostics String and Query Metrics. +- Azure-spring-data-cosmos also supports `Response Diagnostics String` and `Query Metrics`. Set `queryMetricsEnabled` flag to true in application.properties to enable query metrics. In addition to setting the flag, implement `ResponseDiagnosticsProcessor` to log diagnostics information. @@ -222,7 +91,8 @@ public class AppConfiguration extends AbstractCosmosConfiguration { } ``` -Or if you want to customize your config: +### Customizing Configuration +You can customize `DirectConnectionConfig` or `GatewayConnectionConfig` or both and provide them to `CosmosClientBuilder` bean to customize `CosmosAsyncClient` ```java @@ -244,17 +114,18 @@ public CosmosConfig cosmosConfig() { .build(); } ``` -By default, `@EnableCosmosRepositories` will scan the current package for any interfaces that extend one of Spring Data's repository interfaces. Using it to annotate your Configuration class to scan a different root package by `@EnableCosmosRepositories(basePackageClass=UserRepository.class)` if your project layout has multiple projects and it's not finding your repositories. +By default, `@EnableCosmosRepositories` will scan the current package for any interfaces that extend one of Spring Data's repository interfaces. +Use it to annotate your Configuration class to scan a different root package by `@EnableCosmosRepositories(basePackageClass=UserRepository.class)` if your project layout has multiple projects. ### Define an entity -Define a simple entity as Item in Azure Cosmos DB. +- Define a simple entity as item in Azure Cosmos DB. -You can define entities by adding the `@Container` annotation and specifying properties related to the container, such as the container name, request units (RUs), time to live, and auto-create container. +- You can define entities by adding the `@Container` annotation and specifying properties related to the container, such as the container name, request units (RUs), time to live, and auto-create container. -Containers will be created automatically unless you don't want them to: Set `autoCreateContainer` to false in `@Container` annotation to disable auto creation of containers. +- Containers will be created automatically unless you don't want them to. Set `autoCreateContainer` to false in `@Container` annotation to disable auto creation of containers. -Note: By default request units assigned to newly created containers is 4000. Specify different ru value to customize request units for the container created by the SDK (minimum RU value is 400). +- Note: By default request units assigned to newly created containers is 400. Specify different ru value to customize request units for the container created by the SDK (minimum RU value is 400). ```java @Container(containerName = "myContainer", ru = "400") @@ -307,10 +178,10 @@ public class User { } } ``` -`id` field will be used as Item id in Azure Cosmos DB. If you want use another field like `emailAddress` as Item `id`, just annotate that field with `@Id` annotation. +- `id` field will be used as Item id in Azure Cosmos DB. If you want use another field like `firstName` as item `id`, just annotate that field with `@Id` annotation. -Annotation `@Container(containerName="myContainer")` is used to specify container name in Azure Cosmos DB. -Annotation `@PartitionKey` on `lastName` field is used to specify this field be partition key in Azure Cosmos DB. +- Annotation `@Container(containerName="myContainer")` specifies container name in Azure Cosmos DB. +- Annotation `@PartitionKey` on `lastName` field specifies this field as partition key in Azure Cosmos DB. ```java @Container(containerName = "myContainer") @@ -323,21 +194,21 @@ public class UserSample { ### Create repositories Extends CosmosRepository interface, which provides Spring Data repository support. - + ```java @Repository public interface UserRepository extends CosmosRepository { - List findByFirstName(String firstName); + Iterable findByFirstName(String firstName); User findOne(String id, String lastName); } ``` -`findByFirstName` method is custom query method, it will find Items per FirstName. +- `findByFirstName` method is custom query method, it will find items per firstName. ### Create an Application class Here create an application class with all the components - + ```java @SpringBootApplication @@ -362,8 +233,6 @@ public class SampleApplication implements CommandLineRunner { // to find by Id, please specify partition key value if collection is partitioned final User result = repository.findOne(testUser.getId(), testUser.getLastName()); - // if emailAddress is mapped to id, then - // final User result = respository.findOne(testUser.getEmailAddress(), testUser.getLastName()); // Switch to secondary key UserRepositoryConfiguration bean = @@ -376,23 +245,135 @@ public class SampleApplication implements CommandLineRunner { } } ``` -Autowired UserRepository interface, then can do save, delete and find operations. Spring Data Azure Cosmos DB uses the CosmosTemplate to execute the queries behind *find*, *save* methods. You can use the template yourself for more complex queries. +- Autowire UserRepository interface, to perform operations like save, delete, find, etc. +- Spring Data Azure Cosmos DB uses the `CosmosTemplate` and `ReactiveCosmosTemplate` to execute the queries behind *find*, *save* methods. You can use the template yourself for more complex queries. -## Support multi-database configuration -The `azure-spring-data-cosmos-core` support multi-database configuration, includes "multiple account" and "single account, multiple database". Here is an example. +## Key concepts -### Add the dependency -[//]: # "{x-version-update-start;com.azure:azure-spring-data-cosmos-core;current}" -```xml - - com.azure - azure-spring-data-cosmos-core - 3.0.0-beta.1 - +### CrudRepository and ReactiveCrudRepository +- Azure Spring Data Cosmos supports ReactiveCrudRepository and CrudRepository which provides basic CRUD functionality + - save + - findAll + - findOne by Id + - deleteAll + - delete by Id + - delete entity + +### Spring Data Annotations +- Spring Data [@Id annotation][spring_data_commons_id_annotation]. + There are 2 ways to map a field in domain class to `id` field of Azure Cosmos DB Item. + - annotate a field in domain class with `@Id`, this field will be mapped to Item `id` in Cosmos DB. + - set name of this field to `id`, this field will be mapped to Item `id` in Azure Cosmos DB. +- Supports auto generation of string type UUIDs using the @GeneratedValue annotation. The id field of an entity with a string + type id can be annotated with `@GeneratedValue` to automatically generate a random UUID prior to insertion. + + ```java + public class GeneratedIdEntity { + + @Id + @GeneratedValue + private String id; + + } + ``` +- Custom container Name. + By default, container name will be class name of user domain class. To customize it, add the `@Container(containerName="myCustomContainerName")` annotation to the domain class. The container field also supports SpEL expressions (eg. `container = "${dynamic.container.name}"` or `container = "#{@someBean.getContainerName()}"`) in order to provide container names programmatically/via configuration properties. +- Custom IndexingPolicy + By default, IndexingPolicy will be set by azure service. To customize it add annotation `@CosmosIndexingPolicy` to domain class. This annotation has 4 attributes to customize, see following: + +```java +// Indicate if indexing policy use automatic or not +// Default value is true +boolean automatic() default Constants.DEFAULT_INDEXING_POLICY_AUTOMATIC; + +// Indexing policy mode, option Consistent. +IndexingMode mode() default IndexingMode.CONSISTENT; + +// Included paths for indexing +String[] includePaths() default {}; + +// Excluded paths for indexing +String[] excludePaths() default {}; +``` + +### Azure Cosmos DB Partition +- Azure-spring-data-cosmos supports [Azure Cosmos DB partition][azure_cosmos_db_partition]. +- To specify a field of domain class to be partition key field, just annotate it with `@PartitionKey`. +- When you perform CRUD operation, specify your partition value. +- For more sample on partition CRUD, please refer [test here][address_repository_it_test] + +### Optimistic Locking +- Azure-spring-data-cosmos supports Optimistic Locking for specific containers, which means upserts/deletes by item will fail with an exception in case the item is modified by another process in the meanwhile. +- To enable Optimistic Locking for a container, just create a string `_etag` field and mark it with the `@Version` annotation. See the following: + + +```java +@Container(containerName = "myContainer") +public class MyItem { + String id; + String data; + @Version + String _etag; +} ``` -[//]: # ({x-version-update-end}) +### Spring Data custom query, pageable and sorting +- Azure-spring-data-cosmos supports [spring data custom queries][spring_data_custom_query] +- Example, find operation, e.g., `findByAFieldAndBField` +- Supports [Spring Data pageable and sort](https://docs.spring.io/spring-data/commons/docs/current/reference/html/#repositories.special-parameters). + - Based on available RUs on the database account, cosmosDB can return items less than or equal to the requested size. + - Due to this variable number of returned items in every iteration, user should not rely on the totalPageSize, and instead iterating over pageable should be done in this way. + +```java +private List findAllWithPageSize(int pageSize) { + + final CosmosPageRequest pageRequest = new CosmosPageRequest(0, pageSize, null); + Page page = repository.findAll(pageRequest); + List pageContent = page.getContent(); + while (page.hasNext()) { + Pageable nextPageable = page.nextPageable(); + page = repository.findAll(nextPageable); + pageContent = page.getContent(); + } + return pageContent; +} +``` + +### Spring Boot Starter Data Rest +- Azure-spring-data-cosmos supports [spring-boot-starter-data-rest](https://projects.spring.io/spring-data-rest/). +- Supports List and nested type in domain class. +- Configurable ObjectMapper bean with unique name `cosmosObjectMapper`, only configure customized ObjectMapper if you really need to. e.g., + +```java +@Bean(name = "cosmosObjectMapper") +public ObjectMapper objectMapper() { + return new ObjectMapper(); // Do configuration to the ObjectMapper if required +} +``` + +### Auditing +- Azure-spring-data-cosmos supports auditing fields on database entities using standard spring-data annotations. +- This feature can be enabled by adding `@EnableCosmosAuditing` annotation to your application configuration. +- Entities can annotate fields using `@CreatedBy`, `@CreatedDate`, `@LastModifiedBy` and `@LastModifiedDate`. These fields will be updated automatically. + +```java +@Container(containerName = "myContainer") +public class AuditableUser { + private String id; + private String firstName; + @CreatedBy + private String createdBy; + @CreatedDate + private OffsetDateTime createdDate; + @LastModifiedBy + private String lastModifiedBy; + @LastModifiedDate + private OffsetDateTime lastModifiedByDate; +} +``` + +### Multi-database configuration +- Azure-spring-data-cosmos supports multi-database configuration, including "multiple database accounts" and "single account, with multiple databases". -### Config application properties The example uses the `application.properties` file ```properties # primary account cosmos config @@ -410,13 +391,10 @@ azure.cosmos.secondary.database=your-secondary-cosmosDb-dbName azure.cosmos.secondary.populateQueryMetrics=if-populate-query-metrics ``` -### Define Entities and Repositories -The [Entity](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-cosmos-core#define-an-entity) and [Repository](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-cosmos-core#create-repositories) definition is similar as above. -You can put different database entities into different packages. +- The [Entity](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-cosmos-core#define-an-entity) and [Repository](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-cosmos-core#create-repositories) definition is similar as above. You can put different database entities into different packages. -### Setup configuration -The `@EnableReactiveCosmosRepositories` or `@EnableCosmosRepositories` support user-define the cosmos template, use `reactiveCosmosTemplateRef` or `cosmosTemplateRef` to config the name of the `ReactiveCosmosTemplate` or `CosmosTemplate` bean to be used with the repositories detected. -If you have multiple cosmos database accounts, you can define multiple `CosmosAsyncClient`. If the single cosmos account has multiple databases, you can use the same `CosmosAsyncClient` to initialize the cosmos template. +- The `@EnableReactiveCosmosRepositories` or `@EnableCosmosRepositories` support user-define the cosmos template, use `reactiveCosmosTemplateRef` or `cosmosTemplateRef` to config the name of the `ReactiveCosmosTemplate` or `CosmosTemplate` bean to be used with the repositories detected. +- If you have multiple cosmos database accounts, you can define multiple `CosmosAsyncClient`. If the single cosmos account has multiple databases, you can use the same `CosmosAsyncClient` to initialize the cosmos template. ```java @@ -520,7 +498,7 @@ public class DatabaseConfiguration extends AbstractCosmosConfiguration { } ``` -In the above example, we have two cosmos account, each account has two databases. For each account, we can use the same Cosmos Client. You can create the `CosmosAsyncClient` like this: +- In the above example, we have two cosmos account, each account has two databases. For each account, we can use the same Cosmos Client. You can create the `CosmosAsyncClient` like this: @@ -531,7 +509,7 @@ public CosmosAsyncClient getCosmosAsyncClient(CosmosClientBuilder secondaryCosmo } ``` -Besides, if you want to define `queryMetricsEnabled` or `ResponseDiagnosticsProcessor` , you can create the `CosmosConfig` for your cosmos template. +- Besides, if you want to define `queryMetricsEnabled` or `ResponseDiagnosticsProcessor` , you can create the `CosmosConfig` for your cosmos template. @@ -545,7 +523,7 @@ public CosmosConfig getCosmosConfig() { } ``` -### Create an Application class +- Create an Application class @@ -595,25 +573,79 @@ public class MultiDatasourceApplication implements CommandLineRunner { Beta version built from `master` branch are available, you can refer to the [instruction](https://github.com/Azure/azure-sdk-for-java/blob/master/CONTRIBUTING.md#nightly-package-builds) to use beta version packages. - ## Troubleshooting +### General + If you encounter any bug, please file an issue [here](https://github.com/Azure/azure-sdk-for-java/issues/new). To suggest a new feature or changes that could be made, file an issue the same way you would for a bug. -## Contributing - -Contribution is welcome. Please follow [this instruction](./CONTRIBUTING.md) to contribute code. - -## Code of Conduct +### Enable Client Logging +- Azure-spring-data-cosmos uses SLF4j as the logging facade that supports logging into popular logging frameworks such as log4j and logback. +For example, if you want to use spring logback as logging framework, add the following xml to resources folder. -This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. +```xml + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + +``` -### Data +## Examples +- Please refer to [sample project here][samples]. - This project collects usage data and sends it to Microsoft to help improve our products and services. Read our [privacy](https://privacy.microsoft.com/privacystatement) statement to learn more. +## Next steps +- Read more about azure spring data cosmos [here][azure_spring_data_cosmos_docs]. +- [Read more about Azure CosmosDB Service][cosmos_docs] -## Key concepts +## Contributing -## Next steps +This project welcomes contributions and suggestions. Most contributions require you to agree to a +[Contributor License Agreement (CLA)][cla] declaring that you have the right to, and actually do, grant us the rights +to use your contribution. + +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate +the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to +do this once across all repos using our CLA. + +This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For more information see the [Code of Conduct FAQ][coc_faq] +or contact [opencode@microsoft.com][coc_contact] with any additional questions or comments. + + +[source_code]: src +[cosmos_introduction]: https://docs.microsoft.com/en-us/azure/cosmos-db/ +[cosmos_docs]: https://docs.microsoft.com/en-us/azure/cosmos-db/introduction +[jdk]: https://docs.microsoft.com/java/azure/java-supported-jdk-runtime?view=azure-java-stable +[maven]: https://maven.apache.org/ +[cla]: https://cla.microsoft.com +[coc]: https://opensource.microsoft.com/codeofconduct/ +[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ +[coc_contact]: mailto:opencode@microsoft.com +[azure_subscription]: https://azure.microsoft.com/free/ +[samples]: https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos +[sql_api_query]: https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sql-query +[local_emulator]: https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator +[local_emulator_export_ssl_certificates]: https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator-export-ssl-certificates +[azure_spring_data_2_2_cosmos]: https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-2-2-cosmos +[azure_spring_data_2_3_cosmos]: https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-2-3-cosmos +[azure_spring_data_2_2_cosmos_readme]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/cosmos/azure-spring-data-2-2-cosmos/README.md +[azure_spring_data_2_3_cosmos_readme]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/cosmos/azure-spring-data-2-3-cosmos/README.md +[spring_data_commons_id_annotation]: https://github.com/spring-projects/spring-data-commons/blob/master/src/main/java/org/springframework/data/annotation/Id.java +[azure_cosmos_db_partition]: https://docs.microsoft.com/azure/cosmos-db/partition-data +[address_repository_it_test]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AddressRepositoryIT.java +[azure_spring_data_cosmos_docs]: https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-java-spring +[spring_data_custom_query]: https://docs.spring.io/spring-data/commons/docs/current/reference/html/#repositories.query-methods.details + +![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-java%2Fsdk%2Fcosmos%2FREADME.png) diff --git a/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/CosmosIndexingPolicyCodeSnippet.java b/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/CosmosIndexingPolicyCodeSnippet.java index 951f89c20c332..18bad443d43db 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/CosmosIndexingPolicyCodeSnippet.java +++ b/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/CosmosIndexingPolicyCodeSnippet.java @@ -14,9 +14,10 @@ public @interface CosmosIndexingPolicyCodeSnippet { // Indicate if indexing policy use automatic or not + // Default value is true boolean automatic() default Constants.DEFAULT_INDEXING_POLICY_AUTOMATIC; - // Indexing policy mode, option Consistent|Lazy|None. + // Indexing policy mode, option Consistent. IndexingMode mode() default IndexingMode.CONSISTENT; // Included paths for indexing diff --git a/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/SampleApplication.java b/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/SampleApplication.java index 34f408063d3af..db46116814d09 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/SampleApplication.java +++ b/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/SampleApplication.java @@ -36,8 +36,6 @@ public void run(String... var1) { // to find by Id, please specify partition key value if collection is partitioned final User result = repository.findOne(testUser.getId(), testUser.getLastName()); - // if emailAddress is mapped to id, then - // final User result = respository.findOne(testUser.getEmailAddress(), testUser.getLastName()); // Switch to secondary key UserRepositoryConfiguration bean = diff --git a/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/UserRepository.java b/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/UserRepository.java index e89d1e2dac876..13cd1fcb5d9c2 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/UserRepository.java +++ b/sdk/cosmos/azure-spring-data-cosmos-core/src/samples/java/com/azure/cosmos/UserRepository.java @@ -5,8 +5,6 @@ import com.azure.spring.data.cosmos.repository.CosmosRepository; import org.springframework.stereotype.Repository; -import java.util.List; - /** * WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS * ARE USED TO EXTRACT APPROPRIATE CODE SEGMENTS FROM THIS FILE. ADD NEW CODE AT THE BOTTOM TO AVOID CHANGING @@ -16,6 +14,6 @@ @Repository public interface UserRepository extends CosmosRepository { - List findByFirstName(String firstName); + Iterable findByFirstName(String firstName); User findOne(String id, String lastName); } diff --git a/sdk/cosmos/azure-spring-data-cosmos-core/src/test/java/com/azure/spring/data/cosmos/repository/CosmosAnnotationUnitTest.java b/sdk/cosmos/azure-spring-data-cosmos-core/src/test/java/com/azure/spring/data/cosmos/repository/CosmosAnnotationUnitTest.java index 75a509ab069e7..687263dcf669a 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-core/src/test/java/com/azure/spring/data/cosmos/repository/CosmosAnnotationUnitTest.java +++ b/sdk/cosmos/azure-spring-data-cosmos-core/src/test/java/com/azure/spring/data/cosmos/repository/CosmosAnnotationUnitTest.java @@ -47,8 +47,6 @@ public void testDefaultIndexingPolicyAnnotation() { // IncludedPaths and ExcludedPaths // We do not use testIndexingPolicyPathsEquals generic here, for unit test do not create cosmosdb instance, // and the paths of policy will never be set from azure service. - // testIndexingPolicyPathsEquals(policy.getIncludedPaths(), TestConstants.DEFAULT_INCLUDEDPATHS); - // testIndexingPolicyPathsEquals(policy.getExcludedPaths(), TestConstants.DEFAULT_EXCLUDEDPATHS); Assert.isTrue(policy.getIncludedPaths().isEmpty(), "default includedpaths size must be 0"); Assert.isTrue(policy.getExcludedPaths().isEmpty(), "default excludedpaths size must be 0"); } @@ -70,10 +68,6 @@ public void testIndexingPolicyAnnotation() { "should be Role(class) indexing policy automatic"); Assert.isTrue(policy.getIndexingMode() == TestConstants.INDEXING_POLICY_MODE, "should be Role(class) indexing policy mode"); - - // IncludedPaths and ExcludedPaths -// TestUtils.testIndexingPolicyPathsEquals(policy.getIncludedPaths(), TestConstants.INCLUDED_PATHS); -// TestUtils.testIndexingPolicyPathsEquals(policy.getExcludedPaths(), TestConstants.EXCLUDED_PATHS); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/common/TestConstants.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/common/TestConstants.java index 31b4ab81d264e..b573687cefe73 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/common/TestConstants.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/common/TestConstants.java @@ -17,47 +17,23 @@ public final class TestConstants { public static final String ROLE_COLLECTION_NAME = "RoleCollectionName"; public static final int TIME_TO_LIVE = 5; - public static final boolean INDEXING_POLICY_AUTOMATIC = false; + public static final boolean INDEXING_POLICY_AUTOMATIC = true; public static final IndexingMode INDEXING_POLICY_MODE = IndexingMode.CONSISTENT; - public static final String INCLUDED_PATH_0 = "{\"path\":\"/*\",\"indexes\":[" - + "{\"kind\":\"Range\",\"dataType\":\"Number\",\"precision\":2}," - + "{\"kind\":\"Hash\",\"dataType\":\"String\",\"precision\":2}," - + "{\"kind\":\"Spatial\",\"dataType\":\"Point\"}" - + "]}"; - public static final String INCLUDED_PATH_1 = "{\"path\":\"/cache/*\",\"indexes\":[" - + "{\"kind\":\"Range\",\"dataType\":\"Number\",\"precision\":3}," - + "{\"kind\":\"Hash\",\"dataType\":\"String\",\"precision\":3}," - + "{\"kind\":\"Spatial\",\"dataType\":\"LineString\"}" - + "]}"; - public static final String INCLUDED_PATH_2 = "{\"path\":\"/entities/*\",\"indexes\":[" - + "{\"kind\":\"Range\",\"dataType\":\"Number\",\"precision\":4}," - + "{\"kind\":\"Hash\",\"dataType\":\"String\",\"precision\":4}," - + "{\"kind\":\"Spatial\",\"dataType\":\"Polygon\"}" - + "]}"; + public static final String INCLUDED_PATH_0 = "/*"; + public static final String INCLUDED_PATH_1 = "/cache/*"; + public static final String INCLUDED_PATH_2 = "/entities/*"; public static final String[] INCLUDED_PATHS = { INCLUDED_PATH_0, INCLUDED_PATH_1, INCLUDED_PATH_2, }; - public static final String EXCLUDED_PATH_0 = "{\"path\":\"/excluded/*\"}"; - public static final String EXCLUDED_PATH_1 = "{\"path\":\"/props/*\"}"; + public static final String EXCLUDED_PATH_0 = "/excluded/*"; + public static final String EXCLUDED_PATH_1 = "/props/*"; + public static final String EXCLUDED_PATH_2 = "/_etag/?"; public static final String[] EXCLUDED_PATHS = { EXCLUDED_PATH_0, EXCLUDED_PATH_1, - }; - - public static final String ORDER_BY_STRING_PATH = "{\"path\":\"/*\",\"indexes\":[" - + "{\"kind\":\"Range\",\"dataType\":\"String\",\"precision\":-1}," - + "]}"; - - public static final String STARTS_WITH_INCLUDED_PATH = - "{\"path\":\"/*\",\"indexes\":[" - + "{\"kind\":\"Range\",\"dataType\":\"Number\",\"precision\":-1}," - + "{\"kind\":\"Range\",\"dataType\":\"String\",\"precision\":3}" - + "]}"; - - public static final String[] PERSON_INCLUDED_PATHS = { - STARTS_WITH_INCLUDED_PATH + EXCLUDED_PATH_2, }; public static final String DB_NAME = "testdb"; diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/domain/Role.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/domain/Role.java index c513732614785..958eb0d74a686 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/domain/Role.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/domain/Role.java @@ -13,7 +13,17 @@ @CosmosIndexingPolicy( mode = IndexingMode.CONSISTENT, - automatic = TestConstants.INDEXING_POLICY_AUTOMATIC) + automatic = TestConstants.INDEXING_POLICY_AUTOMATIC, + includePaths = { + TestConstants.INCLUDED_PATH_0, + TestConstants.INCLUDED_PATH_1, + TestConstants.INCLUDED_PATH_2, + }, + excludePaths = { + TestConstants.EXCLUDED_PATH_0, + TestConstants.EXCLUDED_PATH_1, + TestConstants.EXCLUDED_PATH_2, + }) @Container(containerName = TestConstants.ROLE_COLLECTION_NAME, autoCreateContainer = false) public class Role { diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CosmosAnnotationIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CosmosAnnotationIT.java index 9d8e4ef9de11c..e57e803f68787 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CosmosAnnotationIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CosmosAnnotationIT.java @@ -5,6 +5,8 @@ import com.azure.cosmos.CosmosAsyncClient; import com.azure.cosmos.CosmosClientBuilder; import com.azure.cosmos.models.CosmosContainerProperties; +import com.azure.cosmos.models.ExcludedPath; +import com.azure.cosmos.models.IncludedPath; import com.azure.cosmos.models.IndexingPolicy; import com.azure.cosmos.models.PartitionKey; import com.azure.spring.data.cosmos.CosmosFactory; @@ -22,7 +24,6 @@ import org.junit.After; import org.junit.AfterClass; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -37,14 +38,16 @@ import reactor.test.StepVerifier; import java.util.Arrays; +import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestRepositoryConfig.class) public class CosmosAnnotationIT { + private static final Role TEST_ROLE_1 = new Role(TestConstants.ID_1, true, TestConstants.LEVEL, - TestConstants.ROLE_NAME); + TestConstants.ROLE_NAME); private static final Role TEST_ROLE_2 = new Role(TestConstants.ID_2, false, TestConstants.LEVEL, TestConstants.ROLE_NAME); private static final Role TEST_ROLE_3 = new Role(TestConstants.ID_3, true, TestConstants.LEVEL, @@ -162,17 +165,24 @@ public void testTimeToLiveAnnotation() { } @Test - @Ignore // TODO(kuthapar): Ignore this test case for now, will update this from service update. public void testIndexingPolicyAnnotation() { final IndexingPolicy policy = collectionRole.getIndexingPolicy(); Assert.isTrue(policy.getIndexingMode() == TestConstants.INDEXING_POLICY_MODE, - "unmatched collection policy indexing mode of class Role"); + "unmatched collection policy indexing mode of class Role"); Assert.isTrue(policy.isAutomatic() == TestConstants.INDEXING_POLICY_AUTOMATIC, "unmatched collection policy automatic of class Role"); - TestUtils.testIndexingPolicyPathsEquals(policy.getIncludedPaths(), TestConstants.INCLUDED_PATHS); - TestUtils.testIndexingPolicyPathsEquals(policy.getExcludedPaths(), TestConstants.EXCLUDED_PATHS); + TestUtils.testIndexingPolicyPathsEquals(policy.getIncludedPaths() + .stream() + .map(IncludedPath::getPath) + .collect(Collectors.toList()), + TestConstants.INCLUDED_PATHS); + TestUtils.testIndexingPolicyPathsEquals(policy.getExcludedPaths() + .stream() + .map(ExcludedPath::getPath) + .collect(Collectors.toList()), + TestConstants.EXCLUDED_PATHS); } } diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/resources/logback-test.xml b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/resources/logback-test.xml index 45f2eda3202c8..e0a557553523f 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/resources/logback-test.xml +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/resources/logback-test.xml @@ -9,7 +9,7 @@ - + \ No newline at end of file