package co.je.conductor.api.resources;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import fj.data.Either;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.testing.junit.ResourceTestRule;

import java.util.Map;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.junit.ClassRule;
import org.junit.Test;
import org.mockito.Mockito;

import co.je.conductor.domain.business.JobBusiness;
import co.je.conductor.domain.entities.JobRequest;
import co.je.conductor.domain.exceptions.UnsupportedJsonNodeException;
import co.je.conductor.infrastructure.exceptions.IException;
import co.je.conductor.infrastructure.exceptions.TechnicalException;
import co.je.conductor.utils.JobRequestFactoryForTests;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JobRequestResourceTest {

	private static final ObjectMapper objectMapper = Jackson.newObjectMapper();
	private static final JobBusiness jobBusinessMock = Mockito.mock(JobBusiness.class);
	private static final JobRequestResource jobRequestResource = new JobRequestResource(jobBusinessMock);

	@ClassRule
	public static final ResourceTestRule resources = ResourceTestRule.builder()
	.setMapper(objectMapper).addResource(jobRequestResource).build();

	private Builder getDefaultHttpClient(String uri) {

		return resources.client().target(uri)
				.request(MediaType.APPLICATION_JSON_TYPE)
				.accept(MediaType.APPLICATION_JSON_TYPE);
	}

	@Test
	public void testCreateJobRequest_OK() {

		String uri = "/jobs";
		JobRequest jobRequest = JobRequestFactoryForTests.getJobRequest();

		try {

			String expectedJobId = "234";
			Either<IException, String> createdJobRequestIdEither = Either.right(expectedJobId);
			Mockito.when(jobBusinessMock.createJobRequest(Mockito.any(JobRequest.class))).thenReturn(createdJobRequestIdEither);

			Response response = getDefaultHttpClient(uri).post(Entity.json(jobRequest));
			assertNotNull(response);

			int statusCode = response.getStatus();
			assertEquals(201, statusCode);

			Map<String, Object> mapResponse = response.readEntity(Map.class);
			assertNotNull(mapResponse);
			assertEquals(expectedJobId, mapResponse.get("jobId"));

		} catch (UnsupportedJsonNodeException e) {

			fail("Unexpected exception: " + e.getMessage());
		}
	}

	@Test
	public void testCreateJobRequest_NOK_failedCreatingJobRequest() {

		String uri = "/jobs";
		JobRequest jobRequest = JobRequestFactoryForTests.getJobRequest();

		try {

			IException technicalException = new TechnicalException("The job request could not be created.");
			Either<IException, String> createdJobRequestIdEither = Either.left(technicalException);
			Mockito.when(jobBusinessMock.createJobRequest(Mockito.any(JobRequest.class))).thenReturn(createdJobRequestIdEither);

			Response response = getDefaultHttpClient(uri).post(Entity.json(jobRequest));
			assertNotNull(response);

			int statusCode = response.getStatus();
			assertEquals(500, statusCode);

			IException exception = response.readEntity(IException.class);
			assertNotNull(exception);
			assertEquals(0, technicalException.compareTo(exception));

		} catch (UnsupportedJsonNodeException e) {

			fail("Unexpected exception: " + e.getMessage());
		}
	}
}
@@ -466,21 +466,16 @@
 g.junit.
-Class
 Rule;%0Aim
@@ -982,31 +982,24 @@
 %7B%0A%0A%09private 
-static 
 final Object
@@ -1048,39 +1048,32 @@
 per();%0A%09private 
-static 
 final JobBusines
@@ -1134,23 +1134,16 @@
 private 
-static 
 final Jo
@@ -1228,13 +1228,8 @@
 %0A%0A%09@
-Class
 Rule
@@ -1237,23 +1237,16 @@
 %09public 
-static 
 final Re
@@ -1298,18 +1298,16 @@
 uilder()
-%0A%09
 .setMapp
@@ -3268,32 +3268,764 @@
 Exception e) %7B%0A%0A
+%09%09%09fail(%22Unexpected exception: %22 + e.getMessage());%0A%09%09%7D%0A%09%7D%0A%09%0A%09@Test%0A%09public void testCreateJobRequest_NOK_unsupportedJsonNodeException() %7B%0A%0A%09%09String uri = %22/jobs%22;%0A%09%09JobRequest jobRequest = JobRequestFactoryForTests.getJobRequest();%0A%0A%09%09try %7B%0A%0A%09%09%09Mockito.doThrow(UnsupportedJsonNodeException.class).when(jobBusinessMock).createJobRequest(Mockito.any(JobRequest.class));%0A%09%09%09Response response = getDefaultHttpClient(uri).post(Entity.json(jobRequest));%0A%09%09%09assertNotNull(response);%0A%0A%09%09%09int statusCode = response.getStatus();%0A%09%09%09assertEquals(500, statusCode);%0A%0A%09%09%09UnsupportedJsonNodeException exception = response.readEntity(UnsupportedJsonNodeException.class);%0A%09%09%09assertNotNull(exception);%0A%0A%09%09%7D catch (UnsupportedJsonNodeException e) %7B%0A%0A
 %09%09%09fail(%22Unexpec

Add test case when throw an UnsupportedJsonNodeException.