<commit_before>package ca.corefacility.bioinformatics.irida.ria.unit.web.oauth;

import ca.corefacility.bioinformatics.irida.model.RemoteAPI;
import ca.corefacility.bioinformatics.irida.ria.web.ajax.RemoteAPIAjaxController;
import ca.corefacility.bioinformatics.irida.ria.web.ajax.dto.ajax.AjaxCreateItemSuccessResponse;
import ca.corefacility.bioinformatics.irida.ria.web.models.tables.TableRequest;
import ca.corefacility.bioinformatics.irida.ria.web.models.tables.TableResponse;
import ca.corefacility.bioinformatics.irida.ria.web.rempoteapi.dto.RemoteAPITableModel;
import ca.corefacility.bioinformatics.irida.ria.web.services.UIRemoteAPIService;
import ca.corefacility.bioinformatics.irida.service.RemoteAPIService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.*;

/**
 * Testing for {@link RemoteAPIAjaxController}
 */
public class RemoteAPIAjaxControllerTest {
	private RemoteAPIService remoteAPIService;
	private UIRemoteAPIService uiRemoteAPIService;
	private RemoteAPIAjaxController controller;
	private MessageSource messageSource;

	private final RemoteAPI REMOTE_API_01 = new RemoteAPI("Toronto", "http://toronto.nowhere", "", "toronto", "123456");
	private final RemoteAPI REMOTE_API_02 = new RemoteAPI("Washington", "http://washington.nowhere", "", "washington",
			"654321");

	@Before
	public void init() {
		remoteAPIService = mock(RemoteAPIService.class);
		uiRemoteAPIService = mock(UIRemoteAPIService.class);
		messageSource = mock(MessageSource.class);
		controller = new RemoteAPIAjaxController(remoteAPIService, uiRemoteAPIService, messageSource);

		Page<RemoteAPI> remoteAPIPage = new Page<>() {
			@Override
			public int getTotalPages() {
				return 10;
			}

			@Override
			public long getTotalElements() {
				return 104;
			}

			@Override
			public <U> Page<U> map(Function<? super RemoteAPI, ? extends U> function) {
				return null;
			}

			@Override
			public int getNumber() {
				return 0;
			}

			@Override
			public int getSize() {
				return 0;
			}

			@Override
			public int getNumberOfElements() {
				return 104;
			}

			@Override
			public List<RemoteAPI> getContent() {
				return List.of(REMOTE_API_01);
			}

			@Override
			public boolean hasContent() {
				return true;
			}

			@Override
			public Sort getSort() {
				return null;
			}

			@Override
			public boolean isFirst() {
				return false;
			}

			@Override
			public boolean isLast() {
				return false;
			}

			@Override
			public boolean hasNext() {
				return false;
			}

			@Override
			public boolean hasPrevious() {
				return false;
			}

			@Override
			public Pageable nextPageable() {
				return null;
			}

			@Override
			public Pageable previousPageable() {
				return null;
			}

			@Override
			public Iterator<RemoteAPI> iterator() {
				return null;
			}
		};

		when(remoteAPIService.search(any(), anyInt(), anyInt(), any(Sort.Direction.class),
				any(String[].class))).thenReturn(remoteAPIPage);
		when(remoteAPIService.read(1L)).thenReturn(REMOTE_API_01);
		when(remoteAPIService.read(2L)).thenReturn(REMOTE_API_02);
	}

	@Test
	public void getAjaxAPIListTest() {
		TableRequest request = new TableRequest();
		request.setSortColumn("label");
		request.setSortDirection("ascend");
		request.setCurrent(0);
		request.setPageSize(10);

		TableResponse<RemoteAPITableModel> response = controller.getAjaxAPIList(request);
		verify(remoteAPIService, times(1)).search(any(), anyInt(), anyInt(), any(Sort.Direction.class),
				any(String[].class));
		assertEquals("Should have 1 Remote API", 1, response.getDataSource()
				.size());
	}

