* Updated tests to optimize resources, not create resources multiple times, and re-use existing resources

* Updated cosmosItemProperties toObject to mappingDocumentDbConverter read() API call
This commit is contained in:
Kushagra Thapar 2019-09-25 08:07:18 -07:00 коммит произвёл GitHub
Родитель 488660839d
Коммит b500a9ac7f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 235 добавлений и 164 удалений

Просмотреть файл

@ -276,7 +276,7 @@ public class DocumentDbTemplate implements DocumentDbOperations, ApplicationCont
try {
return findDocuments(query, domainClass, collectionName)
.stream()
.map(cosmosItemProperties -> cosmosItemProperties.toObject(domainClass))
.map(cosmosItemProperties -> toDomainObject(domainClass, cosmosItemProperties))
.collect(Collectors.toList());
} catch (Exception e) {
throw new DocumentDBAccessException("Failed to execute find operation from " + collectionName, e);
@ -496,4 +496,8 @@ public class DocumentDbTemplate implements DocumentDbOperations, ApplicationCont
.delete(options)
.block();
}
private <T> T toDomainObject(@NonNull Class<T> domainClass, CosmosItemProperties cosmosItemProperties) {
return mappingDocumentDbConverter.read(domainClass, cosmosItemProperties);
}
}

Просмотреть файл

