package ca.corefacility.bioinformatics.irida.ria.unit.web.services;

import java.util.Locale;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.ria.web.ajax.dto.SampleNameValidationResponse;
import ca.corefacility.bioinformatics.irida.ria.web.services.UIProjectSampleService;
import ca.corefacility.bioinformatics.irida.service.ProjectService;
import ca.corefacility.bioinformatics.irida.service.sample.SampleService;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class UIProjectSampleServiceTest {
	private UIProjectSampleService service;
	private ProjectService projectService;
	private SampleService sampleService;

	// DATA
	private final Long PROJECT_1_ID = 1L;
	private final Project PROJECT_1 = new Project("PROJECT_1");
	private final String BAD_NAME = "bad name with spaces";
	private final String SHORT_NAME = "sho";
	private final String GOOD_NAME = "good_name";

	@BeforeEach
	public void setUp() {
		projectService = mock(ProjectService.class);
		sampleService = mock(SampleService.class);
		MessageSource messageSource = mock(MessageSource.class);

		service = new UIProjectSampleService(projectService, sampleService, messageSource);

		when(projectService.read(PROJECT_1_ID)).thenReturn(PROJECT_1);
		when(sampleService.getSampleBySampleName(PROJECT_1, GOOD_NAME)).thenThrow(
				new EntityNotFoundException("Sample not found"));
	}

	@Test
	public void testValidateNewSampleName() {
		ResponseEntity<SampleNameValidationResponse> response = service.validateNewSampleName("sampleName",
				PROJECT_1_ID, Locale.ENGLISH);
		response = service.validateNewSampleName(GOOD_NAME, 1L, Locale.ENGLISH);
		assertEquals(HttpStatus.OK, response.getStatusCode(), "Good sample name should return OK");

		response = service.validateNewSampleName(BAD_NAME, 1L, Locale.ENGLISH);
		assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, response.getStatusCode()), "Sample with bad characters should return UNPROCESSABLE_ENTITY");

		response = service.validateNewSampleName(SHORT_NAME, 1L, Locale.ENGLISH);
		assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, response.getStatusCode()), "Sample names not long enough should return UNPROCESSABLE_ENTITY");
	}
}

@@ -441,22 +441,311 @@
 del.
-project.Projec
+joins.Join;%0Aimport ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin;%0Aimport ca.corefacility.bioinformatics.irida.model.project.Project;%0Aimport ca.corefacility.bioinformatics.irida.model.sample.Sample;%0Aimport ca.corefacility.bioinformatics.irida.ria.web.ajax.dto.CreateSampleReques
 t;%0Ai
@@ -834,16 +834,96 @@
 sponse;%0A
+import ca.corefacility.bioinformatics.irida.ria.web.ajax.dto.ajax.AjaxResponse;%0A
 import c
@@ -1203,16 +1203,64 @@
 Equals;%0A
+import static org.mockito.ArgumentMatchers.any;%0A
 import s
@@ -1418,86 +1418,8 @@
 ice;
-%0A%09private ProjectService projectService;%0A%09private SampleService sampleService;
 %0A%0A%09/
@@ -1710,16 +1710,31 @@
 p() %7B%0A%09%09
+ProjectService 
 projectS
@@ -1772,16 +1772,30 @@
 ass);%0A%09%09
+SampleService 
 sampleSe
@@ -2172,16 +2172,281 @@
 und%22));%0A
+%09%09Sample sample = new Sample(GOOD_NAME);%0A%09%09sample.setId(11L);%0A%09%09Join%3CProject, Sample%3E join = new ProjectSampleJoin(PROJECT_1, sample, true);%0A%09%09when(projectService.addSampleToProject(any(Project.class), any(Sample.class), any(Boolean.class))).thenReturn(%0A%09%09%09%09join);%0A
 %09%7D%0A%0A%09@Te
@@ -2936,35 +2936,38 @@
 .getStatusCode()
-), 
+,%0A%09%09%09%09
 %22Sample with bad
@@ -3165,19 +3165,22 @@
 usCode()
-), 
+,%0A%09%09%09%09
 %22Sample 
@@ -3241,11 +3241,318 @@
 TY%22);%0A%09%7D
+%0A%0A%09@Test%0A%09public void testCreateSample() %7B%0A%09%09CreateSampleRequest request = new CreateSampleRequest(GOOD_NAME, null);%0A%09%09ResponseEntity%3CAjaxResponse%3E response = service.createSample(request, PROJECT_1_ID, Locale.ENGLISH);%0A%09%09assertEquals(HttpStatus.OK, response.getStatusCode(), %22Sample should be created%22);%0A%09%7D
 %0A%7D%0A

Test creating a sample