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.junit.Assert.assertTrue;
26 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import org.junit.Test;
33 * Test the Model2Cli utility.
35 public class Model2CliTest {
37 public void testModel2Cli() {
38 final String[] cliArgs = {"-h"};
40 Model2CliMain.main(cliArgs);
44 public void testModel2CliNoOptions() {
45 final String[] cliArgs = new String[] {};
47 final String outputString = runModel2Cli(cliArgs);
49 assertTrue(outputString.contains("gen-model2cli: no '-m' model file given, cannot proceed (try -h for help)"));
53 public void testModel2CliBadOptions() {
54 final String[] cliArgs = {"-zabbu"};
56 final String outputString = runModel2Cli(cliArgs);
58 assertTrue(outputString.contains("usage: gen-model2cli"));
62 public void testModel2CliHelp() {
63 final String[] cliArgs = {"-h"};
65 final String outputString = runModel2Cli(cliArgs);
67 assertTrue(outputString.contains("usage: gen-model2cli"));
71 public void testModel2CliVersion() {
72 final String[] cliArgs = {"-v"};
74 final String outputString = runModel2Cli(cliArgs);
76 assertTrue(outputString.contains("gen-model2cli"));
80 public void testModel2CliOverwrite() throws IOException {
81 File tempFile = File.createTempFile("AvroModel", ".apex");
82 tempFile.deleteOnExit();
84 final String[] cliArgs = {"-m", "src/test/resources/models/AvroModel.json", "-o", tempFile.getCanonicalPath()};
86 final String outputString = runModel2Cli(cliArgs);
88 assertTrue(outputString.contains("gen-model2cli: error with '-o' option: \"file already exists\""));
92 public void testModel2CliAvro() throws IOException {
93 testModel2CliModel("target/examples/models/pcvs/vpnsla", "vpnsla");
97 public void testModel2CliAadm() throws IOException {
98 testModel2CliModel("target/examples/models/AADM", "AADMPolicyModel");
102 public void testModel2CliAnomaly() throws IOException {
103 testModel2CliModel("target/examples/models/Adaptive", "AnomalyDetectionPolicyModel");
107 public void testModel2CliAutoLearn() throws IOException {
108 testModel2CliModel("target/examples/models/Adaptive", "AutoLearnPolicyModel");
112 public void testModel2CliJms() throws IOException {
113 testModel2CliModel("target/examples/models/JMS", "JMSTestModel");
117 public void testModel2CliMfp() throws IOException {
118 testModel2CliModel("target/examples/models/MyFirstPolicy/2", "MyFirstPolicyModel_0.0.1");
122 public void testModel2CliSample() throws IOException {
123 testModel2CliModel("target/examples/models/SampleDomain", "SamplePolicyModelJAVASCRIPT");
127 * Run the application.
129 * @param cliArgs the command arguments
130 * @return a string containing the command output
132 private String runModel2Cli(final String[] cliArgs) {
133 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
134 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
136 new Model2CliMain(cliArgs, new PrintStream(baosOut, true), new PrintStream(baosErr, true));
138 String outString = baosOut.toString();
139 String errString = baosErr.toString();
141 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
145 * Test CLI generation.
147 * @param modelName the name of the model file
149 private void testModel2CliModel(final String modelPath, final String modelName) throws IOException {
150 File tempFile = File.createTempFile(modelName, ".apex");
151 tempFile.deleteOnExit();
154 final String[] cliArgs = {
156 modelPath + "/" + modelName + ".json",
158 tempFile.getCanonicalPath(),
162 runModel2Cli(cliArgs);
164 assertTrue(tempFile.isFile());
165 assertTrue(tempFile.length() > 0);