package org.innovateuk.ifs.management.model;

import org.innovateuk.ifs.application.builder.ApplicationResourceBuilder;
import org.innovateuk.ifs.application.resource.ApplicationPageResource;
import org.innovateuk.ifs.application.resource.ApplicationResource;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.competition.service.CompetitionPostSubmissionRestService;
import org.innovateuk.ifs.competition.service.CompetitionRestService;
import org.innovateuk.ifs.management.viewmodel.PaginationViewModel;
import org.innovateuk.ifs.management.viewmodel.UnsuccessfulApplicationsViewModel;
import org.innovateuk.ifs.user.resource.UserResource;
import org.innovateuk.ifs.user.resource.UserRoleType;
import org.innovateuk.ifs.user.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.List;

import static org.innovateuk.ifs.commons.rest.RestResult.restSuccess;
import static org.innovateuk.ifs.competition.builder.CompetitionResourceBuilder.newCompetitionResource;
import static org.innovateuk.ifs.user.builder.UserResourceBuilder.newUserResource;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class UnsuccessfulApplicationsModelPopulatorTest {

    @InjectMocks
    private UnsuccessfulApplicationsModelPopulator unsuccessfulApplicationsModelPopulator;

    @Mock
    private CompetitionRestService competitionRestService;

    @Mock
    private CompetitionPostSubmissionRestService competitionPostSubmissionRestService;

    @Mock
    private UserService userService;

    @Test
    public void populateModel() throws Exception {

        Long competitionId = 1L;
        int pageNumber = 0;
        int pageSize = 20;
        String sortField = "id";
        String existingQueryString = "";
        boolean isIfsAdmin = true;

        String competitionName = "Competition One";

        CompetitionResource competitionResource = newCompetitionResource()
                .withId(competitionId)
                .withName(competitionName)
                .build();

        UserResource userResource = newUserResource()
                .withId(5L)
                .build();

        List<ApplicationResource> unsuccessfulApplications = ApplicationResourceBuilder.newApplicationResource().build(2);
        ApplicationPageResource unsuccessfulApplicationsPagedResult = Mockito.mock(ApplicationPageResource.class);
        when(unsuccessfulApplicationsPagedResult.getContent()).thenReturn(unsuccessfulApplications);
        when(unsuccessfulApplicationsPagedResult.getTotalElements()).thenReturn((long)unsuccessfulApplications.size());

        when(competitionRestService.getCompetitionById(competitionId))
                .thenReturn(restSuccess(competitionResource));
        when(competitionPostSubmissionRestService.findUnsuccessfulApplications(competitionId, pageNumber, pageSize, sortField))
                .thenReturn(restSuccess(unsuccessfulApplicationsPagedResult));
        when(userService.existsAndHasRole(5L, UserRoleType.IFS_ADMINISTRATOR)).thenReturn(true);

        UnsuccessfulApplicationsViewModel viewModel = unsuccessfulApplicationsModelPopulator.populateModel(competitionId,
                pageNumber, pageSize, sortField, userResource, existingQueryString);

        assertEquals(competitionId, viewModel.getCompetitionId());
        assertEquals(competitionName, viewModel.getCompetitionName());
        assertEquals(isIfsAdmin, viewModel.isIfsAdmin());
        assertEquals(unsuccessfulApplications, viewModel.getUnsuccessfulApplications());
        assertEquals(unsuccessfulApplications.size(), viewModel.getUnsuccessfulApplicationsSize());
        assertEquals(new PaginationViewModel(unsuccessfulApplicationsPagedResult, existingQueryString), viewModel.getUnsuccessfulApplicationsPagination());
    }
}
@@ -245,32 +245,102 @@
 cationResource;%0A
+import org.innovateuk.ifs.application.service.ApplicationRestService;%0A
 import org.innov
@@ -383,32 +383,32 @@
 titionResource;%0A
-
 import org.innov
@@ -1710,67 +1710,39 @@
 ate 
-CompetitionPostSubmissionRestService competitionPostSubmiss
+ApplicationRestService applicat
 ionR
@@ -3019,30 +3019,16 @@
 hen(
-competitionPostSubmiss
+applicat
 ionR

move of unsuccessful application from CompetitionPostSubmission to ApplicationController