1804aca305a29749cbb1f2b8d64bb2ed16dc0597
[policy/apex-pdp.git] / tools / model-generator / src / test / java / org / onap / policy / apex / tools / model / generator / model2event / Model2EventTest.java
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.model2event;
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 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
33
34 /**
35  * Test the Model2Event utility.
36  */
37 public class Model2EventTest {
38     @Test
39     public void testModel2Event() {
40         try {
41             final String[] EventArgs =
42                 { "-h" };
43
44             Model2EventMain.main(EventArgs);
45         } catch (Exception exc) {
46             fail("test should not throw an exception");
47         }
48     }
49
50     @Test
51     public void testModel2EventNoOptions() {
52         final String[] EventArgs = new String[]
53             {};
54
55         final String outputString = runModel2Event(EventArgs);
56
57         assertTrue(outputString.contains("gen-model2event: no model file given, cannot proceed (try -h for help)"));
58     }
59
60     @Test
61     public void testModel2EventBadOptions() {
62         final String[] EventArgs =
63             { "-zabbu" };
64
65         final String outputString = runModel2Event(EventArgs);
66
67         assertTrue(outputString.contains("usage: gen-model2event"));
68     }
69
70     @Test
71     public void testModel2EventHelp() {
72         final String[] EventArgs =
73             { "-h" };
74
75         final String outputString = runModel2Event(EventArgs);
76
77         assertTrue(outputString.contains("usage: gen-model2event"));
78     }
79
80     @Test
81     public void testModel2EventVersion() {
82         final String[] EventArgs =
83             { "-v" };
84
85         final String outputString = runModel2Event(EventArgs);
86
87         assertTrue(outputString.contains("gen-model2event"));
88     }
89
90     @Test
91     public void testModel2EventNoType() {
92         final String[] EventArgs =
93             { "-m", "src/test/resources/models/AvroModel.json" };
94
95         final String outputString = runModel2Event(EventArgs);
96
97         assertTrue(outputString.contains("gen-model2event: no event type given, cannot proceed (try -h for help)"));
98     }
99
100     @Test
101     public void testModel2EventBadType() {
102         final String[] EventArgs =
103             { "-m", "src/test/resources/models/AvroModel.json", "-t", "Zooby" };
104
105         final String outputString = runModel2Event(EventArgs);
106
107         assertTrue(outputString.contains("gen-model2event: unknown type <Zooby>, cannot proceed (try -h for help)"));
108     }
109
110     @Test
111     public void testModel2EventAadm() throws IOException {
112         testModel2EventModel("AADMPolicyModel");
113     }
114
115     @Test
116     public void testModel2EventAnomaly() {
117         testModel2EventModel("AnomalyDetectionPolicyModel");
118     }
119
120     @Test
121     public void testModel2EventAutoLearn() {
122         testModel2EventModel("AutoLearnPolicyModel");
123     }
124
125     @Test
126     public void testModel2EventMfp() {
127         testModel2EventModel("MyFirstPolicyModel");
128     }
129
130     @Test
131     public void testModel2EventSample() {
132         testModel2EventModel("SamplePolicyModelJAVASCRIPT");
133     }
134
135     /**
136      * Run the application.
137      * 
138      * @param eventArgs the command arguments
139      * @return a string containing the command output
140      */
141     private String runModel2Event(final String[] eventArgs) {
142         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
143         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
144
145         new Model2EventMain(eventArgs, new PrintStream(baosOut, true));
146
147         String outString = baosOut.toString();
148         String errString = baosErr.toString();
149
150         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
151     }
152
153     /**
154      * Test Event generation.
155      * 
156      * @param modelName the name of the model file
157      */
158     private void testModel2EventModel(String modelName) {
159         try {
160             File tempFile = File.createTempFile(modelName, ".apex");
161             tempFile.deleteOnExit();
162
163             final String[] eventArgs0 =
164                 { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" };
165             final String outputString0 = runModel2Event(eventArgs0);
166
167             assertTrue(outputString0.contains("type: stimuli"));
168
169             final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" };
170             final String outputString1 = runModel2Event(eventArgs1);
171
172             assertTrue(outputString1.contains("type: response"));
173
174             final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" };
175             final String outputString2 = runModel2Event(eventArgs2);
176
177             assertTrue(outputString2.contains("type: internal"));
178         } catch (Exception e) {
179             throw new ApexRuntimeException("test should not throw an exception", e);
180         }
181     }
182 }