<commit_before>package org.innovateuk.ifs.supporter.service;

import org.innovateuk.ifs.BaseServiceUnitTest;
import org.innovateuk.ifs.supporter.resource.SupporterAssignmentResource;
import org.innovateuk.ifs.util.EncryptedCookieService;
import org.innovateuk.ifs.util.JsonUtil;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import java.util.Optional;

import static org.innovateuk.ifs.supporter.service.SupporterCookieService.SUPPORTER_PREVIOUS_RESPONSE;
import static org.innovateuk.ifs.supporter.builder.SupporterAssignmentResourceBuilder.newSupporterAssignmentResource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

public class SupporterCookieServiceTest  extends BaseServiceUnitTest<SupporterCookieService> {

    @Mock
    private EncryptedCookieService cookieUtil;

    private MockHttpServletResponse response;
    private MockHttpServletRequest request;

    @Override
    protected SupporterCookieService supplyServiceUnderTest() {
        return new SupporterCookieService();
    }

    @Before
    public void setUp() {
        response = new MockHttpServletResponse();
        request = new MockHttpServletRequest();

        super.setup();
    }

    @Test
    public void saveToSupporterPreviousResponseCookie() throws Exception {
        SupporterAssignmentResource supporterAssignmentResource = newSupporterAssignmentResource().build();

        service.saveToSupporterPreviousResponseCookie(supporterAssignmentResource, response);

        verify(cookieUtil, times(1)).saveToCookie(response, SUPPORTER_PREVIOUS_RESPONSE, JsonUtil.getSerializedObject(supporterAssignmentResource));
    }

    @Test
    public void getSupporterPreviousResponseCookie() throws Exception {
        SupporterAssignmentResource supporterAssignmentResource = newSupporterAssignmentResource().build();

        when(cookieUtil.getCookieValue(request, SUPPORTER_PREVIOUS_RESPONSE)).thenReturn(JsonUtil.getSerializedObject(supporterAssignmentResource));

        Optional<SupporterAssignmentResource> result = service.getSupporterPreviousResponseCookie(request);

        assertTrue(result.isPresent());
        assertEquals(supporterAssignmentResource, result.get());
        verify(cookieUtil, times(1)).getCookieValue(request, SUPPORTER_PREVIOUS_RESPONSE);
    }

}<commit_msg>added a case to support maximum word count<commit_after>package org.innovateuk.ifs.supporter.service;

import org.innovateuk.ifs.BaseServiceUnitTest;
import org.innovateuk.ifs.supporter.resource.SupporterAssignmentResource;
import org.innovateuk.ifs.util.EncryptedCookieService;
import org.innovateuk.ifs.util.JsonUtil;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.innovateuk.ifs.supporter.service.SupporterCookieService.SUPPORTER_PREVIOUS_RESPONSE;
import static org.innovateuk.ifs.supporter.builder.SupporterAssignmentResourceBuilder.newSupporterAssignmentResource;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class SupporterCookieServiceTest  extends BaseServiceUnitTest<SupporterCookieService> {

    @Mock
    private EncryptedCookieService cookieUtil;

    private MockHttpServletResponse response;
    private MockHttpServletRequest request;

    @Override
    protected SupporterCookieService supplyServiceUnderTest() {
        return new SupporterCookieService();
    }

    @Before
    public void setUp() {
        response = new MockHttpServletResponse();
        request = new MockHttpServletRequest();

        super.setup();
    }

    @Test
    public void saveToSupporterPreviousResponseCookie() {
        SupporterAssignmentResource supporterAssignmentResource = newSupporterAssignmentResource().build();

        service.saveToSupporterPreviousResponseCookie(supporterAssignmentResource, response);

        verify(cookieUtil, times(1)).saveToCookie(response, SUPPORTER_PREVIOUS_RESPONSE,
                JsonUtil.getSerializedObject(supporterAssignmentResource));
    }

    @Test
    public void getSupporterPreviousResponseCookie() {
        SupporterAssignmentResource supporterAssignmentResource = newSupporterAssignmentResource().build();

        when(cookieUtil.getCookieValue(request, SUPPORTER_PREVIOUS_RESPONSE)).thenReturn(JsonUtil.getSerializedObject(supporterAssignmentResource));

        Optional<SupporterAssignmentResource> result = service.getSupporterPreviousResponseCookie(request);

        assertTrue(result.isPresent());
        assertEquals(supporterAssignmentResource, result.get());
        verify(cookieUtil, times(1)).getCookieValue(request, SUPPORTER_PREVIOUS_RESPONSE);
    }

    @Test
    public void saveToSupporterPreviousResponseCookieForMaximumWordCount() {
        long wordCount = 250;

        SupporterAssignmentResource supporterAssignmentResource = newSupporterAssignmentResource()
                .withComments(Stream.generate(() -> "ABCDEFGHIJKLMNOPQRSTUVWXYZ ").limit(wordCount).collect(Collectors.joining()))
                .build();

        when(cookieUtil.getCookieValue(request, SUPPORTER_PREVIOUS_RESPONSE)).thenReturn(JsonUtil.getSerializedObject(supporterAssignmentResource));

        service.saveToSupporterPreviousResponseCookie(supporterAssignmentResource, response);
        Optional<SupporterAssignmentResource> result = service.getSupporterPreviousResponseCookie(request);

        assertTrue(result.isPresent());
        assertNotNull(result.get().getComments());
        assertEquals(wordCount, result.get().getComments().split(" ").length);

        verify(cookieUtil, times(1)).saveToCookie(response, SUPPORTER_PREVIOUS_RESPONSE,
                JsonUtil.getSerializedObject(supporterAssignmentResource));
        verify(cookieUtil, times(1)).getCookieValue(request, SUPPORTER_PREVIOUS_RESPONSE);
    }
}
{'fraction_non_alphanumeric': 0.0827140549273021, 'fraction_numerical': 0.0014539579967689823, 'mean_word_length': 5.31734693877551, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, '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': '6967021', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1638, 'n_words': 247}