package uk.ac.ebi.subs.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mashape.unirest.http.Unirest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import uk.ac.ebi.subs.ApiApplication;
import uk.ac.ebi.subs.api.services.SubmittableValidationDispatcher;
import uk.ac.ebi.subs.repository.model.Sample;
import uk.ac.ebi.subs.repository.model.Study;
import uk.ac.ebi.subs.repository.repos.SubmissionRepository;
import uk.ac.ebi.subs.repository.repos.status.SubmissionStatusRepository;
import uk.ac.ebi.subs.repository.repos.submittables.SampleRepository;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;

import static org.mockito.Mockito.*;

/**
 * Created by rolando on 12/06/2017.
 */
@RunWith(SpringRunner.class)
@ActiveProfiles({"SubmittableValidationDispatcherTest","basic_auth"})
@SpringBootTest(classes = ApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SubmittableHandlerTest {
    @LocalServerPort
    private int port;
    private String rootUri;

    private ApiIntegrationTestHelper testHelper;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private SubmissionRepository submissionRepository;

    @Autowired
    SubmissionStatusRepository submissionStatusRepository;

    @Autowired
    private SampleRepository sampleRepository;

    @Autowired
    private SubmittableValidationDispatcher submittableValidationDispatcher;

    @Before
    public void buildUp() throws URISyntaxException {
        rootUri = "http://localhost:" + port + "/api";

        testHelper = new ApiIntegrationTestHelper(objectMapper, rootUri,
                Arrays.asList(submissionRepository, sampleRepository, submissionStatusRepository));
    }

    @After
    public void tearDown() throws IOException {
        Unirest.shutdown();
        submissionRepository.deleteAll();
        sampleRepository.deleteAll();
        submissionStatusRepository.deleteAll();
    }

    @Test
    public void testValidationMessageSamplesOnSubmit() throws Exception {
        testHelper.submissionWithSamples(testHelper.rootRels());

        // assert that created Samples caused a validation request event
        verify(submittableValidationDispatcher, atLeast(1)).validateCreate(any(Sample.class));
    }

    @Test
    public void testValidationMessageStudiesOnSubmit() throws Exception {
        testHelper.submissionWithStudies(testHelper.rootRels());

        verify(submittableValidationDispatcher, atLeast(1)).validateCreate(any(Study.class));
    }
}

@@ -26,67 +26,8 @@
 i;%0A%0A
-import com.fasterxml.jackson.core.JsonProcessingException;%0A
 impo
@@ -224,36 +224,8 @@
 th;%0A
-import org.mockito.Mockito;%0A
 impo
@@ -1020,16 +1020,38 @@
 .Arrays;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -1972,16 +1972,557 @@
 %22/api%22;%0A
+        final Map%3CString, String%3E standardGetContentHeader = ApiIntegrationTestHelper.createStandardGetHeader();%0A        standardGetContentHeader.putAll(ApiIntegrationTestHelper.createBasicAuthheaders(TestWebSecurityConfig.USI_USER,TestWebSecurityConfig.USI_PASSWORD));%0A        final Map%3CString, String%3E standardPostContentHeader = ApiIntegrationTestHelper.createStandardGetHeader();%0A        standardPostContentHeader.putAll(ApiIntegrationTestHelper.createBasicAuthheaders(TestWebSecurityConfig.USI_USER,TestWebSecurityConfig.USI_PASSWORD));
 %0A       
@@ -2684,16 +2684,67 @@
 ository)
+,standardGetContentHeader,standardPostContentHeader
 );%0A    %7D

use TestWebSecurityConfig username and password constants