package uk.ac.ebi.spot.goci.curation.controller;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import uk.ac.ebi.spot.goci.curation.model.PubmedIdForImport;
import uk.ac.ebi.spot.goci.curation.service.StudyOperationsService;
import uk.ac.ebi.spot.goci.model.Study;
import uk.ac.ebi.spot.goci.repository.AssociationRepository;
import uk.ac.ebi.spot.goci.repository.CurationStatusRepository;
import uk.ac.ebi.spot.goci.repository.CuratorRepository;
import uk.ac.ebi.spot.goci.repository.DiseaseTraitRepository;
import uk.ac.ebi.spot.goci.repository.EfoTraitRepository;
import uk.ac.ebi.spot.goci.repository.EthnicityRepository;
import uk.ac.ebi.spot.goci.repository.HousekeepingRepository;
import uk.ac.ebi.spot.goci.repository.StudyRepository;
import uk.ac.ebi.spot.goci.repository.UnpublishReasonRepository;
import uk.ac.ebi.spot.goci.service.DefaultPubMedSearchService;

import static org.hamcrest.Matchers.isA;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

/**
 * Created by emma on 12/02/2016.
 *
 * @author emma
 *         <p>
 *         Study controller test
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class StudyControllerTest {

    @Mock
    private StudyRepository studyRepository;

    @Mock
    private HousekeepingRepository housekeepingRepository;

    @Mock
    private DiseaseTraitRepository diseaseTraitRepository;

    @Mock
    private EfoTraitRepository efoTraitRepository;

    @Mock
    private CuratorRepository curatorRepository;

    @Mock
    private CurationStatusRepository curationStatusRepository;

    @Mock
    private AssociationRepository associationRepository;

    @Mock
    private EthnicityRepository ethnicityRepository;

    @Mock
    private UnpublishReasonRepository unpublishReasonRepository;

    @Mock
    private DefaultPubMedSearchService defaultPubMedSearchService;

    @Mock
    private StudyOperationsService studyService;

    @Mock
    private Study study;

    private MockMvc mockMvc;


    @Before
    public void setUpMock() {
        StudyController studyController = new StudyController(studyRepository,
                                                              studyService,
                                                              defaultPubMedSearchService,
                                                              unpublishReasonRepository,
                                                              ethnicityRepository,
                                                              associationRepository,
                                                              curationStatusRepository,
                                                              curatorRepository,
                                                              efoTraitRepository,
                                                              diseaseTraitRepository,
                                                              housekeepingRepository);
        this.mockMvc = MockMvcBuilders.standaloneSetup(studyController).build();
    }

    @Test
    public void testNewStudyForm() throws Exception {
        this.mockMvc.perform(MockMvcRequestBuilders.get("/studies/new").accept(MediaType.TEXT_HTML_VALUE))
                .andExpect(status().isOk())
                .andExpect(view().name("add_study"))
                .andExpect(model().attributeExists("study"))
                .andExpect(model().attributeExists("pubmedIdForImport"))
                .andExpect(model().attribute("study", isA(Study.class)))
                .andExpect(model().attribute("pubmedIdForImport", isA(PubmedIdForImport.class)));
    }
}

@@ -172,34 +172,125 @@
 ito.
-runners.MockitoJUnitRunner
+Mockito;%0Aimport org.mockito.runners.MockitoJUnitRunner;%0Aimport org.springframework.beans.factory.annotation.Autowired
 ;%0Aim
@@ -348,32 +348,77 @@
 nConfiguration;%0A
+import org.springframework.data.domain.Sort;%0A
 import org.sprin
@@ -829,24 +829,137 @@
 vcBuilders;%0A
+import uk.ac.ebi.spot.goci.CurationApplication;%0Aimport uk.ac.ebi.spot.goci.curation.builder.DiseaseTraitBuilder;%0A
 import uk.ac
@@ -1075,16 +1075,63 @@
 ervice;%0A
+import uk.ac.ebi.spot.goci.model.DiseaseTrait;%0A
 import u
@@ -1769,16 +1769,122 @@
 rvice;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Collection;%0Aimport java.util.List;%0A%0A
 import s
@@ -1916,16 +1916,56 @@
 rs.isA;%0A
+import static org.mockito.Mockito.when;%0A
 import s
@@ -2318,16 +2318,21 @@
 troller 
+unit 
 test%0A */
@@ -2414,34 +2414,35 @@
 asses = 
-MockServletContext
+CurationApplication
 .class)%0A
@@ -2507,774 +2507,61 @@
    @
-Mock%0A    private StudyRepository studyRepository;%0A%0A    @Mock%0A    private HousekeepingRepository housekeepingRepository;%0A%0A    @Mock%0A    private DiseaseTraitRepository diseaseTraitRepository;%0A%0A    @Mock%0A    private EfoTraitRepository efoTraitRepository;%0A%0A    @Mock%0A    private CuratorRepository curatorRepository;%0A%0A    @Mock%0A    private CurationStatusRepository curationStatusRepository;%0A%0A    @Mock%0A    private AssociationRepository associationRepository;%0A%0A    @Mock%0A    private EthnicityRepository ethnicityRepository;%0A%0A    @Mock%0A    private UnpublishReasonRepository unpublishReasonRepository;%0A%0A    @Mock%0A    private DefaultPubMedSearchService defaultPubMedSearchService;%0A%0A    @Mock%0A    private StudyOperationsService studyService;%0A%0A    @Mock%0A    private Study study
+Autowired%0A    private StudyController studyController
 ;%0A%0A 
@@ -2589,17 +2589,16 @@
 ckMvc;%0A%0A
-%0A
     @Bef
@@ -2635,934 +2635,8 @@
 ) %7B%0A
-        StudyController studyController = new StudyController(studyRepository,%0A                                                              studyService,%0A                                                              defaultPubMedSearchService,%0A                                                              unpublishReasonRepository,%0A                                                              ethnicityRepository,%0A                                                              associationRepository,%0A                                                              curationStatusRepository,%0A                                                              curatorRepository,%0A                                                              efoTraitRepository,%0A                                                              diseaseTraitRepository,%0A                                                              housekeepingRepository);%0A
     

Update test to use SpringJUnit4ClassRunner.class