package org.innovateuk.ifs.management.competition.setup.applicationsubmission.sectionupdater;

import org.innovateuk.ifs.commons.service.ServiceResult;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.competition.resource.CompetitionSetupSection;
import org.innovateuk.ifs.competition.service.CompetitionSetupRestService;
import org.innovateuk.ifs.management.competition.setup.applicationsubmission.form.ApplicationSubmissionForm;
import org.innovateuk.ifs.management.competition.setup.core.form.CompetitionSetupForm;
import org.innovateuk.ifs.user.resource.UserResource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
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.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.Silent.class)
public class ApplicationSubmissionSectionUpdaterTest {

    @InjectMocks
    private ApplicationSubmissionSectionUpdater updater;

    @Mock
    private CompetitionSetupRestService competitionSetupRestService;

    @Test
    public void doSaveSection() {

        CompetitionResource competition = newCompetitionResource()
                .withAlwaysOpen(true)
                .build();
        ApplicationSubmissionForm form = new ApplicationSubmissionForm(true);

        UserResource loggedInUser = newUserResource().build();

        when(competitionSetupRestService.update(competition)).thenReturn(restSuccess());

        ServiceResult<Void> updateResult = updater.doSaveSection(competition, form, loggedInUser);

        assertThat(updateResult.isSuccess()).isTrue();

        verify(competitionSetupRestService, times(1)).update(competition);
    }

    @Test
    public void getNextSection() {

        ReflectionTestUtils.setField(updater, "assessmentStageCompetitionEnabled", true);

        CompetitionResource competition = newCompetitionResource()
                .withId(1L)
                .withAlwaysOpen(false)
                .build();
        ApplicationSubmissionForm form = new ApplicationSubmissionForm(true);

        String nextSection = updater.getNextSection(form, competition, CompetitionSetupSection.MILESTONES);

        assertThat(nextSection).isEqualTo("redirect:/competition/setup/1/section/milestones");
    }

    @Test
    public void getNextSectionAlwaysOpen() {

        ReflectionTestUtils.setField(updater, "assessmentStageCompetitionEnabled", true);

        CompetitionResource competition = newCompetitionResource()
                .withId(1L)
                .withAlwaysOpen(true)
                .build();
        ApplicationSubmissionForm form = new ApplicationSubmissionForm(true);

        String nextSection = updater.getNextSection(form, competition, CompetitionSetupSection.APPLICATION_ASSESSMENT);

        assertThat(nextSection).isEqualTo("redirect:/competition/setup/1/section/application-assessment");
    }

    @Test
    public void getNextSectionAlwaysOpenNoAssessmentStage() {

        CompetitionResource competition = newCompetitionResource()
                .withId(1L)
                .withAlwaysOpen(true)
                .withHasAssessmentStage(false)
                .build();
        ApplicationSubmissionForm form = new ApplicationSubmissionForm(true);

        String nextSection = updater.getNextSection(form, competition, CompetitionSetupSection.MILESTONES);

        assertThat(nextSection).isEqualTo("redirect:/competition/setup/1/section/milestones");
    }

    @Test
    public void supportsFrom() {
        assertThat(updater.supportsForm(ApplicationSubmissionForm.class)).isTrue();
        assertThat(updater.supportsForm(CompetitionSetupForm.class)).isFalse();
    }
}

@@ -2177,34 +2177,17 @@
 r, %22
-assessmentStageCompetition
+isHecpTcp
 Enab
@@ -2757,34 +2757,17 @@
 r, %22
-assessmentStageCompetition
+isHecpTcp
 Enab
@@ -3135,24 +3135,24 @@
 SESSMENT);%0A%0A
+
         asse
@@ -3257,577 +3257,8 @@
  %7D%0A%0A
-    @Test%0A    public void getNextSectionAlwaysOpenNoAssessmentStage() %7B%0A%0A        CompetitionResource competition = newCompetitionResource()%0A                .withId(1L)%0A                .withAlwaysOpen(true)%0A                .withHasAssessmentStage(false)%0A                .build();%0A        ApplicationSubmissionForm form = new ApplicationSubmissionForm(true);%0A%0A        String nextSection = updater.getNextSection(form, competition, CompetitionSetupSection.MILESTONES);%0A%0A        assertThat(nextSection).isEqualTo(%22redirect:/competition/setup/1/section/milestones%22);%0A    %7D%0A%0A
     

Revert "IFS-11486 - Fix to unit tests."