<artifactId>spring-core</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
+ <dependency>
+ <groupId>org.onap.cli</groupId>
+ <artifactId>cli-sample-mock-generator</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
package org.onap.cli.fw.cmd;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.onap.cli.fw.conf.OnapCommandConfg;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandExecutionFailed;
+import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
import org.onap.cli.fw.http.HttpInput;
import org.onap.cli.fw.http.HttpResult;
-import org.onap.cli.fw.http.mock.MockJsonGenerator;
import org.onap.cli.fw.output.OnapCommandResultAttribute;
import org.onap.cli.fw.utils.OnapCommandUtils;
+import org.onap.cli.http.mock.MockJsonGenerator;
+import org.onap.cli.http.mock.MockRequest;
+import org.onap.cli.http.mock.MockResponse;
/**
* Onap Command.
for (OnapCommandResultAttribute attr : this.getResult().getRecords()) {
attr.setValues(results.get(attr.getName()));
}
+ generateJsonMock(httpInput, output, this.getName());
+ }
- if (OnapCommandConfg.isMocoGenerateEnabled()) {
- MockJsonGenerator.generateMocking(httpInput, output, this.getName());
+ private void generateJsonMock(HttpInput httpInput, HttpResult httpResult, String fileName)
+ throws OnapCommandFailedMocoGenerate {
+
+ if (OnapCommandConfg.isSampleGenerateEnabled()) {
+ try {
+ MockRequest mockRequest = new MockRequest();
+ mockRequest.setMethod(httpInput.getMethod());
+ mockRequest.setUri(httpInput.getUri());
+ mockRequest.setHeaders(httpInput.getReqHeaders());
+ mockRequest.setJson(httpInput.getBody());
+
+ MockResponse mockResponse = new MockResponse();
+ mockResponse.setStatus(httpResult.getStatus());
+ mockResponse.setJson(httpResult.getBody());
+
+ MockJsonGenerator.generateMocking(mockRequest, mockResponse, OnapCommandConfg.getSampleGenerateTargetFolder()
+ + "/" + fileName);
+ } catch (IOException error) {
+ throw new OnapCommandFailedMocoGenerate(fileName, error);
+ }
}
}
}
public static final String DISCOVER_ALWAYS = "discover_always";
- public static final String MOCO_ENABLED = "cli.moco.enable";
- public static final String MOCO_TARGET_FOLDER = "cli.moco.target";
+ public static final String SAMPLE_GEN_ENABLED = "cli.sample.gen.enable";
+ public static final String SAMPLE_GEN_TARGET_FOLDER = "cli.sample.gen.target";
private Constants() {
}
return Arrays.stream(prps.getProperty(key).split(",")).map(String::trim).collect(Collectors.toList()); // NOSONAR
}
- public static String getMocoTargetFolder() {
- return prps.getProperty(Constants.MOCO_TARGET_FOLDER, ".");
+ public static String getSampleGenerateTargetFolder() {
+ return prps.getProperty(Constants.SAMPLE_GEN_TARGET_FOLDER, ".");
}
- public static boolean isMocoGenerateEnabled() {
- return "true".equals(prps.getProperty(Constants.MOCO_ENABLED));
+ public static boolean isSampleGenerateEnabled() {
+ return "true".equals(prps.getProperty(Constants.SAMPLE_GEN_ENABLED));
}
}
+++ /dev/null
-/*
- * 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.fw.http.mock;
-
-import java.io.File;
-import java.io.IOException;
-import java.text.SimpleDateFormat;
-import java.util.Arrays;
-import java.util.Date;
-
-import org.onap.cli.fw.conf.OnapCommandConfg;
-import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
-import org.onap.cli.fw.http.HttpInput;
-import org.onap.cli.fw.http.HttpResult;
-
-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(HttpInput httpInput, HttpResult httpResult,
- String jsonFileName) throws OnapCommandFailedMocoGenerate {
-
- MockRequest mockRequest = new MockRequest();
- mockRequest.setMethod(httpInput.getMethod());
- mockRequest.setUri(httpInput.getUri());
- mockRequest.setHeaders(httpInput.getReqHeaders());
- mockRequest.setJson(httpInput.getBody());
-
- MockResponse mockResponse = new MockResponse();
- mockResponse.setStatus(httpResult.getStatus());
- mockResponse.setJson(httpResult.getBody());
-
- MockObject mockObject = new MockObject();
- mockObject.setRequest(mockRequest);
- mockObject.setResponse(mockResponse);
-
- ObjectMapper mapper = new ObjectMapper();
- ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
- try {
- String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
- writer.writeValue(new File(OnapCommandConfg.getMocoTargetFolder() + "/" + jsonFileName + "-" + timeStamp + "-moco.json"),
- Arrays.asList(mockObject));
- } catch (IOException error ) {
- throw new OnapCommandFailedMocoGenerate(jsonFileName, error);
- }
- }
-}
cli.product.version=cli-1.0
# moco properties
-cli.moco.enable=false
-cli.moco.target=.
+cli.sample.gen.enable=false
+cli.sample.gen.target=.
+++ /dev/null
-/*
- * Copyright 2016-17 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.fw.http.mock;
-
-import org.junit.Test;
-import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
-import org.onap.cli.fw.http.HttpInput;
-import org.onap.cli.fw.http.HttpResult;
-
-public class MockJsonGeneratorTest {
-
- @Test
- public void mocoGenerateTest() throws OnapCommandFailedMocoGenerate {
- HttpInput httpInput = new HttpInput();
- httpInput.setBody("{\"value\" : \"234sdf-345\"}");
- httpInput.setMethod("get");
- httpInput.setUri("http://1.1.1.1:80/getResource");
-
- HttpResult httpResult = new HttpResult();
- httpResult.setStatus(200);
- httpResult.setBody("{\"value\" : \"234sdf-345\"}");
-
- MockJsonGenerator.generateMocking(httpInput, httpResult, "test");
- }
-
- @Test(expected=OnapCommandFailedMocoGenerate.class)
- public void mocoGenerateFailedInvalidBodyTest() throws OnapCommandFailedMocoGenerate {
- HttpInput httpInput = new HttpInput();
- httpInput.setBody("{\"value\" : \"234sdf-345\"");
- httpInput.setMethod("get");
- httpInput.setUri("http://1.1.1.1:80/getResource");
-
- HttpResult httpResult = new HttpResult();
- httpResult.setStatus(200);
- httpResult.setBody("{\"value\" : \"234sdf-345\"");
-
- MockJsonGenerator.generateMocking(httpInput, httpResult, "test");
- }
-
- @Test(expected=OnapCommandFailedMocoGenerate.class)
- public void mocoGenerateFailedInvalidUrlTest() throws OnapCommandFailedMocoGenerate {
- HttpInput httpInput = new HttpInput();
- httpInput.setBody("{\"value\" : \"234sdf-345\"");
- httpInput.setMethod("get");
- httpInput.setUri("http://1.1.1.1:80:invalid");
-
- HttpResult httpResult = new HttpResult();
- httpResult.setStatus(200);
- httpResult.setBody("{\"value\" : \"234sdf-345\"");
-
- MockJsonGenerator.generateMocking(httpInput, httpResult, "test");
- }
-}
<name>cli/main</name>
<packaging>jar</packaging>
<dependencies>
+ <dependency>
+ <groupId>org.onap.cli</groupId>
+ <artifactId>cli-sample-yaml-generator</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
import org.onap.cli.fw.OnapCommand;
import org.onap.cli.fw.OnapCommandRegistrar;
import org.onap.cli.fw.conf.Constants;
+import org.onap.cli.fw.conf.OnapCommandConfg;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandHelpFailed;
+import org.onap.cli.fw.error.OnapCommandInvalidSample;
import org.onap.cli.fw.error.OnapCommandWarning;
import org.onap.cli.fw.input.OnapCommandParameter;
import org.onap.cli.fw.output.OnapCommandResult;
import org.onap.cli.main.conf.OnapCliConstants;
import org.onap.cli.main.interactive.StringCompleter;
import org.onap.cli.main.utils.OnapCliUtils;
+import org.onap.cli.sample.yaml.SampleYamlGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
OnapCliUtils.populateParams(cmd.getParameters(), args);
OnapCommandResult result = cmd.execute();
+
this.print(result.getDebugInfo());
this.print(result.print());
this.exitSuccessfully();
+
+ generateSmapleYaml(result);
} catch (Exception e) {
this.print(cmd.getResult().getDebugInfo());
if (e instanceof OnapCommandWarning) {
}
}
+ private void generateSmapleYaml(OnapCommandResult result) throws OnapCommandException {
+ if (OnapCommandConfg.isSampleGenerateEnabled() && this.getExitCode() == OnapCliConstants.EXIT_SUCCESS) {
+ try {
+ SampleYamlGenerator.generateSampleYaml(args, result.print(),
+ OnapCommandRegistrar.getRegistrar().getEnabledProductVersion(),
+ OnapCommandConfg.getSampleGenerateTargetFolder(),
+ result.isDebug());
+ } catch (IOException error) {
+ throw new OnapCommandInvalidSample(args.get(0), error);
+ }
+ }
+ }
+
/**
* Handles all client input.
*/
<module>plugins</module>
<module>main</module>
<module>deployment</module>
- <module>validation</module>
+ <module>validate</module>
</modules>
<distributionManagement>
</plugin>
</plugins>
</build>
-</project>
\ No newline at end of file
+</project>
--- /dev/null
+<?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>
--- /dev/null
+<?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>
--- /dev/null
+/*
+ * 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));
+ }
+}
* limitations under the License.
*/
-package org.onap.cli.fw.http.mock;
+package org.onap.cli.http.mock;
public class MockObject {
private MockRequest request;
* limitations under the License.
*/
-package org.onap.cli.fw.http.mock;
+package org.onap.cli.http.mock;
import java.io.IOException;
-import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
-import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
-
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.JsonNodeFactory;
public class MockRequest {
private String method;
return uri;
}
- public void setUri(String url) throws OnapCommandFailedMocoGenerate {
+ public void setUri(String url) throws IOException {
URL urlt;
- try {
- urlt = new URL(url);
- } catch (MalformedURLException error) {
- throw new OnapCommandFailedMocoGenerate(null, error);
- }
+ urlt = new URL(url);
this.uri = urlt.getPath();
}
return json;
}
- public void setJson(String json) throws OnapCommandFailedMocoGenerate {
- if (json.isEmpty()) {
- this.json = JsonNodeFactory.instance.objectNode();
- return;
- }
-
- ObjectMapper objectMapper = new ObjectMapper();
- try {
+ public void setJson(String json) throws IOException {
+ if (!json.isEmpty()) {
+ ObjectMapper objectMapper = new ObjectMapper();
this.json = objectMapper.readTree(json);
- }catch (IOException error) {
- throw new OnapCommandFailedMocoGenerate(null, error);
}
+
}
}
* limitations under the License.
*/
-package org.onap.cli.fw.http.mock;
+package org.onap.cli.http.mock;
import java.io.IOException;
-import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
-
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.JsonNodeFactory;
public class MockResponse {
private int status;
return json;
}
- public void setJson(String json) throws OnapCommandFailedMocoGenerate {
- if (json.isEmpty()) {
- this.json = JsonNodeFactory.instance.objectNode();
- return;
- }
-
- ObjectMapper objectMapper = new ObjectMapper();
- try {
+ public void setJson(String json) throws IOException {
+ if (!json.isEmpty()) {
+ ObjectMapper objectMapper = new ObjectMapper();
this.json = objectMapper.readTree(json);
- } catch (IOException error) {
- throw new OnapCommandFailedMocoGenerate(null, error);
}
}
}
--- /dev/null
+/*
+ * 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");
+ }
+}
--- /dev/null
+<?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>
--- /dev/null
+/*
+ * 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--;
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+
+}