From c564c740bf2271ff434e84f7df66acfe7cce69dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1ncsodi=20M=C3=A1rk?= Date: Tue, 17 Sep 2019 09:32:54 +0200 Subject: [PATCH] feat(SwaggerDiff): added swagger v2 json string support, and tests test(SwaggerDiffTest): test for new raw swagger json compare --- .../deepoove/swagger/diff/SwaggerDiff.java | 26 ++++++++ .../swagger/test/SwaggerDiffTest.java | 62 ++++++++++++++++--- 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/deepoove/swagger/diff/SwaggerDiff.java b/src/main/java/com/deepoove/swagger/diff/SwaggerDiff.java index e41a5071..1742d608 100644 --- a/src/main/java/com/deepoove/swagger/diff/SwaggerDiff.java +++ b/src/main/java/com/deepoove/swagger/diff/SwaggerDiff.java @@ -54,11 +54,37 @@ public static SwaggerDiff compareV2(String oldSpec, String newSpec) { } + /** + * compare two swagger v2.0 doc + * + * @param oldSpec old api-doc json as string + * @param newSpec new api-doc json as string + */ + public static SwaggerDiff compareV2Raw(String oldSpec, String newSpec) { + return new SwaggerDiff(oldSpec, newSpec).compare(); + } + + public static SwaggerDiff compare(String oldSpec, String newSpec, List auths, String version) { return new SwaggerDiff(oldSpec, newSpec, auths, version).compare(); } + + /** + * @param rawOldSpec + * @param rawNewSpec + */ + private SwaggerDiff(String rawOldSpec, String rawNewSpec) { + SwaggerParser swaggerParser = new SwaggerParser(); + oldSpecSwagger = swaggerParser.parse(rawOldSpec); + newSpecSwagger = swaggerParser.parse(rawNewSpec); + + if (null == oldSpecSwagger || null == newSpecSwagger) { + throw new RuntimeException("cannot read api-doc from spec."); + } + } + /** * @param oldSpec * @param newSpec diff --git a/src/test/java/com/deepoove/swagger/test/SwaggerDiffTest.java b/src/test/java/com/deepoove/swagger/test/SwaggerDiffTest.java index 7dcd7999..72454b9a 100644 --- a/src/test/java/com/deepoove/swagger/test/SwaggerDiffTest.java +++ b/src/test/java/com/deepoove/swagger/test/SwaggerDiffTest.java @@ -1,13 +1,5 @@ package com.deepoove.swagger.test; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; - -import org.junit.Assert; -import org.junit.Test; - import com.deepoove.swagger.diff.SwaggerDiff; import com.deepoove.swagger.diff.model.ChangedEndpoint; import com.deepoove.swagger.diff.model.Endpoint; @@ -15,6 +7,12 @@ import com.deepoove.swagger.diff.output.MarkdownRender; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Assert; +import org.junit.Test; + +import java.io.*; +import java.util.List; +import java.util.stream.Collectors; public class SwaggerDiffTest { @@ -86,7 +84,7 @@ public void testDiff() { String html = new HtmlRender("Changelog", "http://deepoove.com/swagger-diff/stylesheets/demo.css") .render(diff); - + try { FileWriter fw = new FileWriter( "testDiff.html"); @@ -116,6 +114,41 @@ public void testDiffAndMarkdown() { } + @Test + public void testEqualRaw() throws IOException { + String rawJson = load(SWAGGER_V2_DOC2); + + SwaggerDiff diff = SwaggerDiff.compareV2Raw(rawJson, rawJson); + assertEqual(diff); + } + + + @Test + public void testNewApiRaw() throws IOException { + SwaggerDiff diff = SwaggerDiff.compareV2Raw(load(SWAGGER_V2_EMPTY_DOC), load(SWAGGER_V2_DOC2)); + + List newEndpoints = diff.getNewEndpoints(); + List missingEndpoints = diff.getMissingEndpoints(); + List changedEndPoints = diff.getChangedEndpoints(); + + Assert.assertTrue(newEndpoints.size() > 0); + Assert.assertTrue(missingEndpoints.isEmpty()); + Assert.assertTrue(changedEndPoints.isEmpty()); + + } + + @Test + public void testDeprecatedApiRaw() throws IOException { + SwaggerDiff diff = SwaggerDiff.compareV2Raw(load(SWAGGER_V2_DOC1), load(SWAGGER_V2_EMPTY_DOC)); + List newEndpoints = diff.getNewEndpoints(); + List missingEndpoints = diff.getMissingEndpoints(); + List changedEndPoints = diff.getChangedEndpoints(); + + Assert.assertTrue(newEndpoints.isEmpty()); + Assert.assertTrue(missingEndpoints.size() > 0); + Assert.assertTrue(changedEndPoints.isEmpty()); + } + @Test public void testEqualJson() { try { @@ -129,6 +162,17 @@ public void testEqualJson() { } + private String load(String location) throws IOException { + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + try (InputStream is = classLoader.getResourceAsStream(location)) { + if (is == null) return null; + try (InputStreamReader isr = new InputStreamReader(is); + BufferedReader reader = new BufferedReader(isr)) { + return reader.lines().collect(Collectors.joining(System.lineSeparator())); + } + } + } + private void assertEqual(SwaggerDiff diff) { List newEndpoints = diff.getNewEndpoints(); List missingEndpoints = diff.getMissingEndpoints();