From 32f961fef7786708f17a096aaf05491ed51eefd2 Mon Sep 17 00:00:00 2001 From: Trevor Anderson Date: Wed, 10 Aug 2022 12:29:52 -0400 Subject: [PATCH 1/4] Adding NOT CONTAINS support to Criteria Queries. Adding unit tests for NOT CONTAINS and CONTAINS since they did not exist. --- .../data/cosmos/core/CosmosTemplateIT.java | 46 +++++++++++++++++++ .../generator/AbstractQueryGenerator.java | 1 + .../data/cosmos/core/query/CriteriaType.java | 11 ++++- 3 files changed, 57 insertions(+), 1 deletion(-) 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..1127fd5b1614 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,52 @@ 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 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 testIsNotNullCriteriaCaseSensitive() { Criteria hasLastName = Criteria.getInstance(CriteriaType.IS_NOT_NULL, "lastName", 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 Date: Wed, 10 Aug 2022 12:32:27 -0400 Subject: [PATCH 2/4] Updating CHANGELOG.md --- sdk/cosmos/azure-spring-data-cosmos/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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 From 81c4b7f6e17d3e2182c7cd928b01901fd9e1b7df Mon Sep 17 00:00:00 2001 From: Trevor Anderson Date: Wed, 10 Aug 2022 14:32:21 -0400 Subject: [PATCH 3/4] Adding more tests. --- .../data/cosmos/core/CosmosTemplateIT.java | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) 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 1127fd5b1614..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 @@ -650,17 +650,39 @@ public void testContainsCriteria() { 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))); @@ -673,17 +695,39 @@ public void testNotContainsCriteria() { 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", From 12587619c0b7f1d782a540a18f8343a9cdd26764 Mon Sep 17 00:00:00 2001 From: Trevor Anderson Date: Thu, 11 Aug 2022 10:10:47 -0400 Subject: [PATCH 4/4] Fixing ordering. --- .../spring/data/cosmos/core/query/CriteriaType.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/query/CriteriaType.java b/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/query/CriteriaType.java index 28295b83de2d..50e2354b9171 100644 --- a/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/query/CriteriaType.java +++ b/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/query/CriteriaType.java @@ -95,11 +95,6 @@ public enum CriteriaType { */ CONTAINING("CONTAINS"), - /** - * Not Contain - */ - NOT_CONTAINING("NOT CONTAINS"), - /** * Ends with */ @@ -133,7 +128,12 @@ public enum CriteriaType { /** * String equals */ - STRING_EQUALS("STRINGEQUALS"); + STRING_EQUALS("STRINGEQUALS"), + + /** + * Not Contain + */ + NOT_CONTAINING("NOT CONTAINS"); private String sqlKeyword;