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