simplify assertHasLegacyId

This commit is contained in:
Michael Hoennig 2025-01-22 10:07:55 +01:00
parent c71b9b5ee0
commit 447f43811b

View File

@ -13,8 +13,6 @@ import jakarta.persistence.PersistenceContext;
import jakarta.persistence.Table;
import java.sql.Timestamp;
import static org.assertj.core.api.Assertions.assertThat;
@Import(RbacGrantsDiagramService.class)
public abstract class ContextBasedTest {
@ -77,28 +75,10 @@ public abstract class ContextBasedTest {
final var table = entity.getClass().getAnnotation(Table.class);
final var legacyIdTable = table.schema() + "." + table.name().replace("_rv", "") + "_legacy_id";
final var legacyId = getSingleLegacyIdByUuidOrFail(entity, legacyIdTable, legacyIdColumnName);
assertThatLegacyIdExistsExactlyOnce(legacyIdColumnName, legacyIdTable, legacyId);
}
private void assertThatLegacyIdExistsExactlyOnce(final String legacyIdColumnName, final String legacyIdTable, final long legacyId) {
em.createNativeQuery("SELECT *" +
em.createNativeQuery("SELECT " + legacyIdColumnName +
" FROM " + legacyIdTable +
" WHERE " + legacyIdColumnName + " = '" + legacyId + "'")
.getSingleResult(); // fails if there is not exactly one result
" WHERE uuid = '" + entity.getUuid() + "'", Long.class)
.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;
}
}