Skip to content

Commit

Permalink
add test cases for parameter name generator using async API
Browse files Browse the repository at this point in the history
Assert the change in the behaviour of the query parameter name generator by adding more tests
using the reactive cosmos template

References #23084
  • Loading branch information
gogopavl committed Aug 20, 2021
1 parent dce6877 commit 5399af2
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.azure.spring.data.cosmos.domain.Address;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class TestConstants {

Expand Down Expand Up @@ -94,6 +96,11 @@ public final class TestConstants {

public static final Integer AGE = 24;

public static final Map<String, String> PASSPORT_IDS_BY_COUNTRY = new HashMap<String, String>() {{
put("United States of America", "123456789");
put("Côte d'Ivoire", "IC1234567");
}};

private TestConstants() {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import static com.azure.spring.data.cosmos.common.TestConstants.PAGE_SIZE_1;
import static com.azure.spring.data.cosmos.common.TestConstants.PAGE_SIZE_2;
import static com.azure.spring.data.cosmos.common.TestConstants.PAGE_SIZE_3;
import static com.azure.spring.data.cosmos.common.TestConstants.PASSPORT_IDS_BY_COUNTRY;
import static com.azure.spring.data.cosmos.common.TestConstants.UPDATED_FIRST_NAME;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
Expand All @@ -79,15 +80,13 @@
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class CosmosTemplateIT {
private static final Person TEST_PERSON = new Person(ID_1, FIRST_NAME, LAST_NAME, HOBBIES,
ADDRESSES, AGE);
ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);

private static final Person TEST_PERSON_2 = new Person(ID_2,
NEW_FIRST_NAME,
NEW_LAST_NAME, HOBBIES, ADDRESSES, AGE);
private static final Person TEST_PERSON_2 = new Person(ID_2, NEW_FIRST_NAME, NEW_LAST_NAME, HOBBIES,
ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);

private static final Person TEST_PERSON_3 = new Person(ID_3,
NEW_FIRST_NAME,
NEW_LAST_NAME, HOBBIES, ADDRESSES, AGE);
private static final Person TEST_PERSON_3 = new Person(ID_3, NEW_FIRST_NAME, NEW_LAST_NAME, HOBBIES,
ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);

private static final String PRECONDITION_IS_NOT_MET = "is not met";

Expand Down Expand Up @@ -134,7 +133,6 @@ public void setUp() throws ClassNotFoundException {

collectionManager.ensureContainersCreatedAndEmpty(cosmosTemplate, Person.class,
GenIdEntity.class, AuditableEntity.class);

insertedPerson = cosmosTemplate.insert(Person.class.getSimpleName(), TEST_PERSON,
new PartitionKey(TEST_PERSON.getLastName()));
}
Expand All @@ -157,7 +155,7 @@ public void testInsertDuplicateIdShouldFailWithConflictException() {

@Test(expected = CosmosAccessException.class)
public void testInsertShouldFailIfColumnNotAnnotatedWithAutoGenerate() {
final Person person = new Person(null, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES, AGE);
final Person person = new Person(null, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);
cosmosTemplate.insert(Person.class.getSimpleName(), person, new PartitionKey(person.getLastName()));
}

Expand Down Expand Up @@ -222,8 +220,8 @@ public void testUpsertNewDocument() {
final String firstName = NEW_FIRST_NAME
+ "_"
+ UUID.randomUUID().toString();
final Person newPerson = new Person(TEST_PERSON.getId(), firstName,
NEW_FIRST_NAME, null, null, AGE);
final Person newPerson = new Person(TEST_PERSON.getId(), firstName, NEW_FIRST_NAME, null, null,
AGE, PASSPORT_IDS_BY_COUNTRY);

final Person person = cosmosTemplate.upsertAndReturnEntity(Person.class.getSimpleName(), newPerson);

Expand All @@ -236,7 +234,8 @@ public void testUpsertNewDocument() {
@Test
public void testUpdateWithReturnEntity() {
final Person updated = new Person(TEST_PERSON.getId(), UPDATED_FIRST_NAME,
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(), TEST_PERSON.getShippingAddresses(), AGE);
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(), TEST_PERSON.getShippingAddresses(),
AGE, PASSPORT_IDS_BY_COUNTRY);
updated.set_etag(insertedPerson.get_etag());

final Person updatedPerson = cosmosTemplate.upsertAndReturnEntity(Person.class.getSimpleName(), updated);
Expand All @@ -251,7 +250,8 @@ public void testUpdateWithReturnEntity() {
@Test
public void testUpdate() {
final Person updated = new Person(TEST_PERSON.getId(), UPDATED_FIRST_NAME,
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(), TEST_PERSON.getShippingAddresses(), AGE);
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(), TEST_PERSON.getShippingAddresses(),
AGE, PASSPORT_IDS_BY_COUNTRY);
updated.set_etag(insertedPerson.get_etag());

final Person person = cosmosTemplate.upsertAndReturnEntity(Person.class.getSimpleName(), updated);
Expand All @@ -265,7 +265,8 @@ public void testUpdate() {
@Test
public void testOptimisticLockWhenUpdatingWithWrongEtag() {
final Person updated = new Person(TEST_PERSON.getId(), UPDATED_FIRST_NAME,
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(), TEST_PERSON.getShippingAddresses(), AGE);
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(), TEST_PERSON.getShippingAddresses(),
AGE, PASSPORT_IDS_BY_COUNTRY);
updated.set_etag(WRONG_ETAG);

try {
Expand Down Expand Up @@ -436,9 +437,12 @@ public void testPaginationQuery() {

@Test
public void testFindWithSortAndLimit() {
final Person testPerson4 = new Person("id_4", "fred", NEW_LAST_NAME, HOBBIES, ADDRESSES, AGE);
final Person testPerson5 = new Person("id_5", "barney", NEW_LAST_NAME, HOBBIES, ADDRESSES, AGE);
final Person testPerson6 = new Person("id_6", "george", NEW_LAST_NAME, HOBBIES, ADDRESSES, AGE);
final Person testPerson4 = new Person("id_4", "fred", NEW_LAST_NAME, HOBBIES,
ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);
final Person testPerson5 = new Person("id_5", "barney", NEW_LAST_NAME, HOBBIES,
ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);
final Person testPerson6 = new Person("id_6", "george", NEW_LAST_NAME, HOBBIES,
ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);

insertPerson(testPerson4);
insertPerson(testPerson5);
Expand Down Expand Up @@ -491,8 +495,10 @@ public void testFindAllWithPageableAndSort() {

@Test
public void testFindAllWithTwoPagesAndVerifySortOrder() {
final Person testPerson4 = new Person("id_4", "barney", NEW_LAST_NAME, HOBBIES, ADDRESSES, AGE);
final Person testPerson5 = new Person("id_5", "fred", NEW_LAST_NAME, HOBBIES, ADDRESSES, AGE);
final Person testPerson4 = new Person("id_4", "barney", NEW_LAST_NAME, HOBBIES,
ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);
final Person testPerson5 = new Person("id_5", "fred", NEW_LAST_NAME, HOBBIES,
ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);

cosmosTemplate.insert(TEST_PERSON_2,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2)));
Expand Down Expand Up @@ -568,7 +574,7 @@ public void testBetweenCriteria() {
}

@Test
public void testEqualCriteriaWithNestedProperty() {
public void testFindWithEqualCriteriaContainingNestedProperty() {
String postalCode = ADDRESSES.get(0).getPostalCode();
String subjectWithNestedProperty = "shippingAddresses[0]['postalCode']";
Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, subjectWithNestedProperty,
Expand All @@ -580,6 +586,19 @@ public void testEqualCriteriaWithNestedProperty() {
assertThat(people).containsExactly(TEST_PERSON);
}

@Test
public void testRunQueryWithEqualCriteriaContainingSpaces() {
String usaPassportId = PASSPORT_IDS_BY_COUNTRY.get("United States of America");
String subjectWithSpaces = "passportIdsByCountry['United States of America']";
Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, subjectWithSpaces,
Collections.singletonList(usaPassportId), Part.IgnoreCaseType.NEVER);
final SqlQuerySpec sqlQuerySpec = new FindQuerySpecGenerator().generateCosmos(new CosmosQuery(criteria));

List<Person> people = TestUtils.toList(cosmosTemplate.runQuery(sqlQuerySpec, Person.class, Person.class));

assertThat(people).containsExactly(TEST_PERSON);
}

@Test
public void testRunQueryWithSimpleReturnType() {
Criteria ageBetween = Criteria.getInstance(CriteriaType.BETWEEN, "age", Arrays.asList(AGE - 1, AGE + 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosException;
import com.azure.cosmos.implementation.ConflictException;
import com.azure.cosmos.models.CosmosContainerProperties;
import com.azure.cosmos.models.CosmosContainerResponse;
import com.azure.cosmos.models.PartitionKey;
import com.azure.cosmos.models.SqlQuerySpec;
Expand Down Expand Up @@ -60,6 +59,7 @@
import static com.azure.spring.data.cosmos.common.TestConstants.FIRST_NAME;
import static com.azure.spring.data.cosmos.common.TestConstants.HOBBIES;
import static com.azure.spring.data.cosmos.common.TestConstants.LAST_NAME;
import static com.azure.spring.data.cosmos.common.TestConstants.PASSPORT_IDS_BY_COUNTRY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand All @@ -70,21 +70,17 @@
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class ReactiveCosmosTemplateIT {
private static final Person TEST_PERSON = new Person(TestConstants.ID_1,
TestConstants.FIRST_NAME,
TestConstants.LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE);
private static final Person TEST_PERSON = new Person(TestConstants.ID_1, TestConstants.FIRST_NAME,
TestConstants.LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);

private static final Person TEST_PERSON_2 = new Person(TestConstants.ID_2,
TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE);
private static final Person TEST_PERSON_2 = new Person(TestConstants.ID_2, TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);

private static final Person TEST_PERSON_3 = new Person(TestConstants.ID_3,
TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE);
private static final Person TEST_PERSON_3 = new Person(TestConstants.ID_3, TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);

private static final Person TEST_PERSON_4 = new Person(TestConstants.ID_4,
TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE);
private static final Person TEST_PERSON_4 = new Person(TestConstants.ID_4, TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);

private static final String PRECONDITION_IS_NOT_MET = "is not met";
private static final String WRONG_ETAG = "WRONG_ETAG";
Expand Down Expand Up @@ -245,7 +241,7 @@ public void testInsertWithContainerName() {

@Test
public void testInsertShouldFailIfColumnNotAnnotatedWithAutoGenerate() {
final Person person = new Person(null, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES, AGE);
final Person person = new Person(null, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);
Mono<GenIdEntity> entityMono = cosmosTemplate.insert(Person.class.getSimpleName(),
person, new PartitionKey(person.getLastName()));
StepVerifier.create(entityMono).verifyError(CosmosAccessException.class);
Expand Down Expand Up @@ -280,8 +276,8 @@ public void testUpsert() {
@Test
public void testOptimisticLockWhenUpdatingWithWrongEtag() {
final Person updated = new Person(TEST_PERSON.getId(), TestConstants.UPDATED_FIRST_NAME,
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(),
TEST_PERSON.getShippingAddresses(), AGE);
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(), TEST_PERSON.getShippingAddresses(),
AGE, PASSPORT_IDS_BY_COUNTRY);
updated.set_etag(WRONG_ETAG);

try {
Expand Down Expand Up @@ -508,6 +504,31 @@ public void testRunQueryWithReturnTypeContainingLocalDateTime() {
StepVerifier.create(flux).expectNextCount(1).verifyComplete();
}

@Test
public void testFindWithEqualCriteriaContainingNestedProperty() {
String postalCode = ADDRESSES.get(0).getPostalCode();
String subjectWithNestedProperty = "shippingAddresses[0]['postalCode']";
Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, subjectWithNestedProperty,
Collections.singletonList(postalCode), Part.IgnoreCaseType.NEVER);

final Flux<Person> people = cosmosTemplate.find(new CosmosQuery(criteria), Person.class, containerName);

StepVerifier.create(people).expectNextCount(1).verifyComplete();
}

@Test
public void testRunQueryWithEqualCriteriaContainingSpecialChars() {
String ivoryCoastPassportId = PASSPORT_IDS_BY_COUNTRY.get("Côte d'Ivoire");
String subjectWithSpecialChars = "passportIdsByCountry[\"Côte d'Ivoire\"]";
Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, subjectWithSpecialChars,
Collections.singletonList(ivoryCoastPassportId), Part.IgnoreCaseType.NEVER);
final SqlQuerySpec sqlQuerySpec = new FindQuerySpecGenerator().generateCosmos(new CosmosQuery(criteria));

final Flux<Person> people = cosmosTemplate.runQuery(sqlQuerySpec, Person.class, Person.class);

StepVerifier.create(people).expectNextCount(1).verifyComplete();
}

@Test
public void createWithAutoscale() {
final CosmosEntityInformation<AutoScaleSample, String> autoScaleSampleInfo =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.data.annotation.Version;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Container()
Expand All @@ -22,17 +23,19 @@ public class Person {
private List<String> hobbies;
private List<Address> shippingAddresses;
private Integer age;
private Map<String, String> passportIdsByCountry;
@Version
private String _etag;

public Person(String id, String firstName, String lastName, List<String> hobbies, List<Address> shippingAddresses,
Integer age) {
Integer age, Map<String, String> passportIDsByCountry) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.hobbies = hobbies;
this.shippingAddresses = shippingAddresses;
this.age = age;
this.passportIdsByCountry = passportIDsByCountry;
}

public Person() {
Expand Down Expand Up @@ -94,6 +97,14 @@ public void setAge(Integer age) {
this.age = age;
}

public Map<String, String> getPassportIdsByCountry() {
return passportIdsByCountry;
}

public void setPassportIdsByCountry(Map<String, String> passportIdsByCountry) {
this.passportIdsByCountry = passportIdsByCountry;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -108,12 +119,13 @@ public boolean equals(Object o) {
&& Objects.equals(lastName, person.lastName)
&& Objects.equals(hobbies, person.hobbies)
&& Objects.equals(shippingAddresses, person.shippingAddresses)
&& Objects.equals(age, person.age);
&& Objects.equals(age, person.age)
&& Objects.equals(passportIdsByCountry, person.passportIdsByCountry);
}

@Override
public int hashCode() {
return Objects.hash(id, firstName, lastName, hobbies, shippingAddresses, age);
return Objects.hash(id, firstName, lastName, hobbies, shippingAddresses, age, passportIdsByCountry);
}

@Override
Expand All @@ -134,6 +146,8 @@ public String toString() {
+ hobbies
+ ", shippingAddresses="
+ shippingAddresses
+ ", passportIdsByCountry="
+ passportIdsByCountry
+ ", _etag='"
+ _etag
+ '\''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -24,16 +23,15 @@
import java.lang.reflect.Field;

import static com.azure.spring.data.cosmos.common.TestConstants.AGE;
import static com.azure.spring.data.cosmos.common.TestConstants.PASSPORT_IDS_BY_COUNTRY;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestRepositoryConfig.class, SecondaryTestRepositoryConfig.class})
public class MultiCosmosTemplateIT {
private static final Person PRIMARY_TEST_PERSON = new Person(TestConstants.ID_1,
TestConstants.FIRST_NAME,
TestConstants.LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE);
private static final Person SECONDARY_TEST_PERSON = new Person(TestConstants.ID_2,
TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE);
private static final Person PRIMARY_TEST_PERSON = new Person(TestConstants.ID_1, TestConstants.FIRST_NAME,
TestConstants.LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);
private static final Person SECONDARY_TEST_PERSON = new Person(TestConstants.ID_2, TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE, PASSPORT_IDS_BY_COUNTRY);

@ClassRule
public static final ReactiveIntegrationTestCollectionManager primaryCollectionManager = new ReactiveIntegrationTestCollectionManager();
Expand Down

0 comments on commit 5399af2

Please sign in to comment.