package org.innovateuk.ifs.application.creation.controller;

import org.innovateuk.ifs.BaseControllerMockMVCTest;
import org.innovateuk.ifs.registration.service.RegistrationCookieService;
import org.innovateuk.ifs.user.service.UserService;
import org.junit.Test;
import org.mockito.Mock;

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.result.MockMvcResultMatchers.*;

public class ApplicationCreationAuthenticatedControllerTest extends BaseControllerMockMVCTest<ApplicationCreationAuthenticatedController> {

    @Mock
    private UserService userService;

    @Mock
    private RegistrationCookieService registrationCookieService;

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

    @Test
    public void testGetRequestWithExistingApplication() throws Exception {
        when(userService.userHasApplicationForCompetition(loggedInUser.getId(), 1L)).thenReturn(true);
        mockMvc.perform(get("/application/create-authenticated/{competitionId}", 1L))
                .andExpect(status().is2xxSuccessful())
                .andExpect(view().name("create-application/confirm-new-application"));
        verify(userService).userHasApplicationForCompetition(loggedInUser.getId(), 1L);
    }

    @Test
    public void testGetRequestWithoutExistingApplication() throws Exception {
        when(userService.userHasApplicationForCompetition(loggedInUser.getId(), 1L)).thenReturn(false);
        mockMvc.perform(get("/application/create-authenticated/{competitionId}", 1L))
                .andExpect(status().is3xxRedirection())
                .andExpect(redirectedUrl("/organisation/select"));

        verify(registrationCookieService).deleteAllRegistrationJourneyCookies(any());
        verify(registrationCookieService).saveToCompetitionIdCookie(eq(1L), any());
        verify(userService, only()).userHasApplicationForCompetition(loggedInUser.getId(), 1L);
    }

    @Test
    public void testPostEmptyFormShouldThrowError() throws Exception {
        mockMvc.perform(post("/application/create-authenticated/1"))
                .andExpect(status().isOk())
                .andExpect(model().hasErrors())
                .andExpect(model().attributeHasFieldErrors("form", "createNewApplication"))
                .andReturn();
    }

    @Test
    public void testPostCreateNewApplication() throws Exception {
        long competitionId = 1L;
        mockMvc.perform(post("/application/create-authenticated/{competitionId}", competitionId)
                .param("createNewApplication", "1"))
                .andExpect(status().is3xxRedirection())
                .andExpect(redirectedUrl("/organisation/select"));

        verify(registrationCookieService).deleteAllRegistrationJourneyCookies(any());
        verify(registrationCookieService).saveToCompetitionIdCookie(eq(1L), any());
    }

    @Test
    public void testPostNoNewApplication() throws Exception {
        // This should just redirect to the dashboard.
        mockMvc.perform(post("/application/create-authenticated/1").param("createNewApplication", "0"))
                .andExpect(status().is3xxRedirection())
                .andExpect(redirectedUrl("/"));
    }
}
@@ -103,24 +103,249 @@
 ockMVCTest;%0A
+import org.innovateuk.ifs.commons.rest.RestResult;%0Aimport org.innovateuk.ifs.competition.resource.CompetitionOrganisationConfigResource;%0Aimport org.innovateuk.ifs.competition.service.CompetitionOrganisationConfigRestService;%0A
 import org.i
@@ -398,32 +398,32 @@
 nCookieService;%0A
-
 import org.innov
@@ -1064,24 +1064,130 @@
 ieService;%0A%0A
+    @Mock%0A    private CompetitionOrganisationConfigRestService competitionOrganisationConfigRestService;%0A%0A
     @Overrid
@@ -2046,24 +2046,196 @@
 urn(false);%0A
+        when(competitionOrganisationConfigRestService.findByCompetitionId(1L)).thenReturn(RestResult.restSuccess(new CompetitionOrganisationConfigResource(false, false)));%0A
         mock
@@ -2778,32 +2778,204 @@
 ows Exception %7B%0A
+        when(competitionOrganisationConfigRestService.findByCompetitionId(1L)).thenReturn(RestResult.restSuccess(new CompetitionOrganisationConfigResource(false, false)));%0A
         mockMvc.
@@ -3316,32 +3316,204 @@
 ows Exception %7B%0A
+        when(competitionOrganisationConfigRestService.findByCompetitionId(1L)).thenReturn(RestResult.restSuccess(new CompetitionOrganisationConfigResource(false, false)));%0A
         long com
@@ -4044,32 +4044,204 @@
 ows Exception %7B%0A
+        when(competitionOrganisationConfigRestService.findByCompetitionId(1L)).thenReturn(RestResult.restSuccess(new CompetitionOrganisationConfigResource(false, false)));%0A
         // This 
@@ -4431,32 +4431,32 @@
 xRedirection())%0A
-
                 
@@ -4492,10 +4492,687 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testPostInternationalOrganisationApplication() throws Exception %7B%0A        when(competitionOrganisationConfigRestService.findByCompetitionId(1L)).thenReturn(RestResult.restSuccess(new CompetitionOrganisationConfigResource(true, true)));%0A        long competitionId = 1L;%0A        // This should just redirect to create international organisation.%0A        mockMvc.perform(post(%22/application/create-authenticated/%7BcompetitionId%7D%22, competitionId)%0A                .param(%22createNewApplication%22, %221%22))%0A                .andExpect(status().is3xxRedirection())%0A                .andExpect(redirectedUrl(%22/organisation/create/international-organisation%22));%0A    %7D
 %0A%7D

test to create new international application