<commit_before>package ca.corefacility.bioinformatics.irida.web.controller.test.unit.samples;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.springframework.hateoas.Link;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;

import ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException;
import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.sample.Sample;
import ca.corefacility.bioinformatics.irida.model.sample.SampleSequenceFileJoin;
import ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin;
import ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile;
import ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile;
import ca.corefacility.bioinformatics.irida.service.SequenceFilePairService;
import ca.corefacility.bioinformatics.irida.service.SequenceFileService;
import ca.corefacility.bioinformatics.irida.service.SequencingObjectService;
import ca.corefacility.bioinformatics.irida.service.SequencingRunService;
import ca.corefacility.bioinformatics.irida.service.sample.SampleService;
import ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource;
import ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection;
import ca.corefacility.bioinformatics.irida.web.assembler.resource.RootResource;
import ca.corefacility.bioinformatics.irida.web.assembler.resource.sequencefile.SequenceFileResource;
import ca.corefacility.bioinformatics.irida.web.controller.api.RESTGenericController;
import ca.corefacility.bioinformatics.irida.web.controller.api.samples.RESTSampleSequenceFilesController;
import ca.corefacility.bioinformatics.irida.web.controller.test.unit.TestDataFactory;

import com.google.common.collect.Lists;
import com.google.common.net.HttpHeaders;

/**
 * Unit tests for {@link RESTSampleSequenceFilesController}.
 */
public class SampleSequenceFilesControllerTest {
	private RESTSampleSequenceFilesController controller;
	private SequenceFileService sequenceFileService;
	private SequenceFilePairService sequenceFilePairService;
	private SampleService sampleService;
	private SequencingRunService miseqRunService;
	private SequencingObjectService sequencingObjectService;

	@Before
	public void setUp() {
		sampleService = mock(SampleService.class);
		sequenceFileService = mock(SequenceFileService.class);
		sequenceFilePairService = mock(SequenceFilePairService.class);
		miseqRunService= mock(SequencingRunService.class);
		sequencingObjectService = mock(SequencingObjectService.class);

		controller = new RESTSampleSequenceFilesController(sequenceFileService, sampleService, miseqRunService, sequencingObjectService);
	}

	@Test
	public void testGetSampleSequenceFiles() throws IOException {
		Sample s = TestDataFactory.constructSample();
		SingleEndSequenceFile so = TestDataFactory.constructSingleEndSequenceFile();
		SampleSequencingObjectJoin r = new SampleSequencingObjectJoin(s, so);

		List<SampleSequencingObjectJoin> relationships = Lists.newArrayList(r);

		// mock out the service calls
		when(sampleService.read(s.getId())).thenReturn(s);

		when(sequencingObjectService.getSequencingObjectsForSample(s)).thenReturn(relationships);

		ModelMap modelMap = controller.getSampleSequenceFiles(s.getId());

		// verify that the service calls were used.
		verify(sampleService).read(s.getId());
		verify(sequencingObjectService).getSequencingObjectsForSample(s);

		Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
		assertTrue(o instanceof ResourceCollection);
		@SuppressWarnings("unchecked")
		ResourceCollection<SequenceFile> resources = (ResourceCollection<SequenceFile>) o;
		assertNotNull(resources);
		assertEquals(1, resources.size());

		Link selfCollection = resources.getLink(Link.REL_SELF);
		Link sample = resources.getLink(RESTSampleSequenceFilesController.REL_SAMPLE);
		String sampleLocation = "http://localhost/api/samples/" + s.getId();
		String sequenceFileLocation = sampleLocation + "/unpaired/" + so.getIdentifier() + "/files/"
				+ so.getSequenceFile().getId();

		assertEquals(sampleLocation + "/sequenceFiles", selfCollection.getHref());
		assertEquals(sampleLocation, sample.getHref());

		// confirm that the self rel for an individual sequence file exists
		SequenceFile sfr = resources.iterator().next();
		Link self = sfr.getLink(Link.REL_SELF);
		assertEquals(sequenceFileLocation, self.getHref());
		assertEquals(so.getSequenceFile().getFile(), sfr.getFile());
	}

