Search in sources :

Example 1 with IdentifiableTestEntity

use of ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity in project irida by phac-nml.

the class GenericControllerTest method testCreateGoodEntity.

@Test
public void testCreateGoodEntity() {
    when(crudService.create(entity)).thenReturn(entity);
    when(crudService.read(identifier)).thenReturn(entity);
    ModelMap model = controller.create(entity, new MockHttpServletResponse());
    assertTrue("Model should contain resource", model.containsKey("resource"));
    IdentifiableTestEntity testResource = (IdentifiableTestEntity) model.get("resource");
    assertNotNull("Resource should not be null", testResource);
    assertTrue("Resource from model should be equivalent to resource added to model", testResource.equals(entity));
    assertTrue("Model should contain a self-reference", testResource.getLink(Link.REL_SELF).getHref().endsWith(identifier.toString()));
}
Also used : IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity) ModelMap(org.springframework.ui.ModelMap) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with IdentifiableTestEntity

use of ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity in project irida by phac-nml.

the class ControllerExceptionHandlerTest method testHandleConstraintViolations.

@Test
public void testHandleConstraintViolations() {
    final String MESSAGES_BASENAME = "ValidationMessages";
    Configuration<?> configuration = Validation.byDefaultProvider().configure();
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename(MESSAGES_BASENAME);
    configuration.messageInterpolator(new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator(MESSAGES_BASENAME)));
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<?>> constraintViolations = new HashSet<>();
    Set<ConstraintViolation<IdentifiableTestEntity>> violations = validator.validate(new IdentifiableTestEntity());
    for (ConstraintViolation<IdentifiableTestEntity> v : violations) {
        constraintViolations.add(v);
    }
    ResponseEntity<Map<String, List<String>>> response = controller.handleConstraintViolations(new ConstraintViolationException(constraintViolations));
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
    // assertEquals("{\"label\":[\"You must provide a label.\"]}", response.getBody());
    Map<String, List<String>> body = response.getBody();
    assertTrue("The response must contain an error about a missing label.", body.containsKey("label"));
    List<String> labels = body.get("label");
    assertEquals("There must only be one error with the label.", 1, labels.size());
    String error = labels.get(0);
    assertEquals("The error must be 'You must provide a label.'", "You must provide a label.", error);
}
Also used : IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity) ValidatorFactory(javax.validation.ValidatorFactory) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) PlatformResourceBundleLocator(org.hibernate.validator.resourceloading.PlatformResourceBundleLocator) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) List(java.util.List) Map(java.util.Map) Validator(javax.validation.Validator) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with IdentifiableTestEntity

use of ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity in project irida by phac-nml.

the class GenericControllerTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() {
    crudService = mock(CRUDService.class);
    entity = new IdentifiableTestEntity();
    identifier = 1L;
    entity.setId(identifier);
    controller = new RESTGenericController<IdentifiableTestEntity>(crudService, IdentifiableTestEntity.class) {
    };
    updatedFields = new HashMap<>();
}
Also used : CRUDService(ca.corefacility.bioinformatics.irida.service.CRUDService) IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity) Before(org.junit.Before)

Example 4 with IdentifiableTestEntity

use of ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity in project irida by phac-nml.

the class GenericControllerTest method testGetResource.

@Test
public void testGetResource() {
    when(crudService.read(identifier)).thenReturn(entity);
    ModelMap model = null;
    try {
        model = controller.getResource(identifier);
    } catch (GenericsException e) {
        fail();
    }
    assertTrue(model.containsKey("resource"));
    IdentifiableTestEntity resource = (IdentifiableTestEntity) model.get("resource");
    assertTrue(resource.getLink(Link.REL_SELF).getHref().endsWith(identifier.toString()));
}
Also used : IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity) GenericsException(ca.corefacility.bioinformatics.irida.web.controller.api.exception.GenericsException) ModelMap(org.springframework.ui.ModelMap) Test(org.junit.Test)

Aggregations

IdentifiableTestEntity (ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity)4 Test (org.junit.Test)3 ModelMap (org.springframework.ui.ModelMap)2 CRUDService (ca.corefacility.bioinformatics.irida.service.CRUDService)1 GenericsException (ca.corefacility.bioinformatics.irida.web.controller.api.exception.GenericsException)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 Validator (javax.validation.Validator)1 ValidatorFactory (javax.validation.ValidatorFactory)1 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)1 PlatformResourceBundleLocator (org.hibernate.validator.resourceloading.PlatformResourceBundleLocator)1 Before (org.junit.Before)1 ResourceBundleMessageSource (org.springframework.context.support.ResourceBundleMessageSource)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1