Fix dependencies on VPN-AVRO model deleted with PCVS examples
[policy/apex-pdp.git] / tools / model-generator / src / test / java / org / onap / policy / apex / tools / model / generator / model2cli / Model2CliTest.java
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.tools.model.generator.model2cli;
24
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;
28
29 import java.io.ByteArrayOutputStream;
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import org.junit.Test;
34
35 /**
36  * Test the Model2Cli utility.
37  */
38 public class Model2CliTest {
39     @Test
40     public void testModel2Cli() {
41         final String[] cliArgs = {"-h"};
42
43         assertThatCode(() -> Model2CliMain.main(cliArgs)).doesNotThrowAnyException();
44     }
45
46     @Test
47     public void testModel2CliNoOptions() {
48         final String[] cliArgs = new String[] {};
49
50         final String outputString = runModel2Cli(cliArgs);
51
52         assertTrue(outputString.contains("gen-model2cli: no '-m' model file given, cannot proceed (try -h for help)"));
53     }
54
55     @Test
56     public void testModel2CliBadOptions() {
57         assertThat(runModel2Cli(new String[] {"-zabbu"})).contains("usage: gen-model2cli");
58     }
59
60     @Test
61     public void testModel2CliHelp() {
62         assertThat(runModel2Cli(new String[] {"-h"})).contains("usage: gen-model2cli");
63     }
64
65     @Test
66     public void testModel2CliVersion() {
67         assertThat(runModel2Cli(new String[] {"-v"})).contains("gen-model2cli").doesNotContain("usage:");
68     }
69
70     @Test
71     public void testModel2CliOverwrite() throws IOException {
72         File tempFile = File.createTempFile("AvroModel", ".apex");
73         tempFile.deleteOnExit();
74
75         final String[] cliArgs = {"-m", "src/test/resources/models/AvroModel.json", "-o", tempFile.getCanonicalPath()};
76
77         final String outputString = runModel2Cli(cliArgs);
78
79         assertTrue(outputString.contains("gen-model2cli: error with '-o' option: \"file already exists\""));
80     }
81
82     @Test
83     public void testModel2CliAadm() throws IOException {
84         testModel2CliModel("target/examples/models/AADM", "AADMPolicyModel");
85     }
86
87     @Test
88     public void testModel2CliAnomaly() throws IOException {
89         testModel2CliModel("target/examples/models/Adaptive", "AnomalyDetectionPolicyModel");
90     }
91
92     @Test
93     public void testModel2CliAutoLearn() throws IOException {
94         testModel2CliModel("target/examples/models/Adaptive", "AutoLearnPolicyModel");
95     }
96
97     @Test
98     public void testModel2CliJms() throws IOException {
99         testModel2CliModel("target/examples/models/JMS", "JMSTestModel");
100     }
101
102     @Test
103     public void testModel2CliMfp() throws IOException {
104         testModel2CliModel("target/examples/models/MyFirstPolicy/2", "MyFirstPolicyModel_0.0.1");
105     }
106
107     @Test
108     public void testModel2CliSample() throws IOException {
109         testModel2CliModel("target/examples/models/SampleDomain", "SamplePolicyModelJAVASCRIPT");
110     }
111
112     /**
113      * Run the application.
114      *
115      * @param cliArgs the command arguments
116      * @return a string containing the command output
117      */
118     private String runModel2Cli(final String[] cliArgs) {
119         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
120         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
121
122         new Model2CliMain(cliArgs, new PrintStream(baosOut, true), new PrintStream(baosErr, true));
123
124         String outString = baosOut.toString();
125         String errString = baosErr.toString();
126
127         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
128     }
129
130     /**
131      * Test CLI generation.
132      *
133      * @param modelName the name of the model file
134      */
135     private void testModel2CliModel(final String modelPath, final String modelName) throws IOException {
136         File tempFile = File.createTempFile(modelName, ".apex");
137         tempFile.deleteOnExit();
138
139         // @formatter:off
140         final String[] cliArgs = {
141             "-m",
142             modelPath + "/" + modelName + ".json",
143             "-o",
144             tempFile.getCanonicalPath(),
145             "-ow"
146         };
147         // @formatter:on
148         runModel2Cli(cliArgs);
149
150         assertTrue(tempFile.isFile());
151         assertTrue(tempFile.length() > 0);
152     }
153 }