package com.phsshp.metrics.reporter.checkstyle;

import com.phsshp.file.FileCache;
import com.phsshp.metrics.model.MetricsReportBuilder;
import com.phsshp.metrics.reporter.ReportingException;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
import com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck;
import org.junit.Test;

import java.io.File;
import java.util.Arrays;

import static com.phsshp.testutils.matchers.FileMeasurementsMatcher.measurementsMatching;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;

public class CheckstyleAdapterAuditListenerTest {

    private final File testFile = new File("src/test/resources/test-project/SomeFile.java");
    private final AuditEventToMeasurementConverter auditEventConverter = new AuditEventToMeasurementConverter();
    private final FileCache fileCache = new FileCache(Arrays.asList(testFile));
    private final MetricsReportBuilder metricsReportBuilder = new MetricsReportBuilder();
    private final CheckstyleAdapterAuditListener auditListener = new CheckstyleAdapterAuditListener(auditEventConverter,
            fileCache, metricsReportBuilder);

    @Test
    public void hardcodedMetricCreation() throws Exception {
        auditListener.addError(new AuditEvent(this, testFile.getAbsolutePath(),
            checkstyleMessageWith("First second third 42 rest", FileLengthCheck.class)));

        assertThat(metricsReportBuilder.build().getMeasurements(), contains(
                measurementsMatching("src/test/resources/test-project/SomeFile.java", 42)));
    }

    @Test(expected = ReportingException.class)
    public void throwExceptionWhenReportingErrorForFileNotTracked() throws Exception {
        auditListener.addError(new AuditEvent(this, "NoSuchFile.java",
            checkstyleMessageWith("First second third 42 rest", FileLengthCheck.class)));
    }

    @Test(expected = ReportingException.class)
    public void throwExceptionWhenReportingErrorForUnsupportedMetric() throws Exception {
        auditListener.addError(new AuditEvent(this, testFile.getAbsolutePath(),
            checkstyleMessageWith("First second third 42 rest", CheckstyleAdapterAuditListenerTest.class)));
    }

    private LocalizedMessage checkstyleMessageWith(String message, Class<?> eventClass) {
        return new LocalizedMessage(0, 0, null, null, null, SeverityLevel.WARNING, null, eventClass, message);
    }
}
@@ -76,16 +76,105 @@
 eCache;%0A
+import com.phsshp.metrics.model.Measurement;%0Aimport com.phsshp.metrics.model.MetricType;%0A
 import c
@@ -333,196 +333,8 @@
 nt;%0A
-import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;%0Aimport com.puppycrawl.tools.checkstyle.api.SeverityLevel;%0Aimport com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck;%0A
 impo
@@ -588,16 +588,96 @@
 ontains;
+%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when;
 %0A%0Apublic
@@ -887,20 +887,21 @@
 erter = 
-new 
+mock(
 AuditEve
@@ -924,17 +924,22 @@
 onverter
-(
+.class
 );%0A    p
@@ -1350,39 +1350,35 @@
         
-a
+A
 udit
-Listener.addError(
+Event event = 
 new Audi
@@ -1409,32 +1409,39 @@
 tAbsolutePath(),
+ null);
 %0A            che
@@ -1437,87 +1437,141 @@
     
-    checkstyleMessageWith(%22First second third 42 rest%22, FileLengthCheck.class))
+when(auditEventConverter.convert(event)).thenReturn(new Measurement(MetricType.FILE_SIZE, 42));%0A%0A        auditListener.addError(event
 );%0A%0A
@@ -1955,95 +1955,13 @@
 va%22,
-%0A            checkstyleMessageWith(%22First second third 42 rest%22, FileLengthCheck.class)
+ null
 ));%0A
@@ -2112,39 +2112,35 @@
         
-a
+A
 udit
-Listener.addError(
+Event event = 
 new Audi
@@ -2179,16 +2179,23 @@
 ePath(),
+ null);
 %0A       
@@ -2199,314 +2199,129 @@
     
-    checkstyleMessageWith(%22First second third 42 rest%22, CheckstyleAdapterAuditListenerTest.class)));%0A    %7D%0A%0A    private LocalizedMessage checkstyleMessageWith(String message, Class%3C?%3E eventClass) %7B%0A        return new LocalizedMessage(0, 0, null, null, null, SeverityLevel.WARNING, null, eventClass, message
+when(auditEventConverter.convert(event)).thenThrow(new IllegalArgumentException());%0A%0A        auditListener.addError(event
 );%0A 
@@ -2325,9 +2325,10 @@
 ;%0A    %7D%0A
+%0A
 %7D

Refactor test to mock converter