c9180607ef4b2ea090b714e99334e7e0ff58b16f
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.tools.model.generator.model2event;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatCode;
27 import static org.junit.Assert.assertTrue;
28
29 import java.io.ByteArrayOutputStream;
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import org.junit.Test;
34
35 /**
36  * Test the Model2Event utility.
37  */
38 public class Model2EventTest {
39     @Test
40     public void testModel2Event() {
41         final String[] EventArgs =
42             { "-h" };
43
44         assertThatCode(() -> Model2EventMain.main(EventArgs)).doesNotThrowAnyException();
45
46     }
47
48     @Test
49     public void testModel2EventNoOptions() {
50         final String[] EventArgs = new String[]
51             {};
52
53         final String outputString = runModel2Event(EventArgs);
54
55         assertTrue(outputString.contains("gen-model2event: no model file given, cannot proceed (try -h for help)"));
56     }
57
58     @Test
59     public void testModel2EventBadOptions() {
60         assertThat(runModel2Event(new String[] {"-zabbu"})).contains("usage: gen-model2event");
61     }
62
63     @Test
64     public void testModel2EventHelp() {
65         assertThat(runModel2Event(new String[] {"-h"})).contains("usage: gen-model2event");
66     }
67
68     @Test
69     public void testModel2EventVersion() {
70         assertThat(runModel2Event(new String[] {"-v"})).contains("gen-model2event").doesNotContain("usage:");
71     }
72
73     @Test
74     public void testModel2EventNoType() {
75         final String[] EventArgs =
76             { "-m", "src/test/resources/models/AvroModel.json" };
77
78         final String outputString = runModel2Event(EventArgs);
79
80         assertTrue(outputString.contains("gen-model2event: no event type given, cannot proceed (try -h for help)"));
81     }
82
83     @Test
84     public void testModel2EventBadType() {
85         final String[] EventArgs =
86             { "-m", "src/test/resources/models/AvroModel.json", "-t", "Zooby" };
87
88         final String outputString = runModel2Event(EventArgs);
89
90         assertTrue(outputString.contains("gen-model2event: unknown type <Zooby>, cannot proceed (try -h for help)"));
91     }
92
93     @Test
94     public void testModel2EventAadm() throws IOException {
95         testModel2EventModel("AADMPolicyModel");
96     }
97
98     @Test
99     public void testModel2EventAnomaly() throws IOException {
100         testModel2EventModel("AnomalyDetectionPolicyModel");
101     }
102
103     @Test
104     public void testModel2EventAutoLearn() throws IOException {
105         testModel2EventModel("AutoLearnPolicyModel");
106     }
107
108     @Test
109     public void testModel2EventMfp() throws IOException {
110         testModel2EventModel("MyFirstPolicyModel");
111     }
112
113     @Test
114     public void testModel2EventSample() throws IOException {
115         testModel2EventModel("SamplePolicyModelJAVASCRIPT");
116     }
117
118     /**
119      * Run the application.
120      *
121      * @param eventArgs the command arguments
122      * @return a string containing the command output
123      */
124     private String runModel2Event(final String[] eventArgs) {
125         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
126         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
127
128         new Model2EventMain(eventArgs, new PrintStream(baosOut, true));
129
130         String outString = baosOut.toString();
131         String errString = baosErr.toString();
132
133         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
134     }
135
136     /**
137      * Test Event generation.
138      *
139      * @param modelName the name of the model file
140      */
141     private void testModel2EventModel(String modelName) throws IOException {
142         File tempFile = File.createTempFile(modelName, ".apex");
143         tempFile.deleteOnExit();
144
145         final String[] eventArgs0 =
146             { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" };
147         final String outputString0 = runModel2Event(eventArgs0);
148
149         assertTrue(outputString0.contains("type: stimuli"));
150
151         final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" };
152         final String outputString1 = runModel2Event(eventArgs1);
153
154         assertTrue(outputString1.contains("type: response"));
155
156         final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" };
157         final String outputString2 = runModel2Event(eventArgs2);
158
159         assertTrue(outputString2.contains("type: internal"));
160     }
161 }