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;
25 import static org.junit.Assert.fail;
27 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
32 import org.junit.Test;
35 * Test the Model2Cli utility.
37 public class Model2CliTest {
39 public void testModel2Cli() {
41 final String[] cliArgs = {"-h"};
43 Model2CliMain.main(cliArgs);
44 } catch (Exception exc) {
45 fail("test should not throw an exception");
50 public void testModel2CliNoOptions() {
51 final String[] cliArgs = new String[] {};
53 final String outputString = runModel2Cli(cliArgs);
55 assertTrue(outputString.contains("gen-model2cli: no '-m' model file given, cannot proceed (try -h for help)"));
59 public void testModel2CliBadOptions() {
60 final String[] cliArgs = {"-zabbu"};
62 final String outputString = runModel2Cli(cliArgs);
64 assertTrue(outputString.contains("usage: gen-model2cli"));
68 public void testModel2CliHelp() {
69 final String[] cliArgs = {"-h"};
71 final String outputString = runModel2Cli(cliArgs);
73 assertTrue(outputString.contains("usage: gen-model2cli"));
77 public void testModel2CliVersion() {
78 final String[] cliArgs = {"-v"};
80 final String outputString = runModel2Cli(cliArgs);
82 assertTrue(outputString.contains("gen-model2cli"));
86 public void testModel2CliOverwrite() throws IOException {
87 File tempFile = File.createTempFile("AvroModel", ".apex");
88 tempFile.deleteOnExit();
90 final String[] cliArgs = {"-m", "src/test/resources/models/AvroModel.json", "-o", tempFile.getCanonicalPath()};
92 final String outputString = runModel2Cli(cliArgs);
94 assertTrue(outputString.contains("gen-model2cli: error with '-o' option: \"file already exists\""));
98 public void testModel2CliAvro() throws IOException {
99 testModel2CliModel("target/examples/models/pcvs/vpnsla", "vpnsla");
103 public void testModel2CliAadm() throws IOException {
104 testModel2CliModel("target/examples/models/AADM", "AADMPolicyModel");
108 public void testModel2CliAnomaly() {
109 testModel2CliModel("target/examples/models/Adaptive", "AnomalyDetectionPolicyModel");
113 public void testModel2CliAutoLearn() {
114 testModel2CliModel("target/examples/models/Adaptive", "AutoLearnPolicyModel");
118 public void testModel2CliJms() {
119 testModel2CliModel("target/examples/models/JMS", "JMSTestModel");
123 public void testModel2CliMfp() {
124 testModel2CliModel("target/examples/models/MyFirstPolicy/2", "MyFirstPolicyModel_0.0.1");
128 public void testModel2CliSample() {
129 testModel2CliModel("target/examples/models/SampleDomain", "SamplePolicyModelJAVASCRIPT");
133 * Run the application.
135 * @param cliArgs the command arguments
136 * @return a string containing the command output
138 private String runModel2Cli(final String[] cliArgs) {
139 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
140 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
142 new Model2CliMain(cliArgs, new PrintStream(baosOut, true), new PrintStream(baosErr, true));
144 String outString = baosOut.toString();
145 String errString = baosErr.toString();
147 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
151 * Test CLI generation.
153 * @param modelName the name of the model file
155 private void testModel2CliModel(final String modelPath, final String modelName) {
157 File tempFile = File.createTempFile(modelName, ".apex");
158 tempFile.deleteOnExit();
161 final String[] cliArgs = {
163 modelPath + "/" + modelName + ".json",
165 tempFile.getCanonicalPath(),
169 runModel2Cli(cliArgs);
171 assertTrue(tempFile.isFile());
172 assertTrue(tempFile.length() > 0);
173 } catch (Exception e) {
174 fail("test should not throw an exception");