package org.innovateuk.ifs.registration.populator;

import org.innovateuk.ifs.organisation.resource.OrganisationResource;
import org.innovateuk.ifs.registration.service.RegistrationCookieService;
import org.innovateuk.ifs.registration.viewmodel.OrganisationSelectionChoiceViewModel;
import org.innovateuk.ifs.registration.viewmodel.OrganisationSelectionViewModel;
import org.innovateuk.ifs.user.resource.UserResource;
import org.innovateuk.ifs.user.service.OrganisationRestService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

import static org.innovateuk.ifs.commons.rest.RestResult.restSuccess;
import static org.innovateuk.ifs.organisation.builder.OrganisationResourceBuilder.newOrganisationResource;
import static org.innovateuk.ifs.user.builder.UserResourceBuilder.newUserResource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

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

    @InjectMocks
    private OrganisationSelectionViewModelPopulator populator;

    @Mock
    private OrganisationRestService organisationRestService;

    @Mock
    private RegistrationCookieService registrationCookieService;

    @Test
    public void populate() {
        UserResource user = newUserResource().build();
        HttpServletRequest request = mock(HttpServletRequest.class);
        String url = "Something.com";
        List<OrganisationResource> organisations = newOrganisationResource()
                .withName("Organisation 1", "Organisation 2")
                .withOrganisationTypeName("Type 1", "Type 2")
                .withIsInternational(false)
                .build(2);

        when(organisationRestService.getAllByUserId(user.getId())).thenReturn(restSuccess(organisations));
        when(registrationCookieService.isCollaboratorJourney(request)).thenReturn(false);

        OrganisationSelectionViewModel viewModel = populator.populate(user, request, url);

        organisations.forEach((organisation) -> {
            Optional<OrganisationSelectionChoiceViewModel> maybeChoice = viewModel.getChoices().stream().filter(choice -> choice.getName().equals(organisation.getName())).findAny();
            assertTrue(maybeChoice.isPresent());
            assertEquals(organisation.getOrganisationTypeName(), maybeChoice.get().getType());
            assertEquals(organisation.getId(), (Long) maybeChoice.get().getId());
        });

        assertEquals(viewModel.getNewOrganisationUrl(), url);
        assertEquals(viewModel.isCollaboratorJourney(), false);
    }


}

@@ -1026,63 +1026,9 @@
 ert.
-assertEquals;%0Aimport static org.junit.Assert.assertTrue
+*
 ;%0Aim
@@ -1356,25 +1356,24 @@
 estService;%0A
-%0A
     @Mock%0A  
@@ -1954,19 +1954,21 @@
 .get
-AllByUserId
+Organisations
 (use
@@ -1976,16 +1976,23 @@
 .getId()
+, false
 )).thenR
@@ -2748,38 +2748,37 @@
 ;%0A        assert
-Equ
+F
 als
+e
 (viewModel.isCol
@@ -2795,23 +2795,16 @@
 ourney()
-, false
 );%0A    %7D

fix another broken test