* add test assertions to RoleRepositoryCollectionIT

* have integration tests use TestRepositoryConfig so cosmosdb.database will be respected if set

* unignore test

* update AbstractCosmosConfigurationIT to respect cosmos.database if set
This commit is contained in:
Blackbaud-MikeLueders 2020-02-07 17:12:41 -06:00 коммит произвёл GitHub
Родитель 26f0912e0d
Коммит b493fbbc03
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 179 добавлений и 91 удалений

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

@ -7,10 +7,12 @@ package com.microsoft.azure.spring.data.cosmosdb;
import com.microsoft.azure.spring.data.cosmosdb.config.CosmosDBConfig;
import com.microsoft.azure.spring.data.cosmosdb.exception.CosmosDBAccessException;
import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.COSMOSDB_FAKE_CONNECTION_STRING;
@ -20,8 +22,8 @@ import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.COSM
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.DB_NAME;
import static org.assertj.core.api.Assertions.assertThat;
@PropertySource(value = {"classpath:application.properties"})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class CosmosDbFactoryTestIT {
@Value("${cosmosdb.uri:}")

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

@ -27,6 +27,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.util.StringUtils;
import static org.junit.Assert.assertNotNull;
@ -94,12 +95,16 @@ public class AbstractCosmosConfigurationIT {
@Value("${cosmosdb.key:}")
private String cosmosDbKey;
@Value("${cosmosdb.database:}")
private String database;
@Mock
private CosmosClient mockClient;
@Bean
public CosmosDBConfig getConfig() {
return CosmosDBConfig.builder(cosmosDbUri, cosmosDbKey, TestConstants.DB_NAME).build();
final String dbName = StringUtils.hasText(this.database) ? this.database : TestConstants.DB_NAME;
return CosmosDBConfig.builder(cosmosDbUri, cosmosDbKey, dbName).build();
}
@Override
@ -126,6 +131,9 @@ public class AbstractCosmosConfigurationIT {
@Value("${cosmosdb.key:}")
private String cosmosDbKey;
@Value("${cosmosdb.database:}")
private String database;
private RequestOptions getRequestOptions() {
final RequestOptions options = new RequestOptions();
@ -137,8 +145,9 @@ public class AbstractCosmosConfigurationIT {
@Bean
public CosmosDBConfig getConfig() {
final String dbName = StringUtils.hasText(this.database) ? this.database : TestConstants.DB_NAME;
final RequestOptions options = getRequestOptions();
return CosmosDBConfig.builder(cosmosDbUri, cosmosDbKey, TestConstants.DB_NAME)
return CosmosDBConfig.builder(cosmosDbUri, cosmosDbKey, dbName)
.requestOptions(options)
.build();
}

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

@ -19,6 +19,7 @@ import com.microsoft.azure.spring.data.cosmosdb.core.query.CriteriaType;
import com.microsoft.azure.spring.data.cosmosdb.core.query.DocumentQuery;
import com.microsoft.azure.spring.data.cosmosdb.domain.Person;
import com.microsoft.azure.spring.data.cosmosdb.exception.CosmosDBAccessException;
import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig;
import com.microsoft.azure.spring.data.cosmosdb.repository.support.CosmosEntityInformation;
import org.assertj.core.util.Lists;
import org.junit.After;
@ -26,14 +27,13 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.domain.EntityScanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Collections;
@ -43,7 +43,6 @@ import java.util.UUID;
import static com.microsoft.azure.spring.data.cosmosdb.common.PageTestUtils.validateLastPage;
import static com.microsoft.azure.spring.data.cosmosdb.common.PageTestUtils.validateNonLastPage;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.ADDRESSES;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.DB_NAME;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.FIRST_NAME;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.HOBBIES;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.ID_1;
@ -62,7 +61,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource(value = { "classpath:application.properties" })
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class CosmosTemplateIT {
private static final Person TEST_PERSON = new Person(ID_1, FIRST_NAME, LAST_NAME, HOBBIES,
ADDRESSES);
@ -79,32 +78,23 @@ public class CosmosTemplateIT {
private static final String WRONG_ETAG = "WRONG_ETAG";
@Value("${cosmosdb.uri}")
private String cosmosDbUri;
@Value("${cosmosdb.key}")
private String cosmosDbKey;
@Value("${cosmosdb.populateQueryMetrics}")
private boolean populateQueryMetrics;
private static CosmosTemplate cosmosTemplate;
private static CosmosEntityInformation<Person, String> personInfo;
private static String collectionName;
private static boolean initialized;
private static ResponseDiagnosticsTestUtils responseDiagnosticsTestUtils;
private Person insertedPerson;
@Autowired
private ApplicationContext applicationContext;
@Autowired
private CosmosDBConfig dbConfig;
@Autowired
private ResponseDiagnosticsTestUtils responseDiagnosticsTestUtils;
@Before
public void setup() throws ClassNotFoundException {
if (!initialized) {
responseDiagnosticsTestUtils = new ResponseDiagnosticsTestUtils();
final CosmosDBConfig dbConfig = CosmosDBConfig.builder(cosmosDbUri,
cosmosDbKey, DB_NAME).build();
dbConfig.setResponseDiagnosticsProcessor(responseDiagnosticsTestUtils.getResponseDiagnosticsProcessor());
dbConfig.setPopulateQueryMetrics(populateQueryMetrics);
final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig);
final CosmosMappingContext mappingContext = new CosmosMappingContext();
@ -115,7 +105,7 @@ public class CosmosTemplateIT {
final MappingCosmosConverter cosmosConverter = new MappingCosmosConverter(mappingContext,
null);
cosmosTemplate = new CosmosTemplate(cosmosDbFactory, cosmosConverter, DB_NAME);
cosmosTemplate = new CosmosTemplate(cosmosDbFactory, cosmosConverter, dbConfig.getDatabase());
cosmosTemplate.createCollectionIfNotExists(personInfo);
initialized = true;
}

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

@ -16,6 +16,7 @@ import com.microsoft.azure.spring.data.cosmosdb.core.query.Criteria;
import com.microsoft.azure.spring.data.cosmosdb.core.query.CriteriaType;
import com.microsoft.azure.spring.data.cosmosdb.core.query.DocumentQuery;
import com.microsoft.azure.spring.data.cosmosdb.domain.PartitionPerson;
import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig;
import com.microsoft.azure.spring.data.cosmosdb.repository.support.CosmosEntityInformation;
import org.junit.After;
import org.junit.Before;
@ -29,6 +30,7 @@ import org.springframework.context.annotation.PropertySource;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
@ -44,7 +46,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource(value = {"classpath:application.properties"})
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class CosmosTemplatePartitionIT {
private static final PartitionPerson TEST_PERSON = new PartitionPerson(ID_1, FIRST_NAME, LAST_NAME,
HOBBIES, ADDRESSES);
@ -52,11 +54,6 @@ public class CosmosTemplatePartitionIT {
private static final PartitionPerson TEST_PERSON_2 = new PartitionPerson(ID_2, NEW_FIRST_NAME,
TEST_PERSON.getLastName(), HOBBIES, ADDRESSES);
@Value("${cosmosdb.uri}")
private String cosmosDbUri;
@Value("${cosmosdb.key}")
private String cosmosDbKey;
private static CosmosTemplate cosmosTemplate;
private static String collectionName;
private static CosmosEntityInformation<PartitionPerson, String> personInfo;
@ -64,11 +61,12 @@ public class CosmosTemplatePartitionIT {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private CosmosDBConfig dbConfig;
@Before
public void setup() throws ClassNotFoundException {
if (!initialized) {
final CosmosDBConfig dbConfig = CosmosDBConfig.builder(cosmosDbUri, cosmosDbKey, DB_NAME).build();
final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig);
final CosmosMappingContext mappingContext = new CosmosMappingContext();
@ -77,7 +75,7 @@ public class CosmosTemplatePartitionIT {
final MappingCosmosConverter dbConverter = new MappingCosmosConverter(mappingContext, null);
cosmosTemplate = new CosmosTemplate(cosmosDbFactory, dbConverter, DB_NAME);
cosmosTemplate = new CosmosTemplate(cosmosDbFactory, dbConverter, dbConfig.getDatabase());
collectionName = personInfo.getCollectionName();
cosmosTemplate.createCollectionIfNotExists(personInfo);

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

@ -18,6 +18,7 @@ import com.microsoft.azure.spring.data.cosmosdb.core.query.CriteriaType;
import com.microsoft.azure.spring.data.cosmosdb.core.query.DocumentQuery;
import com.microsoft.azure.spring.data.cosmosdb.domain.Person;
import com.microsoft.azure.spring.data.cosmosdb.exception.CosmosDBAccessException;
import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig;
import com.microsoft.azure.spring.data.cosmosdb.repository.support.CosmosEntityInformation;
import io.reactivex.subscribers.TestSubscriber;
import org.junit.After;
@ -29,8 +30,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.domain.EntityScanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.annotation.Persistent;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@ -40,13 +41,12 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.DB_NAME;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource(value = { "classpath:application.properties" })
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class ReactiveCosmosTemplateIT {
private static final Person TEST_PERSON = new Person(TestConstants.ID_1,
TestConstants.FIRST_NAME,
@ -64,14 +64,8 @@ public class ReactiveCosmosTemplateIT {
TestConstants.NEW_FIRST_NAME,
TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES);
@Value("${cosmosdb.uri}")
private String cosmosDbUri;
@Value("${cosmosdb.key}")
private String cosmosDbKey;
@Value("${cosmosdb.secondaryKey}")
private String cosmosDbSecondaryKey;
@Value("${cosmosdb.populateQueryMetrics}")
private boolean populateQueryMetrics;
private static ReactiveCosmosTemplate cosmosTemplate;
private static String containerName;
@ -79,20 +73,18 @@ public class ReactiveCosmosTemplateIT {
private static CosmosKeyCredential cosmosKeyCredential;
private static boolean initialized;
private static ResponseDiagnosticsTestUtils responseDiagnosticsTestUtils;
@Autowired
private ApplicationContext applicationContext;
@Autowired
private CosmosDBConfig dbConfig;
@Autowired
private ResponseDiagnosticsTestUtils responseDiagnosticsTestUtils;
@Before
public void setUp() throws ClassNotFoundException {
if (!initialized) {
responseDiagnosticsTestUtils = new ResponseDiagnosticsTestUtils();
cosmosKeyCredential = new CosmosKeyCredential(cosmosDbKey);
final CosmosDBConfig dbConfig = CosmosDBConfig.builder(cosmosDbUri,
cosmosKeyCredential, DB_NAME).build();
dbConfig.setResponseDiagnosticsProcessor(responseDiagnosticsTestUtils.getResponseDiagnosticsProcessor());
dbConfig.setPopulateQueryMetrics(populateQueryMetrics);
cosmosKeyCredential = new CosmosKeyCredential(dbConfig.getKey());
final CosmosDbFactory dbFactory = new CosmosDbFactory(dbConfig);
final CosmosMappingContext mappingContext = new CosmosMappingContext();
@ -103,7 +95,7 @@ public class ReactiveCosmosTemplateIT {
final MappingCosmosConverter dbConverter =
new MappingCosmosConverter(mappingContext, null);
cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, dbConverter, DB_NAME);
cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, dbConverter, dbConfig.getDatabase());
cosmosTemplate.createCollectionIfNotExists(personInfo).block().container();
initialized = true;
}
@ -115,7 +107,7 @@ public class ReactiveCosmosTemplateIT {
@After
public void cleanup() {
// Reset master key
cosmosKeyCredential.key(cosmosDbKey);
cosmosKeyCredential.key(dbConfig.getKey());
cosmosTemplate.deleteAll(Person.class.getSimpleName(),
personInfo.getPartitionKeyFieldName()).block();
}

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

@ -13,6 +13,7 @@ import com.microsoft.azure.spring.data.cosmosdb.core.mapping.CosmosMappingContex
import com.microsoft.azure.spring.data.cosmosdb.core.query.Criteria;
import com.microsoft.azure.spring.data.cosmosdb.core.query.DocumentQuery;
import com.microsoft.azure.spring.data.cosmosdb.domain.PartitionPerson;
import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig;
import com.microsoft.azure.spring.data.cosmosdb.repository.support.CosmosEntityInformation;
import org.junit.After;
import org.junit.Assert;
@ -25,6 +26,7 @@ import org.springframework.boot.autoconfigure.domain.EntityScanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.annotation.Persistent;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@ -50,7 +52,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource(value = {"classpath:application.properties"})
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class ReactiveCosmosTemplatePartitionIT {
private static final PartitionPerson TEST_PERSON = new PartitionPerson(ID_1, FIRST_NAME, LAST_NAME,
HOBBIES, ADDRESSES);
@ -58,11 +60,6 @@ public class ReactiveCosmosTemplatePartitionIT {
private static final PartitionPerson TEST_PERSON_2 = new PartitionPerson(ID_2, NEW_FIRST_NAME,
TEST_PERSON.getLastName(), HOBBIES, ADDRESSES);
@Value("${cosmosdb.uri}")
private String cosmosDbUri;
@Value("${cosmosdb.key}")
private String cosmosDbKey;
private static ReactiveCosmosTemplate cosmosTemplate;
private static String containerName;
private static CosmosEntityInformation<PartitionPerson, String> personInfo;
@ -71,12 +68,12 @@ public class ReactiveCosmosTemplatePartitionIT {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private CosmosDBConfig dbConfig;
@Before
public void setUp() throws ClassNotFoundException {
if (!initialized) {
final CosmosDBConfig dbConfig = CosmosDBConfig.builder(cosmosDbUri, cosmosDbKey,
DB_NAME).build();
final CosmosDbFactory dbFactory = new CosmosDbFactory(dbConfig);
final CosmosMappingContext mappingContext = new CosmosMappingContext();
@ -88,7 +85,7 @@ public class ReactiveCosmosTemplatePartitionIT {
final MappingCosmosConverter dbConverter = new MappingCosmosConverter(mappingContext,
null);
cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, dbConverter, DB_NAME);
cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, dbConverter, dbConfig.getDatabase());
cosmosTemplate.createCollectionIfNotExists(personInfo).block();
initialized = true;

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

@ -7,6 +7,7 @@ package com.microsoft.azure.spring.data.cosmosdb.repository;
import com.azure.data.cosmos.ConsistencyLevel;
import com.azure.data.cosmos.internal.RequestOptions;
import com.microsoft.azure.spring.data.cosmosdb.common.ResponseDiagnosticsTestUtils;
import com.microsoft.azure.spring.data.cosmosdb.common.TestConstants;
import com.microsoft.azure.spring.data.cosmosdb.config.AbstractCosmosConfiguration;
import com.microsoft.azure.spring.data.cosmosdb.config.CosmosDBConfig;
@ -35,6 +36,9 @@ public class TestRepositoryConfig extends AbstractCosmosConfiguration {
@Value("${cosmosdb.database:}")
private String database;
@Value("${cosmosdb.populateQueryMetrics}")
private boolean populateQueryMetrics;
private RequestOptions getRequestOptions() {
final RequestOptions options = new RequestOptions();
@ -45,16 +49,26 @@ public class TestRepositoryConfig extends AbstractCosmosConfiguration {
return options;
}
@Bean
public ResponseDiagnosticsTestUtils responseDiagnosticsTestUtils() {
return new ResponseDiagnosticsTestUtils();
}
@Bean
public CosmosDBConfig getConfig() {
final String dbName = StringUtils.hasText(this.database) ? this.database : TestConstants.DB_NAME;
final RequestOptions options = getRequestOptions();
final CosmosDBConfig.CosmosDBConfigBuilder builder;
if (StringUtils.hasText(this.cosmosDbUri) && StringUtils.hasText(this.cosmosDbKey)) {
return CosmosDBConfig.builder(cosmosDbUri, cosmosDbKey, dbName).requestOptions(options).build();
builder = CosmosDBConfig.builder(cosmosDbUri, cosmosDbKey, dbName);
} else {
builder = CosmosDBConfig.builder(connectionString, dbName);
}
return CosmosDBConfig.builder(connectionString, dbName).requestOptions(options).build();
return builder.requestOptions(options)
.populateQueryMetrics(populateQueryMetrics)
.responseDiagnosticsProcessor(responseDiagnosticsTestUtils().getResponseDiagnosticsProcessor())
.build();
}
@Bean

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

@ -16,6 +16,7 @@ import com.microsoft.azure.spring.data.cosmosdb.core.convert.MappingCosmosConver
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.CosmosMappingContext;
import com.microsoft.azure.spring.data.cosmosdb.domain.Role;
import com.microsoft.azure.spring.data.cosmosdb.domain.TimeToLiveSample;
import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig;
import com.microsoft.azure.spring.data.cosmosdb.repository.support.CosmosEntityInformation;
import org.junit.After;
import org.junit.Before;
@ -28,23 +29,20 @@ import org.springframework.boot.autoconfigure.domain.EntityScanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.annotation.Persistent;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource(value = {"classpath:application.properties"})
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class CosmosAnnotationIT {
private static final Role TEST_ROLE = new Role(TestConstants.ID_1, TestConstants.LEVEL,
TestConstants.ROLE_NAME);
@Value("${cosmosdb.uri}")
private String dbUri;
@Value("${cosmosdb.key}")
private String dbKey;
@Autowired
private ApplicationContext applicationContext;
@Autowired
private CosmosDBConfig dbConfig;
private static CosmosTemplate cosmosTemplate;
private static CosmosContainerProperties collectionRole;
@ -56,7 +54,6 @@ public class CosmosAnnotationIT {
@Before
public void setUp() throws ClassNotFoundException {
if (!initialized) {
final CosmosDBConfig dbConfig = CosmosDBConfig.builder(dbUri, dbKey, TestConstants.DB_NAME).build();
final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig);
roleInfo = new CosmosEntityInformation<>(Role.class);
@ -67,7 +64,7 @@ public class CosmosAnnotationIT {
final MappingCosmosConverter mappingConverter = new MappingCosmosConverter(dbContext, null);
cosmosTemplate = new CosmosTemplate(cosmosDbFactory, mappingConverter, TestConstants.DB_NAME);
cosmosTemplate = new CosmosTemplate(cosmosDbFactory, mappingConverter, dbConfig.getDatabase());
initialized = true;
}
collectionRole = cosmosTemplate.createCollectionIfNotExists(roleInfo);

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

@ -241,4 +241,12 @@ public class MemoRepositoryIT {
public void testFindByStartsWithWithException() {
repository.findByMessageStartsWith(testMemo1.getMessage());
}
@Test
public void testFindByStartsWith() {
final List<Memo> result = repository.findByMessageStartsWith(testMemo1.getMessage().substring(0, 10));
Assert.assertEquals(testMemo1, result.get(0));
Assert.assertEquals(1, result.size());
}
}

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

@ -11,6 +11,7 @@ import com.azure.data.cosmos.FeedOptions;
import com.azure.data.cosmos.FeedResponse;
import com.microsoft.azure.spring.data.cosmosdb.common.TestConstants;
import com.microsoft.azure.spring.data.cosmosdb.common.TestUtils;
import com.microsoft.azure.spring.data.cosmosdb.config.CosmosDBConfig;
import com.microsoft.azure.spring.data.cosmosdb.core.CosmosTemplate;
import com.microsoft.azure.spring.data.cosmosdb.core.query.CosmosPageRequest;
import com.microsoft.azure.spring.data.cosmosdb.domain.Address;
@ -35,7 +36,6 @@ import java.util.List;
import static com.microsoft.azure.spring.data.cosmosdb.common.PageTestUtils.validateLastPage;
import static com.microsoft.azure.spring.data.cosmosdb.common.PageTestUtils.validateNonLastPage;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.DB_NAME;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.PAGE_SIZE_1;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.PAGE_SIZE_3;
import static org.assertj.core.api.Assertions.assertThat;
@ -64,6 +64,9 @@ public class PageableAddressRepositoryIT {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private CosmosDBConfig dbConfig;
@Before
public void setup() {
repository.save(TEST_ADDRESS1_PARTITION1);
@ -167,7 +170,7 @@ public class PageableAddressRepositoryIT {
final CosmosClient cosmosClient = applicationContext.getBean(CosmosClient.class);
final Flux<FeedResponse<CosmosItemProperties>> feedResponseFlux =
cosmosClient.getDatabase(DB_NAME)
cosmosClient.getDatabase(dbConfig.getDatabase())
.getContainer(entityInformation.getCollectionName())
.queryItems(query, options);

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

@ -9,6 +9,7 @@ import com.azure.data.cosmos.CosmosClient;
import com.azure.data.cosmos.CosmosItemProperties;
import com.azure.data.cosmos.FeedOptions;
import com.azure.data.cosmos.FeedResponse;
import com.microsoft.azure.spring.data.cosmosdb.config.CosmosDBConfig;
import com.microsoft.azure.spring.data.cosmosdb.core.CosmosTemplate;
import com.microsoft.azure.spring.data.cosmosdb.core.query.CosmosPageRequest;
import com.microsoft.azure.spring.data.cosmosdb.domain.Importance;
@ -38,7 +39,6 @@ import java.util.Random;
import java.util.Set;
import java.util.UUID;
import static com.microsoft.azure.spring.data.cosmosdb.common.TestConstants.DB_NAME;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ -60,6 +60,9 @@ public class PageableMemoRepositoryIT {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private CosmosDBConfig dbConfig;
private static Set<Memo> memoSet;
private static boolean isSetupDone;
@ -138,7 +141,7 @@ public class PageableMemoRepositoryIT {
final String query = "SELECT * from c OFFSET " + skipCount + " LIMIT " + takeCount;
final CosmosClient cosmosClient = applicationContext.getBean(CosmosClient.class);
return cosmosClient.getDatabase(DB_NAME)
return cosmosClient.getDatabase(dbConfig.getDatabase())
.getContainer(entityInformation.getCollectionName())
.queryItems(query, options);
}

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

@ -26,6 +26,8 @@ import javax.annotation.PreDestroy;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static com.microsoft.azure.spring.data.cosmosdb.common.PageTestUtils.validateLastPage;
@ -151,12 +153,15 @@ public class ProjectRepositorySortIT {
this.repository.findAll(sort);
}
@Test(expected = CosmosDBAccessException.class)
@Ignore // TODO(pan): Ignore this test case for now, will update this from service update.
public void testFindAllSortWithIdName() {
final Sort sort = Sort.by(Sort.Direction.ASC, "id");
final List<Project> projectListSortedById = Lists.newArrayList(PROJECTS);
projectListSortedById.sort(Comparator.comparing(Project::getId));
this.repository.findAll(sort);
final Sort sort = Sort.by(Sort.Direction.ASC, "id");
final List<Project> results = StreamSupport.stream(this.repository.findAll(sort).spliterator(), false)
.collect(Collectors.toList());
Assert.assertEquals(projectListSortedById, results);
}
@Test

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

@ -12,15 +12,24 @@ import com.microsoft.azure.spring.data.cosmosdb.exception.CosmosDBAccessExceptio
import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig;
import com.microsoft.azure.spring.data.cosmosdb.repository.repository.IntegerIdDomainRepository;
import com.microsoft.azure.spring.data.cosmosdb.repository.support.CosmosEntityInformation;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.PreDestroy;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestRepositoryConfig.class)
@ -37,6 +46,11 @@ public class RoleRepositoryCollectionIT {
@Autowired
private CosmosTemplate template;
@Before
public void setUp() {
repository.deleteAll();
}
@PreDestroy
public void cleanUpCollection() {
template.deleteCollection(entityInformation.getCollectionName());
@ -48,13 +62,25 @@ public class RoleRepositoryCollectionIT {
}
@Test
public void testSaveAll() {
this.repository.saveAll(Collections.singleton(DOMAIN));
public void testSaveAndFindById() {
Assert.assertNotNull(this.repository.save(DOMAIN));
final Optional<IntegerIdDomain> savedEntity = this.repository.findById(DOMAIN.getNumber());
Assert.assertTrue(savedEntity.isPresent());
Assert.assertEquals(DOMAIN, savedEntity.get());
}
@Test
public void testFindAll() {
this.repository.findAll();
public void testSaveAllAndFindAll() {
Assert.assertFalse(this.repository.findAll().iterator().hasNext());
final Set<IntegerIdDomain> entitiesToSave = Collections.singleton(DOMAIN);
this.repository.saveAll(entitiesToSave);
final Set<IntegerIdDomain> savedEntities = StreamSupport.stream(this.repository.findAll().spliterator(), false)
.collect(Collectors.toSet());
Assert.assertTrue(entitiesToSave.containsAll(savedEntities));
}
@Test
@ -63,43 +89,87 @@ public class RoleRepositoryCollectionIT {
}
@Test
public void testFindById() {
this.repository.findById(DOMAIN.getNumber());
public void testCount() {
Assert.assertEquals(0, repository.count());
this.repository.save(DOMAIN);
Assert.assertEquals(1, repository.count());
}
@Test
public void testCount() {
this.repository.count();
public void testDeleteById() {
this.repository.save(DOMAIN);
this.repository.deleteById(DOMAIN.getNumber());
Assert.assertEquals(0, this.repository.count());
}
@Test(expected = CosmosDBAccessException.class)
public void testDeleteById() {
public void testDeleteByIdShouldFailIfNothingToDelete() {
this.repository.deleteById(DOMAIN.getNumber());
}
@Test(expected = CosmosDBAccessException.class)
@Test
public void testDelete() {
this.repository.save(DOMAIN);
this.repository.delete(DOMAIN);
Assert.assertEquals(0, this.repository.count());
}
@Test(expected = CosmosDBAccessException.class)
public void testDeleteShouldFailIfNothingToDelete() {
this.repository.delete(DOMAIN);
}
@Test
public void testDeleteAll() {
this.repository.save(DOMAIN);
this.repository.deleteAll(Collections.singleton(DOMAIN));
Assert.assertEquals(0, this.repository.count());
}
@Test
public void testExistsById() {
this.repository.existsById(DOMAIN.getNumber());
this.repository.save(DOMAIN);
Assert.assertTrue(this.repository.existsById(DOMAIN.getNumber()));
}
@Test
public void testFindAllSort() {
this.repository.findAll(Sort.unsorted());
final IntegerIdDomain other = new IntegerIdDomain(DOMAIN.getNumber() + 1, "other-name");
this.repository.save(other);
this.repository.save(DOMAIN);
final Sort ascSort = Sort.by(Sort.Direction.ASC, "number");
final List<IntegerIdDomain> ascending = StreamSupport
.stream(this.repository.findAll(ascSort).spliterator(), false)
.collect(Collectors.toList());
Assert.assertEquals(2, ascending.size());
Assert.assertEquals(DOMAIN, ascending.get(0));
Assert.assertEquals(other, ascending.get(1));
final Sort descSort = Sort.by(Sort.Direction.DESC, "number");
final List<IntegerIdDomain> descending = StreamSupport
.stream(this.repository.findAll(descSort).spliterator(), false)
.collect(Collectors.toList());
Assert.assertEquals(2, descending.size());
Assert.assertEquals(other, descending.get(0));
Assert.assertEquals(DOMAIN, descending.get(1));
}
@Test
public void testFindAllPageable() {
final CosmosPageRequest pageRequest = new CosmosPageRequest(0, 3, null);
this.repository.findAll(pageRequest);
final IntegerIdDomain other = new IntegerIdDomain(DOMAIN.getNumber() + 1, "other-name");
this.repository.save(DOMAIN);
this.repository.save(other);
final Page<IntegerIdDomain> page1 = this.repository.findAll(new CosmosPageRequest(0, 1, null));
final Iterator<IntegerIdDomain> page1Iterator = page1.iterator();
Assert.assertTrue(page1Iterator.hasNext());
Assert.assertEquals(DOMAIN, page1Iterator.next());
final Page<IntegerIdDomain> page2 = this.repository.findAll(new CosmosPageRequest(1, 1, null));
final Iterator<IntegerIdDomain> page2Iterator = page2.iterator();
Assert.assertTrue(page2Iterator.hasNext());
Assert.assertEquals(DOMAIN, page2Iterator.next());
}
}