	@Test
	public void testPostCreateRemoteAPI_goodRequest() {
		RemoteAPI client = new RemoteAPI("NEW CLIENT", "http://example.com", "", "CLIENT_ID", "CLIENT_SECRET");
		RemoteAPI createdClient = new RemoteAPI("NEW CLIENT", "http://example.com", "", "CLIENT_ID", "CLIENT_SECRET");
		createdClient.setId(1L);

		when(remoteAPIService.create(client)).thenReturn(createdClient);
		ResponseEntity response = controller.postCreateRemoteAPI(client, Locale.ENGLISH);
		assertEquals("Should have a response status of 200", response.getStatusCode(), HttpStatus.OK);
		AjaxCreateItemSuccessResponse ajaxCreateItemSuccessResponse = (AjaxCreateItemSuccessResponse) response.getBody();
		assertEquals("Should return the id of the newly created remote api", 1L, ajaxCreateItemSuccessResponse.getId());
		String foobar = "baz";
	}

	@Test
	public void testPostCreateRemoteAPI_badRequest() {
		RemoteAPI client = new RemoteAPI(null, null, "", "CLIENT_ID", "CLIENT_SECRET");
		RemoteAPI createdClient = new RemoteAPI(null, null, "", "CLIENT_ID", "CLIENT_SECRET");
		createdClient.setId(1L);

		when(remoteAPIService.create(client)).thenReturn(createdClient);
		ResponseEntity response = controller.postCreateRemoteAPI(client, Locale.ENGLISH);

		verify(remoteAPIService, times(1)).create(client);
	}
}<commit_msg>Working constraint violations<commit_after>package ca.corefacility.bioinformatics.irida.ria.unit.web.oauth;

import java.util.*;
import java.util.function.Function;

import javax.validation.*;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import ca.corefacility.bioinformatics.irida.model.RemoteAPI;
import ca.corefacility.bioinformatics.irida.ria.web.ajax.RemoteAPIAjaxController;
import ca.corefacility.bioinformatics.irida.ria.web.ajax.dto.ajax.AjaxCreateItemSuccessResponse;
import ca.corefacility.bioinformatics.irida.ria.web.models.tables.TableRequest;
import ca.corefacility.bioinformatics.irida.ria.web.models.tables.TableResponse;
import ca.corefacility.bioinformatics.irida.ria.web.rempoteapi.dto.RemoteAPITableModel;
import ca.corefacility.bioinformatics.irida.ria.web.services.UIRemoteAPIService;
import ca.corefacility.bioinformatics.irida.service.RemoteAPIService;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.*;

/**
 * Testing for {@link RemoteAPIAjaxController}
 */
public class RemoteAPIAjaxControllerTest {
	private RemoteAPIService remoteAPIService;
	private UIRemoteAPIService uiRemoteAPIService;
	private RemoteAPIAjaxController controller;
	private MessageSource messageSource;

	private final RemoteAPI REMOTE_API_01 = new RemoteAPI("Toronto", "http://toronto.nowhere", "", "toronto", "123456");
	private final RemoteAPI REMOTE_API_02 = new RemoteAPI("Washington", "http://washington.nowhere", "", "washington",
			"654321");

