2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020,2022 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.tools.model.generator.model2cli;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatCode;
27 import static org.junit.Assert.assertTrue;
29 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import org.junit.Test;
36 * Test the Model2Cli utility.
38 public class Model2CliTest {
40 public void testModel2Cli() {
41 final String[] cliArgs = {"-h"};
43 assertThatCode(() -> Model2CliMain.main(cliArgs)).doesNotThrowAnyException();
47 public void testModel2CliNoOptions() {
48 final String[] cliArgs = new String[] {};
50 final String outputString = runModel2Cli(cliArgs);
52 assertTrue(outputString.contains("gen-model2cli: no '-m' model file given, cannot proceed (try -h for help)"));
56 public void testModel2CliBadOptions() {
57 assertThat(runModel2Cli(new String[] {"-zabbu"})).contains("usage: gen-model2cli");
61 public void testModel2CliHelp() {
62 assertThat(runModel2Cli(new String[] {"-h"})).contains("usage: gen-model2cli");
66 public void testModel2CliVersion() {
67 assertThat(runModel2Cli(new String[] {"-v"})).contains("gen-model2cli").doesNotContain("usage:");
71 public void testModel2CliOverwrite() throws IOException {
72 File tempFile = File.createTempFile("AvroModel", ".apex");
73 tempFile.deleteOnExit();
75 final String[] cliArgs = {"-m", "src/test/resources/models/AvroModel.json", "-o", tempFile.getCanonicalPath()};
77 final String outputString = runModel2Cli(cliArgs);
79 assertTrue(outputString.contains("gen-model2cli: error with '-o' option: \"file already exists\""));
83 public void testModel2CliAadm() throws IOException {
84 testModel2CliModel("target/examples/models/AADM", "AADMPolicyModel");
88 public void testModel2CliAnomaly() throws IOException {
89 testModel2CliModel("target/examples/models/Adaptive", "AnomalyDetectionPolicyModel");
93 public void testModel2CliAutoLearn() throws IOException {
94 testModel2CliModel("target/examples/models/Adaptive", "AutoLearnPolicyModel");
98 public void testModel2CliJms() throws IOException {
99 testModel2CliModel("target/examples/models/JMS", "JMSTestModel");
103 public void testModel2CliMfp() throws IOException {
104 testModel2CliModel("target/examples/models/MyFirstPolicy/2", "MyFirstPolicyModel_0.0.1");
108 public void testModel2CliSample() throws IOException {
109 testModel2CliModel("target/examples/models/SampleDomain", "SamplePolicyModelJAVASCRIPT");
113 * Run the application.
115 * @param cliArgs the command arguments
116 * @return a string containing the command output
118 private String runModel2Cli(final String[] cliArgs) {
119 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
120 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
122 new Model2CliMain(cliArgs, new PrintStream(baosOut, true), new PrintStream(baosErr, true));
124 String outString = baosOut.toString();
125 String errString = baosErr.toString();
127 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
131 * Test CLI generation.
133 * @param modelName the name of the model file
135 private void testModel2CliModel(final String modelPath, final String modelName) throws IOException {
136 File tempFile = File.createTempFile(modelName, ".apex");
137 tempFile.deleteOnExit();
140 final String[] cliArgs = {
142 modelPath + "/" + modelName + ".json",
144 tempFile.getCanonicalPath(),
148 runModel2Cli(cliArgs);
150 assertTrue(tempFile.isFile());
151 assertTrue(tempFile.length() > 0);