IntegrityConstraintsValidator { public <T> void validate(T instance, EntityType<T> et, boolean skipLazy) { Objects.requireNonNull(instance, ErrorUtils.getNPXMessageSupplier("instance")); Objects.requireNonNull(et, ErrorUtils.getNPXMessageSupplier("et")); final Object id = EntityPropertiesUtils.getIdentifier(instance, et); for (Attribute<? super T, ?> att : et.getAttributes()) { if (skipLazy && att.getFetchType() == FetchType.LAZY) { continue; } final Object value = EntityPropertiesUtils.getAttributeValue(att, instance); validate(id, att, value); } }  static IntegrityConstraintsValidator getValidator(); void validate(T instance, EntityType<T> et, boolean skipLazy); void validate(ObjectChangeSet changeSet, Metamodel metamodel); abstract void validate(Object identifier, FieldSpecification<?, ?> attribute, Object attributeValue);  }
@Test public void validationOfObjectWithoutConstraintsPasses() { final OWLClassA obj = new OWLClassA(); obj.setStringAttribute("aaaa"); validator.validate(obj, metamodel.entity(OWLClassA.class), false); }
@Test public void validationOfObjectChangeSetWithValidChangesPasses() throws Exception { final OWLClassN clone = createInstanceWithMissingRequiredField(); clone.setStringAttribute("newString"); final OWLClassN orig = createInstanceWithMissingRequiredField(); final ObjectChangeSet changeSet = new ObjectChangeSetImpl(orig, clone, new EntityDescriptor()); changeSet.addChangeRecord(new ChangeRecordImpl( metamodel.entity(OWLClassN.class).getFieldSpecification(OWLClassN.getStringAttributeField().getName()), "newString")); validator.validate(changeSet, metamodel); }
@Test public void validationOfValidInstanceWithCardinalityConstraintsPasses() { final OWLClassL obj = new OWLClassL(); obj.setSimpleList(Collections.singletonList(new OWLClassA())); obj.setSet(Collections.singleton(new OWLClassA())); obj.setSingleA(new OWLClassA()); validator.validate(obj, metamodel.entity(OWLClassL.class), false); }
@Test(expected = IntegrityConstraintViolatedException.class) public void missingRequiredAttributeOnObjectFailsValidation() { final OWLClassN n = createInstanceWithMissingRequiredField(); validator.validate(n, metamodel.entity(OWLClassN.class), false); }
@Test(expected = IntegrityConstraintViolatedException.class) public void missingRequiredAttributeInChangeSetFailsValidation() throws Exception { final OWLClassN clone = createInstanceWithMissingRequiredField(); final OWLClassN orig = createInstanceWithMissingRequiredField(); final ObjectChangeSet changeSet = new ObjectChangeSetImpl(orig, clone, new EntityDescriptor()); changeSet.addChangeRecord(new ChangeRecordImpl( metamodel.entity(OWLClassN.class).getFieldSpecification(OWLClassN.getStringAttributeField().getName()), null)); validator.validate(changeSet, metamodel); }
@Test(expected = IntegrityConstraintViolatedException.class) public void missingRequiredFieldValueFailsValidation() throws Exception { final OWLClassN n = createInstanceWithMissingRequiredField(); final Attribute<?, ?> att = metamodel.entity(OWLClassN.class) .getDeclaredAttribute(OWLClassN.getStringAttributeField().getName()); validator.validate(n.getId(), att, n.getStringAttribute()); }
@Test(expected = CardinalityConstraintViolatedException.class) public void violatedMinimumCardinalityConstraintFailsValidation() throws Exception { final OWLClassL obj = new OWLClassL(); obj.setSimpleList(Collections.singletonList(new OWLClassA())); obj.setSingleA(new OWLClassA()); validator.validate(obj, metamodel.entity(OWLClassL.class), false); }
@Test(expected = CardinalityConstraintViolatedException.class) public void violatedMaximumCardinalityConstraintFailsValidation() throws Exception { final OWLClassL orig = new OWLClassL(); final OWLClassL clone = new OWLClassL(); clone.setSimpleList(Collections.singletonList(new OWLClassA())); clone.setSet(Collections.singleton(new OWLClassA())); clone.setSingleA(new OWLClassA()); clone.setReferencedList(new ArrayList<>()); int max = OWLClassL.getReferencedListField().getAnnotation(ParticipationConstraints.class).value()[0].max(); for (int i = 0; i < max + 1; i++) { clone.getReferencedList().add(new OWLClassA()); } final ObjectChangeSet changeSet = new ObjectChangeSetImpl(orig, clone, new EntityDescriptor()); changeSet.addChangeRecord( new ChangeRecordImpl(metamodel.entity(OWLClassL.class) .getFieldSpecification(OWLClassL.getReferencedListField().getName()), clone.getReferencedList())); validator.validate(changeSet, metamodel); }
@Test(expected = CardinalityConstraintViolatedException.class) public void validationDetectsICViolationsInMappedSuperclass() { final OWLClassQ q = new OWLClassQ(); validator.validate(q, metamodel.entity(OWLClassQ.class), false); }
@Test(expected = CardinalityConstraintViolatedException.class) public void violatedNonEmptyConstraintOnPluralAttributeFailsValidationOfObject() { final OWLClassJ j = new OWLClassJ(); j.setUri(Generators.createIndividualIdentifier()); j.setOwlClassA(Collections.emptySet()); validator.validate(j, metamodel.entity(OWLClassJ.class), false); }
@Test(expected = CardinalityConstraintViolatedException.class) public void violatedNonEmptyConstraintOnPluralAttributeFailsValidationOfChangeSet() throws Exception { final OWLClassJ original = new OWLClassJ(Generators.createIndividualIdentifier()); original.setOwlClassA(Collections.singleton(new OWLClassA())); final OWLClassJ clone = new OWLClassJ(original.getUri()); clone.setOwlClassA(Collections.emptySet()); final ObjectChangeSet changeSet = ChangeSetFactory .createObjectChangeSet(original, clone, new EntityDescriptor()); changeSet.addChangeRecord(new ChangeRecordImpl( metamodel.entity(OWLClassJ.class).getFieldSpecification(OWLClassJ.getOwlClassAField().getName()), clone.getOwlClassA())); validator.validate(changeSet, metamodel); }