	@Before
	public void init() {
		remoteAPIService = mock(RemoteAPIService.class);
		uiRemoteAPIService = mock(UIRemoteAPIService.class);
		messageSource = mock(MessageSource.class);
		controller = new RemoteAPIAjaxController(remoteAPIService, uiRemoteAPIService, messageSource);

		Page<RemoteAPI> remoteAPIPage = new Page<>() {
			@Override
			public int getTotalPages() {
				return 10;
			}

			@Override
			public long getTotalElements() {
				return 104;
			}

			@Override
			public <U> Page<U> map(Function<? super RemoteAPI, ? extends U> function) {
				return null;
			}

			@Override
			public int getNumber() {
				return 0;
			}

			@Override
			public int getSize() {
				return 0;
			}

			@Override
			public int getNumberOfElements() {
				return 104;
			}

			@Override
			public List<RemoteAPI> getContent() {
				return List.of(REMOTE_API_01);
			}

			@Override
			public boolean hasContent() {
				return true;
			}

			@Override
			public Sort getSort() {
				return null;
			}

			@Override
			public boolean isFirst() {
				return false;
			}

			@Override
			public boolean isLast() {
				return false;
			}

			@Override
			public boolean hasNext() {
				return false;
			}

			@Override
			public boolean hasPrevious() {
				return false;
			}

			@Override
			public Pageable nextPageable() {
				return null;
			}

			@Override
			public Pageable previousPageable() {
				return null;
			}

			@Override
			public Iterator<RemoteAPI> iterator() {
				return null;
			}
		};

		when(remoteAPIService.search(any(), anyInt(), anyInt(), any(Sort.Direction.class),
				any(String[].class))).thenReturn(remoteAPIPage);
		when(remoteAPIService.read(1L)).thenReturn(REMOTE_API_01);
		when(remoteAPIService.read(2L)).thenReturn(REMOTE_API_02);
	}

	@Test
	public void getAjaxAPIListTest() {
		TableRequest request = new TableRequest();
		request.setSortColumn("label");
		request.setSortDirection("ascend");
		request.setCurrent(0);
		request.setPageSize(10);

		TableResponse<RemoteAPITableModel> response = controller.getAjaxAPIList(request);
		verify(remoteAPIService, times(1)).search(any(), anyInt(), anyInt(), any(Sort.Direction.class),
				any(String[].class));
		assertEquals("Should have 1 Remote API", 1, response.getDataSource()
				.size());
	}

	@Test
	public void testPostCreateRemoteAPI_goodRequest() {
		RemoteAPI client = new RemoteAPI("NEW CLIENT", "http://example.com", "", "CLIENT_ID", "CLIENT_SECRET");
		RemoteAPI createdClient = new RemoteAPI("NEW CLIENT", "http://example.com", "", "CLIENT_ID", "CLIENT_SECRET");
		createdClient.setId(1L);

		when(remoteAPIService.create(client)).thenReturn(createdClient);

		ResponseEntity response = controller.postCreateRemoteAPI(client, Locale.ENGLISH);
		assertEquals("Should have a response status of 200", response.getStatusCode(), HttpStatus.OK);
		AjaxCreateItemSuccessResponse ajaxCreateItemSuccessResponse = (AjaxCreateItemSuccessResponse) response.getBody();
		assertEquals("Should return the id of the newly created remote api", 1L, ajaxCreateItemSuccessResponse.getId());
		String foobar = "baz";
	}

	@Test
	public void testPostCreateRemoteAPI_badRequest() {
		RemoteAPI client = new RemoteAPI(null, null, "", "CLIENT_ID", "CLIENT_SECRET");
		RemoteAPI createdClient = new RemoteAPI(null, null, "", "CLIENT_ID", "CLIENT_SECRET");
		createdClient.setId(1L);

		Configuration<?> configuration = Validation.byDefaultProvider().configure();
		ValidatorFactory factory = configuration.buildValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<RemoteAPI>> violations = validator.validate(client);

        HashSet<ConstraintViolation<?>> constraintViolations = new HashSet<>(violations);


		when(remoteAPIService.create(client)).thenThrow(new ConstraintViolationException(constraintViolations));

		ResponseEntity response = controller.postCreateRemoteAPI(client, Locale.ENGLISH);

		verify(remoteAPIService, times(1)).create(client);
	}
}
{'fraction_non_alphanumeric': 0.11599609201527666, 'fraction_numerical': 0.008348876454392041, 'mean_word_length': 5.063543349488422, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '3756774', 'n_tokens_mistral': 3950, 'n_tokens_neox': 3401, 'n_words': 673}