<commit_before>/*
 * Copyright 2016 EPAM Systems
 * 
 * 
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/service-api
 * 
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Report Portal is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.epam.ta.reportportal.ws.controller.impl;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.*;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.PagedResources;
import org.springframework.security.core.Authentication;
import org.springframework.test.web.servlet.MvcResult;

import com.epam.ta.reportportal.auth.AuthConstants;
import com.epam.ta.reportportal.commons.SendCase;
import com.epam.ta.reportportal.database.dao.ProjectRepository;
import com.epam.ta.reportportal.database.entity.Project;
import com.epam.ta.reportportal.ws.BaseMvcTest;
import com.epam.ta.reportportal.ws.model.project.*;
import com.epam.ta.reportportal.ws.model.project.email.EmailSenderCase;
import com.epam.ta.reportportal.ws.model.project.email.ProjectEmailConfig;
import com.epam.ta.reportportal.ws.model.project.email.UpdateProjectEmailRQ;
import com.epam.ta.reportportal.ws.model.user.UserResource;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * @author Dzmitry_Kavalets
 */
public class ProjectControllerTest extends BaseMvcTest {

	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private ProjectRepository projectRepository;

	@Test
	public void createProjectPositive() throws Exception {
		CreateProjectRQ rq = new CreateProjectRQ();
		rq.setProjectName("TestProject");
		rq.setEntryType("INTERNAL");
		mvcMock.perform(
				post("/project").content(objectMapper.writeValueAsBytes(rq)).contentType(APPLICATION_JSON).principal(authentication()))
				.andExpect(status().isCreated());
		Project project = projectRepository.findOne("TestProject".toLowerCase());
		assertNotNull(project.getConfiguration());
		assertNotNull(project.getConfiguration().getSubTypes());
		assertFalse(project.getConfiguration().getSubTypes().isEmpty());
	}

