2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.tools.model.generator.model2cli;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertTrue;
27 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import org.junit.Test;
34 * Test the Model2Cli utility.
36 public class Model2CliTest {
38 public void testModel2Cli() {
39 final String[] cliArgs = {"-h"};
41 assertThatCode(() -> Model2CliMain.main(cliArgs)).doesNotThrowAnyException();
45 public void testModel2CliNoOptions() {
46 final String[] cliArgs = new String[] {};
48 final String outputString = runModel2Cli(cliArgs);
50 assertTrue(outputString.contains("gen-model2cli: no '-m' model file given, cannot proceed (try -h for help)"));
54 public void testModel2CliBadOptions() {
55 final String[] cliArgs = {"-zabbu"};
57 final String outputString = runModel2Cli(cliArgs);
59 assertTrue(outputString.contains("usage: gen-model2cli"));
63 public void testModel2CliHelp() {
64 final String[] cliArgs = {"-h"};
66 final String outputString = runModel2Cli(cliArgs);
68 assertTrue(outputString.contains("usage: gen-model2cli"));
72 public void testModel2CliVersion() {
73 final String[] cliArgs = {"-v"};
75 final String outputString = runModel2Cli(cliArgs);
77 assertTrue(outputString.contains("gen-model2cli"));
81 public void testModel2CliOverwrite() throws IOException {
82 File tempFile = File.createTempFile("AvroModel", ".apex");
83 tempFile.deleteOnExit();
85 final String[] cliArgs = {"-m", "src/test/resources/models/AvroModel.json", "-o", tempFile.getCanonicalPath()};
87 final String outputString = runModel2Cli(cliArgs);
89 assertTrue(outputString.contains("gen-model2cli: error with '-o' option: \"file already exists\""));
93 public void testModel2CliAvro() throws IOException {
94 testModel2CliModel("target/examples/models/pcvs/vpnsla", "vpnsla");
98 public void testModel2CliAadm() throws IOException {
99 testModel2CliModel("target/examples/models/AADM", "AADMPolicyModel");
103 public void testModel2CliAnomaly() throws IOException {
104 testModel2CliModel("target/examples/models/Adaptive", "AnomalyDetectionPolicyModel");
108 public void testModel2CliAutoLearn() throws IOException {
109 testModel2CliModel("target/examples/models/Adaptive", "AutoLearnPolicyModel");
113 public void testModel2CliJms() throws IOException {
114 testModel2CliModel("target/examples/models/JMS", "JMSTestModel");
118 public void testModel2CliMfp() throws IOException {
119 testModel2CliModel("target/examples/models/MyFirstPolicy/2", "MyFirstPolicyModel_0.0.1");
123 public void testModel2CliSample() throws IOException {
124 testModel2CliModel("target/examples/models/SampleDomain", "SamplePolicyModelJAVASCRIPT");
128 * Run the application.
130 * @param cliArgs the command arguments
131 * @return a string containing the command output
133 private String runModel2Cli(final String[] cliArgs) {
134 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
135 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
137 new Model2CliMain(cliArgs, new PrintStream(baosOut, true), new PrintStream(baosErr, true));
139 String outString = baosOut.toString();
140 String errString = baosErr.toString();
142 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
146 * Test CLI generation.
148 * @param modelName the name of the model file
150 private void testModel2CliModel(final String modelPath, final String modelName) throws IOException {
151 File tempFile = File.createTempFile(modelName, ".apex");
152 tempFile.deleteOnExit();
155 final String[] cliArgs = {
157 modelPath + "/" + modelName + ".json",
159 tempFile.getCanonicalPath(),
163 runModel2Cli(cliArgs);
165 assertTrue(tempFile.isFile());
166 assertTrue(tempFile.length() > 0);