package ca.corefacility.bioinformatics.irida.web.controller.test.unit.exception;

import ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException;
import ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException;
import ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException;
import ca.corefacility.bioinformatics.irida.web.controller.api.exception.ControllerExceptionHandler;
import ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import javax.validation.*;
import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertEquals;

/**
 * Unit tests for {@link ca.corefacility.bioinformatics.irida.web.controller.api.exception.ControllerExceptionHandler}
 *
 * @author Franklin Bristow <franklin.bristow@phac-aspc.gc.ca>
 */
public class ControllerExceptionHandlerTest {

    private ControllerExceptionHandler controller;

    @Before
    public void setUp() {
        controller = new ControllerExceptionHandler();
    }

    @Test
    public void testHandleConstraintViolations() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        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<String> response = controller.handleConstraintViolations(
                new ConstraintViolationException(constraintViolations));
        assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
        assertEquals("{\"label\":[\"may not be null\"]}", response.getBody());
    }

    @Test
    public void testHandleNotFoundException() {
        ResponseEntity<String> response = controller.handleNotFoundException(new EntityNotFoundException("not found"));
        assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
    }

    @Test
    public void testHandleExistsException() {
        ResponseEntity<String> response = controller.handleExistsException(new EntityExistsException("exists"));
        assertEquals(HttpStatus.CONFLICT, response.getStatusCode());
    }

    @Test
    public void testHandleInvalidPropertyException() {
        ResponseEntity<String> response = controller.handleInvalidPropertyException(
                new InvalidPropertyException("invalid property"));
        assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
    }

    @Test
    public void testHandleOtherExceptions() {
        ResponseEntity<String> response = controller.handleAllOtherExceptions(new Exception("exception"));
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
    }
}

@@ -86,440 +86,511 @@
 ort 
-ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException;%0Aimport ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException;%0Aimport ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException;%0Aimport ca.corefacility.bioinformatics.irida.web.controller.api.exception.ControllerExceptionHandler;%0Aimport ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity
+static org.junit.Assert.assertEquals;%0A%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0Aimport javax.validation.Configuration;%0Aimport javax.validation.ConstraintViolation;%0Aimport javax.validation.ConstraintViolationException;%0Aimport javax.validation.Validation;%0Aimport javax.validation.Validator;%0Aimport javax.validation.ValidatorFactory;%0A%0Aimport org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;%0Aimport org.hibernate.validator.resourceloading.PlatformResourceBundleLocator
 ;%0Aim
@@ -635,16 +635,88 @@
 t.Test;%0A
+import org.springframework.context.support.ResourceBundleMessageSource;%0A
 import o
@@ -811,120 +811,440 @@
 ort 
-javax.validation.*;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0Aimport static org.junit.Assert.assertEquals
+ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException;%0Aimport ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException;%0Aimport ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException;%0Aimport ca.corefacility.bioinformatics.irida.web.controller.api.exception.ControllerExceptionHandler;%0Aimport ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity
 ;%0A%0A/
@@ -1703,62 +1703,503 @@
     
-    ValidatorFactory factory = Validation.buildDefault
+%09final String MESSAGES_BASENAME = %22ca.corefacility.bioinformatics.irida.validation.ValidationMessages%22;%0A%09%09Configuration%3C?%3E configuration = Validation.byDefaultProvider().configure();%0A%09%09ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();%0A%09%09messageSource.setBasename(MESSAGES_BASENAME);%0A%09%09configuration.messageInterpolator(new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator(%0A%09%09%09%09MESSAGES_BASENAME)));%0A%09%09ValidatorFactory factory = configuration.build
 Vali
@@ -2856,23 +2856,33 @@
 :%5B%5C%22
-may not be null
+You must provide a label.
 %5C%22%5D%7D

Use the correct string for validation.