refactoring of AccessMappingsUnitTestBase overloading -> method refs in ctor
This commit is contained in:
parent
0de11a2548
commit
474d51fbae
@ -11,6 +11,7 @@ import java.lang.reflect.Modifier;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -25,9 +26,19 @@ import static org.junit.Assert.fail;
|
||||
*/
|
||||
public abstract class AccessMappingsUnitTestBase<D> {
|
||||
|
||||
private final Class<? extends AccessMappings> dtoClass;
|
||||
private final BiFunction<Long, Long, D> createSampleDTO;
|
||||
private final BiFunction<Long, Long, D> createRandomDTO;
|
||||
|
||||
public AccessMappingsUnitTestBase(Class<? extends AccessMappings> dtoClass, final BiFunction<Long, Long, D> createSampleDTO, final BiFunction<Long, Long, D> createRandomDTO) {
|
||||
this.dtoClass = dtoClass;
|
||||
this.createSampleDTO = createSampleDTO;
|
||||
this.createRandomDTO = createRandomDTO;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToString() {
|
||||
final D sampleDto = createSampleDto(1234L);
|
||||
final D sampleDto = createSampleDTO.apply(1234L, 77L);
|
||||
final String dtoAsString = dtoToString(sampleDto);
|
||||
assertThat(sampleDto.toString()).isEqualTo(dtoAsString);
|
||||
}
|
||||
@ -35,16 +46,16 @@ public abstract class AccessMappingsUnitTestBase<D> {
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void shouldImplementEqualsJustUsingClassAndId() {
|
||||
final D dto = createSampleDto(1234L);
|
||||
final D dto = createSampleDTO.apply(1234L, 77L);
|
||||
assertThat(dto.equals(dto)).isTrue();
|
||||
|
||||
final D dtoWithSameId = createRandomDto(1234L);
|
||||
final D dtoWithSameId = createSampleDTO.apply(1234L, 77L);
|
||||
assertThat(dto.equals(dtoWithSameId)).isTrue();
|
||||
|
||||
final D dtoWithAnotherId = createRandomDto(RandomUtils.nextLong(2000, 9999));
|
||||
final D dtoWithAnotherId = createSampleDTO.apply(RandomUtils.nextLong(2000, 9999), 77L);
|
||||
assertThat(dtoWithAnotherId.equals(dtoWithSameId)).isFalse();
|
||||
|
||||
final D dtoWithoutId = createRandomDto(null);
|
||||
final D dtoWithoutId = createSampleDTO.apply(null, RandomUtils.nextLong());
|
||||
assertThat(dto.equals(dtoWithoutId)).isFalse();
|
||||
assertThat(dtoWithoutId.equals(dto)).isFalse();
|
||||
|
||||
@ -55,16 +66,16 @@ public abstract class AccessMappingsUnitTestBase<D> {
|
||||
@Test
|
||||
public void shouldImplementHashCodeJustUsingClassAndId() {
|
||||
final long randomId = RandomUtils.nextLong();
|
||||
final D dto = createSampleDto(randomId);
|
||||
final D dto = createSampleDTO.apply(randomId, RandomUtils.nextLong());
|
||||
assertThat(dto.hashCode()).isEqualTo(Objects.hashCode(randomId));
|
||||
|
||||
final D dtoWithoutId = createRandomDto(null);
|
||||
final D dtoWithoutId = createSampleDTO.apply(null, RandomUtils.nextLong());
|
||||
assertThat(dtoWithoutId.hashCode()).isEqualTo(Objects.hashCode(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldImplementAccessMappings() {
|
||||
assertThat(getDtoClass().getInterfaces()).as("must implement " + AccessMappings.class).contains(AccessMappings.class);
|
||||
assertThat(dtoClass.getInterfaces()).as("must implement " + AccessMappings.class).contains(AccessMappings.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -79,10 +90,6 @@ public abstract class AccessMappingsUnitTestBase<D> {
|
||||
|
||||
// --- only test fixture below ---
|
||||
|
||||
protected abstract D createSampleDto(final Long id);
|
||||
|
||||
protected abstract D createRandomDto(final Long id);
|
||||
|
||||
protected AccessRightsMatcher initAccessFor(final Class<D> dtoClass, final Role role) {
|
||||
return new AccessRightsMatcher(dtoClass, role, AccessFor::init);
|
||||
}
|
||||
@ -195,7 +202,6 @@ public abstract class AccessMappingsUnitTestBase<D> {
|
||||
}
|
||||
|
||||
private void shouldImplementJsonComponent(final Class<?> expectedSuperclass) {
|
||||
final Class<D> dtoClass = getDtoClass();
|
||||
for (Class<?> declaredClass : dtoClass.getDeclaredClasses()) {
|
||||
if (expectedSuperclass.isAssignableFrom(declaredClass)) {
|
||||
assertThat(declaredClass.isAnnotationPresent(JsonComponent.class))
|
||||
@ -211,8 +217,4 @@ public abstract class AccessMappingsUnitTestBase<D> {
|
||||
}
|
||||
fail("no " + expectedSuperclass + " defined for " + dtoClass);
|
||||
}
|
||||
|
||||
private Class<D> getDtoClass() {
|
||||
return ReflectionUtil.determineGenericClassParameter(this.getClass(), AccessMappingsUnitTestBase.class, 0);
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,10 @@ import java.time.LocalDate;
|
||||
|
||||
public class AssetDTOUnitTest extends AccessMappingsUnitTestBase<AssetDTO> {
|
||||
|
||||
public AssetDTOUnitTest() {
|
||||
super(AssetDTO.class, AssetDTOUnitTest::createSampleDTO, AssetDTOUnitTest::createRandomDTO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveProperAccessForAdmin() {
|
||||
initAccessFor(AssetDTO.class, Role.ADMIN).shouldBeExactlyFor(
|
||||
@ -44,24 +48,20 @@ public class AssetDTOUnitTest extends AccessMappingsUnitTestBase<AssetDTO> {
|
||||
|
||||
// --- only test fixture below ---
|
||||
|
||||
@Override
|
||||
public AssetDTO createSampleDto(final Long id) {
|
||||
public static AssetDTO createSampleDTO(final Long id, final Long parentId) {
|
||||
final AssetDTO dto = new AssetDTO();
|
||||
dto.setId(id);
|
||||
dto.setDocumentDate(LocalDate.parse("2000-12-07"));
|
||||
dto.setAmount(new BigDecimal("512.01"));
|
||||
dto.setAction(AssetAction.PAYMENT);
|
||||
dto.setRemark("Some Remark");
|
||||
dto.setRemark(null);
|
||||
dto.setValueDate(LocalDate.parse("2000-12-18"));
|
||||
dto.setMembershipId(888L);
|
||||
dto.setMembershipId(null);
|
||||
dto.setMembershipId(parentId);
|
||||
dto.setMembershipDisplayLabel("Some Membership");
|
||||
return dto;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AssetDTO createRandomDto(final Long id) {
|
||||
public static AssetDTO createRandomDTO(final Long id, final Long parentId) {
|
||||
final AssetDTO dto = new AssetDTO();
|
||||
dto.setId(id);
|
||||
final LocalDate randomDate = LocalDate.parse("2000-12-07").plusDays(RandomUtils.nextInt(1, 999));
|
||||
@ -70,7 +70,7 @@ public class AssetDTOUnitTest extends AccessMappingsUnitTestBase<AssetDTO> {
|
||||
dto.setAction(RandomUtil.generateEnumValue(AssetAction.class));
|
||||
dto.setRemark(RandomStringUtils.randomAlphanumeric(20));
|
||||
dto.setValueDate(randomDate.plusDays(RandomUtils.nextInt(1, 99)));
|
||||
dto.setMembershipId(RandomUtils.nextLong());
|
||||
dto.setMembershipId(parentId);
|
||||
dto.setMembershipDisplayLabel("The Membership #" + dto.getMembershipId());
|
||||
return dto;
|
||||
}
|
||||
|
@ -11,6 +11,10 @@ import java.time.LocalDate;
|
||||
|
||||
public class ShareDTOUnitTest extends AccessMappingsUnitTestBase<ShareDTO> {
|
||||
|
||||
public ShareDTOUnitTest() {
|
||||
super(ShareDTO.class, ShareDTOUnitTest::createSampleDTO, ShareDTOUnitTest::createRandomDTO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveProperAccessForAdmin() {
|
||||
initAccessFor(ShareDTO.class, Role.ADMIN).shouldBeExactlyFor(
|
||||
@ -43,11 +47,10 @@ public class ShareDTOUnitTest extends AccessMappingsUnitTestBase<ShareDTO> {
|
||||
|
||||
// --- only test fixture below ---
|
||||
|
||||
@Override
|
||||
public ShareDTO createSampleDto(final Long id) {
|
||||
public static ShareDTO createSampleDTO(final Long id, final Long parentId) {
|
||||
final ShareDTO dto = new ShareDTO();
|
||||
dto.setId(id);
|
||||
dto.setMembershipId(888L);
|
||||
dto.setMembershipId(parentId);
|
||||
dto.setAction(ShareAction.SUBSCRIPTION);
|
||||
dto.setQuantity(3);
|
||||
dto.setDocumentDate(LocalDate.parse("2019-04-22"));
|
||||
@ -57,11 +60,10 @@ public class ShareDTOUnitTest extends AccessMappingsUnitTestBase<ShareDTO> {
|
||||
return dto;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShareDTO createRandomDto(final Long id) {
|
||||
public static ShareDTO createRandomDTO(final Long id, final Long parentId) {
|
||||
final ShareDTO dto = new ShareDTO();
|
||||
dto.setId(id);
|
||||
dto.setMembershipId(RandomUtils.nextLong());
|
||||
dto.setMembershipId(parentId);
|
||||
dto.setAction(RandomUtil.generateEnumValue(ShareAction.class));
|
||||
dto.setQuantity(RandomUtils.nextInt());
|
||||
final LocalDate randomDate = LocalDate.parse("2000-12-07").plusDays(RandomUtils.nextInt(1, 999));
|
||||
|
Loading…
Reference in New Issue
Block a user