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