diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplateIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplateIT.java index ba200bab553e..a43d5002f46e 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplateIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplateIT.java @@ -638,6 +638,96 @@ public void testArrayContainsCriteria() { assertThat(people).containsExactly(TEST_PERSON); } + @Test + public void testContainsCriteria() { + cosmosTemplate.insert(TEST_PERSON_2, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2))); + cosmosTemplate.insert(TEST_PERSON_3, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_3))); + Person TEST_PERSON_4 = new Person("id-4", "NEW_FIRST_NAME", NEW_LAST_NAME, HOBBIES, + ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY); + cosmosTemplate.insert(TEST_PERSON_4, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_4))); + + Criteria containsCaseSensitive = Criteria.getInstance(CriteriaType.CONTAINING, "firstName", + Collections.singletonList("first"), Part.IgnoreCaseType.NEVER); + List people = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(containsCaseSensitive), Person.class, + containerName)); + assertThat(people).containsExactly(TEST_PERSON, TEST_PERSON_2, TEST_PERSON_3); + + Criteria containsNotCaseSensitive = Criteria.getInstance(CriteriaType.CONTAINING, "firstName", + Collections.singletonList("first"), Part.IgnoreCaseType.ALWAYS); + List people2 = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(containsNotCaseSensitive), Person.class, + containerName)); + assertThat(people2).containsExactly(TEST_PERSON, TEST_PERSON_2, TEST_PERSON_3, TEST_PERSON_4); + } + + @Test + public void testContainsCriteria2() { + cosmosTemplate.insert(TEST_PERSON_2, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2))); + cosmosTemplate.insert(TEST_PERSON_3, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_3))); + + Criteria containsCaseSensitive = Criteria.getInstance(CriteriaType.CONTAINING, "id", + Collections.singletonList("1"), Part.IgnoreCaseType.NEVER); + List people = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(containsCaseSensitive), Person.class, + containerName)); + assertThat(people).containsExactly(TEST_PERSON); + + Criteria containsCaseSensitive2 = Criteria.getInstance(CriteriaType.CONTAINING, "id", + Collections.singletonList("2"), Part.IgnoreCaseType.NEVER); + List people2 = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(containsCaseSensitive2), Person.class, + containerName)); + assertThat(people2).containsExactly(TEST_PERSON_2); + + Criteria containsCaseSensitive3 = Criteria.getInstance(CriteriaType.CONTAINING, "id", + Collections.singletonList("3"), Part.IgnoreCaseType.NEVER); + List people3 = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(containsCaseSensitive3), Person.class, + containerName)); + assertThat(people3).containsExactly(TEST_PERSON_3); + } + + @Test + public void testNotContainsCriteria() { + cosmosTemplate.insert(TEST_PERSON_2, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2))); + cosmosTemplate.insert(TEST_PERSON_3, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_3))); + Person TEST_PERSON_4 = new Person("id-4", "NEW_FIRST_NAME", NEW_LAST_NAME, HOBBIES, + ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY); + cosmosTemplate.insert(TEST_PERSON_4, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_4))); + + Criteria notContainsCaseSensitive = Criteria.getInstance(CriteriaType.NOT_CONTAINING, "firstName", + Collections.singletonList("li"), Part.IgnoreCaseType.NEVER); + List people = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(notContainsCaseSensitive), Person.class, + containerName)); + assertThat(people).containsExactly(TEST_PERSON_2, TEST_PERSON_3, TEST_PERSON_4); + + Criteria notContainsNotCaseSensitive = Criteria.getInstance(CriteriaType.NOT_CONTAINING, "firstName", + Collections.singletonList("new"), Part.IgnoreCaseType.ALWAYS); + List people2 = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(notContainsNotCaseSensitive), Person.class, + containerName)); + assertThat(people2).containsExactly(TEST_PERSON); + } + + @Test + public void testNotContainsCriteria2() { + cosmosTemplate.insert(TEST_PERSON_2, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2))); + cosmosTemplate.insert(TEST_PERSON_3, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_3))); + + Criteria notContainsCaseSensitive = Criteria.getInstance(CriteriaType.NOT_CONTAINING, "id", + Collections.singletonList("1"), Part.IgnoreCaseType.NEVER); + List people = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(notContainsCaseSensitive), Person.class, + containerName)); + assertThat(people).containsExactly(TEST_PERSON_2, TEST_PERSON_3); + + Criteria notContainsCaseSensitive2 = Criteria.getInstance(CriteriaType.NOT_CONTAINING, "id", + Collections.singletonList("2"), Part.IgnoreCaseType.NEVER); + List people2 = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(notContainsCaseSensitive2), Person.class, + containerName)); + assertThat(people2).containsExactly(TEST_PERSON, TEST_PERSON_3); + + Criteria notContainsCaseSensitive3 = Criteria.getInstance(CriteriaType.NOT_CONTAINING, "id", + Collections.singletonList("3"), Part.IgnoreCaseType.NEVER); + List people3 = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(notContainsCaseSensitive3), Person.class, + containerName)); + assertThat(people3).containsExactly(TEST_PERSON, TEST_PERSON_2); + } + @Test public void testIsNotNullCriteriaCaseSensitive() { Criteria hasLastName = Criteria.getInstance(CriteriaType.IS_NOT_NULL, "lastName", diff --git a/sdk/cosmos/azure-spring-data-cosmos/CHANGELOG.md b/sdk/cosmos/azure-spring-data-cosmos/CHANGELOG.md index 5582c4346338..ca9a43a5ec8e 100644 --- a/sdk/cosmos/azure-spring-data-cosmos/CHANGELOG.md +++ b/sdk/cosmos/azure-spring-data-cosmos/CHANGELOG.md @@ -3,6 +3,7 @@ ### 3.26.0-beta.1 (Unreleased) #### Features Added +* Added support for NOT CONTAINS. - See [PR 30379](https://github.com/Azure/azure-sdk-for-java/pull/30379) #### Breaking Changes diff --git a/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/generator/AbstractQueryGenerator.java b/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/generator/AbstractQueryGenerator.java index 5c480c878500..ca9d9047a47e 100644 --- a/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/generator/AbstractQueryGenerator.java +++ b/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/generator/AbstractQueryGenerator.java @@ -195,6 +195,7 @@ private String generateQueryBody(@NonNull Criteria criteria, @NonNull List