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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.tools.model.generator.model2event;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 import java.io.ByteArrayOutputStream;
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;
34 * Test the Model2Event utility.
36 public class Model2EventTest {
38 public void testModel2Event() {
40 final String[] EventArgs =
43 Model2EventMain.main(EventArgs);
44 } catch (Exception exc) {
45 fail("test should not throw an exception");
50 public void testModel2EventNoOptions() {
51 final String[] EventArgs = new String[]
54 final String outputString = runModel2Event(EventArgs);
56 assertTrue(outputString.contains("gen-model2event: no model file given, cannot proceed (try -h for help)"));
60 public void testModel2EventBadOptions() {
61 final String[] EventArgs =
64 final String outputString = runModel2Event(EventArgs);
66 assertTrue(outputString.contains("usage: gen-model2event"));
70 public void testModel2EventHelp() {
71 final String[] EventArgs =
74 final String outputString = runModel2Event(EventArgs);
76 assertTrue(outputString.contains("usage: gen-model2event"));
80 public void testModel2EventVersion() {
81 final String[] EventArgs =
84 final String outputString = runModel2Event(EventArgs);
86 assertTrue(outputString.contains("gen-model2event"));
90 public void testModel2EventNoType() {
91 final String[] EventArgs =
92 { "-m", "src/test/resources/models/AvroModel.json" };
94 final String outputString = runModel2Event(EventArgs);
96 assertTrue(outputString.contains("gen-model2event: no event type given, cannot proceed (try -h for help)"));
100 public void testModel2EventBadType() {
101 final String[] EventArgs =
102 { "-m", "src/test/resources/models/AvroModel.json", "-t", "Zooby" };
104 final String outputString = runModel2Event(EventArgs);
106 assertTrue(outputString.contains("gen-model2event: unknown type <Zooby>, cannot proceed (try -h for help)"));
110 public void testModel2EventAadm() throws IOException {
111 testModel2EventModel("AADMPolicyModel");
115 public void testModel2EventAnomaly() {
116 testModel2EventModel("AnomalyDetectionPolicyModel");
120 public void testModel2EventAutoLearn() {
121 testModel2EventModel("AutoLearnPolicyModel");
125 public void testModel2EventMfp() {
126 testModel2EventModel("MyFirstPolicyModel");
130 public void testModel2EventSample() {
131 testModel2EventModel("SamplePolicyModelJAVASCRIPT");
135 * Run the application.
137 * @param eventArgs the command arguments
138 * @return a string containing the command output
140 private String runModel2Event(final String[] eventArgs) {
141 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
142 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
144 new Model2EventMain(eventArgs, new PrintStream(baosOut, true));
146 String outString = baosOut.toString();
147 String errString = baosErr.toString();
149 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
153 * Test Event generation.
155 * @param modelName the name of the model file
157 private void testModel2EventModel(String modelName) {
159 File tempFile = File.createTempFile(modelName, ".apex");
160 tempFile.deleteOnExit();
162 final String[] eventArgs0 =
163 { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" };
164 final String outputString0 = runModel2Event(eventArgs0);
166 assertTrue(outputString0.contains("type: stimuli"));
168 final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" };
169 final String outputString1 = runModel2Event(eventArgs1);
171 assertTrue(outputString1.contains("type: response"));
173 final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" };
174 final String outputString2 = runModel2Event(eventArgs2);
176 assertTrue(outputString2.contains("type: internal"));
177 } catch (Exception e) {
178 throw new ApexRuntimeException("test should not throw an exception", e);