	@Test
	public void testRemoveSequenceFileFromSample() throws IOException {
		Sample s = TestDataFactory.constructSample();
		SingleEndSequenceFile so = TestDataFactory.constructSingleEndSequenceFile();

		when(sampleService.read(s.getId())).thenReturn(s);
		when(sequencingObjectService.readSequencingObjectForSample(s, so.getId())).thenReturn(so);

		ModelMap modelMap = controller.removeSequenceFileFromSample(s.getId(), "unpaired", so.getId());

		verify(sampleService, times(1)).read(s.getId());
		verify(sequencingObjectService).readSequencingObjectForSample(s, so.getId());
		verify(sampleService, times(1)).removeSequencingObjectFromSample(s, so);

		Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
		assertNotNull(o);
		assertTrue(o instanceof RootResource);
		RootResource resource = (RootResource) o;

		Link sample = resource.getLink(RESTSampleSequenceFilesController.REL_SAMPLE);
		Link sequenceFiles = resource.getLink(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES);

		String sampleLocation = "http://localhost/api/samples/" + s.getId();

		assertNotNull(sample);
		assertEquals(sampleLocation, sample.getHref());
		assertNotNull(sequenceFiles);
		assertEquals(sampleLocation + "/sequenceFiles", sequenceFiles.getHref());
	}

	@Test
	public void testGetSequenceFileForSample() throws IOException {
		Sample s = TestDataFactory.constructSample();
		SingleEndSequenceFile so = TestDataFactory.constructSingleEndSequenceFile();

		when(sampleService.read(s.getId())).thenReturn(s);
		when(sequencingObjectService.readSequencingObjectForSample(s, so.getId())).thenReturn(so);

		ModelMap modelMap = controller.readSequenceFileForSequencingObject(s.getId(),
				RESTSampleSequenceFilesController.objectLabels.get(so.getClass()), so.getId(), so.getSequenceFile()
						.getId());

		verify(sampleService).read(s.getId());
		verify(sequencingObjectService).readSequencingObjectForSample(s, so.getId());

		Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
		assertNotNull(o);
		assertTrue(o instanceof SequenceFile);
		SequenceFile sfr = (SequenceFile) o;
		assertEquals(so.getSequenceFile().getFile(), sfr.getFile());

		Link self = sfr.getLink(Link.REL_SELF);
		Link sampleSequenceFiles = sfr.getLink(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES);
		Link sample = sfr.getLink(RESTSampleSequenceFilesController.REL_SAMPLE);

		String sampleLocation = "http://localhost/api/samples/" + s.getId();
		String sequenceFileLocation = sampleLocation + "/unpaired/" + so.getIdentifier() + "/files/"
				+ so.getSequenceFile().getId();

		assertNotNull(self);
		assertEquals(sequenceFileLocation, self.getHref());
		assertNotNull(sampleSequenceFiles);
		assertEquals(sampleLocation + "/sequenceFiles", sampleSequenceFiles.getHref());
		assertNotNull(sample);
		assertEquals(sampleLocation, sample.getHref());
	}

	@Test(expected = EntityNotFoundException.class)
	public void testCantGetSequenceFileForOtherSample() {
		Sample s = TestDataFactory.constructSample();
		SequenceFile sf = new SequenceFile();
		sf.setId(5L);

		when(sampleService.read(s.getId())).thenReturn(s);
		when(sequenceFileService.getSequenceFileForSample(s, sf.getId())).thenThrow(
				new EntityNotFoundException("not in sample"));

		controller.getSequenceFileForSample(s.getId(), sf.getId());

	}

