Skip to content

Commit 2bd7351

Browse files
authored
Add file output parameters to Maven plugin (#502)
Co-authored-by: Jochen Schalanda <[email protected]> Closes #501
1 parent 85a5265 commit 2bd7351

File tree

11 files changed

+282
-30
lines changed

11 files changed

+282
-30
lines changed

README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ Add openapi-diff to your POM to show diffs when you test your Maven project. You
145145
<failOnIncompatible>true</failOnIncompatible>
146146
<!-- Fail if API changed (default: false) -->
147147
<failOnChanged>true</failOnChanged>
148+
<!-- Supply file path for console output to file if desired. -->
149+
<consoleOutputFileName>${project.basedir}/../maven/target/diff.txt</consoleOutputFileName>
150+
<!-- Supply file path for json output to file if desired. -->
151+
<jsonOutputFileName>${project.basedir}/../maven/target/diff.json</jsonOutputFileName>
152+
<!-- Supply file path for markdown output to file if desired. -->
153+
<markdownOutputFileName>${project.basedir}/../maven/target/diff.md</markdownOutputFileName>
148154
</configuration>
149155
</execution>
150156
</executions>
@@ -167,6 +173,7 @@ public class Main {
167173
```
168174
169175
### Render difference
176+
170177
---
171178
#### HTML
172179
@@ -176,11 +183,9 @@ String html = new HtmlRender("Changelog",
176183
.render(diff);
177184
178185
try {
179-
FileWriter fw = new FileWriter(
180-
"testNewApi.html");
186+
FileWriter fw = new FileWriter("testNewApi.html");
181187
fw.write(html);
182188
fw.close();
183-
184189
} catch (IOException e) {
185190
e.printStackTrace();
186191
}
@@ -191,11 +196,9 @@ try {
191196
```java
192197
String render = new MarkdownRender().render(diff);
193198
try {
194-
FileWriter fw = new FileWriter(
195-
"testDiff.md");
199+
FileWriter fw = new FileWriter("testDiff.md");
196200
fw.write(render);
197201
fw.close();
198-
199202
} catch (IOException e) {
200203
e.printStackTrace();
201204
}
@@ -207,11 +210,9 @@ try {
207210
```java
208211
String render = new JsonRender().render(diff);
209212
try {
210-
FileWriter fw = new FileWriter(
211-
"testDiff.json");
213+
FileWriter fw = new FileWriter("testDiff.json");
212214
fw.write(render);
213215
fw.close();
214-
215216
} catch (IOException e) {
216217
e.printStackTrace();
217218
}

core/src/main/java/org/openapitools/openapidiff/core/utils/ChangedUtils.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
public class ChangedUtils {
77

8-
private ChangedUtils() {}
8+
private ChangedUtils() {
9+
throw new UnsupportedOperationException("Utility class. Do not instantiate");
10+
}
911

1012
public static boolean isUnchanged(Changed changed) {
1113
return changed == null || changed.isUnchanged();

core/src/main/java/org/openapitools/openapidiff/core/utils/Copy.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
public class Copy {
77

8-
private Copy() {}
8+
private Copy() {
9+
throw new UnsupportedOperationException("Utility class. Do not instantiate");
10+
}
911

1012
public static <K, V> Map<K, V> copyMap(Map<K, V> map) {
1113
if (map == null) {

core/src/main/java/org/openapitools/openapidiff/core/utils/EndpointUtils.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
public class EndpointUtils {
1212

13-
private EndpointUtils() {}
13+
private EndpointUtils() {
14+
throw new UnsupportedOperationException("Utility class. Do not instantiate");
15+
}
1416

1517
public static Collection<Endpoint> convert2Endpoints(
1618
String pathUrl, Map<PathItem.HttpMethod, Operation> map) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.openapitools.openapidiff.core.utils;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
import java.io.OutputStreamWriter;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
9+
import org.openapitools.openapidiff.core.output.Render;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public final class FileUtils {
14+
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
15+
16+
private FileUtils() {
17+
throw new UnsupportedOperationException("Utility class. Do not instantiate");
18+
}
19+
20+
public static void writeToFile(
21+
final Render render, final ChangedOpenApi diff, final String fileName) {
22+
if (fileName == null || fileName.isEmpty()) {
23+
logger.debug("File name cannot be null or empty.");
24+
return;
25+
}
26+
27+
final Path filePath = Paths.get(fileName);
28+
try (final FileOutputStream outputStream = new FileOutputStream(filePath.toFile());
29+
final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream)) {
30+
render.render(diff, outputStreamWriter);
31+
} catch (final IOException e) {
32+
logger.error("Exception while writing to file {}", fileName, e);
33+
}
34+
}
35+
}

core/src/main/resources/logback.xml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>[%thread] %-5level %logger{36} - %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="debug">
9+
<appender-ref ref="STDOUT"/>
10+
</root>
11+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.openapitools.openapidiff.core.utils;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
5+
6+
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.util.Collections;
11+
import org.apache.commons.lang3.StringUtils;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.io.TempDir;
15+
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
16+
import org.openapitools.openapidiff.core.output.ConsoleRender;
17+
18+
class FileUtilsTest {
19+
private ChangedOpenApi changedOpenApi;
20+
21+
@BeforeEach
22+
void setup() {
23+
changedOpenApi = new ChangedOpenApi();
24+
changedOpenApi.setChangedSchemas(Collections.emptyList());
25+
changedOpenApi.setChangedOperations(Collections.emptyList());
26+
changedOpenApi.setNewEndpoints(Collections.emptyList());
27+
changedOpenApi.setMissingEndpoints(Collections.emptyList());
28+
}
29+
30+
@Test
31+
void writeToFile_filenameIsNull_doesNothing() {
32+
assertDoesNotThrow(() -> FileUtils.writeToFile(new ConsoleRender(), changedOpenApi, null));
33+
}
34+
35+
@Test
36+
void writeToFile_filenameIsEmpty_doesNothing() {
37+
assertDoesNotThrow(
38+
() -> FileUtils.writeToFile(new ConsoleRender(), changedOpenApi, StringUtils.EMPTY));
39+
}
40+
41+
@Test
42+
void writeToFile_fileExists_overwrites_file(@TempDir Path tempDir) throws IOException {
43+
final Path path = tempDir.resolve("output.txt");
44+
Files.write(path, "Test".getBytes(StandardCharsets.UTF_8));
45+
46+
assertDoesNotThrow(
47+
() -> FileUtils.writeToFile(new ConsoleRender(), changedOpenApi, path.toString()));
48+
assertThat(path).exists().content().isNotEqualTo("Test");
49+
}
50+
51+
@Test
52+
void writeToFile_fileDoesNotExist_createsFile(@TempDir Path tempDir) {
53+
final Path path = tempDir.resolve("output.txt");
54+
assertDoesNotThrow(
55+
() -> FileUtils.writeToFile(new ConsoleRender(), changedOpenApi, path.toString()));
56+
assertThat(path).exists().content().isNotBlank();
57+
}
58+
}

maven-example/pom.xml

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<oldSpec>${project.basedir}/../maven/src/test/resources/oldspec.yaml</oldSpec>
3434
<newSpec>${project.basedir}/../maven/src/test/resources/newspec.yaml</newSpec>
3535
<failOnIncompatible>true</failOnIncompatible>
36+
<consoleOutputFileName>${project.basedir}/../maven/target/diff.txt</consoleOutputFileName>
37+
<jsonOutputFileName>${project.basedir}/../maven/target/diff.json</jsonOutputFileName>
38+
<markdownOutputFileName>${project.basedir}/../maven/target/diff.md</markdownOutputFileName>
3639
</configuration>
3740
</execution>
3841
</executions>

maven/src/main/java/org/openapitools/openapidiff/maven/OpenApiDiffMojo.java

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package org.openapitools.openapidiff.maven;
22

3+
import static org.openapitools.openapidiff.core.utils.FileUtils.writeToFile;
4+
35
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
47
import java.io.OutputStreamWriter;
8+
import java.io.UncheckedIOException;
59
import org.apache.maven.plugin.AbstractMojo;
610
import org.apache.maven.plugin.MojoExecutionException;
711
import org.apache.maven.plugin.MojoFailureException;
@@ -11,10 +15,13 @@
1115
import org.openapitools.openapidiff.core.OpenApiCompare;
1216
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
1317
import org.openapitools.openapidiff.core.output.ConsoleRender;
18+
import org.openapitools.openapidiff.core.output.JsonRender;
19+
import org.openapitools.openapidiff.core.output.MarkdownRender;
1420

1521
/** A Maven Mojo that diffs two OpenAPI specifications and reports on differences. */
1622
@Mojo(name = "diff", defaultPhase = LifecyclePhase.TEST)
1723
public class OpenApiDiffMojo extends AbstractMojo {
24+
1825
@Parameter(property = "oldSpec")
1926
String oldSpec;
2027

@@ -30,6 +37,15 @@ public class OpenApiDiffMojo extends AbstractMojo {
3037
@Parameter(property = "skip", defaultValue = "false")
3138
Boolean skip = false;
3239

40+
@Parameter(property = "consoleOutputFileName")
41+
String consoleOutputFileName;
42+
43+
@Parameter(property = "jsonOutputFileName")
44+
String jsonOutputFileName;
45+
46+
@Parameter(property = "markdownOutputFileName")
47+
String markdownOutputFileName;
48+
3349
@Override
3450
public void execute() throws MojoExecutionException, MojoFailureException {
3551
if (Boolean.TRUE.equals(skip)) {
@@ -39,10 +55,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
3955

4056
try {
4157
final ChangedOpenApi diff = OpenApiCompare.fromLocations(oldSpec, newSpec);
42-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
43-
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
44-
new ConsoleRender().render(diff, outputStreamWriter);
45-
getLog().info(outputStream.toString());
58+
59+
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
60+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream)) {
61+
new ConsoleRender().render(diff, outputStreamWriter);
62+
getLog().info(outputStream.toString());
63+
} catch (IOException e) {
64+
throw new UncheckedIOException(e);
65+
}
66+
67+
writeDiffAsTextToFile(diff);
68+
writeDiffAsJsonToFile(diff);
69+
writeDiffAsMarkdownToFile(diff);
4670

4771
if (failOnIncompatible && diff.isIncompatible()) {
4872
throw new BackwardIncompatibilityException("The API changes broke backward compatibility");
@@ -55,4 +79,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
5579
throw new MojoExecutionException("Unexpected error", e);
5680
}
5781
}
82+
83+
private void writeDiffAsTextToFile(final ChangedOpenApi diff) {
84+
writeToFile(new ConsoleRender(), diff, consoleOutputFileName);
85+
}
86+
87+
private void writeDiffAsJsonToFile(final ChangedOpenApi diff) {
88+
writeToFile(new JsonRender(), diff, jsonOutputFileName);
89+
}
90+
91+
private void writeDiffAsMarkdownToFile(final ChangedOpenApi diff) {
92+
writeToFile(new MarkdownRender(), diff, markdownOutputFileName);
93+
}
5894
}

0 commit comments

Comments
 (0)