support List in domain fields
This commit is contained in:
Родитель
edd63da01b
Коммит
47434f8490
5
pom.xml
5
pom.xml
|
@ -127,6 +127,11 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -9,7 +9,6 @@ package com.microsoft.azure.spring.data.documentdb.core;
|
|||
import com.microsoft.azure.documentdb.*;
|
||||
import com.microsoft.azure.documentdb.internal.HttpConstants;
|
||||
import com.microsoft.azure.spring.data.documentdb.DocumentDbFactory;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.convert.DocumentDbConverter;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.convert.MappingDocumentDbConverter;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.query.Query;
|
||||
import org.slf4j.Logger;
|
||||
|
|
|
@ -5,10 +5,12 @@
|
|||
*/
|
||||
package com.microsoft.azure.spring.data.documentdb.core.convert;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.microsoft.azure.documentdb.Document;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.DocumentDbPersistentEntity;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.DocumentDbPersistentProperty;
|
||||
import org.apache.commons.lang3.ClassUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
|
@ -20,6 +22,7 @@ import org.springframework.data.mapping.context.MappingContext;
|
|||
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
@ -55,7 +58,7 @@ public class MappingDocumentDbConverter
|
|||
final Document sourceDocument) {
|
||||
final R result = instantiate(type);
|
||||
|
||||
final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(result);
|
||||
final PersistentPropertyAccessor accessor = getPropertyAccessor(result);
|
||||
|
||||
final DocumentDbPersistentProperty idProperty = entity.getIdProperty();
|
||||
final Object idValue = sourceDocument.getId();
|
||||
|
@ -64,12 +67,37 @@ public class MappingDocumentDbConverter
|
|||
accessor.setProperty(idProperty, idValue);
|
||||
}
|
||||
|
||||
entity.doWithProperties((PropertyHandler<DocumentDbPersistentProperty>) prop -> {
|
||||
if (idProperty != null && idProperty.equals(prop)) {
|
||||
return;
|
||||
}
|
||||
accessor.setProperty(prop, sourceDocument.get(prop.getName()));
|
||||
entity.doWithProperties((PropertyHandler<DocumentDbPersistentProperty>) (DocumentDbPersistentProperty prop) -> {
|
||||
if (idProperty != null && idProperty.equals(prop)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Object fieldValue = sourceDocument.get(prop.getName());
|
||||
|
||||
if (fieldValue instanceof JSONArray) {
|
||||
final List<Object> mappedList = new ArrayList<>();
|
||||
for (int i = 0; i < ((JSONArray) fieldValue).length(); i++) {
|
||||
Object value;
|
||||
if (prop.getActualType().isPrimitive() || prop.getActualType().equals(String.class)) {
|
||||
value = ((JSONArray) fieldValue).getString(i);
|
||||
} else {
|
||||
final String curJson = ((JSONArray) fieldValue).getJSONObject(i).toString();
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
value = objectMapper.readValue(curJson, prop.getActualType());
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Cannot convert json string " + curJson
|
||||
+ " to type " + prop.getActualType(), e);
|
||||
}
|
||||
}
|
||||
mappedList.add(value);
|
||||
}
|
||||
accessor.setProperty(prop, mappedList);
|
||||
} else {
|
||||
final Object converted = conversionService.convert(fieldValue, prop.getActualType());
|
||||
accessor.setProperty(prop, converted);
|
||||
}
|
||||
}
|
||||
);
|
||||
return result;
|
||||
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository;
|
||||
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
import com.microsoft.azure.spring.data.documentdb.domain.Address;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Constants {
|
||||
public static final List<String> HOBBIES = Arrays.asList("photography", "fishing");
|
||||
|
||||
private static final Address ADDRESS_1 = new Address("201107", "Zixing Road", "Shanghai");
|
||||
private static final Address ADDRESS_2 = new Address("200000", "Xuhui", "Shanghai");
|
||||
public static final List<Address> ADDRESSES = Arrays.asList(ADDRESS_1, ADDRESS_2);
|
||||
}
|
|
@ -9,8 +9,10 @@ package com.microsoft.azure.spring.data.documentdb.core;
|
|||
import com.microsoft.azure.documentdb.ConnectionPolicy;
|
||||
import com.microsoft.azure.documentdb.ConsistencyLevel;
|
||||
import com.microsoft.azure.documentdb.DocumentClient;
|
||||
import com.microsoft.azure.spring.data.documentdb.Constants;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.convert.MappingDocumentDbConverter;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.DocumentDbMappingContext;
|
||||
import com.microsoft.azure.spring.data.documentdb.domain.Address;
|
||||
import com.microsoft.azure.spring.data.documentdb.domain.Person;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
@ -27,16 +29,18 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@PropertySource(value = {"classpath:application.properties"})
|
||||
public class DocumentDbTemplateIT {
|
||||
private static final String TEST_ID = "template_it_id";
|
||||
private static final String TEST_NOTEXIST_ID = "non_exist_id";
|
||||
|
||||
private static final String TEST_ID = "testid";
|
||||
private static final String TEST_NOTEXIST_ID = "testid2";
|
||||
|
||||
private static final String TEST_DB_NAME = "testdb";
|
||||
private static final Person TEST_PERSON = new Person(TEST_ID, "testfirstname", "testlastname");
|
||||
private static final String TEST_DB_NAME = "template_it_db";
|
||||
private static final List<String> HOBBIES = Constants.HOBBIES;
|
||||
private static final List<Address> ADDRESSES = Constants.ADDRESSES;
|
||||
private static final Person TEST_PERSON = new Person(TEST_ID, "testfirstname", "testlastname", HOBBIES, ADDRESSES);
|
||||
|
||||
private static final String PARTITION_KEY = "lastName";
|
||||
|
||||
|
@ -88,9 +92,7 @@ public class DocumentDbTemplateIT {
|
|||
public void testFindAll() {
|
||||
final List<Person> result = dbTemplate.findAll(Person.class.getSimpleName(), Person.class, null, null);
|
||||
assertThat(result.size()).isEqualTo(1);
|
||||
assertThat(result.get(0).getId()).isEqualTo(TEST_PERSON.getId());
|
||||
assertThat(result.get(0).getFirstName()).isEqualTo(TEST_PERSON.getFirstName());
|
||||
assertThat(result.get(0).getLastName()).isEqualTo(TEST_PERSON.getLastName());
|
||||
assertThat(result.get(0)).isEqualTo(TEST_PERSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -101,18 +103,14 @@ public class DocumentDbTemplateIT {
|
|||
Person.class, PARTITION_KEY, TEST_PERSON.getLastName());
|
||||
|
||||
assertThat(result.size()).isEqualTo(1);
|
||||
assertThat(result.get(0).getId()).isEqualTo(TEST_PERSON.getId());
|
||||
assertThat(result.get(0).getFirstName()).isEqualTo(TEST_PERSON.getFirstName());
|
||||
assertThat(result.get(0).getLastName()).isEqualTo(TEST_PERSON.getLastName());
|
||||
assertThat(result.get(0)).isEqualTo(TEST_PERSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindById() {
|
||||
final Person result = dbTemplate.findById(Person.class.getSimpleName(),
|
||||
TEST_PERSON.getId(), Person.class, null);
|
||||
assertThat(result.getId()).isEqualTo(TEST_PERSON.getId());
|
||||
assertThat(result.getFirstName()).isEqualTo(TEST_PERSON.getFirstName());
|
||||
assertThat(result.getLastName()).isEqualTo(TEST_PERSON.getLastName());
|
||||
assertTrue(result.equals(TEST_PERSON));
|
||||
|
||||
final Person nullResult = dbTemplate.findById(Person.class.getSimpleName(),
|
||||
TEST_NOTEXIST_ID, Person.class, null);
|
||||
|
@ -125,9 +123,7 @@ public class DocumentDbTemplateIT {
|
|||
|
||||
final Person result = dbTemplate.findById(Person.class.getSimpleName(),
|
||||
TEST_PERSON.getId(), Person.class, TEST_PERSON.getLastName());
|
||||
assertThat(result.getId()).isEqualTo(TEST_PERSON.getId());
|
||||
assertThat(result.getFirstName()).isEqualTo(TEST_PERSON.getFirstName());
|
||||
assertThat(result.getLastName()).isEqualTo(TEST_PERSON.getLastName());
|
||||
assertTrue(result.equals(TEST_PERSON));
|
||||
|
||||
final Person nullResult = dbTemplate.findById(Person.class.getSimpleName(),
|
||||
TEST_NOTEXIST_ID, Person.class, TEST_PERSON.getLastName());
|
||||
|
@ -137,35 +133,31 @@ public class DocumentDbTemplateIT {
|
|||
@Test
|
||||
public void testUpdate() {
|
||||
final Person updated = new Person(TEST_PERSON.getId(), "updatedname",
|
||||
TEST_PERSON.getLastName());
|
||||
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(), TEST_PERSON.getShippingAddresses());
|
||||
dbTemplate.update(Person.class.getSimpleName(), updated, updated.getId(), null);
|
||||
|
||||
final Person result = dbTemplate.findById(Person.class.getSimpleName(),
|
||||
updated.getId(), Person.class, null);
|
||||
|
||||
assertThat(result.getId()).isEqualTo(updated.getId());
|
||||
assertThat(result.getFirstName()).isEqualTo(updated.getFirstName());
|
||||
assertThat(result.getLastName()).isEqualTo(updated.getLastName());
|
||||
assertTrue(result.equals(updated));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePartition() {
|
||||
setupPartition();
|
||||
final Person updated = new Person(TEST_PERSON.getId(), "updatedname",
|
||||
TEST_PERSON.getLastName());
|
||||
TEST_PERSON.getLastName(), TEST_PERSON.getHobbies(), TEST_PERSON.getShippingAddresses());
|
||||
dbTemplate.update(Person.class.getSimpleName(), updated, updated.getId(), updated.getLastName());
|
||||
|
||||
final Person result = dbTemplate.findById(Person.class.getSimpleName(),
|
||||
updated.getId(), Person.class, updated.getLastName());
|
||||
|
||||
assertThat(result.getId()).isEqualTo(updated.getId());
|
||||
assertThat(result.getFirstName()).isEqualTo(updated.getFirstName());
|
||||
assertThat(result.getLastName()).isEqualTo(updated.getLastName());
|
||||
assertTrue(result.equals(updated));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteById() {
|
||||
final Person person2 = new Person("newid", "newfn", "newln");
|
||||
final Person person2 = new Person("newid", "newfn", "newln", HOBBIES, ADDRESSES);
|
||||
dbTemplate.insert(person2, null);
|
||||
assertThat(dbTemplate.findAll(Person.class, null, null).size()).isEqualTo(2);
|
||||
|
||||
|
@ -173,10 +165,7 @@ public class DocumentDbTemplateIT {
|
|||
|
||||
final List<Person> result = dbTemplate.findAll(Person.class, null, null);
|
||||
assertThat(result.size()).isEqualTo(1);
|
||||
assertThat(result.get(0).getId()).isEqualTo(person2.getId());
|
||||
assertThat(result.get(0).getFirstName()).isEqualTo(person2.getFirstName());
|
||||
assertThat(result.get(0).getLastName()).isEqualTo(person2.getLastName());
|
||||
|
||||
assertTrue(result.get(0).equals(person2));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -184,7 +173,7 @@ public class DocumentDbTemplateIT {
|
|||
setupPartition();
|
||||
|
||||
// insert new document with same partition key
|
||||
final Person person2 = new Person("newid", "newfn", TEST_PERSON.getLastName());
|
||||
final Person person2 = new Person("newid", "newfn", TEST_PERSON.getLastName(), HOBBIES, ADDRESSES);
|
||||
dbTemplate.insert(Person.class.getSimpleName(), person2, person2.getLastName());
|
||||
|
||||
assertThat(dbTemplate.findAll(Person.class, PARTITION_KEY, person2.getLastName()).size()).isEqualTo(2);
|
||||
|
@ -194,10 +183,7 @@ public class DocumentDbTemplateIT {
|
|||
|
||||
final List<Person> result = dbTemplate.findAll(Person.class, PARTITION_KEY, person2.getLastName());
|
||||
assertThat(result.size()).isEqualTo(1);
|
||||
assertThat(result.get(0).getId()).isEqualTo(person2.getId());
|
||||
assertThat(result.get(0).getFirstName()).isEqualTo(person2.getFirstName());
|
||||
assertThat(result.get(0).getLastName()).isEqualTo(person2.getLastName());
|
||||
|
||||
assertTrue(result.get(0).equals(person2));
|
||||
}
|
||||
|
||||
private void setupPartition() {
|
||||
|
|
|
@ -8,22 +8,33 @@ package com.microsoft.azure.spring.data.documentdb.core.converter;
|
|||
|
||||
|
||||
import com.microsoft.azure.documentdb.Document;
|
||||
import com.microsoft.azure.spring.data.documentdb.Constants;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.convert.DocumentDbConverter;
|
||||
import com.microsoft.azure.spring.data.documentdb.domain.Address;
|
||||
import com.microsoft.azure.spring.data.documentdb.domain.Person;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DocumentDbConverterUnitTest {
|
||||
|
||||
private static final String id = "testId";
|
||||
private static final String id = "db_converter_test_id";
|
||||
private static final String firstName = "testFirstName";
|
||||
private static final String lastName = "testLastName";
|
||||
private static final List<String> hobbies = Constants.HOBBIES;
|
||||
private static final List<Address> addresses = Constants.ADDRESSES;
|
||||
|
||||
private static final String idPropertyName = "id";
|
||||
private static final String firstNamePropertyName = "firstName";
|
||||
private static final String lastNamePropertyName = "lastName";
|
||||
private static final String hobbiesPropertyName = "hobbies";
|
||||
private static final String shippingAddressesPropertyName = "shippingAddresses";
|
||||
|
||||
private DocumentDbConverter dbConverter;
|
||||
|
||||
@Before
|
||||
|
@ -33,15 +44,24 @@ public class DocumentDbConverterUnitTest {
|
|||
|
||||
@Test
|
||||
public void testConvertFromEntityToDocument() {
|
||||
final Person person = new Person(id, firstName, lastName);
|
||||
final Person person = new Person(id, firstName, lastName, hobbies, addresses);
|
||||
final Document document = dbConverter.convertToDocument(person);
|
||||
|
||||
assertThat(document.has(idPropertyName)).isTrue();
|
||||
assertThat(document.has(firstNamePropertyName)).isTrue();
|
||||
assertThat(document.has(lastNamePropertyName)).isTrue();
|
||||
assertTrue(document.has(idPropertyName));
|
||||
assertTrue(document.has(firstNamePropertyName));
|
||||
assertTrue(document.has(lastNamePropertyName));
|
||||
assertTrue(document.has(hobbiesPropertyName));
|
||||
assertTrue(document.has(shippingAddressesPropertyName));
|
||||
assertThat(document.getId()).isEqualTo(id);
|
||||
assertThat(document.getString(firstNamePropertyName)).isEqualTo(firstName);
|
||||
assertThat(document.getString(lastNamePropertyName)).isEqualTo(lastName);
|
||||
|
||||
final Collection<String> convertedHobbies = document.getCollection(hobbiesPropertyName, String.class);
|
||||
assertTrue(hobbies.equals(convertedHobbies));
|
||||
|
||||
final Collection<Address> convertedAddresses =
|
||||
document.getCollection(shippingAddressesPropertyName, Address.class);
|
||||
assertTrue(addresses.equals(convertedAddresses));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -50,12 +70,15 @@ public class DocumentDbConverterUnitTest {
|
|||
json.put(idPropertyName, id);
|
||||
json.put(firstNamePropertyName, firstName);
|
||||
json.put(lastNamePropertyName, lastName);
|
||||
json.put(hobbiesPropertyName, hobbies);
|
||||
json.put(shippingAddressesPropertyName, addresses);
|
||||
|
||||
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);
|
||||
|
||||
assertThat(person.getHobbies().equals(hobbies));
|
||||
assertThat(person.getShippingAddresses().equals(addresses));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,43 +7,17 @@ package com.microsoft.azure.spring.data.documentdb.domain;
|
|||
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.Document;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.PartitionKey;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
@Document(ru = "1000")
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class Address {
|
||||
@Id
|
||||
String postalCode;
|
||||
String street;
|
||||
@PartitionKey
|
||||
String city;
|
||||
|
||||
public Address(String postalCode, String city, String street) {
|
||||
this.postalCode = postalCode;
|
||||
this.city = city;
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getPostalCode() {
|
||||
return this.postalCode;
|
||||
}
|
||||
|
||||
public void setPostalCode(String code) {
|
||||
this.postalCode = code;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,4 +18,5 @@ public class Person {
|
|||
private String firstName;
|
||||
private String lastName;
|
||||
private List<String> hobbies;
|
||||
private List<Address> shippingAddresses;
|
||||
}
|
||||
|
|
|
@ -23,9 +23,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
@ContextConfiguration(classes = ContactRepositoryConfig.class)
|
||||
public class AddressRepositoryIT {
|
||||
|
||||
private static final Address TEST_ADDRESS1_PARTITION1 = new Address("111", "redmond", "111st avenue");
|
||||
private static final Address TEST_ADDRESS2_PARTITION1 = new Address("222", "redmond", "98th street");
|
||||
private static final Address TEST_ADDRESS1_PARTITION2 = new Address("333", "bellevue", "103rd street");
|
||||
private static final Address TEST_ADDRESS1_PARTITION1 = new Address("111", "111st avenue", "redmond");
|
||||
private static final Address TEST_ADDRESS2_PARTITION1 = new Address("222", "98th street", "redmond");
|
||||
private static final Address TEST_ADDRESS1_PARTITION2 = new Address("333", "103rd street", "bellevue");
|
||||
|
||||
@Autowired
|
||||
AddressRepository repository;
|
||||
|
@ -83,8 +83,8 @@ public class AddressRepositoryIT {
|
|||
|
||||
@Test
|
||||
public void testUpdateEntity() {
|
||||
final Address updatedAddress = new Address(TEST_ADDRESS1_PARTITION1.getPostalCode(),
|
||||
TEST_ADDRESS1_PARTITION1.getCity(), "new street");
|
||||
final Address updatedAddress = new Address(TEST_ADDRESS1_PARTITION1.getPostalCode(), "new street",
|
||||
TEST_ADDRESS1_PARTITION1.getCity());
|
||||
|
||||
repository.update(updatedAddress, updatedAddress.getCity());
|
||||
|
||||
|
|
|
@ -6,7 +6,9 @@
|
|||
|
||||
package com.microsoft.azure.spring.data.documentdb.repository;
|
||||
|
||||
import com.microsoft.azure.spring.data.documentdb.Constants;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.DocumentDbOperations;
|
||||
import com.microsoft.azure.spring.data.documentdb.domain.Address;
|
||||
import com.microsoft.azure.spring.data.documentdb.domain.Person;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.support.DocumentDbEntityInformation;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.support.SimpleDocumentDbRepository;
|
||||
|
@ -17,6 +19,7 @@ import org.mockito.Mock;
|
|||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
|
@ -25,8 +28,10 @@ import static org.mockito.Mockito.when;
|
|||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SimpleDocumentDbRepositoryUnitTest {
|
||||
private static final Person TEST_PERSON =
|
||||
new Person("test_person", "firstname", "lastname",
|
||||
Constants.HOBBIES, Constants.ADDRESSES);
|
||||
|
||||
private static final Person TEST_PERSON = new Person("aaa", "firstname", "lastname");
|
||||
SimpleDocumentDbRepository<Person, String> repository;
|
||||
@Mock
|
||||
DocumentDbOperations dbOperations;
|
||||
|
@ -49,8 +54,9 @@ public class SimpleDocumentDbRepositoryUnitTest {
|
|||
public void testSave() {
|
||||
repository.save(TEST_PERSON);
|
||||
|
||||
assertEquals(1, repository.findAll(TEST_PERSON.getLastName()).size());
|
||||
assertEquals(TEST_PERSON.getFirstName(), repository.findAll(TEST_PERSON.getLastName()).get(0).getFirstName());
|
||||
final List<Person> result = repository.findAll(TEST_PERSON.getLastName());
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(TEST_PERSON, result.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -60,23 +66,21 @@ public class SimpleDocumentDbRepositoryUnitTest {
|
|||
repository.save(TEST_PERSON);
|
||||
|
||||
final Person result = repository.findOne(TEST_PERSON.getId(), TEST_PERSON.getLastName());
|
||||
|
||||
assertEquals(result.getId(), TEST_PERSON.getId());
|
||||
assertEquals(result.getFirstName(), TEST_PERSON.getFirstName());
|
||||
assertEquals(result.getLastName(), TEST_PERSON.getLastName());
|
||||
assertEquals(TEST_PERSON, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
final Person updatedPerson = new Person(TEST_PERSON.getId(), "updated", "updated");
|
||||
final List<Address> updatedAddress =
|
||||
Arrays.asList(new Address("12345", "updated city", "updated street"));
|
||||
final Person updatedPerson =
|
||||
new Person(TEST_PERSON.getId(), "updated", "updated",
|
||||
Arrays.asList("updated hobbies"), updatedAddress);
|
||||
repository.update(updatedPerson);
|
||||
|
||||
when(dbOperations.findById(anyString(), any(), any(), anyString())).thenReturn(updatedPerson);
|
||||
|
||||
final Person result = repository.findOne(TEST_PERSON.getId(), TEST_PERSON.getLastName());
|
||||
|
||||
assertEquals(result.getId(), updatedPerson.getId());
|
||||
assertEquals(result.getFirstName(), updatedPerson.getFirstName());
|
||||
assertEquals(result.getLastName(), updatedPerson.getLastName());
|
||||
assertEquals(updatedPerson, result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,17 +5,26 @@
|
|||
*/
|
||||
package com.microsoft.azure.spring.data.documentdb.repository.support;
|
||||
|
||||
import com.microsoft.azure.spring.data.documentdb.Constants;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.Document;
|
||||
import com.microsoft.azure.spring.data.documentdb.domain.Address;
|
||||
import com.microsoft.azure.spring.data.documentdb.domain.Person;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DocumentDbEntityInformationUnitTest {
|
||||
private static final String ID = "entity_info_test_id";
|
||||
private static final String FIRST_NAME = "first name";
|
||||
private static final String LAST_NAME = "last name";
|
||||
private static final List<String> HOBBIES = Constants.HOBBIES;
|
||||
private static final List<Address> ADDRESSES = Constants.ADDRESSES;
|
||||
|
||||
@Test
|
||||
public void testGetId() {
|
||||
final Person testPerson = new Person("test", "test", "test");
|
||||
final Person testPerson = new Person(ID, FIRST_NAME, LAST_NAME, HOBBIES, ADDRESSES);
|
||||
final DocumentDbEntityInformation<Person, String> entityInformation =
|
||||
new DocumentDbEntityInformation<Person, String>(Person.class);
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче