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

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

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import ca.corefacility.bioinformatics.irida.exceptions.ProjectWithoutOwnerException;
import ca.corefacility.bioinformatics.irida.model.enums.ProjectRole;
import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.user.User;
import ca.corefacility.bioinformatics.irida.ria.web.exceptions.UIProjectWithoutOwnerException;
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.projects.dto.ProjectMemberTableModel;
import ca.corefacility.bioinformatics.irida.ria.web.projects.settings.dto.NewProjectMemberRequest;
import ca.corefacility.bioinformatics.irida.ria.web.services.UIProjectMembersService;
import ca.corefacility.bioinformatics.irida.service.ProjectService;
import ca.corefacility.bioinformatics.irida.service.user.UserService;
import ca.corefacility.bioinformatics.irida.web.controller.test.unit.TestDataFactory;

import com.google.common.collect.ImmutableList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.*;

public class UIProjectMembersServiceTest {
	/*
	Default values
	 */
	private final Long PROJECT_ID = 1L;
	private final Project PROJECT = TestDataFactory.constructProject();
	private final User USER_1 = new User(1L, "user1", "<EMAIL>", "SDF123", "USER", "ONE", "7777");
	private final User USER_2 = new User(2L, "user2", "<EMAIL>", "SDF456", "USER", "TWO", "7777");
	private final User USER_3 = new User(3L, "user3", "<EMAIL>", "SDF789", "USER", "TWO", "7777");
	private final TableRequest TABLE_REQUEST = new TableRequest(0, 10, "createdDate", "asc", "");
	private final List<Join<Project, User>> projectUserJoins = ImmutableList.of(
			new ProjectUserJoin(PROJECT, USER_1, ProjectRole.PROJECT_OWNER),
			new ProjectUserJoin(PROJECT, USER_2, ProjectRole.PROJECT_USER));
	private final Locale LOCALE = Locale.CANADA;

	@Rule
	public ExpectedException noManagerException = ExpectedException.none();
	private UIProjectMembersService service;
	private ProjectService projectService;
	private UserService userService;

	@Before
	public void setUp() throws ProjectWithoutOwnerException {
		projectService = mock(ProjectService.class);
		userService = mock(UserService.class);
		MessageSource messageSource = mock(MessageSource.class);

		service = new UIProjectMembersService(projectService, userService, messageSource);

		when(projectService.read(PROJECT_ID)).thenReturn(PROJECT);
		when(userService.read(USER_1.getId())).thenReturn(USER_1);
		when(userService.read(USER_2.getId())).thenReturn(USER_2);
		when(userService.read(USER_3.getId())).thenReturn(USER_3);
		Sort SORT = Sort.by(Sort.Direction.ASC, "label");
		String search = "";
		int page = 0;
		when(userService.searchUsersForProject(PROJECT, TABLE_REQUEST.getSearch(), TABLE_REQUEST.getCurrent(),
				TABLE_REQUEST.getPageSize(), TABLE_REQUEST.getSort())).thenReturn(getPagedUsersForProject());

		when(messageSource.getMessage("projectRole." + ProjectRole.PROJECT_OWNER.toString(), new Object[] {},
				LOCALE)).thenReturn("Manager");
		when(messageSource.getMessage("projectRole." + ProjectRole.PROJECT_USER.toString(), new Object[] {},
				LOCALE)).thenReturn("Collaborator");

		doThrow(ProjectWithoutOwnerException.class).when(projectService)
				.removeUserFromProject(PROJECT, USER_3);
		doThrow(ProjectWithoutOwnerException.class).when(projectService)
				.updateUserProjectRole(PROJECT, USER_3, ProjectRole.PROJECT_OWNER);
	}

	@Test
	public void testGetProjectMembers() {
		TableResponse<ProjectMemberTableModel> response = service.getProjectMembers(PROJECT_ID, TABLE_REQUEST);
		verify(userService, times(1)).searchUsersForProject(PROJECT, TABLE_REQUEST.getSearch(),
				TABLE_REQUEST.getCurrent(), TABLE_REQUEST.getPageSize(), TABLE_REQUEST.getSort());
		assertEquals(Long.valueOf(2L), response.getTotal());
		assertNotNull(response.getDataSource());
		List<ProjectMemberTableModel> members = response.getDataSource();
		assertEquals("Should have 2 members", 2, members.size());
	}

	@Test
	public void testRemoveUserFromProject() throws ProjectWithoutOwnerException {
		String response = service.removeUserFromProject(PROJECT_ID, USER_2.getId(), LOCALE);
		verify(projectService, times(1)).removeUserFromProject(PROJECT, USER_2);
	}

	@Test(expected = ProjectWithoutOwnerException.class)
	public void testRemoveLastManagerFromProject() throws UIProjectWithoutOwnerException {
		service.removeUserFromProject(PROJECT_ID, USER_3.getId(), LOCALE);
	}

	@Test
	public void testUpdateUserRoleOnProject() throws ProjectWithoutOwnerException {
		String response = service.updateUserRoleOnProject(PROJECT_ID, USER_2.getId(),
				ProjectRole.PROJECT_OWNER.toString(), LOCALE);
		verify(projectService, times(1)).updateUserProjectRole(PROJECT, USER_2, ProjectRole.PROJECT_OWNER);
	}

	@Test(expected = ProjectWithoutOwnerException.class)
	public void testUpdateUserRoleOnProjectNoManager() throws UIProjectWithoutOwnerException {
		service.updateUserRoleOnProject(PROJECT_ID, USER_3.getId(), ProjectRole.PROJECT_OWNER.toString(), LOCALE);
	}

	@Test
	public void testAddMemberToProject() {
		NewProjectMemberRequest request = new NewProjectMemberRequest(USER_3.getId(),
				ProjectRole.PROJECT_USER.toString());
		String response = service.addMemberToProject(PROJECT_ID, request, LOCALE);
		verify(projectService, times(1)).addUserToProject(PROJECT, USER_3, ProjectRole.PROJECT_USER);
	}

	private Page<Join<Project, User>> getPagedUsersForProject() {
		return new Page<Join<Project, User>>() {
			@Override
			public int getTotalPages() {
				return 0;
			}

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

			@Override
			public <U> Page<U> map(Function<? super Join<Project, User>, ? extends U> converter) {
				return null;
			}

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

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

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

			@Override
			public List<Join<Project, User>> getContent() {
				return projectUserJoins;
			}

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

			@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<Join<Project, User>> iterator() {
				return projectUserJoins.iterator();
			}
		};
	}
}
<commit_msg>🚨️ Removed unused response values<commit_after>package ca.corefacility.bioinformatics.irida.ria.unit.web;

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

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import ca.corefacility.bioinformatics.irida.exceptions.ProjectWithoutOwnerException;
import ca.corefacility.bioinformatics.irida.model.enums.ProjectRole;
import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.user.User;
import ca.corefacility.bioinformatics.irida.ria.web.exceptions.UIProjectWithoutOwnerException;
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.projects.dto.ProjectMemberTableModel;
import ca.corefacility.bioinformatics.irida.ria.web.projects.settings.dto.NewProjectMemberRequest;
import ca.corefacility.bioinformatics.irida.ria.web.services.UIProjectMembersService;
import ca.corefacility.bioinformatics.irida.service.ProjectService;
import ca.corefacility.bioinformatics.irida.service.user.UserService;
import ca.corefacility.bioinformatics.irida.web.controller.test.unit.TestDataFactory;

import com.google.common.collect.ImmutableList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.*;

public class UIProjectMembersServiceTest {
	/*
	Default values
	 */
	private final Long PROJECT_ID = 1L;
	private final Project PROJECT = TestDataFactory.constructProject();
	private final User USER_1 = new User(1L, "user1", "<EMAIL>", "SDF123", "USER", "ONE", "7777");
	private final User USER_2 = new User(2L, "user2", "<EMAIL>", "SDF456", "USER", "TWO", "7777");
	private final User USER_3 = new User(3L, "user3", "<EMAIL>", "SDF789", "USER", "TWO", "7777");
	private final TableRequest TABLE_REQUEST = new TableRequest(0, 10, "createdDate", "asc", "");
	private final List<Join<Project, User>> projectUserJoins = ImmutableList.of(
			new ProjectUserJoin(PROJECT, USER_1, ProjectRole.PROJECT_OWNER),
			new ProjectUserJoin(PROJECT, USER_2, ProjectRole.PROJECT_USER));
	private final Locale LOCALE = Locale.CANADA;

	@Rule
	public ExpectedException noManagerException = ExpectedException.none();
	private UIProjectMembersService service;
	private ProjectService projectService;
	private UserService userService;

