DATAJDBC-438 - Polishing.
Code style. Choose better matching exception.
This commit is contained in:
@@ -20,7 +20,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.dao.TransientDataAccessResourceException;
|
||||
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
|
||||
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
|
||||
import org.springframework.data.mapping.PersistentPropertyPath;
|
||||
import org.springframework.data.relational.core.conversion.DbAction;
|
||||
@@ -52,6 +52,7 @@ import org.springframework.util.Assert;
|
||||
@RequiredArgsConstructor
|
||||
class DefaultJdbcInterpreter implements Interpreter {
|
||||
|
||||
public static final String UPDATE_FAILED = "Failed to update entity [%s]. Id [%s] not found in database.";
|
||||
private final RelationalMappingContext context;
|
||||
private final DataAccessStrategy accessStrategy;
|
||||
|
||||
@@ -84,11 +85,11 @@ class DefaultJdbcInterpreter implements Interpreter {
|
||||
*/
|
||||
@Override
|
||||
public <T> void interpret(Update<T> update) {
|
||||
boolean updated = accessStrategy.update(update.getEntity(), update.getEntityType());
|
||||
if (!updated) {
|
||||
Object idValue = getIdFrom(update);
|
||||
throw new TransientDataAccessResourceException(String.format(
|
||||
"Failed to update entity [%s]. Id [%s] does not exist.", update.getEntityType(), idValue));
|
||||
|
||||
if (!accessStrategy.update(update.getEntity(), update.getEntityType())) {
|
||||
|
||||
throw new IncorrectUpdateSemanticsDataAccessException(
|
||||
String.format(UPDATE_FAILED, update.getEntity(), getIdFrom(update)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,11 +99,11 @@ class DefaultJdbcInterpreter implements Interpreter {
|
||||
*/
|
||||
@Override
|
||||
public <T> void interpret(UpdateRoot<T> update) {
|
||||
boolean updated = accessStrategy.update(update.getEntity(), update.getEntityType());
|
||||
if (!updated) {
|
||||
Object idValue = getIdFrom(update);
|
||||
throw new TransientDataAccessResourceException(String.format(
|
||||
"Failed to update root [%s]. Id [%s] does not exist.", update.getEntityType(), idValue));
|
||||
|
||||
if (!accessStrategy.update(update.getEntity(), update.getEntityType())) {
|
||||
|
||||
throw new IncorrectUpdateSemanticsDataAccessException(
|
||||
String.format(UPDATE_FAILED, update.getEntity(), getIdFrom(update)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
|
||||
import org.springframework.dao.TransientDataAccessResourceException;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
|
||||
@@ -156,13 +156,18 @@ public class DefaultJdbcInterpreterUnitTests {
|
||||
);
|
||||
}
|
||||
|
||||
@Test(expected = TransientDataAccessResourceException.class) // DATAJDBC-438
|
||||
@Test // DATAJDBC-438
|
||||
public void throwExceptionUpdateFailedRootDoesNotExist() {
|
||||
|
||||
container.id = CONTAINER_ID;
|
||||
UpdateRoot<Container> containerUpdate = new UpdateRoot<>(container);
|
||||
when(dataAccessStrategy.update(container, Container.class)).thenReturn(false);
|
||||
|
||||
interpreter.interpret(containerUpdate);
|
||||
assertThatExceptionOfType(IncorrectUpdateSemanticsDataAccessException.class).isThrownBy(() -> {
|
||||
interpreter.interpret(containerUpdate);
|
||||
}) //
|
||||
.withMessageContaining(Long.toString(CONTAINER_ID)) //
|
||||
.withMessageContaining(container.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
@@ -21,7 +21,6 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@@ -42,6 +41,7 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.ReadOnlyProperty;
|
||||
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
|
||||
@@ -205,12 +205,15 @@ public class JdbcAggregateTemplateIntegrationTests {
|
||||
softly.assertAll();
|
||||
}
|
||||
|
||||
@Test(expected = DbActionExecutionException.class) // DATAJDBC-438
|
||||
@Test // DATAJDBC-438
|
||||
public void updateFailedRootDoesNotExist() {
|
||||
LegoSet entity = new LegoSet();
|
||||
entity.setId(100L); // not exist
|
||||
|
||||
template.save(entity);
|
||||
LegoSet entity = new LegoSet();
|
||||
entity.setId(100L); // does not exist in the database
|
||||
|
||||
assertThatExceptionOfType(DbActionExecutionException.class) //
|
||||
.isThrownBy(() -> template.save(entity)) //
|
||||
.withCauseInstanceOf(IncorrectUpdateSemanticsDataAccessException.class);
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-112
|
||||
@@ -622,7 +625,8 @@ public class JdbcAggregateTemplateIntegrationTests {
|
||||
template.save(entity);
|
||||
|
||||
assertThat(
|
||||
jdbcTemplate.queryForObject("SELECT read_only FROM with_read_only", Collections.emptyMap(), String.class)).isEqualTo("from-db");
|
||||
jdbcTemplate.queryForObject("SELECT read_only FROM with_read_only", Collections.emptyMap(), String.class))
|
||||
.isEqualTo("from-db");
|
||||
}
|
||||
|
||||
private static NoIdMapChain4 createNoIdMapTree() {
|
||||
@@ -886,8 +890,7 @@ public class JdbcAggregateTemplateIntegrationTests {
|
||||
static class WithReadOnly {
|
||||
@Id Long id;
|
||||
String name;
|
||||
@ReadOnlyProperty
|
||||
String readOnly;
|
||||
@ReadOnlyProperty String readOnly;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
||||
@@ -86,7 +86,6 @@ public class SimpleJdbcRepositoryEventsUnitTests {
|
||||
|
||||
this.dataAccessStrategy = spy(new DefaultDataAccessStrategy(generatorSource, context, converter, operations));
|
||||
delegatingDataAccessStrategy.setDelegate(dataAccessStrategy);
|
||||
|
||||
doReturn(true).when(dataAccessStrategy).update(any(), any());
|
||||
|
||||
JdbcRepositoryFactory factory = new JdbcRepositoryFactory(dataAccessStrategy, context, converter, publisher,
|
||||
|
||||
Reference in New Issue
Block a user