/*
 * Copyright 2018 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epam.ta.reportportal.ws.controller;

import com.epam.ta.reportportal.ws.BaseMvcTest;
import com.epam.ta.reportportal.ws.model.project.config.CreateIssueSubTypeRQ;
import com.epam.ta.reportportal.ws.model.project.config.ProjectSettingsResource;
import com.epam.ta.reportportal.ws.model.project.config.UpdateIssueSubTypeRQ;
import com.epam.ta.reportportal.ws.model.project.config.UpdateOneIssueSubTypeRQ;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.web.servlet.MvcResult;

import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
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;

/**
 * @author <a href="mailto:ihar_kahadouski@epam.com">Ihar Kahadouski</a>
 */
@Sql("/db/project-settings/project-settings-fill.sql")
public class ProjectSettingsControllerTest extends BaseMvcTest {

	@Autowired
	private ObjectMapper objectMapper;

	@Test
	public void createSubType() throws Exception {
		CreateIssueSubTypeRQ rq = new CreateIssueSubTypeRQ();
		rq.setTypeRef("PRODUCT_BUG");
		rq.setColor("color");
		rq.setLongName("LongName");
		rq.setShortName("name");
		mockMvc.perform(post(DEFAULT_PROJECT_BASE_URL + "/settings/sub-type").contentType(APPLICATION_JSON)
				.with(token(oAuthHelper.getDefaultToken()))
				.content(objectMapper.writeValueAsBytes(rq))).andExpect(status().isCreated());
	}

	@Test
	public void getProjectSettings() throws Exception {
		final MvcResult mvcResult = mockMvc.perform(get(DEFAULT_PROJECT_BASE_URL + "/settings").with(token(oAuthHelper.getDefaultToken())))
				.andExpect(status().isOk())
				.andReturn();
		final ProjectSettingsResource projectSettingsResource = objectMapper.readValue(mvcResult.getResponse().getContentAsString(),
				ProjectSettingsResource.class
		);
		assertEquals(8,
				projectSettingsResource.getSubTypes().values().stream().flatMap(Collection::stream).collect(Collectors.toList()).size()
		);
	}

	@Test
	public void deleteSubType() throws Exception {
		mockMvc.perform(delete(DEFAULT_PROJECT_BASE_URL + "/settings/6").with(token(oAuthHelper.getDefaultToken())))
				.andExpect(status().isOk());
	}

	@Test
	public void updateSubType() throws Exception {
		UpdateIssueSubTypeRQ request = new UpdateIssueSubTypeRQ();
		final UpdateOneIssueSubTypeRQ updateOneIssueSubTypeRQ = new UpdateOneIssueSubTypeRQ();
		updateOneIssueSubTypeRQ.setColor("updated");
		updateOneIssueSubTypeRQ.setLocator("custom_ti");
		updateOneIssueSubTypeRQ.setLongName("updated");
		updateOneIssueSubTypeRQ.setShortName("updated");
		updateOneIssueSubTypeRQ.setTypeRef("toInvestigate");
		request.setIds(Collections.singletonList(updateOneIssueSubTypeRQ));
		mockMvc.perform(put(DEFAULT_PROJECT_BASE_URL + "/settings").with(token(oAuthHelper.getDefaultToken()))).andExpect(status().isOk());
	}
}
@@ -3018,16 +3018,25 @@
 ettings/
+sub-type/
 6%22).with
@@ -3500,36 +3500,32 @@
 etShortName(%22upd
-ated
 %22);%0A%09%09updateOneI
@@ -3554,21 +3554,22 @@
 ef(%22
-toInvestigate
+TO_INVESTIGATE
 %22);%0A
@@ -3689,32 +3689,41 @@
 URL + %22/settings
+/sub-type
 %22).with(token(oA
@@ -3751,16 +3751,105 @@
 oken()))
+%0A%09%09%09%09.contentType(APPLICATION_JSON)%0A%09%09%09%09.content(objectMapper.writeValueAsBytes(request))
 ).andExp

Fix requests