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