Skip to content

Commit

Permalink
#1 Ability to use more than one reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
galovics committed Dec 10, 2018
1 parent 593e985 commit 39e3a52
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import io.redskap.swagger.brake.cli.options.CliOptions;
import io.redskap.swagger.brake.runner.Options;
import io.redskap.swagger.brake.runner.OutputFormat;
Expand All @@ -18,17 +21,18 @@
public class OutputFormatHandler implements CliOptionHandler {
@Override
public void handle(String propertyValue, Options options) {
OutputFormat format = OutputFormat.STDOUT;
Set<OutputFormat> formats = ImmutableSet.of(OutputFormat.STDOUT);

if (!StringUtils.isBlank(propertyValue)) {
try {
format = OutputFormat.valueOf(propertyValue.toUpperCase());
String[] formatStrings = propertyValue.split(",");
formats = Arrays.stream(formatStrings).map(String::trim).map(String::toUpperCase).map(OutputFormat::valueOf).collect(toSet());
} catch (IllegalArgumentException e) {
log.debug("Format cannot be resolved", e);
}
}

options.setOutputFormat(format);
options.setOutputFormats(ImmutableSet.copyOf(formats));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.redskap.swagger.brake.report;

import io.redskap.swagger.brake.runner.OutputFormat;

public interface CheckableReporter extends Reporter {
boolean canReport(OutputFormat format);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.redskap.swagger.brake.report;

import java.util.Collection;

import io.redskap.swagger.brake.core.BreakingChange;
import io.redskap.swagger.brake.runner.Options;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class CompositeReporter implements Reporter {
private final Collection<Reporter> delegates;

@Override
public void report(Collection<BreakingChange> breakingChanges, Options options) {
delegates.forEach(d -> d.report(breakingChanges, options));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

@Component
@Slf4j
public class HtmlReporter extends AbstractFileReporter {
public class HtmlReporter extends AbstractFileReporter implements CheckableReporter {
private static final String FILENAME = "swagger-brake.html";
private final JsonConverter jsonConverter;
private final MustacheContentResolver mustacheContentResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@Slf4j
@Component
public class JsonReporter extends AbstractFileReporter {
public class JsonReporter extends AbstractFileReporter implements CheckableReporter {
private static final String FILENAME = "swagger-brake.json";
private final JsonConverter jsonConverter;

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/io/redskap/swagger/brake/report/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

import io.redskap.swagger.brake.core.BreakingChange;
import io.redskap.swagger.brake.runner.Options;
import io.redskap.swagger.brake.runner.OutputFormat;

public interface Reporter {
void report(Collection<BreakingChange> breakingChanges, Options options);

boolean canReport(OutputFormat format);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.redskap.swagger.brake.report;

import java.util.Collection;
import java.util.stream.Collectors;

import io.redskap.swagger.brake.runner.Options;
import io.redskap.swagger.brake.runner.OutputFormat;
Expand All @@ -10,11 +11,14 @@
@Component
@RequiredArgsConstructor
public class ReporterFactory {
private final StdOutReporter stdOutReporter;
private final Collection<Reporter> reporters;
private final Collection<CheckableReporter> reporters;

public Reporter create(Options options) {
OutputFormat outputFormat = options.getOutputFormat();
return reporters.stream().filter(r -> r.canReport(outputFormat)).findAny().orElse(stdOutReporter);
Collection<Reporter> reporters = options.getOutputFormats().stream().map(this::findReporter).collect(Collectors.toList());
return new CompositeReporter(reporters);
}

private Reporter findReporter(OutputFormat outputFormat) {
return reporters.stream().filter(r -> r.canReport(outputFormat)).findAny().orElseThrow(() -> new RuntimeException("Reporter cannot be found for format " + outputFormat));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Component
@Slf4j
public class StdOutReporter implements Reporter {
public class StdOutReporter implements Reporter, CheckableReporter {
@Override
public void report(Collection<BreakingChange> breakingChanges, Options options) {
if (!breakingChanges.isEmpty()) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/redskap/swagger/brake/runner/Options.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package io.redskap.swagger.brake.runner;

import java.util.Collections;
import java.util.Set;

import lombok.Data;

@Data
public class Options {
private String oldApiPath;
private String newApiPath;

private OutputFormat outputFormat;
private Set<OutputFormat> outputFormats = Collections.emptySet();
private String outputFilePath;

private String mavenRepoUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.redskap.swagger.brake.cli.options.handler;

import static com.google.common.collect.ImmutableSet.of;
import static org.assertj.core.api.Assertions.assertThat;

import io.redskap.swagger.brake.runner.Options;
Expand All @@ -16,7 +17,7 @@ public void testHandleShouldSetFormatToStandardOutWhenPropertyValueIsNull() {
// when
underTest.handle(null, options);
// then
assertThat(options.getOutputFormat()).isEqualTo(OutputFormat.STDOUT);
assertThat(options.getOutputFormats()).isEqualTo(of(OutputFormat.STDOUT));
}

@Test
Expand All @@ -26,7 +27,7 @@ public void testHandleShouldSetFormatToStandardOutWhenPropertyValueIsEmpty() {
// when
underTest.handle("", options);
// then
assertThat(options.getOutputFormat()).isEqualTo(OutputFormat.STDOUT);
assertThat(options.getOutputFormats()).isEqualTo(of(OutputFormat.STDOUT));
}

@Test
Expand All @@ -36,7 +37,7 @@ public void testHandleShouldSetFormatToStandardOutWhenPropertyValueIsNotMappable
// when
underTest.handle("something", options);
// then
assertThat(options.getOutputFormat()).isEqualTo(OutputFormat.STDOUT);
assertThat(options.getOutputFormats()).isEqualTo(of(OutputFormat.STDOUT));
}

@Test
Expand All @@ -46,16 +47,37 @@ public void testHandleShouldSetFormatToStandardOutWhenPropertyValueIsStdOut() {
// when
underTest.handle("stdout", options);
// then
assertThat(options.getOutputFormat()).isEqualTo(OutputFormat.STDOUT);
assertThat(options.getOutputFormats()).isEqualTo(of(OutputFormat.STDOUT));
}

@Test
public void testHandleShouldSetFormatToStandardOutWhenPropertyValueIsJson() {
public void testHandleShouldSetFormatToJsonWhenPropertyValueIsJson() {
// given
Options options = new Options();
// when
underTest.handle("json", options);
// then
assertThat(options.getOutputFormat()).isEqualTo(OutputFormat.JSON);
assertThat(options.getOutputFormats()).isEqualTo(of(OutputFormat.JSON));
}

@Test
public void testHandleShouldSetFormatToJsonAndHtmlWhenValueIsJsonCommaHtml() {
// given
Options options = new Options();
// when
underTest.handle("json,html", options);
// then
assertThat(options.getOutputFormats()).isEqualTo(of(OutputFormat.HTML, OutputFormat.JSON));
}


@Test
public void testHandleShouldSetFormatToJsonAndHtmlWhenValueIsJsonCommaHtmlWithSpaces() {
// given
Options options = new Options();
// when
underTest.handle("json, html", options);
// then
assertThat(options.getOutputFormats()).isEqualTo(of(OutputFormat.HTML, OutputFormat.JSON));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;

import com.google.common.collect.ImmutableSet;
import io.redskap.swagger.brake.core.BreakingChange;
import io.redskap.swagger.brake.core.CoreConfiguration;
import io.redskap.swagger.brake.maven.MavenConfiguration;
Expand All @@ -22,7 +23,7 @@ protected Collection<BreakingChange> execute(String oldApiPath, String newApiPat
Options options = new Options();
options.setOldApiPath(oldApiPath);
options.setNewApiPath(newApiPath);
options.setOutputFormat(OutputFormat.STDOUT);
options.setOutputFormats(ImmutableSet.of(OutputFormat.STDOUT));
return underTest.run(options);
}
}

0 comments on commit 39e3a52

Please sign in to comment.