2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.tools.model.generator.model2cli;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.PrintStream;
31 import org.junit.Test;
34 * Test the Model2Cli utility.
36 public class Model2CliTest {
38 public void testModel2Cli() {
40 final String[] cliArgs =
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[]
54 final String outputString = runModel2Cli(cliArgs);
56 assertTrue(outputString
57 .contains("gen-model2cli: no '-m' model file given, cannot proceed (try -h for help)"));
61 public void testModel2CliBadOptions() {
62 final String[] cliArgs =
65 final String outputString = runModel2Cli(cliArgs);
67 assertTrue(outputString.contains("usage: gen-model2cli"));
71 public void testModel2CliHelp() {
72 final String[] cliArgs =
75 final String outputString = runModel2Cli(cliArgs);
77 assertTrue(outputString.contains("usage: gen-model2cli"));
81 public void testModel2CliVersion() {
82 final String[] cliArgs =
85 final String outputString = runModel2Cli(cliArgs);
87 assertTrue(outputString.contains("gen-model2cli"));
91 public void testModel2CliOverwrite() throws IOException {
92 File tempFile = File.createTempFile("AvroModel", ".apex");
93 tempFile.deleteOnExit();
95 final String[] cliArgs =
96 { "-m", "src/test/resources/models/AvroModel.json", "-o", tempFile.getCanonicalPath() };
98 final String outputString = runModel2Cli(cliArgs);
100 assertTrue(outputString.contains("gen-model2cli: error with '-o' option: \"file already exists\""));
104 public void testModel2CliAvro() throws IOException {
105 testModel2CliModel("target/examples/models/pcvs/vpnsla", "PCVS-VpnSla");
109 public void testModel2CliAadm() throws IOException {
110 testModel2CliModel("target/examples/models/AADM", "AADMPolicyModel");
114 public void testModel2CliAnomaly() {
115 testModel2CliModel("target/examples/models/Adaptive", "AnomalyDetectionPolicyModel");
119 public void testModel2CliAutoLearn() {
120 testModel2CliModel("target/examples/models/Adaptive", "AutoLearnPolicyModel");
124 public void testModel2CliJms() {
125 testModel2CliModel("target/examples/models/JMS", "JMSTestModel");
129 public void testModel2CliMfp() {
130 testModel2CliModel("target/examples/models/MyFirstPolicy/2", "MyFirstPolicyModel");
134 public void testModel2CliSample() {
135 testModel2CliModel("target/examples/models/SampleDomain", "SamplePolicyModelJAVASCRIPT");
139 * Run the application.
141 * @param cliArgs the command arguments
142 * @return a string containing the command output
144 private String runModel2Cli(final String[] cliArgs) {
145 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
146 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
148 new Model2CliMain(cliArgs, new PrintStream(baosOut, true), new PrintStream(baosErr, true));
150 String outString = baosOut.toString();
151 String errString = baosErr.toString();
153 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
157 * Test CLI generation.
159 * @param modelName the name of the model file
161 private void testModel2CliModel(final String modelPath, final String modelName) {
163 File tempFile = File.createTempFile(modelName, ".apex");
164 tempFile.deleteOnExit();
166 final String[] cliArgs =
167 { "-m", modelPath + "/" + modelName + ".json", "-o", tempFile.getCanonicalPath(), "-ow" };
168 runModel2Cli(cliArgs);
170 assertTrue(tempFile.isFile());
171 assertTrue(tempFile.length() > 0);
172 } catch (Exception e) {
173 fail("test should not throw an exception");