package uk.ac.ebi.subs.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.rest.webmvc.RestMediaTypes;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import uk.ac.ebi.subs.ApiApplication;
import uk.ac.ebi.subs.repository.model.Submission;
import uk.ac.ebi.subs.repository.repos.SubmissionRepository;
import uk.ac.ebi.tsc.aap.client.repo.DomainService;
import uk.ac.ebi.tsc.aap.client.repo.ProfileRepositoryRest;
import uk.ac.ebi.tsc.aap.client.security.WithMockAAPUser;

import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiApplication.class)
@ActiveProfiles("aap")
public class SubmissionAuditTest {

    public static final String DEFAULT_USER_REFERENCE = "usr-12345";
    public static final String USI_USER = "usi_user";
    public static final String USI_USER_EMAIL = "usi-user@usi.org";
    public static final String USER_FULL_NAME = "Test User";
    private MockMvc mockMvc;

    @Autowired
    private SubmissionRepository submissionRepository;

    @Autowired
    private WebApplicationContext context;

    @MockBean
    private DomainService domainService;

    @MockBean
    private ProfileRepositoryRest profileRepositoryRest;

    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
                .build();

        ApiIntegrationTestHelper.mockAapProfileAndDomain(domainService,profileRepositoryRest);
    }

    @After
    public void finish() {
        submissionRepository.deleteAll();
    }

    @Test
    @WithMockAAPUser(userName = USI_USER, email = USI_USER_EMAIL, userReference = DEFAULT_USER_REFERENCE, fullName = USER_FULL_NAME, domains = {Helpers.TEAM_NAME})
    public void postSubmissionAndCheckAAPAuditInfo() throws Exception {
        final Submission submission = postSubmission();
        Assert.assertEquals(DEFAULT_USER_REFERENCE, submission.getCreatedBy());
    }

    @Test
    @WithMockUser(username = USI_USER, roles = {Helpers.TEAM_NAME})
    public void postSubmissionAndCheckAuditInfo() throws Exception {
        final Submission submission = postSubmission();
        Assert.assertEquals(USI_USER, submission.getCreatedBy());
    }

    private Submission postSubmission() throws Exception {
<<<<<<< HEAD

        String url = "/teams/" + Helpers.TEAM_NAME + "/submissions";

=======
        uk.ac.ebi.subs.data.Submission clientSubmission = new uk.ac.ebi.subs.data.Submission();
        clientSubmission.setSubmitter(new Submitter());
        clientSubmission.getSubmitter().setEmail(USI_USER_EMAIL);
        Team team = new Team();
        team.setName(Helpers.TEAM_NAME);
        clientSubmission.setTeam(team);
        String submissionJson = objectMapper.writeValueAsString(clientSubmission);
>>>>>>> origin/experiment-test-fails-on-pr-build
        this.mockMvc.perform(
                post(url).content("{}")
                        .contentType(MediaType.APPLICATION_JSON_UTF8)
                        .header("Authorization", "Bearer token")
                        .accept(RestMediaTypes.HAL_JSON)).andExpect(status().isCreated());

        return submissionRepository.findAll().get(0);
    }

}

@@ -26,60 +26,8 @@
 i;%0A%0A
-import com.fasterxml.jackson.databind.ObjectMapper;%0A
 impo
@@ -2193,16 +2193,17 @@
 Service,
+ 
 profileR
@@ -3040,22 +3040,8 @@
 n %7B%0A
-%3C%3C%3C%3C%3C%3C%3C HEAD%0A%0A
     
@@ -3108,480 +3108,8 @@
 ns%22;
-%0A%0A=======%0A        uk.ac.ebi.subs.data.Submission clientSubmission = new uk.ac.ebi.subs.data.Submission();%0A        clientSubmission.setSubmitter(new Submitter());%0A        clientSubmission.getSubmitter().setEmail(USI_USER_EMAIL);%0A        Team team = new Team();%0A        team.setName(Helpers.TEAM_NAME);%0A        clientSubmission.setTeam(team);%0A        String submissionJson = objectMapper.writeValueAsString(clientSubmission);%0A%3E%3E%3E%3E%3E%3E%3E origin/experiment-test-fails-on-pr-build
 %0A   

fix merge error