This is an automated email from the ASF dual-hosted git repository. ningjiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-toolkit.git
commit d48cd0bbb12f4efc5074261ecec601a4979235ac Author: kakulisen <[email protected]> AuthorDate: Tue May 28 12:03:37 2019 +0800 add unit test code Signed-off-by: kakulisen <[email protected]> --- common/pom.xml | 7 ++ .../toolkit/common/TextCompareTest.java | 116 +++++++++++++++++++++ .../src/test/resources/compare/HelloEndPoint.yaml | 75 +++++++++++++ .../src/test/resources/compare/HelloEndPoint2.yaml | 75 +++++++++++++ .../src/test/java/docgen/DocGeneratorTest.java | 96 +++++++++++++++++ .../src/test/resources/HelloEndPoint.yaml | 38 +++++++ toolkit-maven-plugin/pom.xml | 36 +++++++ .../java/plugin/GenerateContractsDocMojoTest.java | 53 ++++++++++ .../java/plugin/GenerateContractsMojoTest.java | 68 ++++++++++++ .../projects/project-generateContracts/pom.xml | 46 ++++++++ .../projects/project-generateContractsDoc/pom.xml | 46 ++++++++ 11 files changed, 656 insertions(+) diff --git a/common/pom.xml b/common/pom.xml index 3324cb2..9a718c0 100755 --- a/common/pom.xml +++ b/common/pom.xml @@ -71,6 +71,13 @@ <version>4.12</version> <scope>test</scope> </dependency> + + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>2.25.0</version> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/common/src/test/java/org/apache/servicecomb/toolkit/common/TextCompareTest.java b/common/src/test/java/org/apache/servicecomb/toolkit/common/TextCompareTest.java new file mode 100644 index 0000000..14df512 --- /dev/null +++ b/common/src/test/java/org/apache/servicecomb/toolkit/common/TextCompareTest.java @@ -0,0 +1,116 @@ +package org.apache.servicecomb.toolkit.common; + +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; +import java.util.regex.Pattern; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.*; + +public class TextCompareTest { + + Path sourcePath = Paths.get("./src/test/resources/compare/HelloEndPoint.yaml"); + Path destPath = Paths.get("./src/test/resources/compare/HelloEndPoint2.yaml"); + + @Test + public void contractCompareText() throws IOException { + ContractComparator contractComparator = new ContractComparator(new String(Files.readAllBytes(sourcePath)), + new String(Files.readAllBytes(destPath))); + assertEquals(MyersAlgorithm.class, contractComparator.getAlgorithm().getClass()); + + List<Comparison> comparisonList = contractComparator.compare(); + + assertEquals(3, comparisonList.size()); + } + + @Test + public void contractCompareResultPrint() throws IOException { + ContractComparator contractComparator = new ContractComparator(new String(Files.readAllBytes(sourcePath)), + new String(Files.readAllBytes(destPath))); + assertEquals(MyersAlgorithm.class, contractComparator.getAlgorithm().getClass()); + + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + contractComparator.splitPrint(bout); + + assertTrue(0 < bout.toByteArray().length); + } + + @Test + public void contractCompareAnotherAlgorithm() throws IOException { + + CompareAlgorithm oneLineAlgorithm = mock(CompareAlgorithm.class); + when(oneLineAlgorithm.compare(anyString(), anyString())).then(new Answer<Object>() { + @Override + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + Object[] args = invocationOnMock.getArguments(); + String source = (String) args[0]; + String dest = (String) args[1]; + if ((source == null || "".equals(source.trim())) && dest != null) { + Comparison comparison = new Comparison(ComparisionType.INSERT, 0, 0, 1, 0); + return Collections.singletonList(comparison); + } + + if (source != null && (dest == null || "".equals(dest.trim()))) { + Comparison comparison = new Comparison(ComparisionType.DELETE, 0, 0, 0, 0); + return Collections.singletonList(comparison); + } + + if (hasNewLine(source) || hasNewLine(dest)) { + return null; + } + + if ((dest).equals(source)) { + return Collections.singletonList(new Comparison(ComparisionType.EQUAL, 0, 0, 0, 0)); + } + Comparison comparison = new Comparison(ComparisionType.REPLACE, 0, 1, 0, 1); + return Collections.singletonList(comparison); + } + }); + + ContractComparator contractComparator = new ContractComparator("source line", + "destination line", oneLineAlgorithm); + + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + contractComparator.splitPrint(bout); + bout.flush(); + assertTrue(0 < bout.toByteArray().length); + + bout = new ByteArrayOutputStream(); + contractComparator = new ContractComparator("source line", + "source line", oneLineAlgorithm); + contractComparator.splitPrint(bout); + assertEquals(ComparisionType.EQUAL , contractComparator.compare().get(0).type); + + } + + private boolean hasNewLine(String s) { + return Pattern.compile("\\r?\\n").matcher(s).find(); + } + + @Test + public void contractCompareException() throws IOException { + + try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) { + + ContractComparator contractComparator = new ContractComparator(null, + new String(Files.readAllBytes(destPath))); + assertEquals(MyersAlgorithm.class, contractComparator.getAlgorithm().getClass()); + contractComparator.splitPrint(bout); + + } catch (RuntimeException e) { + assertEquals("source must not be null", e.getMessage()); + } + + } +} diff --git a/common/src/test/resources/compare/HelloEndPoint.yaml b/common/src/test/resources/compare/HelloEndPoint.yaml new file mode 100644 index 0000000..c67fb51 --- /dev/null +++ b/common/src/test/resources/compare/HelloEndPoint.yaml @@ -0,0 +1,75 @@ + +--- +swagger: "2.0" +info: + version: "1.0.0" + title: "swagger definition for org.apache.servicecomb.samples.bmi.HelloEndPoint" + x-java-interface: "gen.swagger.HelloEndPointIntf" +basePath: "/hello" +consumes: +- "application/json" +produces: +- "application/json" +paths: + /sayHello: + get: + operationId: "sayHello" + parameters: + - name: "name" + in: "query" + required: false + type: "string" + - name: "result" + in: "query" + required: false + type: "number" + format: "double" + - name: "instanceId" + in: "query" + required: false + type: "string" + - name: "callTime" + in: "query" + required: false + type: "string" + responses: + 200: + description: "response of 200" + schema: + type: "string" + /sayHi: + get: + operationId: "sayHi" + parameters: + - name: "name" + in: "query" + required: false + type: "string" + - name: "Authorization" + in: "header" + description: "aa" + required: true + type: "string" + responses: + 200: + description: "response of 200" + schema: + type: "string" + /sayNo: + get: + operationId: "sayNo" + parameters: + - name: "name" + in: "query" + required: false + type: "string" + - name: "Authorization" + in: "header" + description: "aa" + required: true + type: "string" + responses: + 200: + description: "response of 200" + schema: + type: "string" \ No newline at end of file diff --git a/common/src/test/resources/compare/HelloEndPoint2.yaml b/common/src/test/resources/compare/HelloEndPoint2.yaml new file mode 100644 index 0000000..be821f5 --- /dev/null +++ b/common/src/test/resources/compare/HelloEndPoint2.yaml @@ -0,0 +1,75 @@ +--- +aaswagfger: "2.066" +ccc +info: + version: "1.0.02" + title: "swagger definition for org.apache.servicecomb.samples.bmi.HelloEndPoint" + x-java-interface: "gen.swagger.HelloEndPointIntf" +basePath: "/hello" +consumes: +- "application/json" +produces: +- "application/json" +paths: + /sayHello: + get: + operationId: "sayHello" + parameters: + - name: "name" + in: "query" + required: false + type: "string" + - name: "result" + in: "query" + required: false + type: "number" + format: "double" + - name: "instanceId" + in: "query" + required: false + type: "string" + - name: "callTime" + in: "query" + required: false + type: "string" + responses: + 200: + description: "response of 200" + schema: + type: "string" + /sayHi: + get: + operationId: "sayHi" + parameters: + - name: "name" + in: "query" + required: false + type: "string" + - name: "Authorization" + in: "header" + description: "aa" + required: true + type: "string" + responses: + 200: + description: "response of 200" + schema: + type: "string" + /sayNo: + get: + operationId: "sayNo" + parameters: + - name: "name" + in: "query" + required: false + type: "string" + - name: "Authorization" + in: "header" + description: "aa" + required: true + type: "string" + responses: + 200: + description: "response of 200" + schema: + type: "string" \ No newline at end of file diff --git a/doc-generator/src/test/java/docgen/DocGeneratorTest.java b/doc-generator/src/test/java/docgen/DocGeneratorTest.java new file mode 100644 index 0000000..0ca5bc9 --- /dev/null +++ b/doc-generator/src/test/java/docgen/DocGeneratorTest.java @@ -0,0 +1,96 @@ +package docgen; + +import io.swagger.models.Swagger; +import io.swagger.parser.Swagger20Parser; +import org.apache.commons.io.FileUtils; +import org.apache.servicecomb.toolkit.docgen.DocGeneratorManager; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class DocGeneratorTest { + + + @Test + public void contractToAsciidoc() throws IOException { + + Swagger20Parser swagger20Parser = new Swagger20Parser(); + InputStream in = DocGeneratorTest.class.getClassLoader().getResourceAsStream("HelloEndPoint.yaml"); + + StringBuilder sb = new StringBuilder(); + byte[] bytes = new byte[1024]; + int len = -1; + while ((len = in.read(bytes)) != -1){ + sb.append(new String(bytes,0,len)); + } + + Swagger swagger = swagger20Parser.parse(sb.toString()); + Path tempDir = Files.createTempDirectory(null); + Path outputPath = Paths.get(tempDir.toFile().getAbsolutePath() + + File.separator + "asciidoc.html"); + + DocGeneratorManager.generate(swagger,outputPath.toFile().getCanonicalPath() , "asciidoc-html"); + + Assert.assertTrue(Files.exists(outputPath)); + FileUtils.deleteDirectory(tempDir.toFile()); + } + + @Test + public void contractTransferToSwaggerUI() throws IOException { + + Swagger20Parser swagger20Parser = new Swagger20Parser(); + + InputStream in = DocGeneratorTest.class.getClassLoader().getResourceAsStream("HelloEndPoint.yaml"); + + StringBuilder sb = new StringBuilder(); + byte[] bytes = new byte[1024]; + int len = -1; + while ((len = in.read(bytes)) != -1) { + sb.append(new String(bytes, 0, len)); + } + + Swagger swagger = swagger20Parser.parse(sb.toString()); + + Path tempDir = Files.createTempDirectory(null); + Path outputPath = Paths.get(tempDir.toFile().getAbsolutePath() + + File.separator + "swagger-ui.html"); + DocGeneratorManager.generate(swagger,outputPath.toFile().getCanonicalPath() , "swagger-ui"); + + Assert.assertTrue(Files.exists(outputPath)); + FileUtils.deleteDirectory(tempDir.toFile()); + } + + @Test + public void contractTransferToOther() throws IOException { + + Swagger20Parser swagger20Parser = new Swagger20Parser(); + + InputStream in = DocGeneratorTest.class.getClassLoader().getResourceAsStream("HelloEndPoint.yaml"); + + StringBuilder sb = new StringBuilder(); + byte[] bytes = new byte[1024]; + int len = -1; + while ((len = in.read(bytes)) != -1) { + sb.append(new String(bytes, 0, len)); + } + + Swagger swagger = swagger20Parser.parse(sb.toString()); + + Path tempDir = Files.createTempDirectory(null); + Path outputPath = Paths.get(tempDir.toFile().getAbsolutePath() + + File.separator + "swagger-ui.html"); + + DocGeneratorManager.generate(swagger, outputPath.toFile().getCanonicalPath(), "other"); + + Assert.assertFalse(Files.exists(outputPath)); + + FileUtils.deleteDirectory(tempDir.toFile()); + } + +} diff --git a/doc-generator/src/test/resources/HelloEndPoint.yaml b/doc-generator/src/test/resources/HelloEndPoint.yaml new file mode 100644 index 0000000..a42df27 --- /dev/null +++ b/doc-generator/src/test/resources/HelloEndPoint.yaml @@ -0,0 +1,38 @@ +--- +swagger: "2.0" +info: + version: "1.0.0" + title: "swagger definition for org.apache.servicecomb.samples.bmi.HelloEndPoint" + x-java-interface: "gen.swagger.HelloEndPointIntf" +basePath: "/hello" +consumes: +- "application/json" +produces: +- "application/json" +paths: + /sayHello: + get: + operationId: "sayHello" + parameters: + - name: "name" + in: "query" + required: false + type: "string" + responses: + 200: + description: "response of 200" + schema: + type: "string" + /sayHi: + get: + operationId: "sayHi" + parameters: + - name: "name" + in: "query" + required: false + type: "string" + responses: + 200: + description: "response of 200" + schema: + type: "string" diff --git a/toolkit-maven-plugin/pom.xml b/toolkit-maven-plugin/pom.xml index 9574db1..5bb48ef 100755 --- a/toolkit-maven-plugin/pom.xml +++ b/toolkit-maven-plugin/pom.xml @@ -64,6 +64,42 @@ <artifactId>toolkit-common</artifactId> <version>${project.version}</version> </dependency> + + <!-- for test --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.maven.plugin-testing</groupId> + <artifactId>maven-plugin-testing-harness</artifactId> + <version>3.3.0</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-aether-provider</artifactId> + <version>3.2.2</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-compat</artifactId> + <version>3.2.2</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>2.25.0</version> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java b/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java new file mode 100644 index 0000000..1fa05c4 --- /dev/null +++ b/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java @@ -0,0 +1,53 @@ +package plugin; + +import org.apache.maven.plugin.testing.MojoRule; +import org.apache.maven.plugin.testing.resources.TestResources; +import org.apache.maven.project.MavenProject; +import org.apache.servicecomb.toolkit.plugin.GenerateContractsDocMojo; +import org.junit.Rule; +import org.junit.Test; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; + + +public class GenerateContractsDocMojoTest { + + private static final String PLUGIN_GOAL = "generateDoc"; + + private static final String TEST_PROJECT = "project-generateContractsDoc"; + + @Rule + public MojoRule rule = new MojoRule(); + + @Rule + public TestResources resources = new TestResources(); + + + @Test + public void testGenerateContractsDoc() throws Exception { + File baseDir = this.resources.getBasedir(TEST_PROJECT); + GenerateContractsDocMojo generateContractsDocMojo = mock(GenerateContractsDocMojo.class); + List<String> runtimeUrlPath = new ArrayList<>(); + runtimeUrlPath.add(baseDir + "/target/classes"); + final MavenProject project = mock(MavenProject.class); + given(project.getRuntimeClasspathElements()).willReturn(runtimeUrlPath); + + assertNotNull(generateContractsDocMojo); + rule.setVariableValueToObject(generateContractsDocMojo, "project", project); + rule.setVariableValueToObject(generateContractsDocMojo, "format", ".yaml"); + assertNotNull(this.rule.getVariableValueFromObject(generateContractsDocMojo, "project")); + assertEquals(".yaml", this.rule.getVariableValueFromObject(generateContractsDocMojo, "format")); + rule.executeMojo(project, PLUGIN_GOAL); + generateContractsDocMojo.execute(); + + } + + +} diff --git a/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsMojoTest.java b/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsMojoTest.java new file mode 100644 index 0000000..f69c312 --- /dev/null +++ b/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsMojoTest.java @@ -0,0 +1,68 @@ +package plugin; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.testing.MojoRule; +import org.apache.maven.plugin.testing.resources.TestResources; +import org.apache.maven.project.MavenProject; +import org.apache.servicecomb.toolkit.plugin.GenerateContractsMojo; +import org.junit.Rule; +import org.junit.Test; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; + +public class GenerateContractsMojoTest { + + private static final String PLUGIN_GOAL = "generateContracts"; + + @Rule + public MojoRule rule = new MojoRule(); + + @Rule + public TestResources resources = new TestResources(); + + @Test + public void testGenerateContracts() throws Exception { + executeMojo("project-generateContracts", PLUGIN_GOAL); + } + + protected void executeMojo(String projectName, String goalName) throws Exception { + + File baseDir = this.resources.getBasedir(projectName); + assertNotNull(baseDir); + assertTrue(baseDir.exists()); + assertTrue(baseDir.isDirectory()); + + File pom = new File(baseDir, "pom.xml"); + AbstractMojo generateContractsMojo = (AbstractMojo) this.rule.lookupMojo(goalName, pom); + + assertNotNull(generateContractsMojo); + assertEquals(GenerateContractsMojo.class, generateContractsMojo.getClass()); + + final MavenProject project = mock(MavenProject.class); + given(project.getFile()).willReturn(pom); + List<String> runtimeUrlPath = new ArrayList<>(); + runtimeUrlPath.add(baseDir + "/target/classes"); + given(project.getRuntimeClasspathElements()).willReturn(runtimeUrlPath); + + rule.setVariableValueToObject(generateContractsMojo, "project", project); + assertNotNull(this.rule.getVariableValueFromObject(generateContractsMojo, "project")); + + assertEquals("target/test_output_contracts", this.rule.getVariableValueFromObject(generateContractsMojo, "outputDir")); + assertEquals(".yaml", this.rule.getVariableValueFromObject(generateContractsMojo, "format")); + + generateContractsMojo.execute(); + + assertTrue(new File("target/test_output_contracts").exists()); + } + +} diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContracts/pom.xml b/toolkit-maven-plugin/src/test/projects/project-generateContracts/pom.xml new file mode 100644 index 0000000..9a71eb5 --- /dev/null +++ b/toolkit-maven-plugin/src/test/projects/project-generateContracts/pom.xml @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + Copyright 2014 Linagora, Université Joseph Fourier + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> + +<project + xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <prerequisites> + <maven>3.0.3</maven> + </prerequisites> + + <groupId>org.apache.servicecomb</groupId> + <artifactId>this-is-for-test-only</artifactId> + <version>0.1.0-SNAPSHOT</version> + <name>This is for Test ONLY</name> + <packaging>jar</packaging> + + <build> + <plugins> + <plugin> + <groupId>org.apache.servicecomb</groupId> + <artifactId>toolkit-maven-plugin</artifactId> + <version>${project.version}</version> + <extensions>true</extensions> + <configuration> + <outputDir>target/test_output_contracts</outputDir> + <format>.yaml</format> + </configuration> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml new file mode 100644 index 0000000..2b05bdf --- /dev/null +++ b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + Copyright 2014 Linagora, Université Joseph Fourier + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> + +<project + xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <prerequisites> + <maven>3.0.3</maven> + </prerequisites> + <groupId>org.apache.servicecomb</groupId> + <artifactId>this-is-for-test-only</artifactId> + <version>1.0-SNAPSHOT</version> + <name>This is for Test ONLY</name> + <packaging>jar</packaging> + + <build> + <plugins> + <plugin> + <groupId>org.apache.servicecomb</groupId> + <artifactId>toolkit-maven-plugin</artifactId> + <version>${project.version}</version> + <extensions>true</extensions> + <configuration> + <outputDir>./target/plugin-test-output</outputDir> + <docOutputDir>./target/plugin-test-output</docOutputDir> + <format>.yaml</format> + </configuration> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file
