TSK-950: Add Pojo Tests for field validation
This commit is contained in:
parent
0877e946a9
commit
82fb6bba3d
|
@ -108,6 +108,12 @@
|
|||
<version>${version.equalsverifier}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.openpojo</groupId>
|
||||
<artifactId>openpojo</artifactId>
|
||||
<version>${version.openpojo}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<!-- this repository is needed to fetch com.ibm.db2.jcc -->
|
||||
|
|
|
@ -525,6 +525,70 @@ public class TaskSummaryImpl implements TaskSummary {
|
|||
this.custom16 = custom16;
|
||||
}
|
||||
|
||||
public String getCustom1() {
|
||||
return custom1;
|
||||
}
|
||||
|
||||
public String getCustom2() {
|
||||
return custom2;
|
||||
}
|
||||
|
||||
public String getCustom3() {
|
||||
return custom3;
|
||||
}
|
||||
|
||||
public String getCustom4() {
|
||||
return custom4;
|
||||
}
|
||||
|
||||
public String getCustom5() {
|
||||
return custom5;
|
||||
}
|
||||
|
||||
public String getCustom6() {
|
||||
return custom6;
|
||||
}
|
||||
|
||||
public String getCustom7() {
|
||||
return custom7;
|
||||
}
|
||||
|
||||
public String getCustom8() {
|
||||
return custom8;
|
||||
}
|
||||
|
||||
public String getCustom9() {
|
||||
return custom9;
|
||||
}
|
||||
|
||||
public String getCustom10() {
|
||||
return custom10;
|
||||
}
|
||||
|
||||
public String getCustom11() {
|
||||
return custom11;
|
||||
}
|
||||
|
||||
public String getCustom12() {
|
||||
return custom12;
|
||||
}
|
||||
|
||||
public String getCustom13() {
|
||||
return custom13;
|
||||
}
|
||||
|
||||
public String getCustom14() {
|
||||
return custom14;
|
||||
}
|
||||
|
||||
public String getCustom15() {
|
||||
return custom15;
|
||||
}
|
||||
|
||||
public String getCustom16() {
|
||||
return custom16;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
|
|
|
@ -79,14 +79,18 @@ public class WorkbasketAccessItemImpl implements WorkbasketAccessItem {
|
|||
return accessId;
|
||||
}
|
||||
|
||||
public void setAccessId(String accessId) {
|
||||
public void setAccessIdWithSanitizing(String accessId) {
|
||||
if (TaskanaEngineConfiguration.shouldUseLowerCaseForAccessIds()) {
|
||||
this.accessId = accessId != null ? accessId.toLowerCase() : null;
|
||||
setAccessId(accessId != null ? accessId.toLowerCase() : null);
|
||||
} else {
|
||||
this.accessId = accessId;
|
||||
setAccessId(accessId);
|
||||
}
|
||||
}
|
||||
|
||||
public void setAccessId(String accessId) {
|
||||
this.accessId = accessId;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see pro.taskana.impl.WorkbasketAccessItem#getAccessName()
|
||||
|
|
|
@ -159,7 +159,7 @@ public class WorkbasketServiceImpl implements WorkbasketService {
|
|||
public WorkbasketAccessItem newWorkbasketAccessItem(String workbasketId, String accessId) {
|
||||
WorkbasketAccessItemImpl accessItem = new WorkbasketAccessItemImpl();
|
||||
accessItem.setWorkbasketId(workbasketId);
|
||||
accessItem.setAccessId(accessId);
|
||||
accessItem.setAccessIdWithSanitizing(accessId);
|
||||
return accessItem;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
package pro.taskana;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.DynamicTest;
|
||||
import org.junit.jupiter.api.TestFactory;
|
||||
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import nl.jqno.equalsverifier.Warning;
|
||||
import pro.taskana.impl.AttachmentImpl;
|
||||
import pro.taskana.impl.AttachmentSummaryImpl;
|
||||
import pro.taskana.impl.ClassificationImpl;
|
||||
import pro.taskana.impl.ClassificationSummaryImpl;
|
||||
import pro.taskana.impl.TaskImpl;
|
||||
import pro.taskana.impl.TaskSummaryImpl;
|
||||
import pro.taskana.impl.WorkbasketAccessItemImpl;
|
||||
import pro.taskana.impl.WorkbasketImpl;
|
||||
import pro.taskana.impl.WorkbasketSummaryImpl;
|
||||
|
||||
/**
|
||||
* check classes with a custom equals and hashcode implementation for correctness.
|
||||
*/
|
||||
class EqualsAndHashCodeTest {
|
||||
|
||||
@TestFactory
|
||||
Collection<DynamicTest> equalsContract() {
|
||||
return
|
||||
getPojoClasses().stream()
|
||||
.map(cl -> DynamicTest.dynamicTest("Check Hash and Equals for " + cl.getSimpleName(),
|
||||
() -> {
|
||||
EqualsVerifier.forClass(cl)
|
||||
.suppress(Warning.NONFINAL_FIELDS, Warning.STRICT_INHERITANCE)
|
||||
.withRedefinedSuperclass()
|
||||
.verify();
|
||||
}))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
//TODO find a way to dynamically create a list with custom implemented equals or hash methods.
|
||||
private List<Class<?>> getPojoClasses() {
|
||||
return Arrays.asList(
|
||||
KeyDomain.class,
|
||||
ObjectReference.class,
|
||||
TimeInterval.class,
|
||||
AttachmentImpl.class,
|
||||
AttachmentSummaryImpl.class,
|
||||
ClassificationImpl.class,
|
||||
ClassificationSummaryImpl.class,
|
||||
TaskImpl.class,
|
||||
TaskSummaryImpl.class,
|
||||
WorkbasketAccessItemImpl.class,
|
||||
WorkbasketImpl.class,
|
||||
WorkbasketSummaryImpl.class
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
package pro.taskana;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.DynamicTest;
|
||||
import org.junit.jupiter.api.TestFactory;
|
||||
|
||||
import com.openpojo.reflection.impl.PojoClassFactory;
|
||||
import com.openpojo.validation.ValidatorBuilder;
|
||||
import com.openpojo.validation.rule.Rule;
|
||||
import com.openpojo.validation.rule.impl.GetterMustExistRule;
|
||||
import com.openpojo.validation.rule.impl.NoPublicFieldsRule;
|
||||
import com.openpojo.validation.rule.impl.NoStaticExceptFinalRule;
|
||||
import com.openpojo.validation.rule.impl.SetterMustExistRule;
|
||||
import com.openpojo.validation.test.Tester;
|
||||
import com.openpojo.validation.test.impl.GetterTester;
|
||||
import com.openpojo.validation.test.impl.SetterTester;
|
||||
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import nl.jqno.equalsverifier.Warning;
|
||||
import pro.taskana.impl.AttachmentImpl;
|
||||
import pro.taskana.impl.AttachmentSummaryImpl;
|
||||
import pro.taskana.impl.ClassificationImpl;
|
||||
import pro.taskana.impl.ClassificationSummaryImpl;
|
||||
import pro.taskana.impl.TaskImpl;
|
||||
import pro.taskana.impl.TaskSummaryImpl;
|
||||
import pro.taskana.impl.WorkbasketAccessItemImpl;
|
||||
import pro.taskana.impl.WorkbasketImpl;
|
||||
import pro.taskana.impl.WorkbasketSummaryImpl;
|
||||
|
||||
/**
|
||||
* check classes with a custom equals and hashcode implementation for correctness.
|
||||
*/
|
||||
class PojoTest {
|
||||
|
||||
@TestFactory
|
||||
Collection<DynamicTest> equalsContract() {
|
||||
return
|
||||
getPojoClasses()
|
||||
.map(cl -> DynamicTest.dynamicTest("Check Hash and Equals for " + cl.getSimpleName(),
|
||||
() -> verifyHashAndEquals(cl)))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@TestFactory
|
||||
Collection<DynamicTest> validateGetters() {
|
||||
return getPojoClasses()
|
||||
.map(cl -> DynamicTest.dynamicTest("Check Getter exist for " + cl.getSimpleName(),
|
||||
() -> validateWithRules(cl, new GetterMustExistRule())
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
@TestFactory
|
||||
Collection<DynamicTest> validateSetters() {
|
||||
return getPojoClasses()
|
||||
.map(cl -> DynamicTest.dynamicTest("Check Setter for " + cl.getSimpleName(),
|
||||
() -> validateWithRules(cl, new SetterMustExistRule())
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@TestFactory
|
||||
Collection<DynamicTest> validateGetAndSet() {
|
||||
return getPojoClasses()
|
||||
.map(cl -> DynamicTest.dynamicTest("Test set & get " + cl.getSimpleName(),
|
||||
() -> validateWithTester(cl, new GetterTester(), new SetterTester())
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@TestFactory
|
||||
Collection<DynamicTest> validateNoStaticExceptFinalFields() {
|
||||
return getPojoClasses()
|
||||
.map(cl -> DynamicTest.dynamicTest("Check static fields for " + cl.getSimpleName(),
|
||||
() -> validateWithRules(cl, new NoStaticExceptFinalRule())
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@TestFactory
|
||||
Collection<DynamicTest> validateNoPublicFields() {
|
||||
return getPojoClasses()
|
||||
.map(cl -> DynamicTest.dynamicTest("Check public fields for " + cl.getSimpleName(),
|
||||
() -> validateWithRules(cl, new NoPublicFieldsRule())
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void validateWithRules(Class<?> cl, Rule... rules) {
|
||||
ValidatorBuilder.create()
|
||||
.with(rules)
|
||||
.build()
|
||||
.validate(PojoClassFactory.getPojoClass(cl));
|
||||
}
|
||||
private void validateWithTester(Class<?> cl, Tester... testers) {
|
||||
ValidatorBuilder.create()
|
||||
.with(testers)
|
||||
.build()
|
||||
.validate(PojoClassFactory.getPojoClass(cl));
|
||||
}
|
||||
|
||||
private void verifyHashAndEquals(Class<?> cl) {
|
||||
EqualsVerifier.forClass(cl)
|
||||
.suppress(Warning.NONFINAL_FIELDS, Warning.STRICT_INHERITANCE)
|
||||
.withRedefinedSuperclass()
|
||||
.verify();
|
||||
}
|
||||
|
||||
//TODO find a way to dynamically create a list with custom implemented equals or hash methods.
|
||||
private Stream<Class<?>> getPojoClasses() {
|
||||
return Stream.of(
|
||||
KeyDomain.class,
|
||||
ObjectReference.class,
|
||||
TimeInterval.class,
|
||||
AttachmentImpl.class,
|
||||
AttachmentSummaryImpl.class,
|
||||
ClassificationImpl.class,
|
||||
ClassificationSummaryImpl.class,
|
||||
TaskImpl.class,
|
||||
TaskSummaryImpl.class,
|
||||
WorkbasketAccessItemImpl.class,
|
||||
WorkbasketImpl.class,
|
||||
WorkbasketSummaryImpl.class
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue