package org.innovateuk.ifs.competition.service;

import org.innovateuk.ifs.BaseRestServiceUnitTest;
import org.innovateuk.ifs.commons.rest.RestResult;
import org.innovateuk.ifs.invite.resource.InviteUserResource;
import org.innovateuk.ifs.user.builder.UserResourceBuilder;
import org.innovateuk.ifs.user.resource.UserResource;
import org.junit.Test;
import org.springframework.http.HttpStatus;

import java.util.List;

import static org.innovateuk.ifs.commons.service.ParameterizedTypeReferences.userListType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class CompetitionSetupStakeholderRestServiceImplTest extends BaseRestServiceUnitTest<CompetitionSetupStakeholderRestServiceImpl> {

    private static final String competitionSetupStakeholderRestURL = "/competition/setup/";

    @Override
    protected CompetitionSetupStakeholderRestServiceImpl registerRestServiceUnderTest() {
        return new CompetitionSetupStakeholderRestServiceImpl();
    }

    @Test
    public void inviteStakeholder() {
        long competitionId = 1L;
        InviteUserResource inviteUserResource = new InviteUserResource();

        String url = competitionSetupStakeholderRestURL + competitionId + "/stakeholder/invite";
        setupPostWithRestResultExpectations(url, inviteUserResource, HttpStatus.OK);

        RestResult<Void> result = service.inviteStakeholder(inviteUserResource, competitionId);
        assertTrue(result.isSuccess());

        setupPostWithRestResultVerifications(url, Void.class, inviteUserResource);
    }

    @Test
    public void findStakeholders() {

        long competitionId = 1L;
        List<UserResource> responseBody = UserResourceBuilder.newUserResource().build(2);

        String url = competitionSetupStakeholderRestURL + competitionId + "/stakeholder/find-all";
        setupGetWithRestResultExpectations(url, userListType(), responseBody);

        List<UserResource> response = service.findStakeholders(competitionId).getSuccess();
        assertEquals(responseBody, response);
    }

    @Test
    public void addStakeholder() {
        long competitionId = 1L;
        long stakeholderUserId = 2L;

        String url = competitionSetupStakeholderRestURL + competitionId + "/stakeholder/" + stakeholderUserId + "/add";
        setupPostWithRestResultExpectations(url, HttpStatus.OK);

        RestResult<Void> result = service.addStakeholder(competitionId, stakeholderUserId);
        assertTrue(result.isSuccess());

        setupPostWithRestResultVerifications(url, Void.class);
    }

    @Test
    public void removeStakeholder() {
        long competitionId = 1L;
        long stakeholderUserId = 2L;

        String url = competitionSetupStakeholderRestURL + competitionId + "/stakeholder/" + stakeholderUserId + "/remove";
        setupPostWithRestResultExpectations(url, HttpStatus.OK);

        RestResult<Void> result = service.removeStakeholder(competitionId, stakeholderUserId);
        assertTrue(result.isSuccess());

        setupPostWithRestResultVerifications(url, Void.class);
    }

    @Test
    public void findPendingStakeholderInvites() {

        long competitionId = 1L;
        List<UserResource> responseBody = UserResourceBuilder.newUserResource().build(2);

        String url = competitionSetupStakeholderRestURL + competitionId + "/stakeholder/pending-invites";
        setupGetWithRestResultExpectations(url, userListType(), responseBody);

        List<UserResource> response = service.findPendingStakeholderInvites(competitionId).getSuccess();
        assertEquals(responseBody, response);
    }
}


@@ -236,40 +236,130 @@
 ifs.
-user.builder.UserResourceBuilder
+invite.resource.StakeholderInviteResource;%0Aimport org.innovateuk.ifs.registration.resource.StakeholderRegistrationResource
 ;%0Aim
@@ -593,16 +593,343 @@
 stType;%0A
+import static org.innovateuk.ifs.stakeholder.builder.StakeholderInviteResourceBuilder.newStakeholderInviteResource;%0Aimport static org.innovateuk.ifs.stakeholder.builder.StakeholderRegistrationResourceBuilder.newStakeholderRegistrationResource;%0Aimport static org.innovateuk.ifs.user.builder.UserResourceBuilder.newUserResource;%0A
 import s
@@ -2106,36 +2106,16 @@
 eBody = 
-UserResourceBuilder.
 newUserR
@@ -2451,32 +2451,996 @@
 sponse);%0A    %7D%0A%0A
+    @Test%0A    public void getInvite() %7B%0A%0A        String hash = %22hash1234%22;%0A        StakeholderInviteResource invite = newStakeholderInviteResource().build();%0A        String url = competitionSetupStakeholderRestURL + %22get-invite/%22 + hash;%0A        setupGetWithRestResultAnonymousExpectations(url, StakeholderInviteResource.class, invite);%0A%0A        StakeholderInviteResource response = service.getInvite(hash).getSuccess();%0A        assertEquals(invite, response);%0A    %7D%0A%0A    @Test%0A    public void createStakeholder() %7B%0A%0A        String hash = %22hash1234%22;%0A        StakeholderRegistrationResource resource = newStakeholderRegistrationResource().build();%0A        String url = competitionSetupStakeholderRestURL + %22stakeholder/create/%22 + hash;%0A        setupPostWithRestResultAnonymousExpectations(url, Void.class, resource, null, HttpStatus.OK);%0A%0A        RestResult%3CVoid%3E result = service.createStakeholder(hash, resource);%0A        assertTrue(result.isSuccess());%0A%0A    %7D%0A%0A
     @Test%0A    pu
@@ -4528,32 +4528,32 @@
 etitionId = 1L;%0A
+
         List%3CUse
@@ -4578,36 +4578,16 @@
 eBody = 
-UserResourceBuilder.
 newUserR

Add rest service unit tests