package org.innovateuk.ifs.competition.controller;

import org.innovateuk.ifs.BaseControllerMockMVCTest;
import org.innovateuk.ifs.competition.builder.CompetitionResourceBuilder;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.user.resource.UserResource;
import org.junit.Test;
import org.springframework.http.MediaType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.innovateuk.ifs.commons.service.ServiceResult.serviceSuccess;
import static org.innovateuk.ifs.competition.builder.CompetitionResourceBuilder.newCompetitionResource;
import static org.innovateuk.ifs.util.JsonMappingUtil.toJson;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class CompetitionControllerTest extends BaseControllerMockMVCTest<CompetitionController> {

    @Override
    protected CompetitionController supplyControllerUnderTest() {
        return new CompetitionController();
    }


    @Test
    public void getCompetitionsByUserId() throws Exception {
        final Long userId = 1L;

        when(competitionServiceMock.getCompetitionsByUserId(userId)).thenReturn(serviceSuccess(newCompetitionResource().build(1)));

        mockMvc.perform(get("/competition/getCompetitionsByUserId/{userId}", userId))
                .andExpect(status().isOk());

        verify(competitionServiceMock, only()).getCompetitionsByUserId(userId);
    }

    @Test
    public void notifyAssessors() throws Exception {
        final Long competitionId = 1L;

        when(competitionServiceMock.notifyAssessors(competitionId)).thenReturn(serviceSuccess());
        when(assessorServiceMock.notifyAssessorsByCompetition(competitionId)).thenReturn(serviceSuccess());

        mockMvc.perform(put("/competition/{id}/notify-assessors", competitionId))
                .andExpect(status().isOk());

        verify(competitionServiceMock, only()).notifyAssessors(competitionId);
        verify(assessorServiceMock).notifyAssessorsByCompetition(competitionId);
    }

    @Test
    public void updateCompetitionInitialDetails() throws Exception {
        final Long competitionId = 1L;
        final Long leadTechnologistUserId = 7L;

        CompetitionResource competitionResource = CompetitionResourceBuilder.newCompetitionResource()
                .withInnovationAreaNames(Collections.emptySet())
                .withLeadTechnologist(leadTechnologistUserId)
                .build();

        when(competitionServiceMock.getCompetitionById(competitionId)).thenReturn(serviceSuccess(competitionResource));
        when(competitionSetupServiceMock.updateCompetitionInitialDetails(any(), any(), any())).thenReturn(serviceSuccess());

        mockMvc.perform(put("/competition/{id}/update-competition-initial-details", competitionId)
                .contentType(MediaType.APPLICATION_JSON)
                .content(toJson(competitionResource)))
                .andExpect(status().isOk());

        verify(competitionServiceMock, only()).getCompetitionById(competitionId);
        verify(competitionSetupServiceMock, only()).updateCompetitionInitialDetails(competitionId, competitionResource, leadTechnologistUserId);
    }

    @Test
    public void closeAssessment() throws Exception {
        final Long competitionId = 1L;

        when(competitionServiceMock.closeAssessment(competitionId)).thenReturn(serviceSuccess());

        mockMvc.perform(put("/competition/{id}/close-assessment", competitionId))
                .andExpect(status().isOk())
                .andExpect(content().string(""));

        verify(competitionServiceMock, only()).closeAssessment(competitionId);

    }

    @Test
    public void findInnovationLeads() throws Exception {
        final Long competitionId = 1L;

        List<UserResource> innovationLeads = new ArrayList<>();
        when(competitionServiceMock.findInnovationLeads(competitionId)).thenReturn(serviceSuccess(innovationLeads));

        mockMvc.perform(get("/competition/{id}/innovation-leads", competitionId))
                .andExpect(status().isOk())
                .andExpect(content().json(toJson(innovationLeads)));

        verify(competitionServiceMock, only()).findInnovationLeads(competitionId);

    }

    @Test
    public void addInnovationLead() throws Exception {
        final Long competitionId = 1L;
        final Long innovationLeadUserId = 2L;

        when(competitionServiceMock.addInnovationLead(competitionId, innovationLeadUserId )).thenReturn(serviceSuccess());

        mockMvc.perform(post("/competition/{id}/add-innovation-lead/{innovationLeadUserId}", competitionId, innovationLeadUserId))
                .andExpect(status().isOk());

        verify(competitionServiceMock, only()).addInnovationLead(competitionId, innovationLeadUserId);

    }

    @Test
    public void removeInnovationLead() throws Exception {
        final Long competitionId = 1L;
        final Long innovationLeadUserId = 2L;

        when(competitionServiceMock.removeInnovationLead(competitionId, innovationLeadUserId )).thenReturn(serviceSuccess());

        mockMvc.perform(post("/competition/{id}/remove-innovation-lead/{innovationLeadUserId}", competitionId, innovationLeadUserId))
                .andExpect(status().isOk());

        verify(competitionServiceMock, only()).removeInnovationLead(competitionId, innovationLeadUserId);

    }

    @Test
    public void releaseFeedback() throws Exception {
        final Long competitionId = 1L;

        when(competitionServiceMock.releaseFeedback(competitionId)).thenReturn(serviceSuccess());
        when(applicationServiceMock.notifyApplicantsByCompetition(competitionId)).thenReturn(serviceSuccess());

        mockMvc.perform(put("/competition/{id}/release-feedback", competitionId))
                .andExpect(status().isOk());

        verify(competitionServiceMock, only()).releaseFeedback(competitionId);
        verify(applicationServiceMock).notifyApplicantsByCompetition(competitionId);
    }
}

@@ -94,24 +94,166 @@
 ockMVCTest;%0A
+import org.innovateuk.ifs.application.builder.ApplicationResourceBuilder;%0Aimport org.innovateuk.ifs.application.resource.ApplicationResource;%0A
 import org.i
@@ -5931,32 +5931,722 @@
 serId);%0A%0A    %7D%0A%0A
+    @Test%0A    public void findUnsuccessfulApplications() throws Exception %7B%0A        final Long competitionId = 1L;%0A%0A        List%3CApplicationResource%3E unsuccessfulApplications = ApplicationResourceBuilder.newApplicationResource().build(2);%0A%0A        when(competitionServiceMock.findUnsuccessfulApplications(competitionId)).thenReturn(serviceSuccess(unsuccessfulApplications));%0A%0A        mockMvc.perform(get(%22/competition/%7Bid%7D/unsuccessful-applications%22, competitionId))%0A                .andExpect(status().isOk())%0A                .andExpect(content().json(toJson(unsuccessfulApplications)));%0A%0A        verify(competitionServiceMock, only()).findUnsuccessfulApplications(competitionId);%0A%0A    %7D%0A%0A
     @Test%0A    pu

IFS-1459-Unsuccessful-App-List