<commit_before>package net.contargo.iris.api;

import net.contargo.iris.security.UserAuthenticationService;

import org.hamcrest.Matchers;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import org.unitils.thirdparty.org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.hamcrest.MatcherAssert.assertThat;

import static org.hamcrest.Matchers.is;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import static java.util.Arrays.asList;


/**
 * Unit test for {@link WebExceptionHandler}.
 *
 * @author  <NAME> - <EMAIL>
 * @author  <NAME> - <EMAIL>
 * @author  <NAME> - <EMAIL>
 */
public class WebExceptionHandlerUnitTest {

    private static final String REPORT_PATH = "test-tmp";
    private WebExceptionHandler sut;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private UserAuthenticationService userAuthenticationServiceMock;

    @Before
    public void setUp() {

        userAuthenticationServiceMock = mock(UserAuthenticationService.class);
        sut = new WebExceptionHandler(new File(REPORT_PATH), userAuthenticationServiceMock);

        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
    }


    @After
    public void tearDown() {

        // clean up the directory and delete all the files created for this test

        File dir = new File(REPORT_PATH);
        File[] files = dir.listFiles();

        for (File file : files) {
            file.delete();
        }

        dir.delete();
    }


    /**
     * Test of doResolveException method, of class WebExceptionHandler.
     */
    @Test
    public void testDoResolveExceptionErrorReportContainsTheRelevantInformation() throws Exception {

        sut.doResolveException(request, response, sut, new IOException("My Foo-Exception"));

        File dir = new File(REPORT_PATH);
        File[] files = dir.listFiles();

        Assert.assertThat(files.length, Matchers.equalTo(1));

        String content = IOUtils.toString(new FileInputStream(files[0]));

        Assert.assertThat(content, Matchers.containsString("java.io.IOException: My Foo-Exception"));
    }


    @Test
    public void textualRepresentationOfCurrentUser() {

        UserDetails userDetails = new User("<EMAIL>", "password", asList(new SimpleGrantedAuthority("USER")));
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
            new UsernamePasswordAuthenticationToken(userDetails, null);

        when(userAuthenticationServiceMock.getCurrentUser()).thenReturn(usernamePasswordAuthenticationToken);

        String user = sut.textualRepresentationOfCurrentUser();

        assertThat(user, is("user: <EMAIL>\n"));
    }


    @Test
    public void textualRepresentationOfCurrentUserNull() {

        when(userAuthenticationServiceMock.getCurrentUser()).thenReturn(null);

        String user = sut.textualRepresentationOfCurrentUser();

        assertThat(user, is("?"));
    }
}<commit_msg>cleanup tests, use hemcrest assertThat, correct naming, ...<commit_after>package net.contargo.iris.api;

import net.contargo.iris.security.UserAuthenticationService;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.junit.runner.RunWith;

import org.mockito.Mock;

import org.mockito.runners.MockitoJUnitRunner;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import org.unitils.thirdparty.org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import static org.hamcrest.MatcherAssert.assertThat;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

import static org.mockito.Mockito.when;

import static java.util.Arrays.asList;


/**
 * Unit test for {@link WebExceptionHandler}.
 *
 * @author  <NAME> - <EMAIL>
 * @author  <NAME> - <EMAIL>
 * @author  <NAME> - <EMAIL>
 */
@RunWith(MockitoJUnitRunner.class)
public class WebExceptionHandlerUnitTest {

    private static final String REPORT_PATH = "test-tmp";

    private WebExceptionHandler sut;

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    @Mock
    private UserAuthenticationService userAuthenticationServiceMock;

    @Before
    public void setUp() {

        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();

        sut = new WebExceptionHandler(new File(REPORT_PATH), userAuthenticationServiceMock);
    }


    @After
    public void tearDown() {

        // clean up the directory and delete all the files created for this test

        File dir = new File(REPORT_PATH);
        File[] files = dir.listFiles();

        for (File file : files) {
            file.delete();
        }

        dir.delete();
    }


    @Test
    public void doResolveExceptionErrorReportContainsTheRelevantInformation() throws Exception {

        sut.doResolveException(request, response, sut, new IOException("Exception"));

        File dir = new File(REPORT_PATH);
        File[] files = dir.listFiles();

        assertThat(files.length, equalTo(1));
        assertThat(IOUtils.toString(new FileInputStream(files[0])), containsString("java.io.IOException: Exception"));
    }


    @Test
    public void textualRepresentationOfCurrentUser() {

        UserDetails userDetails = new User("<EMAIL>", "password", asList(new SimpleGrantedAuthority("USER")));
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
            new UsernamePasswordAuthenticationToken(userDetails, null);

        when(userAuthenticationServiceMock.getCurrentUser()).thenReturn(usernamePasswordAuthenticationToken);

        String user = sut.textualRepresentationOfCurrentUser();
        assertThat(user, is("user: <EMAIL>\n"));
    }


    @Test
    public void textualRepresentationOfCurrentUserNull() {

        when(userAuthenticationServiceMock.getCurrentUser()).thenReturn(null);

        String user = sut.textualRepresentationOfCurrentUser();
        assertThat(user, is("?"));
    }
}
{'fraction_non_alphanumeric': 0.09831223628691983, 'fraction_numerical': 0.0005625879043600563, 'mean_word_length': 4.075660242683798, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '6272994', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1883, 'n_words': 404}