	@Test
	public void testAddNewSequenceFileToSample() throws IOException {
		Sample s = TestDataFactory.constructSample();
		SequenceFile sf = TestDataFactory.constructSequenceFile();
		Join<Sample, SequenceFile> r = new SampleSequenceFileJoin(s, sf);
		SequenceFileResource resource = new SequenceFileResource();
		Path f = Files.createTempFile(null, null);
		MockMultipartFile mmf = new MockMultipartFile("filename", "filename", "blurgh", FileCopyUtils.copyToByteArray(f
				.toFile()));
		MockHttpServletResponse response = new MockHttpServletResponse();
		when(sampleService.read(s.getId())).thenReturn(s);
		when(sequenceFileService.createSequenceFileInSample(Matchers.any(SequenceFile.class), Matchers.eq(s)))
				.thenReturn(r);
		when(sequenceFileService.read(sf.getId())).thenReturn(sf);
		ModelMap modelMap = controller.addNewSequenceFileToSample(s.getId(), mmf, resource,response);<commit_msg>updated access denied test<commit_after>package ca.corefacility.bioinformatics.irida.web.controller.test.unit.samples;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.springframework.hateoas.Link;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;

import ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException;
import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.sample.Sample;
import ca.corefacility.bioinformatics.irida.model.sample.SampleSequenceFileJoin;
import ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin;
import ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile;
import ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile;
import ca.corefacility.bioinformatics.irida.service.SequenceFilePairService;
import ca.corefacility.bioinformatics.irida.service.SequenceFileService;
import ca.corefacility.bioinformatics.irida.service.SequencingObjectService;
import ca.corefacility.bioinformatics.irida.service.SequencingRunService;
import ca.corefacility.bioinformatics.irida.service.sample.SampleService;
import ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource;
import ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection;
import ca.corefacility.bioinformatics.irida.web.assembler.resource.RootResource;
import ca.corefacility.bioinformatics.irida.web.assembler.resource.sequencefile.SequenceFileResource;
import ca.corefacility.bioinformatics.irida.web.controller.api.RESTGenericController;
import ca.corefacility.bioinformatics.irida.web.controller.api.samples.RESTSampleSequenceFilesController;
import ca.corefacility.bioinformatics.irida.web.controller.test.unit.TestDataFactory;

import com.google.common.collect.Lists;
import com.google.common.net.HttpHeaders;

/**
 * Unit tests for {@link RESTSampleSequenceFilesController}.
 */
public class SampleSequenceFilesControllerTest {
	private RESTSampleSequenceFilesController controller;
	private SequenceFileService sequenceFileService;
	private SequenceFilePairService sequenceFilePairService;
	private SampleService sampleService;
	private SequencingRunService miseqRunService;
	private SequencingObjectService sequencingObjectService;

	@Before
	public void setUp() {
		sampleService = mock(SampleService.class);
		sequenceFileService = mock(SequenceFileService.class);
		sequenceFilePairService = mock(SequenceFilePairService.class);
		miseqRunService= mock(SequencingRunService.class);
		sequencingObjectService = mock(SequencingObjectService.class);

		controller = new RESTSampleSequenceFilesController(sequenceFileService, sampleService, miseqRunService, sequencingObjectService);
	}

	@Test
	public void testGetSampleSequenceFiles() throws IOException {
		Sample s = TestDataFactory.constructSample();
		SingleEndSequenceFile so = TestDataFactory.constructSingleEndSequenceFile();
		SampleSequencingObjectJoin r = new SampleSequencingObjectJoin(s, so);

		List<SampleSequencingObjectJoin> relationships = Lists.newArrayList(r);

		// mock out the service calls
		when(sampleService.read(s.getId())).thenReturn(s);

		when(sequencingObjectService.getSequencingObjectsForSample(s)).thenReturn(relationships);

		ModelMap modelMap = controller.getSampleSequenceFiles(s.getId());

		// verify that the service calls were used.
		verify(sampleService).read(s.getId());
		verify(sequencingObjectService).getSequencingObjectsForSample(s);

		Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
		assertTrue(o instanceof ResourceCollection);
		@SuppressWarnings("unchecked")
		ResourceCollection<SequenceFile> resources = (ResourceCollection<SequenceFile>) o;
		assertNotNull(resources);
		assertEquals(1, resources.size());

		Link selfCollection = resources.getLink(Link.REL_SELF);
		Link sample = resources.getLink(RESTSampleSequenceFilesController.REL_SAMPLE);
		String sampleLocation = "http://localhost/api/samples/" + s.getId();
		String sequenceFileLocation = sampleLocation + "/unpaired/" + so.getIdentifier() + "/files/"
				+ so.getSequenceFile().getId();

		assertEquals(sampleLocation + "/sequenceFiles", selfCollection.getHref());
		assertEquals(sampleLocation, sample.getHref());

		// confirm that the self rel for an individual sequence file exists
		SequenceFile sfr = resources.iterator().next();
		Link self = sfr.getLink(Link.REL_SELF);
		assertEquals(sequenceFileLocation, self.getHref());
		assertEquals(so.getSequenceFile().getFile(), sfr.getFile());
	}

	@Test
	public void testRemoveSequenceFileFromSample() throws IOException {
		Sample s = TestDataFactory.constructSample();
		SingleEndSequenceFile so = TestDataFactory.constructSingleEndSequenceFile();

		when(sampleService.read(s.getId())).thenReturn(s);
		when(sequencingObjectService.readSequencingObjectForSample(s, so.getId())).thenReturn(so);

		ModelMap modelMap = controller.removeSequenceFileFromSample(s.getId(), "unpaired", so.getId());

		verify(sampleService, times(1)).read(s.getId());
		verify(sequencingObjectService).readSequencingObjectForSample(s, so.getId());
		verify(sampleService, times(1)).removeSequencingObjectFromSample(s, so);

		Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
		assertNotNull(o);
		assertTrue(o instanceof RootResource);
		RootResource resource = (RootResource) o;

		Link sample = resource.getLink(RESTSampleSequenceFilesController.REL_SAMPLE);
		Link sequenceFiles = resource.getLink(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES);

		String sampleLocation = "http://localhost/api/samples/" + s.getId();

		assertNotNull(sample);
		assertEquals(sampleLocation, sample.getHref());
		assertNotNull(sequenceFiles);
		assertEquals(sampleLocation + "/sequenceFiles", sequenceFiles.getHref());
	}

	@Test
	public void testGetSequenceFileForSample() throws IOException {
		Sample s = TestDataFactory.constructSample();
		SingleEndSequenceFile so = TestDataFactory.constructSingleEndSequenceFile();

		when(sampleService.read(s.getId())).thenReturn(s);
		when(sequencingObjectService.readSequencingObjectForSample(s, so.getId())).thenReturn(so);

		ModelMap modelMap = controller.readSequenceFileForSequencingObject(s.getId(),
				RESTSampleSequenceFilesController.objectLabels.get(so.getClass()), so.getId(), so.getSequenceFile()
						.getId());

		verify(sampleService).read(s.getId());
		verify(sequencingObjectService).readSequencingObjectForSample(s, so.getId());

		Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
		assertNotNull(o);
		assertTrue(o instanceof SequenceFile);
		SequenceFile sfr = (SequenceFile) o;
		assertEquals(so.getSequenceFile().getFile(), sfr.getFile());

		Link self = sfr.getLink(Link.REL_SELF);
		Link sampleSequenceFiles = sfr.getLink(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES);
		Link sample = sfr.getLink(RESTSampleSequenceFilesController.REL_SAMPLE);

		String sampleLocation = "http://localhost/api/samples/" + s.getId();
		String sequenceFileLocation = sampleLocation + "/unpaired/" + so.getIdentifier() + "/files/"
				+ so.getSequenceFile().getId();

		assertNotNull(self);
		assertEquals(sequenceFileLocation, self.getHref());
		assertNotNull(sampleSequenceFiles);
		assertEquals(sampleLocation + "/sequenceFiles", sampleSequenceFiles.getHref());
		assertNotNull(sample);
		assertEquals(sampleLocation, sample.getHref());
	}

	@Test(expected = EntityNotFoundException.class)
	public void testCantGetSequenceFileForOtherSample() {
		Sample s = TestDataFactory.constructSample();

		Long objectId = 5L;
		Long fileId = 3L;

		when(sampleService.read(s.getId())).thenReturn(s);
		when(sequencingObjectService.readSequencingObjectForSample(s, objectId)).thenThrow(
				new EntityNotFoundException("not in sample"));

		controller.readSequenceFileForSequencingObject(s.getId(), "unpaired", objectId, fileId);
	}

	@Test
	public void testAddNewSequenceFileToSample() throws IOException {
		Sample s = TestDataFactory.constructSample();
		SequenceFile sf = TestDataFactory.constructSequenceFile();
		Join<Sample, SequenceFile> r = new SampleSequenceFileJoin(s, sf);
		SequenceFileResource resource = new SequenceFileResource();
		Path f = Files.createTempFile(null, null);
		MockMultipartFile mmf = new MockMultipartFile("filename", "filename", "blurgh", FileCopyUtils.copyToByteArray(f
				.toFile()));
		MockHttpServletResponse response = new MockHttpServletResponse();
		when(sampleService.read(s.getId())).thenReturn(s);
		when(sequenceFileService.createSequenceFileInSample(Matchers.any(SequenceFile.class), Matchers.eq(s)))
				.thenReturn(r);
		when(sequenceFileService.read(sf.getId())).thenReturn(sf);
		ModelMap modelMap = controller.addNewSequenceFileToSample(s.getId(), mmf, resource,response);
{'fraction_non_alphanumeric': 0.10854250378333247, 'fraction_numerical': 0.00046965506444711163, 'mean_word_length': 7.214316330904415, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1925420', 'n_tokens_mistral': 5720, 'n_tokens_neox': 5361, 'n_words': 873}