DATAJDBC-438 - Polishing.

Code style.
Choose better matching exception.
This commit is contained in:
Jens Schauder
2019-11-13 12:44:02 +01:00
parent dfaf0ac2e6
commit 92b6b0486c
4 changed files with 31 additions and 23 deletions

View File

@@ -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)));
}
}

View File

@@ -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")

View File

@@ -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

View File

@@ -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,