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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.tools.model.generator.model2event;
24 import static org.junit.Assert.assertTrue;
26 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import org.junit.Test;
33 * Test the Model2Event utility.
35 public class Model2EventTest {
37 public void testModel2Event() {
38 final String[] EventArgs =
41 Model2EventMain.main(EventArgs);
46 public void testModel2EventNoOptions() {
47 final String[] EventArgs = new String[]
50 final String outputString = runModel2Event(EventArgs);
52 assertTrue(outputString.contains("gen-model2event: no model file given, cannot proceed (try -h for help)"));
56 public void testModel2EventBadOptions() {
57 final String[] EventArgs =
60 final String outputString = runModel2Event(EventArgs);
62 assertTrue(outputString.contains("usage: gen-model2event"));
66 public void testModel2EventHelp() {
67 final String[] EventArgs =
70 final String outputString = runModel2Event(EventArgs);
72 assertTrue(outputString.contains("usage: gen-model2event"));
76 public void testModel2EventVersion() {
77 final String[] EventArgs =
80 final String outputString = runModel2Event(EventArgs);
82 assertTrue(outputString.contains("gen-model2event"));
86 public void testModel2EventNoType() {
87 final String[] EventArgs =
88 { "-m", "src/test/resources/models/AvroModel.json" };
90 final String outputString = runModel2Event(EventArgs);
92 assertTrue(outputString.contains("gen-model2event: no event type given, cannot proceed (try -h for help)"));
96 public void testModel2EventBadType() {
97 final String[] EventArgs =
98 { "-m", "src/test/resources/models/AvroModel.json", "-t", "Zooby" };
100 final String outputString = runModel2Event(EventArgs);
102 assertTrue(outputString.contains("gen-model2event: unknown type <Zooby>, cannot proceed (try -h for help)"));
106 public void testModel2EventAadm() throws IOException {
107 testModel2EventModel("AADMPolicyModel");
111 public void testModel2EventAnomaly() throws IOException {
112 testModel2EventModel("AnomalyDetectionPolicyModel");
116 public void testModel2EventAutoLearn() throws IOException {
117 testModel2EventModel("AutoLearnPolicyModel");
121 public void testModel2EventMfp() throws IOException {
122 testModel2EventModel("MyFirstPolicyModel");
126 public void testModel2EventSample() throws IOException {
127 testModel2EventModel("SamplePolicyModelJAVASCRIPT");
131 * Run the application.
133 * @param eventArgs the command arguments
134 * @return a string containing the command output
136 private String runModel2Event(final String[] eventArgs) {
137 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
138 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
140 new Model2EventMain(eventArgs, new PrintStream(baosOut, true));
142 String outString = baosOut.toString();
143 String errString = baosErr.toString();
145 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
149 * Test Event generation.
151 * @param modelName the name of the model file
153 private void testModel2EventModel(String modelName) throws IOException {
154 File tempFile = File.createTempFile(modelName, ".apex");
155 tempFile.deleteOnExit();
157 final String[] eventArgs0 =
158 { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" };
159 final String outputString0 = runModel2Event(eventArgs0);
161 assertTrue(outputString0.contains("type: stimuli"));
163 final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" };
164 final String outputString1 = runModel2Event(eventArgs1);
166 assertTrue(outputString1.contains("type: response"));
168 final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" };
169 final String outputString2 = runModel2Event(eventArgs2);
171 assertTrue(outputString2.contains("type: internal"));