use of javax.validation.ConstraintValidatorContext in project api-core by ca-cwds.
the class SystemCodeIdValidatorTest method isValid_Args__Object__ValidCollection.
@Test
public void isValid_Args__Object__ValidCollection() throws Exception {
ValidSystemCodeId anno = mock(ValidSystemCodeId.class);
when(anno.required()).thenReturn(true);
target.initialize(anno);
Set<String> validCodes = new HashSet<>();
validCodes.add("100");
ConstraintValidatorContext context_ = mock(ConstraintValidatorContext.class);
boolean actual = target.isValid(validCodes, context_);
boolean expected = true;
assertThat(actual, is(equalTo(expected)));
}
use of javax.validation.ConstraintValidatorContext in project dataverse by IQSS.
the class DatasetFieldValidatorTest method testIsValid.
/**
* Test of isValid method, of class DatasetFieldValidator.
*/
@Test
public void testIsValid() {
System.out.println("isValid");
DatasetField value = new DatasetField();
final ConstraintValidatorContext ctx = Mockito.mock(ConstraintValidatorContext.class);
DatasetFieldValidator instance = new DatasetFieldValidator();
// If its a template field it is always valid
value.setTemplate(new Template());
boolean expResult = true;
boolean result = instance.isValid(value, ctx);
assertEquals(expResult, result);
// if not template and required
value.setTemplate(null);
DatasetVersion datasetVersion = new DatasetVersion();
Dataset dataset = new Dataset();
Dataverse dataverse = new Dataverse();
dataset.setOwner(dataverse);
datasetVersion.setDataset(dataset);
value.setDatasetVersion(datasetVersion);
DatasetFieldValue dfv = new DatasetFieldValue();
DatasetFieldType dft = new DatasetFieldType("test", DatasetFieldType.FieldType.TEXT, false);
dft.setRequired(true);
value.setDatasetFieldType(dft);
value.setSingleValue("");
dfv.setValue("");
result = instance.isValid(value, ctx);
assertEquals(false, result);
// Fill in a value - should be valid now....
value.setSingleValue("value");
result = instance.isValid(value, ctx);
assertEquals(true, result);
// if not required - can be blank
dft.setRequired(false);
value.setSingleValue("");
result = instance.isValid(value, ctx);
assertEquals(true, result);
}
use of javax.validation.ConstraintValidatorContext in project api-core by ca-cwds.
the class SystemCodeIdValidatorTest method isValid_Args__Object__ConstraintValidatorContext.
@Test
public void isValid_Args__Object__ConstraintValidatorContext() throws Exception {
Short value = new Short(INVALID_SYSTEM_CODE_VALUE);
ConstraintValidatorContext context_ = mock(ConstraintValidatorContext.class);
boolean actual = target.isValid(value, context_);
boolean expected = false;
assertThat(actual, is(equalTo(expected)));
}
use of javax.validation.ConstraintValidatorContext in project api-core by ca-cwds.
the class SystemCodeIdValidatorTest method isValid_Args__Object__InvalidCollectionValues.
@Test
public void isValid_Args__Object__InvalidCollectionValues() throws Exception {
ValidSystemCodeId anno = mock(ValidSystemCodeId.class);
when(anno.required()).thenReturn(true);
when(anno.category()).thenReturn("ETHNCTYC");
target.initialize(anno);
Set<String> validCodes = new HashSet<>();
validCodes.add("0");
ConstraintValidatorContext context_ = mock(ConstraintValidatorContext.class);
ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class);
when(builder.addPropertyNode(any())).thenReturn(mock(NodeBuilderCustomizableContext.class));
when(context_.buildConstraintViolationWithTemplate(any())).thenReturn(builder);
boolean actual = target.isValid(validCodes, context_);
boolean expected = false;
assertThat(actual, is(equalTo(expected)));
}
use of javax.validation.ConstraintValidatorContext in project api-core by ca-cwds.
the class DroolsValidator method isValid.
@Override
public boolean isValid(T obj, ConstraintValidatorContext context) {
if (obj == null) {
return true;
}
DroolsConfiguration<T> configuration = getConfiguration();
Object validatedFact = configuration.getValidatedFact(obj);
DroolsService droolsService = InjectorHolder.INSTANCE.getInstance(DroolsService.class);
Set<IssueDetails> detailsList = null;
try {
detailsList = droolsService.performBusinessRules(configuration, validatedFact);
} catch (DroolsException e) {
LOGGER.warn(e.getMessage(), e);
throw new RuntimeException(String.format(DroolsErrorMessages.CANT_PERFORM_BUSINESS_VALIDATION, configuration.getAgendaGroup()));
}
if (detailsList.isEmpty()) {
return true;
} else {
context.disableDefaultConstraintViolation();
detailsList.forEach((details -> context.buildConstraintViolationWithTemplate(marshallData(details)).addPropertyNode("").addConstraintViolation()));
return false;
}
}
Aggregations