<commit_before>package com.github.onsdigital.thetrain.routes;

import com.github.onsdigital.thetrain.exception.BadRequestException;
import com.github.onsdigital.thetrain.exception.PublishException;
import com.github.onsdigital.thetrain.json.Transaction;
import com.github.onsdigital.thetrain.service.PublisherService;
import com.github.onsdigital.thetrain.service.TransactionsService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import spark.Request;
import spark.Response;
import spark.Route;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

public class CommitTransactionTest {

    @Mock
    private Request request;

    @Mock
    private Response response;

    @Mock
    private TransactionsService transactionsService;

    @Mock
    private PublisherService publisherService;

    @Mock
    private Transaction transaction;

    private Route route;
    private BadRequestException badRequestException;
    private PublishException publishException;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        route = new CommitTransaction(transactionsService, publisherService);
        badRequestException = new BadRequestException("Boom!");
        publishException = new PublishException("Boom!");
    }

    @Test(expected = BadRequestException.class)
    public void testTransactionServiceBadRequestException() throws Exception {
        when(transactionsService.getTransaction(request)).thenThrow(badRequestException);

        try {
            route.handle(request, response);
        } catch (BadRequestException e) {
            assertThat(e.getMessage(), equalTo(badRequestException.getMessage()));
            verify(transactionsService, times(1)).getTransaction(request);
            verify(transactionsService, times(1)).update(null);
            verifyZeroInteractions(publisherService);
            throw e;
        }
    }

    @Test(expected = PublishException.class)
    public void testTransactionServicePublishException() throws Exception {
        when(transactionsService.getTransaction(request)).thenThrow(publishException);

        try {
            route.handle(request, response);
        } catch (BadRequestException e) {
            assertThat(e.getMessage(), equalTo(publishException.getMessage()));
            verify(transactionsService, times(1)).getTransaction(request);
            verify(transactionsService, times(1)).update(null);
            verifyZeroInteractions(publisherService);
            throw e;
        }
    }
}<commit_msg>added unit test for get website path error<commit_after>package com.github.onsdigital.thetrain.routes;

import com.github.onsdigital.thetrain.exception.BadRequestException;
import com.github.onsdigital.thetrain.exception.PublishException;
import com.github.onsdigital.thetrain.json.Transaction;
import com.github.onsdigital.thetrain.service.PublisherService;
import com.github.onsdigital.thetrain.service.TransactionsService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import spark.Request;
import spark.Response;
import spark.Route;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

public class CommitTransactionTest {

    @Mock
    private Request request;

    @Mock
    private Response response;

    @Mock
    private TransactionsService transactionsService;

    @Mock
    private PublisherService publisherService;

    @Mock
    private Transaction transaction;

    private Route route;
    private BadRequestException badRequestException;
    private PublishException publishException;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        route = new CommitTransaction(transactionsService, publisherService);
        badRequestException = new BadRequestException("Boom!");
        publishException = new PublishException("Boom!");
    }

    @Test(expected = BadRequestException.class)
    public void testTransactionServiceBadRequestException() throws Exception {
        when(transactionsService.getTransaction(request)).thenThrow(badRequestException);

        try {
            route.handle(request, response);
        } catch (BadRequestException e) {
            assertThat(e.getMessage(), equalTo(badRequestException.getMessage()));
            verify(transactionsService, times(1)).getTransaction(request);
            verify(transactionsService, times(1)).update(null);
            verifyZeroInteractions(publisherService);
            throw e;
        }
    }

    @Test(expected = PublishException.class)
    public void testTransactionServicePublishException() throws Exception {
        when(transactionsService.getTransaction(request)).thenThrow(publishException);

        try {
            route.handle(request, response);
        } catch (BadRequestException e) {
            assertThat(e.getMessage(), equalTo(publishException.getMessage()));
            verify(transactionsService, times(1)).getTransaction(request);
            verify(transactionsService, times(1)).update(null);
            verifyZeroInteractions(publisherService);
            throw e;
        }
    }

    @Test(expected = PublishException.class)
    public void testPublisherServiceGetWebsiteError() throws Exception {
        when(transactionsService.getTransaction(request)).thenReturn(transaction);

        when(publisherService.websitePath()).thenThrow(publishException);

        try {
            route.handle(request, response);
        } catch (BadRequestException e) {
            assertThat(e.getMessage(), equalTo(publishException.getMessage()));
            verify(transactionsService, times(1)).getTransaction(request);
            verify(transactionsService, times(1)).update(null);
            verify(publisherService, times(1)).websitePath();
            verifyNoMoreInteractions(transactionsService, publisherService);
            throw e;
        }
    }
}
{'fraction_non_alphanumeric': 0.08969845400275524, 'fraction_numerical': 0.0016837593754783406, 'mean_word_length': 3.8328402366863905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4943198', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1619, 'n_words': 315}