Search in sources :

Example 6 with ConstraintValidatorContext

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)));
}
Also used : ConstraintValidatorContext(javax.validation.ConstraintValidatorContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with ConstraintValidatorContext

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);
}
Also used : ConstraintValidatorContext(javax.validation.ConstraintValidatorContext) Test(org.junit.Test)

Example 8 with ConstraintValidatorContext

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)));
}
Also used : ConstraintValidatorContext(javax.validation.ConstraintValidatorContext) Test(org.junit.Test)

Example 9 with ConstraintValidatorContext

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)));
}
Also used : ConstraintViolationBuilder(javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder) ConstraintValidatorContext(javax.validation.ConstraintValidatorContext) NodeBuilderCustomizableContext(javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with ConstraintValidatorContext

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;
    }
}
Also used : DroolsErrorMessages(gov.ca.cwds.drools.DroolsErrorMessages) Logger(org.slf4j.Logger) ConstraintValidator(javax.validation.ConstraintValidator) ConstraintValidatorContext(javax.validation.ConstraintValidatorContext) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InjectorHolder(gov.ca.cwds.inject.InjectorHolder) DroolsConfiguration(gov.ca.cwds.drools.DroolsConfiguration) IssueDetails(gov.ca.cwds.rest.exception.IssueDetails) DroolsException(gov.ca.cwds.drools.DroolsException) JsonUtils(gov.ca.cwds.utils.JsonUtils) Annotation(java.lang.annotation.Annotation) DroolsService(gov.ca.cwds.drools.DroolsService) DroolsException(gov.ca.cwds.drools.DroolsException) IssueDetails(gov.ca.cwds.rest.exception.IssueDetails) DroolsService(gov.ca.cwds.drools.DroolsService)

Aggregations

ConstraintValidatorContext (javax.validation.ConstraintValidatorContext)11 Test (org.junit.Test)9 HashSet (java.util.HashSet)5 ConstraintViolationBuilder (javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder)2 NodeBuilderCustomizableContext (javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DroolsConfiguration (gov.ca.cwds.drools.DroolsConfiguration)1 DroolsErrorMessages (gov.ca.cwds.drools.DroolsErrorMessages)1 DroolsException (gov.ca.cwds.drools.DroolsException)1 DroolsService (gov.ca.cwds.drools.DroolsService)1 InjectorHolder (gov.ca.cwds.inject.InjectorHolder)1 IssueDetails (gov.ca.cwds.rest.exception.IssueDetails)1 JsonUtils (gov.ca.cwds.utils.JsonUtils)1 Annotation (java.lang.annotation.Annotation)1 Set (java.util.Set)1 ConstraintValidator (javax.validation.ConstraintValidator)1 TimerSettingsValue (org.kie.workbench.common.stunner.bpmn.definition.property.event.timer.TimerSettingsValue)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1