DATAREST-1260 - Tweaked DomainObjectReader to support immutable entities.

DomainObjectReader now aborts the recursive merges in case it encounters an entity that's described as @Immutable (just introduced in Spring Data Commons).

Fixed some generics and removed DomainObjectMerger as it's unused.

Related ticket: DATACMNS-1322.
This commit is contained in:
Oliver Gierke
2018-07-12 10:30:54 +02:00
parent c644eb979f
commit 429706a77e
8 changed files with 37 additions and 330 deletions

View File

@@ -15,7 +15,9 @@
*/
package org.springframework.data.rest.core.domain;
import lombok.AccessLevel;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import java.util.ArrayList;
import java.util.Calendar;
@@ -24,6 +26,7 @@ import java.util.List;
import java.util.UUID;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.annotation.Reference;
import org.springframework.data.rest.core.annotation.RestResource;
@@ -34,11 +37,16 @@ import org.springframework.data.rest.core.annotation.RestResource;
* @author Oliver Gierke
*/
@Data
@RequiredArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class Person {
private final @Id UUID id = UUID.randomUUID();
private final @Id UUID id;
private final String firstName, lastName;
public Person(String firstName, String lastName) {
this(UUID.randomUUID(), firstName, lastName);
}
private @Reference List<Person> siblings = new ArrayList<Person>();
private @RestResource(path = "father-mapped") @Reference Person father;
private Date created = Calendar.getInstance().getTime();

View File

@@ -1,90 +0,0 @@
/*
* Copyright 2014-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.core.support;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.rest.core.support.DomainObjectMerger.NullHandlingPolicy.*;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.domain.JpaRepositoryConfig;
import org.springframework.data.rest.core.domain.Person;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration tests for {@link DomainObjectMerger}.
*
* @author Greg Turnquist
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = JpaRepositoryConfig.class)
public class DomainObjectMergerTests {
@Autowired ConfigurableApplicationContext context;
DomainObjectMerger merger;
@Before
public void setUp() {
this.merger = new DomainObjectMerger(new Repositories(context.getBeanFactory()), new DefaultConversionService());
}
@Test // DATAREST-130
public void mergeNewValue() {
Person incoming = new Person("Bilbo", "Baggins");
Person existingDomainObject = new Person("Frodo", "Baggins");
merger.merge(incoming, existingDomainObject, APPLY_NULLS);
assertThat(existingDomainObject.getFirstName()).isEqualTo(incoming.getFirstName());
assertThat(existingDomainObject.getLastName()).isEqualTo(incoming.getLastName());
}
@Test // DATAREST-130
public void mergeNullValue() {
Person incoming = new Person(null, null);
Person existingDomainObject = new Person("Frodo", "Baggins");
merger.merge(incoming, existingDomainObject, APPLY_NULLS);
assertThat(existingDomainObject.getFirstName()).isEqualTo(incoming.getFirstName());
assertThat(existingDomainObject.getLastName()).isEqualTo(incoming.getLastName());
}
@Test // DATAREST-327
public void doesNotMergeEmptyCollectionsForReferences() {
Person bilbo = new Person("Bilbo", "Baggins");
Person frodo = new Person("Frodo", "Baggins");
frodo.setSiblings(Arrays.asList(bilbo));
merger.merge(new Person("Sam", null), frodo, IGNORE_NULLS);
assertThat(frodo.getSiblings()).isNotEmpty();
}
}

View File

@@ -1,55 +0,0 @@
/*
* Copyright 2014-2018 original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.core.support;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.rest.core.support.DomainObjectMerger.*;
import java.util.Collections;
import java.util.Iterator;
import org.junit.Test;
/**
* Unit tests for {@link DomainObjectMerger}.
*
* @author Oliver Gierke
*/
public class DomainObjectMergerUnitTests {
@Test // DATAREST-327
public void considersEmptyObjectsEmpty() {
assertThat(isNullOrEmpty(null)).isTrue();
assertThat(isNullOrEmpty(Collections.emptyList())).isTrue();
assertThat(isNullOrEmpty(new Object[0])).isTrue();
assertThat(isNullOrEmpty(new String[0])).isTrue();
assertThat(isNullOrEmpty(new MyIterable())).isTrue();
assertThat(isNullOrEmpty(new Object())).isFalse();
assertThat(isNullOrEmpty(Collections.singleton(new Object()))).isFalse();
assertThat(isNullOrEmpty(new Object[] { "1" })).isFalse();
assertThat(isNullOrEmpty(new String[] { "1" })).isFalse();
}
class MyIterable implements Iterable<Object> {
@Override
public Iterator<Object> iterator() {
return Collections.emptyList().iterator();
}
}
}