add unit tests for spring-data-documentdb (#54)
* add unit test for spring-data-documentdb * fix license header in test file
This commit is contained in:
Родитель
53d0c62de4
Коммит
7478274a93
|
@ -157,6 +157,8 @@
|
|||
<instrumentation>
|
||||
<excludes>
|
||||
<exclude>com/microsoft/azure/spring/common/GetHashMac.class</exclude>
|
||||
<exclude>com/microsoft/azure/sample/**.class</exclude>
|
||||
<exclude>**/*Sample*.class</exclude>
|
||||
</excludes>
|
||||
</instrumentation>
|
||||
</configuration>
|
||||
|
|
|
@ -10,6 +10,7 @@ import com.microsoft.azure.documentdb.ConnectionPolicy;
|
|||
import com.microsoft.azure.documentdb.ConsistencyLevel;
|
||||
import com.microsoft.azure.documentdb.DocumentClient;
|
||||
import com.microsoft.azure.spring.common.GetHashMac;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class DocumentDbFactory {
|
||||
|
||||
|
@ -22,6 +23,8 @@ public class DocumentDbFactory {
|
|||
}
|
||||
|
||||
public DocumentDbFactory(String host, String key, boolean isBiEnabled) {
|
||||
Assert.hasText(host, "host must not be empty!");
|
||||
Assert.hasText(key, "key must not be empty!");
|
||||
|
||||
final ConnectionPolicy policy = ConnectionPolicy.GetDefault();
|
||||
|
||||
|
|
|
@ -42,6 +42,7 @@ public class DocumentDbTemplate implements DocumentDbOperations, ApplicationCont
|
|||
DocumentDbConverter documentDbConverter,
|
||||
String dbName) {
|
||||
Assert.notNull(documentDbFactory, "DocumentDbFactory must not be null!");
|
||||
Assert.notNull(documentDbConverter, "DocumentDbConverter must not be null!");
|
||||
|
||||
this.documentDbFactory = documentDbFactory;
|
||||
this.dbConverter = documentDbConverter;
|
||||
|
|
|
@ -34,7 +34,7 @@ public class SimpleDocumentDbRepositoryUnitTest {
|
|||
|
||||
@Before
|
||||
public void setUp() {
|
||||
testPerson1 = new Person("aaa", "firstname", "lastname", "425-111");
|
||||
testPerson1 = new Person("aaa", "firstname", "lastname");
|
||||
|
||||
when(entityInformation.getJavaType()).thenReturn(Person.class);
|
||||
when(entityInformation.getCollectionName()).thenReturn(Person.class.getSimpleName());
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository.autoconfig;
|
||||
|
||||
import com.microsoft.azure.documentdb.DocumentClient;
|
||||
import com.microsoft.azure.spring.data.documentdb.autoconfig.DocumentDbRepositoriesAutoConfiguration;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.DocumentDbTemplate;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.config.EnableDocumentDbRepositories;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.domain.Person;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.domain.PersonRepository;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DocumentDbRepositoriesAutoConfigurationUnitTest {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@MockBean
|
||||
private DocumentDbTemplate dbOperations;
|
||||
|
||||
|
||||
@MockBean
|
||||
private DocumentClient documentClient;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
dbOperations = new DocumentDbTemplate(documentClient, "testdb");
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultRepositoryConfiguration() throws Exception {
|
||||
prepareApplicationContext(TestConfiguration.class);
|
||||
|
||||
assertThat(this.context.getBean(PersonRepository.class)).isNotNull();
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void autConfigNotKickInIfManualConfigDidNotCreateRepositories() throws Exception {
|
||||
prepareApplicationContext(InvalidCustomConfiguration.class);
|
||||
this.context.getBean(PersonRepository.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@TestAutoConfigurationPackage(Person.class)
|
||||
protected static class TestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableDocumentDbRepositories("foo.bar")
|
||||
@TestAutoConfigurationPackage(DocumentDbRepositoriesAutoConfigurationUnitTest.class)
|
||||
protected static class InvalidCustomConfiguration {
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void prepareApplicationContext(Class<?>... configurationClasses) {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(configurationClasses);
|
||||
this.context.register(DocumentDbRepositoriesAutoConfiguration.class);
|
||||
this.context.getBeanFactory().registerSingleton(DocumentDbTemplate.class.getName(), dbOperations);
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository.autoconfig;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Import(TestAutoConfigurationPackageRegistrar.class)
|
||||
public @interface TestAutoConfigurationPackage {
|
||||
|
||||
Class<?> value();
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository.autoconfig;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
public class TestAutoConfigurationPackageRegistrar
|
||||
implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata metadata,
|
||||
BeanDefinitionRegistry registry) {
|
||||
final AnnotationAttributes attributes = AnnotationAttributes
|
||||
.fromMap(metadata.getAnnotationAttributes(
|
||||
TestAutoConfigurationPackage.class.getName(), true));
|
||||
AutoConfigurationPackages.register(registry,
|
||||
ClassUtils.getPackageName(attributes.getString("value")));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository.config;
|
||||
|
||||
import com.microsoft.azure.documentdb.DocumentClient;
|
||||
import com.microsoft.azure.spring.data.documentdb.DocumentDbFactory;
|
||||
import com.microsoft.azure.spring.data.documentdb.config.AbstractDocumentDbConfiguration;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class AbstractDocumentDbConfiguratinUnitTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void containsDocumentDbFactory() throws ClassNotFoundException {
|
||||
final AbstractApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
TestDocumentDbConfiguration.class);
|
||||
|
||||
assertThat(context.getBean(DocumentDbFactory.class)).isNotNull();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestDocumentDbConfiguration extends AbstractDocumentDbConfiguration {
|
||||
@MockBean
|
||||
private DocumentClient mockClient;
|
||||
|
||||
@Override
|
||||
public String getDatabase() {
|
||||
return "testdb";
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocumentClient documentClient() {
|
||||
return mockClient;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository.core;
|
||||
|
||||
import com.microsoft.azure.documentdb.DocumentClient;
|
||||
import com.microsoft.azure.spring.data.documentdb.DocumentDbFactory;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.DocumentDbTemplate;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.convert.DocumentDbConverter;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DocumentDbTemplateUnitTest {
|
||||
|
||||
DocumentDbTemplate dbTemplate;
|
||||
|
||||
@Mock
|
||||
DocumentDbFactory dbFactory;
|
||||
|
||||
@Mock
|
||||
DocumentClient documentClient;
|
||||
|
||||
DocumentDbConverter dbConverter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.dbConverter = new DocumentDbConverter();
|
||||
this.dbFactory = new DocumentDbFactory(this.documentClient);
|
||||
this.dbTemplate = new DocumentDbTemplate(this.dbFactory, this.dbConverter, "testdb");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectNullDbFactory() throws Exception {
|
||||
new DocumentDbTemplate(null, this.dbConverter, "testdb");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultsConverterToDocumentDbConverter() throws Exception {
|
||||
final DocumentDbTemplate template = new DocumentDbTemplate(documentClient, "testdb");
|
||||
assertThat(ReflectionTestUtils.getField(template, "dbConverter") instanceof DocumentDbConverter);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository.core.converter;
|
||||
|
||||
|
||||
import com.microsoft.azure.documentdb.Document;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.convert.DocumentDbConverter;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.domain.Person;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DocumentDbConverterUnitTest {
|
||||
|
||||
private DocumentDbConverter dbConverter;
|
||||
|
||||
private static final String id = "testId";
|
||||
private static final String firstName = "testFirstName";
|
||||
private static final String lastName = "testLastName";
|
||||
|
||||
private static final String idPropertyName = "id";
|
||||
private static final String firstNamePropertyName = "firstName";
|
||||
private static final String lastNamePropertyName = "lastName";
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
dbConverter = new DocumentDbConverter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromEntityToDocument() {
|
||||
final Person person = new Person(id, firstName, lastName);
|
||||
final Document document = dbConverter.convertToDocument(person);
|
||||
|
||||
assertThat(document.has(idPropertyName)).isTrue();
|
||||
assertThat(document.has(firstNamePropertyName)).isTrue();
|
||||
assertThat(document.has(lastNamePropertyName)).isTrue();
|
||||
assertThat(document.getId()).isEqualTo(id);
|
||||
assertThat(document.getString(firstNamePropertyName)).isEqualTo(firstName);
|
||||
assertThat(document.getString(lastNamePropertyName)).isEqualTo(lastName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromDocumentToEntity() {
|
||||
final JSONObject json = new JSONObject();
|
||||
json.put(idPropertyName, id);
|
||||
json.put(firstNamePropertyName, firstName);
|
||||
json.put(lastNamePropertyName, lastName);
|
||||
|
||||
final Document document = new Document(JSONObject.valueToString(json));
|
||||
final Person person = dbConverter.convertFromDocument(document, Person.class);
|
||||
assertThat(person.getId()).isEqualTo(id);
|
||||
assertThat(person.getFirstName()).isEqualTo(firstName);
|
||||
assertThat(person.getLastName()).isEqualTo(lastName);
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository.core.mapping;
|
||||
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.BasicDocumentDbPersistentEntity;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.domain.Person;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BasicDocumentDbPersistentEntityUnitTest {
|
||||
|
||||
@Test
|
||||
public void testGetCollection() {
|
||||
final BasicDocumentDbPersistentEntity entity = new BasicDocumentDbPersistentEntity<Person>(
|
||||
ClassTypeInformation.from(Person.class));
|
||||
assertThat(entity.getCollection()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetLanguage() {
|
||||
final BasicDocumentDbPersistentEntity entity = new BasicDocumentDbPersistentEntity<Person>(
|
||||
ClassTypeInformation.from(Person.class));
|
||||
assertThat(entity.getLanguage()).isEqualTo("");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository.core.mapping;
|
||||
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.BasicDocumentDbPersistentEntity;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.DocumentDbMappingContext;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DocumentDbMappingContextUnitTest {
|
||||
|
||||
@Mock
|
||||
ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void mappingContextWithImplicitIdProperty() {
|
||||
final DocumentDbMappingContext context = new DocumentDbMappingContext();
|
||||
final BasicDocumentDbPersistentEntity<?> entity = context.getPersistentEntity(ClassWithId.class);
|
||||
|
||||
assertThat(entity).isNotNull();
|
||||
}
|
||||
|
||||
class ClassWithId {
|
||||
String field;
|
||||
String id;
|
||||
}
|
||||
}
|
|
@ -10,18 +10,16 @@ package com.microsoft.azure.spring.data.documentdb.repository.domain;
|
|||
public class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String phone;
|
||||
|
||||
private String id;
|
||||
|
||||
public Person() {
|
||||
this(null, null, null, null);
|
||||
this(null, null, null);
|
||||
}
|
||||
|
||||
public Person(String id, String fname, String lname, String phone) {
|
||||
public Person(String id, String fname, String lname) {
|
||||
this.firstName = fname;
|
||||
this.lastName = lname;
|
||||
this.phone = phone;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
@ -48,12 +46,4 @@ public class Person {
|
|||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository.repository.config;
|
||||
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.DocumentDbRepository;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.config.DocumentDbRepositoryConfigurationExtension;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.config.EnableDocumentDbRepositories;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.type.StandardAnnotationMetadata;
|
||||
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
|
||||
import org.springframework.data.repository.config.RepositoryConfiguration;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationSource;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
|
||||
public class DocumentDbRepositoryConfigurationExtensionUnitTest {
|
||||
|
||||
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(Config.class, true);
|
||||
ResourceLoader loader = new PathMatchingResourcePatternResolver();
|
||||
Environment environment = new StandardEnvironment();
|
||||
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
|
||||
EnableDocumentDbRepositories.class, loader, environment);
|
||||
|
||||
@Test
|
||||
public void isStrictMatchIfRepositoryExtendsStoreSpecificBase() {
|
||||
final DocumentDbRepositoryConfigurationExtension extension = new DocumentDbRepositoryConfigurationExtension();
|
||||
assertHashRepo(TestRepository.class, extension.getRepositoryConfigurations(configurationSource, loader, true));
|
||||
}
|
||||
|
||||
private static void assertHashRepo(Class<?> repositoryInterface,
|
||||
Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configs) {
|
||||
for (final RepositoryConfiguration<?> config : configs) {
|
||||
if (config.getRepositoryInterface().equals(repositoryInterface.getName())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fail("expected to find config for repository interface "
|
||||
+ repositoryInterface.getName() + ", but got: " + configs.toString());
|
||||
}
|
||||
|
||||
@EnableDocumentDbRepositories(considerNestedRepositories = true)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
interface TestRepository extends DocumentDbRepository<Object, String> {
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче