<commit_before>package net.kencochrane.raven.marshaller.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import net.kencochrane.raven.event.interfaces.ExceptionInterface;
import net.kencochrane.raven.event.interfaces.ImmutableThrowable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.UUID;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class TestExceptionInterfaceBinding extends AbstractTestInterfaceBinding {
    private ExceptionInterfaceBinding interfaceBinding;
    @Mock
    private ExceptionInterface mockExceptionInterface;

    @Before
    public void setUp() throws Exception {
        super.setUp();
        interfaceBinding = new ExceptionInterfaceBinding();
    }

    @Test
    public void testSimpleException() throws Exception {
        String message = UUID.randomUUID().toString();
        Throwable throwable = new IllegalStateException(message);
        when(mockExceptionInterface.getThrowable()).thenReturn(new ImmutableThrowable(throwable));

        JsonGenerator jsonGenerator = getJsonGenerator();
        interfaceBinding.writeInterface(jsonGenerator, mockExceptionInterface);
        jsonGenerator.close();

        JsonNode rootNode = getMapper().readValue(getJsonParser(), JsonNode.class);
        assertThat(rootNode.isArray(), is(true));
        JsonNode exceptionNode = rootNode.get(0);<commit_msg>Mock the stackTraceBinding for ExceptionBinding tests<commit_after>package net.kencochrane.raven.marshaller.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import net.kencochrane.raven.event.interfaces.ExceptionInterface;
import net.kencochrane.raven.event.interfaces.ImmutableThrowable;
import net.kencochrane.raven.event.interfaces.StackTraceInterface;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

import java.util.UUID;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class TestExceptionInterfaceBinding extends AbstractTestInterfaceBinding {
    private ExceptionInterfaceBinding interfaceBinding;
    @Mock
    private ExceptionInterface mockExceptionInterface;
    @Mock
    private InterfaceBinding<StackTraceInterface> stackTraceInterfaceBinding;

    @Before
    public void setUp() throws Exception {
        super.setUp();
        interfaceBinding = new ExceptionInterfaceBinding(stackTraceInterfaceBinding);

        doAnswer(new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                JsonGenerator jsonGenerator = (JsonGenerator) invocation.getArguments()[0];
                if (invocation.getArguments()[1] != null) {
                    jsonGenerator.writeStartObject();
                    jsonGenerator.writeEndObject();
                } else {
                    jsonGenerator.writeNull();
                }
                return null;
            }
        }).when(stackTraceInterfaceBinding).writeInterface(any(JsonGenerator.class), any(StackTraceInterface.class));
    }

    @Test
    public void testSimpleException() throws Exception {
        String message = UUID.randomUUID().toString();
        Throwable throwable = new IllegalStateException(message);
        when(mockExceptionInterface.getThrowable()).thenReturn(new ImmutableThrowable(throwable));

        JsonGenerator jsonGenerator = getJsonGenerator();
        interfaceBinding.writeInterface(jsonGenerator, mockExceptionInterface);
        jsonGenerator.close();

        JsonNode rootNode = getMapper().readValue(getJsonParser(), JsonNode.class);
        assertThat(rootNode.isArray(), is(true));
        JsonNode exceptionNode = rootNode.get(0);
{'fraction_non_alphanumeric': 0.08888888888888889, 'fraction_numerical': 0.000925925925925926, 'mean_word_length': 4.156324582338902, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4158951', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1100, 'n_words': 212}