	@Test
	public void updateProjectPositive() throws Exception {
		final UpdateProjectRQ rq = new UpdateProjectRQ();
		rq.setCustomer("customer");
		mvcMock.perform(put("/project/project1").content(objectMapper.writeValueAsBytes(rq)).contentType(APPLICATION_JSON)
				.principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void deleteProjectPositive() throws Exception {
		mvcMock.perform(delete("/project/project1").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void getProjectUsersPositive() throws Exception {
		mvcMock.perform(get("/project/project1/users").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void getProjectPositive() throws Exception {
		mvcMock.perform(get("/project/project1").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void unassignProjectUsersPositive() throws Exception {
		UnassignUsersRQ rq = new UnassignUsersRQ();
		rq.setUsernames(singletonList("user2"));
		mvcMock.perform(put("/project/project1/unassign").principal(authentication()).contentType(APPLICATION_JSON)
				.content(objectMapper.writeValueAsBytes(rq))).andExpect(status().is(200));
	}

	@Test
	public void unassignUsersEmptyUserNames() throws Exception {
		UnassignUsersRQ rq = new UnassignUsersRQ();
		rq.setUsernames(new ArrayList<>());
		mvcMock.perform(put("/project/project1/unassign").principal(authentication()).contentType(APPLICATION_JSON)
				.content(objectMapper.writeValueAsBytes(rq))).andExpect(status().is(200));
	}

	@Test
	public void assignProjectUsersPositive() throws Exception {
		AssignUsersRQ rq = new AssignUsersRQ();
		Map<String, String> user = new HashMap<>();
		user.put("user3", "MEMBER");
		rq.setUserNames(user);
		mvcMock.perform(put("/project/project1/assign").principal(authentication()).contentType(APPLICATION_JSON)
				.content(objectMapper.writeValueAsBytes(rq))).andExpect(status().is(200));
	}

	@Test
	public void getUsersForAssignPositive() throws Exception {
		mvcMock.perform(get("/project/project1/assignable").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void getProjectUsersTest() throws Exception {
		mvcMock.perform(get("/project/project1/usernames?filter.cnt.users=user").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void updateUserPreferencePositive() throws Exception {
		UpdateProjectRQ rq = new UpdateProjectRQ();
		mvcMock.perform(put("/project/project1/preference/user1").principal(authentication()).contentType(APPLICATION_JSON)
				.content(objectMapper.writeValueAsBytes(rq))).andExpect(status().is(200));
	}

	@Test
	public void getUserPreferencePositive() throws Exception {
		mvcMock.perform(get("/project/project1/preference/user1").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	@Ignore
	// TODO Test requires commons-dao 2.6.5+
	public void getAllProjectsInfo() throws Exception {
		final MvcResult mvcResult = mvcMock
				.perform(get("/project/list?page.page=1&page.size=51&page.sort=name,DESC&filter.eq.configuration$entryType=INTERNAL")
						.principal(authentication()))
				.andExpect(status().is(200)).andReturn();<commit_msg>Enable test<commit_after>/*
 * Copyright 2016 EPAM Systems
 * 
 * 
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/service-api
 * 
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Report Portal is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.epam.ta.reportportal.ws.controller.impl;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.*;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.PagedResources;
import org.springframework.security.core.Authentication;
import org.springframework.test.web.servlet.MvcResult;

import com.epam.ta.reportportal.auth.AuthConstants;
import com.epam.ta.reportportal.commons.SendCase;
import com.epam.ta.reportportal.database.dao.ProjectRepository;
import com.epam.ta.reportportal.database.entity.Project;
import com.epam.ta.reportportal.ws.BaseMvcTest;
import com.epam.ta.reportportal.ws.model.project.*;
import com.epam.ta.reportportal.ws.model.project.email.EmailSenderCase;
import com.epam.ta.reportportal.ws.model.project.email.ProjectEmailConfig;
import com.epam.ta.reportportal.ws.model.project.email.UpdateProjectEmailRQ;
import com.epam.ta.reportportal.ws.model.user.UserResource;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * @author Dzmitry_Kavalets
 */
public class ProjectControllerTest extends BaseMvcTest {

	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private ProjectRepository projectRepository;

	@Test
	public void createProjectPositive() throws Exception {
		CreateProjectRQ rq = new CreateProjectRQ();
		rq.setProjectName("TestProject");
		rq.setEntryType("INTERNAL");
		mvcMock.perform(
				post("/project").content(objectMapper.writeValueAsBytes(rq)).contentType(APPLICATION_JSON).principal(authentication()))
				.andExpect(status().isCreated());
		Project project = projectRepository.findOne("TestProject".toLowerCase());
		assertNotNull(project.getConfiguration());
		assertNotNull(project.getConfiguration().getSubTypes());
		assertFalse(project.getConfiguration().getSubTypes().isEmpty());
	}

	@Test
	public void updateProjectPositive() throws Exception {
		final UpdateProjectRQ rq = new UpdateProjectRQ();
		rq.setCustomer("customer");
		mvcMock.perform(put("/project/project1").content(objectMapper.writeValueAsBytes(rq)).contentType(APPLICATION_JSON)
				.principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void deleteProjectPositive() throws Exception {
		mvcMock.perform(delete("/project/project1").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void getProjectUsersPositive() throws Exception {
		mvcMock.perform(get("/project/project1/users").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void getProjectPositive() throws Exception {
		mvcMock.perform(get("/project/project1").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void unassignProjectUsersPositive() throws Exception {
		UnassignUsersRQ rq = new UnassignUsersRQ();
		rq.setUsernames(singletonList("user2"));
		mvcMock.perform(put("/project/project1/unassign").principal(authentication()).contentType(APPLICATION_JSON)
				.content(objectMapper.writeValueAsBytes(rq))).andExpect(status().is(200));
	}

	@Test
	public void unassignUsersEmptyUserNames() throws Exception {
		UnassignUsersRQ rq = new UnassignUsersRQ();
		rq.setUsernames(new ArrayList<>());
		mvcMock.perform(put("/project/project1/unassign").principal(authentication()).contentType(APPLICATION_JSON)
				.content(objectMapper.writeValueAsBytes(rq))).andExpect(status().is(200));
	}

	@Test
	public void assignProjectUsersPositive() throws Exception {
		AssignUsersRQ rq = new AssignUsersRQ();
		Map<String, String> user = new HashMap<>();
		user.put("user3", "MEMBER");
		rq.setUserNames(user);
		mvcMock.perform(put("/project/project1/assign").principal(authentication()).contentType(APPLICATION_JSON)
				.content(objectMapper.writeValueAsBytes(rq))).andExpect(status().is(200));
	}

	@Test
	public void getUsersForAssignPositive() throws Exception {
		mvcMock.perform(get("/project/project1/assignable").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void getProjectUsersTest() throws Exception {
		mvcMock.perform(get("/project/project1/usernames?filter.cnt.users=user").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void updateUserPreferencePositive() throws Exception {
		UpdateProjectRQ rq = new UpdateProjectRQ();
		mvcMock.perform(put("/project/project1/preference/user1").principal(authentication()).contentType(APPLICATION_JSON)
				.content(objectMapper.writeValueAsBytes(rq))).andExpect(status().is(200));
	}

	@Test
	public void getUserPreferencePositive() throws Exception {
		mvcMock.perform(get("/project/project1/preference/user1").principal(authentication())).andExpect(status().is(200));
	}

	@Test
	public void getAllProjectsInfo() throws Exception {
		final MvcResult mvcResult = mvcMock
				.perform(get("/project/list?page.page=1&page.size=51&page.sort=name,DESC&filter.eq.configuration$entryType=INTERNAL")
						.principal(authentication()))
				.andExpect(status().is(200)).andReturn();
{'fraction_non_alphanumeric': 0.12748664757334158, 'fraction_numerical': 0.00936605000387027, 'mean_word_length': 6.11453744493392, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '5016114', 'n_tokens_mistral': 4045, 'n_tokens_neox': 3786, 'n_words': 702}