-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file output parameters to Maven plugin (#502)
Co-authored-by: Jochen Schalanda <[email protected]> Closes #501
- Loading branch information
Showing
11 changed files
with
282 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
core/src/main/java/org/openapitools/openapidiff/core/utils/FileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.openapitools.openapidiff.core.utils; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.openapitools.openapidiff.core.model.ChangedOpenApi; | ||
import org.openapitools.openapidiff.core.output.Render; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public final class FileUtils { | ||
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class); | ||
|
||
private FileUtils() { | ||
throw new UnsupportedOperationException("Utility class. Do not instantiate"); | ||
} | ||
|
||
public static void writeToFile( | ||
final Render render, final ChangedOpenApi diff, final String fileName) { | ||
if (fileName == null || fileName.isEmpty()) { | ||
logger.debug("File name cannot be null or empty."); | ||
return; | ||
} | ||
|
||
final Path filePath = Paths.get(fileName); | ||
try (final FileOutputStream outputStream = new FileOutputStream(filePath.toFile()); | ||
final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream)) { | ||
render.render(diff, outputStreamWriter); | ||
} catch (final IOException e) { | ||
logger.error("Exception while writing to file {}", fileName, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>[%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="debug"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
</configuration> |
58 changes: 58 additions & 0 deletions
58
core/src/test/java/org/openapitools/openapidiff/core/utils/FileUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.openapitools.openapidiff.core.utils; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.openapitools.openapidiff.core.model.ChangedOpenApi; | ||
import org.openapitools.openapidiff.core.output.ConsoleRender; | ||
|
||
class FileUtilsTest { | ||
private ChangedOpenApi changedOpenApi; | ||
|
||
@BeforeEach | ||
void setup() { | ||
changedOpenApi = new ChangedOpenApi(); | ||
changedOpenApi.setChangedSchemas(Collections.emptyList()); | ||
changedOpenApi.setChangedOperations(Collections.emptyList()); | ||
changedOpenApi.setNewEndpoints(Collections.emptyList()); | ||
changedOpenApi.setMissingEndpoints(Collections.emptyList()); | ||
} | ||
|
||
@Test | ||
void writeToFile_filenameIsNull_doesNothing() { | ||
assertDoesNotThrow(() -> FileUtils.writeToFile(new ConsoleRender(), changedOpenApi, null)); | ||
} | ||
|
||
@Test | ||
void writeToFile_filenameIsEmpty_doesNothing() { | ||
assertDoesNotThrow( | ||
() -> FileUtils.writeToFile(new ConsoleRender(), changedOpenApi, StringUtils.EMPTY)); | ||
} | ||
|
||
@Test | ||
void writeToFile_fileExists_overwrites_file(@TempDir Path tempDir) throws IOException { | ||
final Path path = tempDir.resolve("output.txt"); | ||
Files.write(path, "Test".getBytes(StandardCharsets.UTF_8)); | ||
|
||
assertDoesNotThrow( | ||
() -> FileUtils.writeToFile(new ConsoleRender(), changedOpenApi, path.toString())); | ||
assertThat(path).exists().content().isNotEqualTo("Test"); | ||
} | ||
|
||
@Test | ||
void writeToFile_fileDoesNotExist_createsFile(@TempDir Path tempDir) { | ||
final Path path = tempDir.resolve("output.txt"); | ||
assertDoesNotThrow( | ||
() -> FileUtils.writeToFile(new ConsoleRender(), changedOpenApi, path.toString())); | ||
assertThat(path).exists().content().isNotBlank(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.