simplify assertHasLegacyId
This commit is contained in:
parent
c71b9b5ee0
commit
447f43811b
@ -13,8 +13,6 @@ import jakarta.persistence.PersistenceContext;
|
|||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
@Import(RbacGrantsDiagramService.class)
|
@Import(RbacGrantsDiagramService.class)
|
||||||
public abstract class ContextBasedTest {
|
public abstract class ContextBasedTest {
|
||||||
|
|
||||||
@ -77,28 +75,10 @@ public abstract class ContextBasedTest {
|
|||||||
final var table = entity.getClass().getAnnotation(Table.class);
|
final var table = entity.getClass().getAnnotation(Table.class);
|
||||||
final var legacyIdTable = table.schema() + "." + table.name().replace("_rv", "") + "_legacy_id";
|
final var legacyIdTable = table.schema() + "." + table.name().replace("_rv", "") + "_legacy_id";
|
||||||
|
|
||||||
final var legacyId = getSingleLegacyIdByUuidOrFail(entity, legacyIdTable, legacyIdColumnName);
|
em.createNativeQuery("SELECT " + legacyIdColumnName +
|
||||||
assertThatLegacyIdExistsExactlyOnce(legacyIdColumnName, legacyIdTable, legacyId);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assertThatLegacyIdExistsExactlyOnce(final String legacyIdColumnName, final String legacyIdTable, final long legacyId) {
|
|
||||||
em.createNativeQuery("SELECT *" +
|
|
||||||
" FROM " + legacyIdTable +
|
" FROM " + legacyIdTable +
|
||||||
" WHERE " + legacyIdColumnName + " = '" + legacyId + "'")
|
" WHERE uuid = '" + entity.getUuid() + "'", Long.class)
|
||||||
.getSingleResult(); // fails if there is not exactly one result
|
.getSingleResult(); // fails if there is 0 or more than 1 result
|
||||||
|
// that the legacyId is unique is checked by a DB constraint, if applicable
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getSingleLegacyIdByUuidOrFail(final BaseEntity<?> entity,
|
|
||||||
final String legacyIdTable,
|
|
||||||
final String legacyIdColumnName) {
|
|
||||||
final var uuid = entity.getUuid();
|
|
||||||
@SuppressWarnings("unchecked") // the auto-formatter should keep this linebreak
|
|
||||||
final var legacyId = (Long) em.createNativeQuery("SELECT " + legacyIdColumnName +
|
|
||||||
" FROM " + legacyIdTable +
|
|
||||||
" WHERE uuid = '" + uuid + "'", Long.class)
|
|
||||||
.getSingleResult();
|
|
||||||
assertThat(legacyId).isNotNull();
|
|
||||||
return legacyId;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user