	@Before
	public void setUp() throws ProjectWithoutOwnerException {
		projectService = mock(ProjectService.class);
		userService = mock(UserService.class);
		MessageSource messageSource = mock(MessageSource.class);

		service = new UIProjectMembersService(projectService, userService, messageSource);

		when(projectService.read(PROJECT_ID)).thenReturn(PROJECT);
		when(userService.read(USER_1.getId())).thenReturn(USER_1);
		when(userService.read(USER_2.getId())).thenReturn(USER_2);
		when(userService.read(USER_3.getId())).thenReturn(USER_3);

		when(userService.searchUsersForProject(PROJECT, TABLE_REQUEST.getSearch(), TABLE_REQUEST.getCurrent(),
				TABLE_REQUEST.getPageSize(), TABLE_REQUEST.getSort())).thenReturn(getPagedUsersForProject());

		when(messageSource.getMessage("projectRole." + ProjectRole.PROJECT_OWNER.toString(), new Object[] {},
				LOCALE)).thenReturn("Manager");
		when(messageSource.getMessage("projectRole." + ProjectRole.PROJECT_USER.toString(), new Object[] {},
				LOCALE)).thenReturn("Collaborator");

		doThrow(ProjectWithoutOwnerException.class).when(projectService)
				.removeUserFromProject(PROJECT, USER_3);
		doThrow(ProjectWithoutOwnerException.class).when(projectService)
				.updateUserProjectRole(PROJECT, USER_3, ProjectRole.PROJECT_OWNER);
	}

	@Test
	public void testGetProjectMembers() {
		TableResponse<ProjectMemberTableModel> response = service.getProjectMembers(PROJECT_ID, TABLE_REQUEST);
		verify(userService, times(1)).searchUsersForProject(PROJECT, TABLE_REQUEST.getSearch(),
				TABLE_REQUEST.getCurrent(), TABLE_REQUEST.getPageSize(), TABLE_REQUEST.getSort());
		assertEquals(Long.valueOf(2L), response.getTotal());
		assertNotNull(response.getDataSource());
		List<ProjectMemberTableModel> members = response.getDataSource();
		assertEquals("Should have 2 members", 2, members.size());
	}

	@Test
	public void testRemoveUserFromProject() throws ProjectWithoutOwnerException {
		service.removeUserFromProject(PROJECT_ID, USER_2.getId(), LOCALE);
		verify(projectService, times(1)).removeUserFromProject(PROJECT, USER_2);
	}

	@Test(expected = ProjectWithoutOwnerException.class)
	public void testRemoveLastManagerFromProject() throws UIProjectWithoutOwnerException {
		service.removeUserFromProject(PROJECT_ID, USER_3.getId(), LOCALE);
	}

	@Test
	public void testUpdateUserRoleOnProject() throws ProjectWithoutOwnerException {
		service.updateUserRoleOnProject(PROJECT_ID, USER_2.getId(), ProjectRole.PROJECT_OWNER.toString(), LOCALE);
		verify(projectService, times(1)).updateUserProjectRole(PROJECT, USER_2, ProjectRole.PROJECT_OWNER);
	}

	@Test(expected = ProjectWithoutOwnerException.class)
	public void testUpdateUserRoleOnProjectNoManager() throws UIProjectWithoutOwnerException {
		service.updateUserRoleOnProject(PROJECT_ID, USER_3.getId(), ProjectRole.PROJECT_OWNER.toString(), LOCALE);
	}

	@Test
	public void testAddMemberToProject() {
		NewProjectMemberRequest request = new NewProjectMemberRequest(USER_3.getId(),
				ProjectRole.PROJECT_USER.toString());
		service.addMemberToProject(PROJECT_ID, request, LOCALE);
		verify(projectService, times(1)).addUserToProject(PROJECT, USER_3, ProjectRole.PROJECT_USER);
	}

	private Page<Join<Project, User>> getPagedUsersForProject() {
		return new Page<Join<Project, User>>() {
			@Override
			public int getTotalPages() {
				return 0;
			}

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

			@Override
			public <U> Page<U> map(Function<? super Join<Project, User>, ? extends U> converter) {
				return null;
			}

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

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

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

			@Override
			public List<Join<Project, User>> getContent() {
				return projectUserJoins;
			}

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

			@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<Join<Project, User>> iterator() {
				return projectUserJoins.iterator();
			}
		};
	}
}

{'fraction_non_alphanumeric': 0.1132517225232457, 'fraction_numerical': 0.00862933975516757, 'mean_word_length': 5.70704351727232, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '3638992', 'n_tokens_mistral': 5068, 'n_tokens_neox': 4499, 'n_words': 831}