<commit_before>package org.innovateuk.ifs.application.forms.questions.granttransferdetails.controller;

import org.innovateuk.ifs.BaseControllerMockMVCTest;
import org.innovateuk.ifs.application.forms.questions.granttransferdetails.form.GrantTransferDetailsForm;
import org.innovateuk.ifs.application.forms.questions.granttransferdetails.populator.GrantTransferDetailsFormPopulator;
import org.innovateuk.ifs.application.forms.questions.granttransferdetails.populator.GrantTransferDetailsViewModelPopulator;
import org.innovateuk.ifs.application.forms.questions.granttransferdetails.saver.GrantTransferDetailsSaver;
import org.innovateuk.ifs.application.forms.questions.granttransferdetails.viewmodel.GrantTransferDetailsViewModel;
import org.innovateuk.ifs.application.service.QuestionStatusRestService;
import org.innovateuk.ifs.user.resource.ProcessRoleResource;
import org.innovateuk.ifs.user.service.UserRestService;
import org.junit.Test;
import org.mockito.Mock;

import java.math.BigDecimal;
import java.util.Collections;

import static org.innovateuk.ifs.commons.rest.RestResult.restSuccess;
import static org.innovateuk.ifs.user.builder.ProcessRoleResourceBuilder.newProcessRoleResource;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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 GrantTransferDetailsControllerTest extends BaseControllerMockMVCTest<GrantTransferDetailsController> {

    @Mock
    private GrantTransferDetailsFormPopulator grantTransferDetailsFormPopulator;

    @Mock
    private GrantTransferDetailsViewModelPopulator grantTransferDetailsViewModelPopulator;

    @Mock
    private GrantTransferDetailsSaver grantTransferDetailsSaver;

    @Mock
    private UserRestService userRestService;

    @Mock
    private QuestionStatusRestService questionStatusRestService;

    @Override
    protected GrantTransferDetailsController supplyControllerUnderTest() {
        return new GrantTransferDetailsController(grantTransferDetailsFormPopulator, grantTransferDetailsViewModelPopulator, grantTransferDetailsSaver, userRestService, questionStatusRestService);
    }

    @Test
    public void viewGrantTransferDetails() throws Exception {
        long questionId = 1L;
        long applicationId = 2L;

        GrantTransferDetailsViewModel viewModel = mock(GrantTransferDetailsViewModel.class);
        when(grantTransferDetailsViewModelPopulator.populate(applicationId, questionId, loggedInUser.getId())).thenReturn(viewModel);
        mockMvc.perform(
                get("/application/{applicationId}/form/question/{questionId}/grant-transfer-details", applicationId, questionId))
                .andExpect(status().isOk())
                .andExpect(view().name("application/questions/grant-transfer-details"))
                .andReturn();

        verify(grantTransferDetailsFormPopulator).populate(any(), eq(applicationId));
    }

    @Test
    public void saveAndReturn() throws Exception {
        long questionId = 1L;
        long applicationId = 2L;

        mockMvc.perform(
                post("/application/{applicationId}/form/question/{questionId}/grant-transfer-details", applicationId, questionId))
                .andExpect(status().is3xxRedirection())
                .andExpect(redirectedUrl(String.format("/application/%d", applicationId)))
                .andReturn();
    }

    @Test
    public void markAsComplete() throws Exception {
        long questionId = 1L;
        long applicationId = 2L;

        GrantTransferDetailsForm grantTransferDetailsForm = new GrantTransferDetailsForm();
        grantTransferDetailsForm.setActionType(1L);
        grantTransferDetailsForm.setFundingContribution(BigDecimal.valueOf(100000L));
        grantTransferDetailsForm.setGrantAgreementNumber("123456");
        grantTransferDetailsForm.setProjectCoordinator(true);
        grantTransferDetailsForm.setStartDateMonth(10);
        grantTransferDetailsForm.setStartDateYear(2000);
        grantTransferDetailsForm.setEndDateMonth(10);
        grantTransferDetailsForm.setEndDateYear(2020);
        grantTransferDetailsForm.setParticipantId("123456789");
        grantTransferDetailsForm.setProjectName("Project Name");

        ProcessRoleResource role = newProcessRoleResource().build();
<commit_msg>IFS-8347 - Unit test fix.<commit_after>package org.innovateuk.ifs.application.forms.questions.granttransferdetails.controller;

import org.innovateuk.ifs.BaseControllerMockMVCTest;
import org.innovateuk.ifs.application.forms.questions.granttransferdetails.form.GrantTransferDetailsForm;
import org.innovateuk.ifs.application.forms.questions.granttransferdetails.populator.GrantTransferDetailsFormPopulator;
import org.innovateuk.ifs.application.forms.questions.granttransferdetails.populator.GrantTransferDetailsViewModelPopulator;
import org.innovateuk.ifs.application.forms.questions.granttransferdetails.saver.GrantTransferDetailsSaver;
import org.innovateuk.ifs.application.forms.questions.granttransferdetails.viewmodel.GrantTransferDetailsViewModel;
import org.innovateuk.ifs.application.service.QuestionStatusRestService;
import org.innovateuk.ifs.user.resource.ProcessRoleResource;
import org.innovateuk.ifs.user.service.UserRestService;
import org.junit.Test;
import org.mockito.Mock;

import java.math.BigDecimal;
import java.util.Collections;

import static org.innovateuk.ifs.commons.rest.RestResult.restSuccess;
import static org.innovateuk.ifs.user.builder.ProcessRoleResourceBuilder.newProcessRoleResource;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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 GrantTransferDetailsControllerTest extends BaseControllerMockMVCTest<GrantTransferDetailsController> {

    @Mock
    private GrantTransferDetailsFormPopulator grantTransferDetailsFormPopulator;

    @Mock
    private GrantTransferDetailsViewModelPopulator grantTransferDetailsViewModelPopulator;

    @Mock
    private GrantTransferDetailsSaver grantTransferDetailsSaver;

    @Mock
    private UserRestService userRestService;

    @Mock
    private QuestionStatusRestService questionStatusRestService;

    @Override
    protected GrantTransferDetailsController supplyControllerUnderTest() {
        return new GrantTransferDetailsController(grantTransferDetailsFormPopulator, grantTransferDetailsViewModelPopulator, grantTransferDetailsSaver, userRestService, questionStatusRestService);
    }

    @Test
    public void viewGrantTransferDetails() throws Exception {
        long questionId = 1L;
        long applicationId = 2L;

        GrantTransferDetailsViewModel viewModel = mock(GrantTransferDetailsViewModel.class);
        when(grantTransferDetailsViewModelPopulator.populate(applicationId, questionId, loggedInUser.getId())).thenReturn(viewModel);
        mockMvc.perform(
                get("/application/{applicationId}/form/question/{questionId}/grant-transfer-details", applicationId, questionId))
                .andExpect(status().isOk())
                .andExpect(view().name("application/questions/grant-transfer-details"))
                .andReturn();

        verify(grantTransferDetailsFormPopulator).populate(any(), eq(applicationId));
    }

    @Test
    public void saveAndReturn() throws Exception {
        long questionId = 1L;
        long applicationId = 2L;

        mockMvc.perform(
                post("/application/{applicationId}/form/question/{questionId}/grant-transfer-details", applicationId, questionId))
                .andExpect(status().is3xxRedirection())
                .andExpect(redirectedUrl(String.format("/application/%d", applicationId)))
                .andReturn();
    }

    @Test
    public void markAsComplete() throws Exception {
        long questionId = 1L;
        long applicationId = 2L;

        GrantTransferDetailsForm grantTransferDetailsForm = new GrantTransferDetailsForm();
        grantTransferDetailsForm.setActionType(1L);
        grantTransferDetailsForm.setFundingContribution(BigDecimal.valueOf(100000L));
        grantTransferDetailsForm.setGrantAgreementNumber("123456");
        grantTransferDetailsForm.setProjectCoordinator(true);
        grantTransferDetailsForm.setStartDateMonth(10);
        grantTransferDetailsForm.setStartDateYear(2000);
        grantTransferDetailsForm.setEndDateMonth(10);
        grantTransferDetailsForm.setEndDateYear(2025);
        grantTransferDetailsForm.setParticipantId("123456789");
        grantTransferDetailsForm.setProjectName("Project Name");

        ProcessRoleResource role = newProcessRoleResource().build();

{'fraction_non_alphanumeric': 0.08291347207009858, 'fraction_numerical': 0.009419496166484118, 'mean_word_length': 4.898578811369509, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3409829', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2265, 'n_words': 333}