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