diff options
Diffstat (limited to 'validate')
16 files changed, 953 insertions, 0 deletions
diff --git a/validate/pom.xml b/validate/pom.xml new file mode 100644 index 00000000..2d75b23d --- /dev/null +++ b/validate/pom.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright 2017 Huawei Technologies Co., Ltd. + + 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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onap.cli</groupId> + <artifactId>cli</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>cli-validate</artifactId> + <name>cli/validate</name> + <packaging>pom</packaging> + + <modules> + <module>sample-mock-generator</module> + <module>sample-yaml-generator</module> + <module>validation</module> + </modules> + + +</project> diff --git a/validate/sample-mock-generator/pom.xml b/validate/sample-mock-generator/pom.xml new file mode 100644 index 00000000..c9263aff --- /dev/null +++ b/validate/sample-mock-generator/pom.xml @@ -0,0 +1,48 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright 2017 Huawei Technologies Co., Ltd. + + 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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onap.cli</groupId> + <artifactId>cli</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>cli-sample-mock-generator</artifactId> + <name>cli/validate/sample-mock-generator</name> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <version>2.6.3</version> + </dependency> + </dependencies> + +</project> diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java new file mode 100644 index 00000000..c3006e32 --- /dev/null +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java @@ -0,0 +1,42 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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. + */ +package org.onap.cli.http.mock; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Date; + +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + +public class MockJsonGenerator { + public static void generateMocking(MockRequest mockRequest, MockResponse mockResponse, + String jsonFilePath) throws IOException { + + MockObject mockObject = new MockObject(); + mockObject.setRequest(mockRequest); + mockObject.setResponse(mockResponse); + + ObjectMapper mapper = new ObjectMapper(); + ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); + String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()); + writer.writeValue(new File(jsonFilePath + "-" + timeStamp + "-moco.json"), + Arrays.asList(mockObject)); + } +} diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockObject.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockObject.java new file mode 100644 index 00000000..2d02e85c --- /dev/null +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockObject.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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. + */ + +package org.onap.cli.http.mock; + +public class MockObject { + private MockRequest request; + private MockResponse response; + + public MockRequest getRequest() { + return request; + } + + public void setRequest(MockRequest request) { + this.request = request; + } + + public MockResponse getResponse() { + return response; + } + + public void setResponse(MockResponse response) { + this.response = response; + } + + +} diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java new file mode 100644 index 00000000..cbb2a26a --- /dev/null +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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. + */ + +package org.onap.cli.http.mock; + +import java.io.IOException; +import java.net.URL; +import java.util.Map; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class MockRequest { + private String method; + private String uri; + private Map<String, String> headers; + private JsonNode json; + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } + + public String getUri() { + return uri; + } + + public void setUri(String url) throws IOException { + URL urlt; + urlt = new URL(url); + this.uri = urlt.getPath(); + } + + public Map<String, String> getHeaders() { + return headers; + } + + public void setHeaders(Map<String, String> headers) { + this.headers = headers; + } + + public JsonNode getJson() { + return json; + } + + public void setJson(String json) throws IOException { + if (!json.isEmpty()) { + ObjectMapper objectMapper = new ObjectMapper(); + this.json = objectMapper.readTree(json); + } + + } +} diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java new file mode 100644 index 00000000..2b8fa826 --- /dev/null +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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. + */ + +package org.onap.cli.http.mock; + +import java.io.IOException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class MockResponse { + private int status; + private JsonNode json; + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public JsonNode getJson() { + return json; + } + + public void setJson(String json) throws IOException { + if (!json.isEmpty()) { + ObjectMapper objectMapper = new ObjectMapper(); + this.json = objectMapper.readTree(json); + } + } +} diff --git a/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java b/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java new file mode 100644 index 00000000..3b79057f --- /dev/null +++ b/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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. + */ +package org.onap.cli.http.mock; + +import java.io.IOException; + +import org.junit.Test; + +public class MockJsonGeneratorTest { + + @Test + public void mocoGenerateTest() throws IOException { + MockRequest mockRequest = new MockRequest(); + mockRequest.setJson("{\"value\" : \"234sdf-345\"}"); + mockRequest.setMethod("get"); + mockRequest.setUri("http://1.1.1.1:80/getResource"); + + MockResponse mockResponse = new MockResponse(); + mockResponse.setStatus(200); + mockResponse.setJson("{\"value\" : \"234sdf-345\"}"); + + MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test"); + } + + @Test(expected=IOException.class) + public void mocoGenerateFailedInvalidBodyTest() throws IOException { + MockRequest mockRequest = new MockRequest(); + mockRequest.setJson("{\"value\" : \"234sdf-345\""); + mockRequest.setMethod("get"); + mockRequest.setUri("http://1.1.1.1:80/getResource"); + + MockResponse mockResponse = new MockResponse(); + mockResponse.setStatus(200); + mockResponse.setJson("{\"value\" : \"234sdf-345\""); + + MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test"); + } + + @Test(expected=IOException.class) + public void mocoGenerateFailedInvalidUrlTest() throws IOException { + MockRequest mockRequest = new MockRequest(); + mockRequest.setJson("{\"value\" : \"234sdf-345\""); + mockRequest.setMethod("get"); + mockRequest.setUri("http://1.1.1.1:80:invalid"); + + MockResponse mockResponse = new MockResponse(); + mockResponse.setStatus(200); + mockResponse.setJson("{\"value\" : \"234sdf-345\""); + + MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test"); + } +} diff --git a/validate/sample-yaml-generator/pom.xml b/validate/sample-yaml-generator/pom.xml new file mode 100644 index 00000000..76ff4848 --- /dev/null +++ b/validate/sample-yaml-generator/pom.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright 2017 Huawei Technologies Co., Ltd. + + 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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onap.cli</groupId> + <artifactId>cli</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>cli-sample-yaml-generator</artifactId> + <name>cli/validate/sample-yaml-generator</name> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + </dependencies> + +</project> diff --git a/validate/sample-yaml-generator/src/main/java/org/onap/cli/sample/yaml/SampleYamlGenerator.java b/validate/sample-yaml-generator/src/main/java/org/onap/cli/sample/yaml/SampleYamlGenerator.java new file mode 100644 index 00000000..5c308154 --- /dev/null +++ b/validate/sample-yaml-generator/src/main/java/org/onap/cli/sample/yaml/SampleYamlGenerator.java @@ -0,0 +1,82 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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. + */ + +package org.onap.cli.sample.yaml; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class SampleYamlGenerator { + + static int nTab; + + public static void generateSampleYaml(List<String> input, String ouput, String version, + String targetFolder, boolean debug) throws IOException { + + String cmdName = input.get(0); + + PrintWriter writer = new PrintWriter(targetFolder + "/" + cmdName + "-sample.yaml", "UTF-8"); + writeKeyValuePair(writer, "onap_cli_sample_version", "1.0"); + writeKeyValuePair(writer, "name", cmdName); + writeKeyValuePair(writer, "version", version); + + writeKey(writer, "samples"); + writeKey(writer, "sample1"); + + writeKeyValuePair(writer, "name", cmdName); + writeKeyValuePair(writer, "input", input.stream().skip(1).collect(Collectors.joining(" "))); + writeKeyValuePair(writer, "moco", cmdName + "-sample-yaml.yaml"); + writeMultilineKeyValue(writer, "ouput", ouput, debug); + + writeEndKey(); + writeEndKey(); + + writer.flush(); + writer.close(); + } + + private static void writeMultilineKeyValue(PrintWriter writer, String key, String value, boolean debug) { + writer.write(printTabs() + key + ": |\n"); + nTab++; + String[] lines = value.split("\n"); + long skipLines = debug ? 12 : 0; + Arrays.stream(lines).skip(skipLines ).forEach(line -> writer.write(printTabs() + line + "\n")); + } + + private static String printTabs() { + StringBuffer spaces = new StringBuffer(); + for (int i=0; i < nTab; i++) { + spaces.append(" "); + } + return spaces.toString(); + } + + private static void writeKeyValuePair(PrintWriter writer, String key, String value) { + writer.write(printTabs() +key + ": " + value + "\n"); + } + + private static void writeKey(PrintWriter writer, String key) { + writer.write(printTabs() + key + ":\n"); + nTab++; + } + + private static void writeEndKey() { + nTab--; + } +} diff --git a/validate/sample-yaml-generator/src/test/java/org/onap/cli/sample/yaml/SampleYamlGeneratorTest.java b/validate/sample-yaml-generator/src/test/java/org/onap/cli/sample/yaml/SampleYamlGeneratorTest.java new file mode 100644 index 00000000..c98cb71b --- /dev/null +++ b/validate/sample-yaml-generator/src/test/java/org/onap/cli/sample/yaml/SampleYamlGeneratorTest.java @@ -0,0 +1,32 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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. + */ + +package org.onap.cli.sample.yaml; + +import java.io.IOException; +import java.util.Arrays; + +import org.junit.Test; + +public class SampleYamlGeneratorTest { + + @Test + public void testGenerateSampleYaml() throws IOException { + SampleYamlGenerator.generateSampleYaml(Arrays.asList("testcmd", "-a", "argument"), + "+--------+\n+val +\n+argument+", "test-version-1.0", "target", false); + } + +} diff --git a/validate/validation/pom.xml b/validate/validation/pom.xml new file mode 100644 index 00000000..618074f6 --- /dev/null +++ b/validate/validation/pom.xml @@ -0,0 +1,90 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright 2017 Huawei Technologies Co., Ltd. + + 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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onap.cli</groupId> + <artifactId>cli</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>cli-validation</artifactId> + <name>cli/validation</name> + <packaging>jar</packaging> + <dependencies> + <dependency> + <groupId>com.github.dreamhead</groupId> + <artifactId>moco-runner</artifactId> + <version>0.11.1</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.onap.cli</groupId> + <artifactId>cli-main</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.cli</groupId> + <artifactId>cli-plugins-msb</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.cli</groupId> + <artifactId>cli-plugins-sdc</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.cli</groupId> + <artifactId>cli-plugins-aai</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.cli</groupId> + <artifactId>cli-plugins-so</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> +<!-- <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.9</version> + <configuration> + <useManifestOnlyJar>false</useManifestOnlyJar> + <useSystemClassLoader>false</useSystemClassLoader> + <additionalClasspathElements> + <additionalClasspathElement> + ${project.build.directory}/../../deployment/zip/target/deployunzip/lib/*.jar + </additionalClasspathElement> + </additionalClasspathElements> + </configuration> + </plugin> + </plugins> + </build> --> +</project> diff --git a/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java b/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java new file mode 100644 index 00000000..7f84704c --- /dev/null +++ b/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java @@ -0,0 +1,172 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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. + */ + +package org.onap.cli.moco; + +import static com.github.dreamhead.moco.MocoJsonRunner.jsonHttpServer; +import static com.github.dreamhead.moco.Runner.runner; +import static com.github.dreamhead.moco.Moco.pathResource; +import static com.github.dreamhead.moco.Moco.file; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.onap.cli.fw.OnapCommandRegistrar; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.error.OnapCommandInvalidSample; +import org.onap.cli.fw.utils.OnapCommandUtils; +import org.onap.cli.main.OnapCli; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.Resource; +import org.yaml.snakeyaml.Yaml; + +import com.github.dreamhead.moco.HttpServer; +import com.github.dreamhead.moco.Runner; + +public class OnapCommandHttpMocoServer { + + public static final String SAMPLE_PATTERN = "onap-cli-sample/**/"; + + public static final String SAMPLE_VERSION = "onap_cli_sample_version"; + public static final String SAMPLE_VERSION_1_0 = "1.0"; + + public static final String SAMPLE_COMMAND_NAME = "name"; + public static final String SAMPLE_PRODUCT = "version"; + public static final String SAMPLE_LIST = "samples"; + public static final String SAMPLE_DESCRIPTION = "name"; + public static final String SAMPLE_INPUT = "input"; + public static final String SAMPLE_OUTPUT = "output"; + public static final String SAMPLE_MOCO = "moco"; + + private static Logger LOG = LoggerFactory.getLogger(OnapCommandHttpMocoServer.class); + + private String samplesToTest = "*.yaml"; + + private int port = 8141; + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public OnapCommandHttpMocoServer(String samplesToTest) { + this.samplesToTest = samplesToTest; + } + + public OnapCommandHttpMocoServer() { + } + + private List<Resource> dicoverSampleYamls() { + Resource[] resources = new Resource [] {}; + try { + resources = OnapCommandUtils.getExternalResources(SAMPLE_PATTERN + this.samplesToTest); + } catch (IOException e) { + LOG.error("Failed to discover the samples", e); + } + + return Arrays.asList(resources); + } + + private String getValue(Map<String, ?> map, String prpName) { + Object o = map.get(prpName); + if (o != null) { + return o.toString(); + } + + return ""; + } + + private List<OnapCommandSample> loadSamples(Resource file) throws OnapCommandInvalidSample { + + List<OnapCommandSample> samples = new ArrayList<>(); + Map<String, ?> values = null; + try { + values = (Map<String, ?>) new Yaml().load(file.getInputStream()); + } catch (Exception e) { + throw new OnapCommandInvalidSample(file.getFilename(), e); + } + + OnapCommandSample sample = new OnapCommandSample(); + + if (!this.getValue(values, SAMPLE_VERSION).equals(SAMPLE_VERSION_1_0)) { + throw new OnapCommandInvalidSample(file.getFilename(), "Invalid sample version " + this.getValue(values, SAMPLE_VERSION)); + } + + sample.setCommandName(this.getValue(values, SAMPLE_COMMAND_NAME)); + sample.setProduct(this.getValue(values, SAMPLE_PRODUCT)); + + //Retrieve the samples + values = (Map<String, Map<String, String>>) values.get(SAMPLE_LIST); + + for (String s: values.keySet()) { + Map<String, ?> sMap = (Map<String, ?>)values.get(s); + sample.setDescription(this.getValue(sMap, SAMPLE_DESCRIPTION)); + sample.setInput(this.getValue(sMap, SAMPLE_INPUT)); + sample.setOutput(this.getValue(sMap, SAMPLE_OUTPUT)); + sample.setMoco(this.getValue(sMap, SAMPLE_MOCO)); + samples.add(sample); + } + + return samples; + } + + private void verifySample(OnapCommandSample sample) throws OnapCommandException { + + List <String> args = new ArrayList<>(); + args.add(sample.getCommandName()); + args.addAll(Arrays.asList(sample.getInput().split(" "))); + + ByteArrayOutputStream bo = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bo)); + + OnapCli cli = new OnapCli(args.toArray(new String []{})); + OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(sample.getProduct()); + cli.handle(); + + String output = new String(bo.toByteArray()); + + //mrkanag uncomment following lines once moco server setup is done + //assert cli.getExitCode() == 0; + + //assert sample.getOutput().equals(output); + } + + public void verifySamples() throws OnapCommandException { + for (Resource rsc : this.dicoverSampleYamls()) { + for(OnapCommandSample sample: this.loadSamples(rsc)) { + + if (!sample.getMoco().isEmpty()) { + HttpServer server = jsonHttpServer(this.getPort(), pathResource(sample.getMoco())); + Runner r = runner(server); + r.start(); + + this.verifySample(sample); + + r.stop(); + } + } + } + } +}
\ No newline at end of file diff --git a/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandSample.java b/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandSample.java new file mode 100644 index 00000000..31448e4b --- /dev/null +++ b/validate/validation/src/test/java/org/onap/cli/moco/OnapCommandSample.java @@ -0,0 +1,80 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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. + */ + +package org.onap.cli.moco; + +public class OnapCommandSample { + + private String commandName; + + private String product; + + private String input; + + private String output; + + private String moco; + + private String description; + + public String getCommandName() { + return commandName; + } + + public void setCommandName(String commandName) { + this.commandName = commandName; + } + + public String getProduct() { + return product; + } + + public void setProduct(String product) { + this.product = product; + } + + public String getInput() { + return input; + } + + public void setInput(String input) { + this.input = input; + } + + public String getOutput() { + return output; + } + + public void setOutput(String output) { + this.output = output; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getMoco() { + return moco; + } + + public void setMoco(String moco) { + this.moco = moco; + } +} diff --git a/validate/validation/src/test/java/org/onap/cli/validation/OnapCliMainTest.java b/validate/validation/src/test/java/org/onap/cli/validation/OnapCliMainTest.java new file mode 100644 index 00000000..779978f9 --- /dev/null +++ b/validate/validation/src/test/java/org/onap/cli/validation/OnapCliMainTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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. + */ + +package org.onap.cli.validation; + +import java.io.IOException; + +import org.aspectj.lang.annotation.After; +import org.junit.Test; +import org.onap.cli.fw.OnapCommandRegistrar; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.utils.ExternalSchema; +import org.onap.cli.main.OnapCli; +import org.onap.cli.moco.OnapCommandHttpMocoServer; + +public class OnapCliMainTest { + + OnapCli cli = null; + + /** + * Clean up. + */ + @After(value = "") + public void cleanup() { + if (this.cli != null) { + if (cli.getExitCode() != 0) { + // Fail test case + } + } + } + + private void handle(String[] args) { + cli = new OnapCli(args); + cli.handle(); + } + + @Test + public void validateCommandSchemas() throws IOException, OnapCommandException { + OnapCommandRegistrar.getRegistrar().setEnabledProductVersion("cli-1.0"); + for (ExternalSchema sch : OnapCommandRegistrar.getRegistrar().listCommandInfo()) { + System.out.println( + "************************* validate '" + sch.getCmdName() + "' *******************************"); + this.handle(new String[] { "schema-validate", "-l", sch.getSchemaName(), "-i"}); + } + } + + @Test + public void usageReadTheDocsTest() throws OnapCommandException { + for (String version: OnapCommandRegistrar.getRegistrar().getAvailableProductVersions()) { + OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(version); + System.out.println(version); + System.out.println("==========================\n\n"); + for (ExternalSchema sch : OnapCommandRegistrar.getRegistrar().listCommandInfo()) { + if (sch.getCmdVersion().equals(version)) { + System.out.println(sch.getCmdName()); + System.out.println("-----------------------------------------------\n\n"); + this.handle(new String[] { sch.getCmdName(), "-h"}); + System.out.println("\n"); + } + } + } + } + + @Test + public void validateCommands() throws OnapCommandException { + OnapCommandHttpMocoServer server = new OnapCommandHttpMocoServer(); + server.verifySamples(); + } + + } diff --git a/validate/validation/src/test/resources/customer-create-sample-1.1-moco.json b/validate/validation/src/test/resources/customer-create-sample-1.1-moco.json new file mode 100644 index 00000000..f6e4d0b2 --- /dev/null +++ b/validate/validation/src/test/resources/customer-create-sample-1.1-moco.json @@ -0,0 +1,11 @@ +[{ + "request" : + { + "method" : "get", + "uri" : "/aai/v11/business/customers/customer/test" + }, + "response" : + { + "status" : 201 + } +}]
\ No newline at end of file diff --git a/validate/validation/src/test/resources/onap-cli-sample/customer/customer-create-sample-1.1.yaml b/validate/validation/src/test/resources/onap-cli-sample/customer/customer-create-sample-1.1.yaml new file mode 100644 index 00000000..30b5b303 --- /dev/null +++ b/validate/validation/src/test/resources/onap-cli-sample/customer/customer-create-sample-1.1.yaml @@ -0,0 +1,9 @@ +onap_cli_sample_version: 1.0 + +name: customer-create +version: onap-1.1 +samples: + sample1: + name: Create a customer + input: -m http://locahost:8141 -u AAI -p AAI --customer-name test --subscriber-name test + moco: customer-create-sample-1.1-moco.json
\ No newline at end of file |