@ -296,17 +296,16 @@ public class ReactiveCosmosTemplate implements ReactiveCosmosOperations, Applica
final boolean isCrossPartitionQuery = query.isCrossPartitionQuery(Collections.singletonList(partitionKeyName));
options.enableCrossPartitionQuery(isCrossPartitionQuery);
return cosmosClient.getDatabase(this.databaseName)
.getContainer(containerName)
.queryItems(sqlQuerySpec, options)
.onErrorResume(this::databaseAccessExceptionHandler)
.map(cosmosItemFeedResponse -> cosmosItemFeedResponse.results()
.stream()
.map(cosmosItemProperties -> cosmosClient
.getDatabase(this.databaseName)
.getContainer(containerName)
.getItem(cosmosItemProperties.id(), partitionKeyName)
.delete(new CosmosItemRequestOptions())))
.then();
.getContainer(containerName)
.queryItems(sqlQuerySpec, options)
.flatMap(cosmosItemFeedResponse -> Flux.fromIterable(cosmosItemFeedResponse.results()))
.flatMap(cosmosItemProperties -> cosmosClient
.getDatabase(this.databaseName)
.getContainer(containerName)
.getItem(cosmosItemProperties.id(), cosmosItemProperties.get(partitionKeyName))
.delete())
.onErrorResume(this::databaseAccessExceptionHandler)
.then();
}
/**

Просмотреть файл

@ -6,10 +6,8 @@
package com.microsoft.azure.spring.data.cosmosdb.core;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.documentdb.DocumentCollection;
import com.microsoft.azure.documentdb.PartitionKey;
import com.microsoft.azure.spring.data.cosmosdb.CosmosDbFactory;
import com.microsoft.azure.spring.data.cosmosdb.common.TestConstants;
import com.microsoft.azure.spring.data.cosmosdb.config.DocumentDBConfig;
import com.microsoft.azure.spring.data.cosmosdb.core.convert.MappingDocumentDbConverter;
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.DocumentDbMappingContext;
@ -47,58 +45,64 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource(value = {"classpath:application.properties"})
@PropertySource(value = { "classpath:application.properties" })
public class DocumentDbTemplateIT {
private static final Person TEST_PERSON = new Person(TestConstants.ID_1, TestConstants.FIRST_NAME,
TestConstants.LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
private static final Person TEST_PERSON = new Person(ID_1, FIRST_NAME, LAST_NAME, HOBBIES,
ADDRESSES);
private static final Person TEST_PERSON_2 = new Person(TestConstants.ID_2, TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
private static final Person TEST_PERSON_2 = new Person(ID_2,
NEW_FIRST_NAME,
NEW_LAST_NAME, HOBBIES, ADDRESSES);
private static final Person TEST_PERSON_3 = new Person(TestConstants.ID_3, TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
private static final Person TEST_PERSON_3 = new Person(ID_3,
NEW_FIRST_NAME,
NEW_LAST_NAME, HOBBIES, ADDRESSES);
@Value("${cosmosdb.uri}")
private String documentDbUri;
@Value("${cosmosdb.key}")
private String documentDbKey;
private DocumentDbTemplate dbTemplate;
private MappingDocumentDbConverter dbConverter;
private DocumentDbMappingContext mappingContext;
private DocumentCollection collectionPerson;
private DocumentDbEntityInformation<Person, String> personInfo;
private String collectionName;
private static DocumentDbTemplate dbTemplate;
private static DocumentDbEntityInformation<Person, String> personInfo;
private static String collectionName;
private static boolean initialized;
@Autowired
private ApplicationContext applicationContext;
@Before
public void setup() throws ClassNotFoundException {
final DocumentDBConfig dbConfig = DocumentDBConfig.builder(documentDbUri, documentDbKey, DB_NAME).build();
final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig);
if (!initialized) {
final DocumentDBConfig dbConfig = DocumentDBConfig.builder(documentDbUri,
documentDbKey, DB_NAME).build();
final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig);
mappingContext = new DocumentDbMappingContext();
personInfo = new DocumentDbEntityInformation<>(Person.class);
collectionName = personInfo.getCollectionName();
final DocumentDbMappingContext mappingContext = new DocumentDbMappingContext();
personInfo = new DocumentDbEntityInformation<>(Person.class);
collectionName = personInfo.getCollectionName();
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
dbConverter = new MappingDocumentDbConverter(mappingContext, null);
dbTemplate = new DocumentDbTemplate(cosmosDbFactory, dbConverter, DB_NAME);
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
collectionPerson = dbTemplate.createCollectionIfNotExists(this.personInfo);
final MappingDocumentDbConverter dbConverter =
new MappingDocumentDbConverter(mappingContext, null);
dbTemplate = new DocumentDbTemplate(cosmosDbFactory, dbConverter, DB_NAME);
dbTemplate.createCollectionIfNotExists(personInfo);
initialized = true;
}
dbTemplate.insert(Person.class.getSimpleName(), TEST_PERSON, null);
}
@After
public void cleanup() {
dbTemplate.deleteCollection(Person.class.getSimpleName());
dbTemplate.deleteAll(Person.class.getSimpleName(), Person.class);
}
@Test(expected = DocumentDBAccessException.class)
public void testInsertDuplicateId() {
dbTemplate.insert(Person.class.getSimpleName(), TEST_PERSON, null);
dbTemplate.insert(Person.class.getSimpleName(), TEST_PERSON,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON)));
}
@Test
@ -111,18 +115,20 @@ public class DocumentDbTemplateIT {
@Test
public void testFindById() {
final Person result = dbTemplate.findById(Person.class.getSimpleName(),
TEST_PERSON.getId(), Person.class);
TEST_PERSON.getId(), Person.class);
assertEquals(result, TEST_PERSON);
final Person nullResult = dbTemplate.findById(Person.class.getSimpleName(),
TestConstants.NOT_EXIST_ID, Person.class);
NOT_EXIST_ID, Person.class);
assertThat(nullResult).isNull();
}
@Test
public void testFindByMultiIds() {
dbTemplate.insert(TEST_PERSON_2, null);
dbTemplate.insert(TEST_PERSON_3, null);
dbTemplate.insert(TEST_PERSON_2,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2)));
dbTemplate.insert(TEST_PERSON_3,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_3)));
final List<Object> ids = Lists.newArrayList(ID_1, ID_2, ID_3);
final List<Person> result = dbTemplate.findByIds(ids, Person.class, collectionName);
@ -135,12 +141,15 @@ public class DocumentDbTemplateIT {
@Test
public void testUpsertNewDocument() {
// Delete first as was inserted in setup
dbTemplate.deleteById(Person.class.getSimpleName(), TEST_PERSON.getId(), null);
dbTemplate.deleteById(Person.class.getSimpleName(), TEST_PERSON.getId(),
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON)));
final String firstName = TestConstants.NEW_FIRST_NAME + "_" + UUID.randomUUID().toString();
final Person newPerson = new Person(TEST_PERSON.getId(), firstName, TestConstants.NEW_FIRST_NAME, null, null);
final String firstName = NEW_FIRST_NAME + "_" + UUID.randomUUID().toString();
final Person newPerson = new Person(TEST_PERSON.getId(), firstName,
NEW_FIRST_NAME, null, null);
dbTemplate.upsert(Person.class.getSimpleName(), newPerson, null);
dbTemplate.upsert(Person.class.getSimpleName(), newPerson,
new PartitionKey(personInfo.getPartitionKeyFieldValue(newPerson)));
final List<Person> result = dbTemplate.findAll(Person.class);
@ -150,12 +159,14 @@ public class DocumentDbTemplateIT {
@Test
public void testUpdate() {
final Person updated = new Person(TEST_PERSON.getId(), TestConstants.UPDATED_FIRST_NAME,
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(), TEST_PERSON.getShippingAddresses());
dbTemplate.upsert(Person.class.getSimpleName(), updated, null);
final Person updated = new Person(TEST_PERSON.getId(), UPDATED_FIRST_NAME,
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(),
TEST_PERSON.getShippingAddresses());
dbTemplate.upsert(Person.class.getSimpleName(), updated,
new PartitionKey(personInfo.getPartitionKeyFieldValue(updated)));
final Person result = dbTemplate.findById(Person.class.getSimpleName(),
updated.getId(), Person.class);
updated.getId(), Person.class);
assertEquals(result, updated);
}
@ -165,7 +176,8 @@ public class DocumentDbTemplateIT {
dbTemplate.insert(TEST_PERSON_2, null);
assertThat(dbTemplate.findAll(Person.class).size()).isEqualTo(2);
dbTemplate.deleteById(Person.class.getSimpleName(), TEST_PERSON.getId(), null);
dbTemplate.deleteById(Person.class.getSimpleName(), TEST_PERSON.getId(),
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON)));
final List<Person> result = dbTemplate.findAll(Person.class);
assertThat(result.size()).isEqualTo(1);
@ -177,7 +189,8 @@ public class DocumentDbTemplateIT {
final long prevCount = dbTemplate.count(collectionName);
assertThat(prevCount).isEqualTo(1);
dbTemplate.insert(TEST_PERSON_2, null);
dbTemplate.insert(TEST_PERSON_2,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2)));
final long newCount = dbTemplate.count(collectionName);
assertThat(newCount).isEqualTo(2);
@ -185,7 +198,8 @@ public class DocumentDbTemplateIT {
@Test
public void testCountByQuery() {
dbTemplate.insert(TEST_PERSON_2, null);
dbTemplate.insert(TEST_PERSON_2,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2)));
final Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, "firstName",
Collections.singletonList(TEST_PERSON_2.getFirstName()));
@ -197,7 +211,8 @@ public class DocumentDbTemplateIT {
@Test
public void testFindAllPageableMultiPages() {
dbTemplate.insert(TEST_PERSON_2, null);
dbTemplate.insert(TEST_PERSON_2,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2)));
final DocumentDbPageRequest pageRequest = new DocumentDbPageRequest(0, PAGE_SIZE_1, null);
final Page<Person> page1 = dbTemplate.findAll(pageRequest, Person.class, collectionName);
@ -205,14 +220,16 @@ public class DocumentDbTemplateIT {
assertThat(page1.getContent().size()).isEqualTo(PAGE_SIZE_1);
validateNonLastPage(page1, PAGE_SIZE_1);
final Page<Person> page2 = dbTemplate.findAll(page1.getPageable(), Person.class, collectionName);
final Page<Person> page2 = dbTemplate.findAll(page1.getPageable(), Person.class,
collectionName);
assertThat(page2.getContent().size()).isEqualTo(1);
validateLastPage(page2, PAGE_SIZE_1);
}
@Test
public void testPaginationQuery() {
dbTemplate.insert(TEST_PERSON_2, null);
dbTemplate.insert(TEST_PERSON_2,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2)));
final Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, "firstName",
Collections.singletonList(FIRST_NAME));
@ -226,8 +243,10 @@ public class DocumentDbTemplateIT {
@Test
public void testFindAllWithPageableAndSort() {
dbTemplate.insert(TEST_PERSON_2, null);
dbTemplate.insert(TEST_PERSON_3, null);
dbTemplate.insert(TEST_PERSON_2,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2)));
dbTemplate.insert(TEST_PERSON_3,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_3)));
final Sort sort = new Sort(Sort.Direction.DESC, "firstName");
final PageRequest pageRequest = DocumentDbPageRequest.of(0, PAGE_SIZE_3, null, sort);
@ -248,15 +267,20 @@ public class DocumentDbTemplateIT {
final Person testPerson4 = new Person("id_4", "barney", NEW_LAST_NAME, HOBBIES, ADDRESSES);
final Person testPerson5 = new Person("id_5", "fred", NEW_LAST_NAME, HOBBIES, ADDRESSES);
dbTemplate.insert(TEST_PERSON_2, null);
dbTemplate.insert(TEST_PERSON_3, null);
dbTemplate.insert(testPerson4, null);
dbTemplate.insert(testPerson5, null);
dbTemplate.insert(TEST_PERSON_2,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2)));
dbTemplate.insert(TEST_PERSON_3,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_3)));
dbTemplate.insert(testPerson4,
new PartitionKey(personInfo.getPartitionKeyFieldValue(testPerson4)));
dbTemplate.insert(testPerson5,
new PartitionKey(personInfo.getPartitionKeyFieldValue(testPerson5)));
final Sort sort = new Sort(Sort.Direction.ASC, "firstName");
final PageRequest pageRequest = DocumentDbPageRequest.of(0, PAGE_SIZE_3, null, sort);
final Page<Person> firstPage = dbTemplate.findAll(pageRequest, Person.class, collectionName);
final Page<Person> firstPage = dbTemplate.findAll(pageRequest, Person.class,
collectionName);
assertThat(firstPage.getContent().size()).isEqualTo(3);
validateNonLastPage(firstPage, PAGE_SIZE_3);
@ -266,7 +290,8 @@ public class DocumentDbTemplateIT {
assertThat(firstPageResults.get(1).getFirstName()).isEqualTo(FIRST_NAME);
assertThat(firstPageResults.get(2).getFirstName()).isEqualTo(testPerson5.getFirstName());
final Page<Person> secondPage = dbTemplate.findAll(firstPage.getPageable(), Person.class, collectionName);
final Page<Person> secondPage = dbTemplate.findAll(firstPage.getPageable(), Person.class,
collectionName);
assertThat(secondPage.getContent().size()).isEqualTo(2);
validateLastPage(secondPage, PAGE_SIZE_3);

Просмотреть файл

@ -6,7 +6,6 @@
package com.microsoft.azure.spring.data.cosmosdb.core;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.documentdb.PartitionKey;
import com.microsoft.azure.spring.data.cosmosdb.CosmosDbFactory;
import com.microsoft.azure.spring.data.cosmosdb.config.DocumentDBConfig;
@ -58,28 +57,33 @@ public class DocumentDbTemplatePartitionIT {
@Value("${cosmosdb.key}")
private String documentDbKey;
private DocumentDbTemplate dbTemplate;
private String collectionName;
private DocumentDbEntityInformation<PartitionPerson, String> personInfo;
private static DocumentDbTemplate dbTemplate;
private static String collectionName;
private static DocumentDbEntityInformation<PartitionPerson, String> personInfo;
private static boolean initialized;
@Autowired
private ApplicationContext applicationContext;
@Before
public void setup() throws ClassNotFoundException {
final DocumentDBConfig dbConfig = DocumentDBConfig.builder(documentDbUri, documentDbKey, DB_NAME).build();
final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig);
final DocumentDbMappingContext mappingContext = new DocumentDbMappingContext();
if (!initialized) {
final DocumentDBConfig dbConfig = DocumentDBConfig.builder(documentDbUri, documentDbKey, DB_NAME).build();
final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig);
final DocumentDbMappingContext mappingContext = new DocumentDbMappingContext();
personInfo = new DocumentDbEntityInformation<>(PartitionPerson.class);
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
personInfo = new DocumentDbEntityInformation<>(PartitionPerson.class);
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
final MappingDocumentDbConverter dbConverter = new MappingDocumentDbConverter(mappingContext, null);
final MappingDocumentDbConverter dbConverter = new MappingDocumentDbConverter(mappingContext, null);
dbTemplate = new DocumentDbTemplate(cosmosDbFactory, dbConverter, DB_NAME);
collectionName = personInfo.getCollectionName();
dbTemplate = new DocumentDbTemplate(cosmosDbFactory, dbConverter, DB_NAME);
collectionName = personInfo.getCollectionName();
dbTemplate.createCollectionIfNotExists(personInfo);
initialized = true;
}
dbTemplate.createCollectionIfNotExists(personInfo);
dbTemplate.insert(PartitionPerson.class.getSimpleName(), TEST_PERSON,
new PartitionKey(TEST_PERSON.getLastName()));
}

Просмотреть файл

@ -8,7 +8,6 @@ package com.microsoft.azure.spring.data.cosmosdb.core;
import com.azure.data.cosmos.CosmosClientException;
import com.azure.data.cosmos.CosmosKeyCredential;
import com.azure.data.cosmos.PartitionKey;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.spring.data.cosmosdb.CosmosDbFactory;
import com.microsoft.azure.spring.data.cosmosdb.common.TestConstants;
import com.microsoft.azure.spring.data.cosmosdb.config.DocumentDBConfig;
@ -46,19 +45,23 @@ import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource(value = {"classpath:application.properties"})
@PropertySource(value = { "classpath:application.properties" })
public class ReactiveCosmosTemplateIT {
private static final Person TEST_PERSON = new Person(TestConstants.ID_1, TestConstants.FIRST_NAME,
TestConstants.LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
private static final Person TEST_PERSON = new Person(TestConstants.ID_1,
TestConstants.FIRST_NAME,
TestConstants.LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
private static final Person TEST_PERSON_2 = new Person(TestConstants.ID_2, TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
private static final Person TEST_PERSON_2 = new Person(TestConstants.ID_2,
TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
private static final Person TEST_PERSON_3 = new Person(TestConstants.ID_3, TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
private static final Person TEST_PERSON_3 = new Person(TestConstants.ID_3,
TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
private static final Person TEST_PERSON_4 = new Person(TestConstants.ID_4, TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
private static final Person TEST_PERSON_4 = new Person(TestConstants.ID_4,
TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
@Value("${cosmosdb.uri}")
private String documentDbUri;
@ -67,42 +70,52 @@ public class ReactiveCosmosTemplateIT {
@Value("${cosmosdb.secondaryKey}")
private String documentDbSecondaryKey;
private ReactiveCosmosTemplate cosmosTemplate;
private String containerName;
private CosmosKeyCredential cosmosKeyCredential;
private static ReactiveCosmosTemplate cosmosTemplate;
private static String containerName;
private static DocumentDbEntityInformation<Person, String> personInfo;
private static CosmosKeyCredential cosmosKeyCredential;
private static boolean initialized;
@Autowired
private ApplicationContext applicationContext;
@Before
public void setUp() throws ClassNotFoundException {
cosmosKeyCredential = new CosmosKeyCredential(documentDbKey);
final DocumentDBConfig dbConfig = DocumentDBConfig.builder(documentDbUri, cosmosKeyCredential, DB_NAME).build();
final CosmosDbFactory dbFactory = new CosmosDbFactory(dbConfig);
if (!initialized) {
cosmosKeyCredential = new CosmosKeyCredential(documentDbKey);
final DocumentDBConfig dbConfig = DocumentDBConfig.builder(documentDbUri,
cosmosKeyCredential, DB_NAME).build();
final CosmosDbFactory dbFactory = new CosmosDbFactory(dbConfig);
final DocumentDbMappingContext mappingContext = new DocumentDbMappingContext();
final DocumentDbEntityInformation<Person, String> personInfo = new DocumentDbEntityInformation<>(Person.class);
containerName = personInfo.getCollectionName();
final DocumentDbMappingContext mappingContext = new DocumentDbMappingContext();
personInfo = new DocumentDbEntityInformation<>(Person.class);
containerName = personInfo.getCollectionName();
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
final MappingDocumentDbConverter dbConverter = new MappingDocumentDbConverter(mappingContext, null);
cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, dbConverter, DB_NAME);
cosmosTemplate.createCollectionIfNotExists(personInfo).block().container();
cosmosTemplate.insert(TEST_PERSON).block();
final MappingDocumentDbConverter dbConverter =
new MappingDocumentDbConverter(mappingContext, null);
cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, dbConverter, DB_NAME);
cosmosTemplate.createCollectionIfNotExists(personInfo).block().container();
initialized = true;
}
cosmosTemplate.insert(TEST_PERSON,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON))).block();
}
@After
public void cleanup() {
// Reset master key
cosmosKeyCredential.key(documentDbKey);
cosmosTemplate.deleteContainer(Person.class.getSimpleName());
cosmosTemplate.deleteAll(Person.class.getSimpleName(),
personInfo.getPartitionKeyFieldName()).block();
}
@Test
public void testInsertDuplicateId() {
final Mono<Person> insertMono = cosmosTemplate.insert(TEST_PERSON);
final Mono<Person> insertMono = cosmosTemplate.insert(TEST_PERSON,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON)));
final TestSubscriber testSubscriber = new TestSubscriber();
insertMono.subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent();
@ -114,8 +127,9 @@ public class ReactiveCosmosTemplateIT {
@Test
public void testFindByID() {
final Mono<Person> findById = cosmosTemplate.findById(Person.class.getSimpleName(), TEST_PERSON.getId(),
Person.class);
final Mono<Person> findById = cosmosTemplate.findById(Person.class.getSimpleName(),
TEST_PERSON.getId(),
Person.class);
StepVerifier.create(findById)
.consumeNextWith(actual -> Assert.assertEquals(actual, TEST_PERSON))
.verifyComplete();
@ -124,7 +138,8 @@ public class ReactiveCosmosTemplateIT {
@Test
public void testFindByIDBySecondaryKey() {
cosmosKeyCredential.key(documentDbSecondaryKey);
final Mono<Person> findById = cosmosTemplate.findById(Person.class.getSimpleName(), TEST_PERSON.getId(),
final Mono<Person> findById = cosmosTemplate.findById(Person.class.getSimpleName(),
TEST_PERSON.getId(),
Person.class);
StepVerifier.create(findById).consumeNextWith(actual -> {
Assert.assertThat(actual.getFirstName(), is(equalTo(TEST_PERSON.getFirstName())));
@ -134,34 +149,39 @@ public class ReactiveCosmosTemplateIT {
@Test
public void testFindAll() {
final Flux<Person> flux = cosmosTemplate.findAll(Person.class.getSimpleName(), Person.class);
final Flux<Person> flux = cosmosTemplate.findAll(Person.class.getSimpleName(),
Person.class);
StepVerifier.create(flux).expectNextCount(1).verifyComplete();
}
@Test
public void testFindByIdWithContainerName() {
StepVerifier.create(cosmosTemplate.findById(Person.class.getSimpleName(), TEST_PERSON.getId(), Person.class))
StepVerifier.create(cosmosTemplate.findById(Person.class.getSimpleName(),
TEST_PERSON.getId(), Person.class))
.consumeNextWith(actual -> Assert.assertEquals(actual, TEST_PERSON))
.verifyComplete();
}
@Test
public void testInsert() {
StepVerifier.create(cosmosTemplate.insert(TEST_PERSON_3))
.expectNext(TEST_PERSON_3).verifyComplete();
StepVerifier.create(cosmosTemplate.insert(TEST_PERSON_3,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_3))))
.expectNext(TEST_PERSON_3).verifyComplete();
}
@Test
public void testInsertBySecondaryKey() {
cosmosKeyCredential.key(documentDbSecondaryKey);
StepVerifier.create(cosmosTemplate.insert(TEST_PERSON_3))
StepVerifier.create(cosmosTemplate.insert(TEST_PERSON_3,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_3))))
.expectNext(TEST_PERSON_3).verifyComplete();
}
@Test
public void testInsertWithCollectionName() {
StepVerifier.create(cosmosTemplate.insert(Person.class.getSimpleName(), TEST_PERSON_2, null))
.expectNext(TEST_PERSON_2).verifyComplete();
StepVerifier.create(cosmosTemplate.insert(Person.class.getSimpleName(), TEST_PERSON_2,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2))))
.expectNext(TEST_PERSON_2).verifyComplete();
}
@Test
@ -170,7 +190,8 @@ public class ReactiveCosmosTemplateIT {
final ArrayList<String> hobbies = new ArrayList<>(p.getHobbies());
hobbies.add("more code");
p.setHobbies(hobbies);
final Mono<Person> upsert = cosmosTemplate.upsert(p, null);
final Mono<Person> upsert = cosmosTemplate.upsert(p,
new PartitionKey(personInfo.getPartitionKeyFieldValue(p)));
StepVerifier.create(upsert).expectNextCount(1).verifyComplete();
}
@ -181,7 +202,8 @@ public class ReactiveCosmosTemplateIT {
final ArrayList<String> hobbies = new ArrayList<>(p.getHobbies());
hobbies.add("more code");
p.setHobbies(hobbies);
final Mono<Person> upsert = cosmosTemplate.upsert(p, null);
final Mono<Person> upsert = cosmosTemplate.upsert(p,
new PartitionKey(personInfo.getPartitionKeyFieldValue(p)));
StepVerifier.create(upsert).expectNextCount(1).verifyComplete();
}
@ -191,17 +213,20 @@ public class ReactiveCosmosTemplateIT {
final ArrayList<String> hobbies = new ArrayList<>(p.getHobbies());
hobbies.add("more code");
p.setHobbies(hobbies);
final Mono<Person> upsert = cosmosTemplate.upsert(Person.class.getSimpleName(), p, null);
final Mono<Person> upsert = cosmosTemplate.upsert(Person.class.getSimpleName(), p,
new PartitionKey(personInfo.getPartitionKeyFieldValue(p)));
StepVerifier.create(upsert).expectNextCount(1).verifyComplete();
}
@Test
public void testDeleteById() {
cosmosTemplate.insert(TEST_PERSON_4).block();
cosmosTemplate.insert(TEST_PERSON_4,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_4))).block();
Flux<Person> flux = cosmosTemplate.findAll(Person.class.getSimpleName(), Person.class);
StepVerifier.create(flux).expectNextCount(2).verifyComplete();
final Mono<Void> voidMono = cosmosTemplate.deleteById(Person.class.getSimpleName(), TEST_PERSON_4.getId(),
PartitionKey.None);
final Mono<Void> voidMono = cosmosTemplate.deleteById(Person.class.getSimpleName(),
TEST_PERSON_4.getId(),
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_4)));
StepVerifier.create(voidMono).verifyComplete();
flux = cosmosTemplate.findAll(Person.class.getSimpleName(), Person.class);
StepVerifier.create(flux).expectNextCount(1).verifyComplete();
@ -210,11 +235,13 @@ public class ReactiveCosmosTemplateIT {
@Test
public void testDeleteByIdBySecondaryKey() {
cosmosKeyCredential.key(documentDbSecondaryKey);
cosmosTemplate.insert(TEST_PERSON_4).block();
cosmosTemplate.insert(TEST_PERSON_4,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_4))).block();
Flux<Person> flux = cosmosTemplate.findAll(Person.class.getSimpleName(), Person.class);
StepVerifier.create(flux).expectNextCount(2).verifyComplete();
final Mono<Void> voidMono = cosmosTemplate.deleteById(Person.class.getSimpleName(), TEST_PERSON_4.getId(),
PartitionKey.None);
final Mono<Void> voidMono = cosmosTemplate.deleteById(Person.class.getSimpleName(),
TEST_PERSON_4.getId(),
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_4)));
StepVerifier.create(voidMono).verifyComplete();
flux = cosmosTemplate.findAll(Person.class.getSimpleName(), Person.class);
StepVerifier.create(flux).expectNextCount(1).verifyComplete();
@ -223,16 +250,17 @@ public class ReactiveCosmosTemplateIT {
@Test
public void testFind() {
final Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, "firstName",
Arrays.asList(TEST_PERSON.getFirstName()));
Arrays.asList(TEST_PERSON.getFirstName()));
final DocumentQuery query = new DocumentQuery(criteria);
final Flux<Person> personFlux = cosmosTemplate.find(query, Person.class, Person.class.getSimpleName());
final Flux<Person> personFlux = cosmosTemplate.find(query, Person.class,
Person.class.getSimpleName());
StepVerifier.create(personFlux).expectNextCount(1).verifyComplete();
}
@Test
public void testExists() {
final Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, "firstName",
Arrays.asList(TEST_PERSON.getFirstName()));
Arrays.asList(TEST_PERSON.getFirstName()));
final DocumentQuery query = new DocumentQuery(criteria);
final Mono<Boolean> exists = cosmosTemplate.exists(query, Person.class, containerName);
StepVerifier.create(exists).expectNext(true).verifyComplete();
@ -254,7 +282,8 @@ public class ReactiveCosmosTemplateIT {
@Test
public void testInvalidSecondaryKey() {
cosmosKeyCredential.key("Invalid secondary key");
final Mono<Person> findById = cosmosTemplate.findById(Person.class.getSimpleName(), TEST_PERSON.getId(),
final Mono<Person> findById = cosmosTemplate.findById(Person.class.getSimpleName(),
TEST_PERSON.getId(),
Person.class);
StepVerifier.create(findById).expectError(IllegalArgumentException.class);
}

Просмотреть файл

@ -64,35 +64,41 @@ public class ReactiveCosmosTemplatePartitionIT {
@Value("${cosmosdb.key}")
private String documentDbKey;
private ReactiveCosmosTemplate cosmosTemplate;
private String containerName;
private static ReactiveCosmosTemplate cosmosTemplate;
private static String containerName;
private static DocumentDbEntityInformation<PartitionPerson, String> personInfo;
private static boolean initialized;
@Autowired
private ApplicationContext applicationContext;
@Before
public void setUp() throws ClassNotFoundException {
final DocumentDBConfig dbConfig = DocumentDBConfig.builder(documentDbUri, documentDbKey, DB_NAME).build();
final CosmosDbFactory dbFactory = new CosmosDbFactory(dbConfig);
if (!initialized) {
final DocumentDBConfig dbConfig = DocumentDBConfig.builder(documentDbUri, documentDbKey, DB_NAME).build();
final CosmosDbFactory dbFactory = new CosmosDbFactory(dbConfig);
final DocumentDbMappingContext mappingContext = new DocumentDbMappingContext();
final ObjectMapper objectMapper = new ObjectMapper();
final DocumentDbEntityInformation<PartitionPerson, String> personInfo =
new DocumentDbEntityInformation<>(PartitionPerson.class);
containerName = personInfo.getCollectionName();
final DocumentDbMappingContext mappingContext = new DocumentDbMappingContext();
final ObjectMapper objectMapper = new ObjectMapper();
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
personInfo = new DocumentDbEntityInformation<>(PartitionPerson.class);
containerName = personInfo.getCollectionName();
final MappingDocumentDbConverter dbConverter = new MappingDocumentDbConverter(mappingContext, objectMapper);
cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, dbConverter, DB_NAME);
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
cosmosTemplate.createCollectionIfNotExists(personInfo).block().container();
final MappingDocumentDbConverter dbConverter = new MappingDocumentDbConverter(mappingContext, objectMapper);
cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, dbConverter, DB_NAME);
cosmosTemplate.createCollectionIfNotExists(personInfo).block();
initialized = true;
}
cosmosTemplate.insert(TEST_PERSON).block();
}
@After
public void cleanup() {
cosmosTemplate.deleteContainer(PartitionPerson.class.getSimpleName());
cosmosTemplate.deleteAll(PartitionPerson.class.getSimpleName(), personInfo.getPartitionKeyFieldName()).block();
}
@Test

Просмотреть файл

@ -8,6 +8,7 @@ package com.microsoft.azure.spring.data.cosmosdb.domain;
import com.microsoft.azure.spring.data.cosmosdb.common.TestConstants;
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.DocumentIndexingPolicy;
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.PartitionKey;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -19,6 +20,8 @@ import java.util.List;
public class Person {
private String id;
private String firstName;
@PartitionKey
private String lastName;
private List<String> hobbies;
private List<Address> shippingAddresses;

Просмотреть файл

@ -49,32 +49,33 @@ public class DocumentDBAnnotationIT {
@Autowired
private ApplicationContext applicationContext;
private DocumentClient dbClient;
private DocumentDbTemplate dbTemplate;
private DocumentCollection collectionRole;
private DocumentCollection collectionExample;
private DocumentDbMappingContext dbContext;
private MappingDocumentDbConverter mappingConverter;
private ObjectMapper objectMapper;
private DocumentDbEntityInformation<Role, String> roleInfo;
private DocumentDbEntityInformation<TimeToLiveSample, String> sampleInfo;
private static DocumentDbTemplate dbTemplate;
private static DocumentCollection collectionRole;
private static DocumentCollection collectionExample;
private static DocumentDbEntityInformation<Role, String> roleInfo;
private static DocumentDbEntityInformation<TimeToLiveSample, String> sampleInfo;
private static boolean initialized;
@Before
public void setUp() throws ClassNotFoundException {
final DocumentDBConfig dbConfig = DocumentDBConfig.builder(dbUri, dbKey, TestConstants.DB_NAME).build();
final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig);
if (!initialized) {
final DocumentDBConfig dbConfig = DocumentDBConfig.builder(dbUri, dbKey, TestConstants.DB_NAME).build();
final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig);
roleInfo = new DocumentDbEntityInformation<>(Role.class);
sampleInfo = new DocumentDbEntityInformation<>(TimeToLiveSample.class);
dbContext = new DocumentDbMappingContext();
objectMapper = new ObjectMapper();
roleInfo = new DocumentDbEntityInformation<>(Role.class);
sampleInfo = new DocumentDbEntityInformation<>(TimeToLiveSample.class);
final DocumentDbMappingContext dbContext = new DocumentDbMappingContext();
final ObjectMapper objectMapper = new ObjectMapper();
dbContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
mappingConverter = new MappingDocumentDbConverter(dbContext, objectMapper);
dbClient = new DocumentClient(dbUri, dbKey, ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);
dbTemplate = new DocumentDbTemplate(cosmosDbFactory, mappingConverter, TestConstants.DB_NAME);
dbContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
final MappingDocumentDbConverter mappingConverter =
new MappingDocumentDbConverter(dbContext, objectMapper);
new DocumentClient(dbUri, dbKey, ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);
dbTemplate = new DocumentDbTemplate(cosmosDbFactory, mappingConverter, TestConstants.DB_NAME);
initialized = true;
}
collectionRole = dbTemplate.createCollectionIfNotExists(roleInfo);
collectionExample = dbTemplate.createCollectionIfNotExists(sampleInfo);
@ -106,7 +107,7 @@ public class DocumentDBAnnotationIT {
@Ignore // TODO(kuthapar): time to live is not supported by v3 SDK.
public void testDocumentAnnotationTimeToLive() {
final TimeToLiveSample sample = new TimeToLiveSample(TestConstants.ID_1);
final Integer timeToLive = this.collectionExample.getDefaultTimeToLive();
final Integer timeToLive = collectionExample.getDefaultTimeToLive();
Assert.notNull(timeToLive, "timeToLive should not be null");
Assert.isTrue(timeToLive == TestConstants.TIME_TO_LIVE, "should be the same timeToLive");