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