Skip to content

Commit

Permalink
Merge pull request #26 from Syngularity-Infinyt/master
Browse files Browse the repository at this point in the history
feat(SwaggerDiff): added swagger v2 json string support, and tests
  • Loading branch information
Sayi authored Jun 29, 2020
2 parents 8d775c1 + 3cbb2fa commit 109c0ea
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/main/java/com/deepoove/swagger/diff/SwaggerDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthorizationValue> 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
Expand Down
53 changes: 49 additions & 4 deletions src/test/java/com/deepoove/swagger/test/SwaggerDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import org.junit.Assert;
import org.junit.Test;

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.util.List;
import java.util.stream.Collectors;


public class SwaggerDiffTest {

Expand Down Expand Up @@ -86,7 +86,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");
Expand Down Expand Up @@ -116,6 +116,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<Endpoint> newEndpoints = diff.getNewEndpoints();
List<Endpoint> missingEndpoints = diff.getMissingEndpoints();
List<ChangedEndpoint> 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<Endpoint> newEndpoints = diff.getNewEndpoints();
List<Endpoint> missingEndpoints = diff.getMissingEndpoints();
List<ChangedEndpoint> changedEndPoints = diff.getChangedEndpoints();

Assert.assertTrue(newEndpoints.isEmpty());
Assert.assertTrue(missingEndpoints.size() > 0);
Assert.assertTrue(changedEndPoints.isEmpty());
}

@Test
public void testEqualJson() {
try {
Expand All @@ -129,6 +164,16 @@ 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()));
}
}
}

@Test
public void testJsonRender() {
Expand Down

0 comments on commit 109c0ea

Please sign in to comment.