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