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