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;
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
35 * Test the Model2Event utility.
37 public class Model2EventTest {
39 public void testModel2Event() {
41 final String[] EventArgs =
44 Model2EventMain.main(EventArgs);
45 } catch (Exception exc) {
46 fail("test should not throw an exception");
51 public void testModel2EventNoOptions() {
52 final String[] EventArgs = new String[]
55 final String outputString = runModel2Event(EventArgs);
57 assertTrue(outputString.contains("gen-model2event: no model file given, cannot proceed (try -h for help)"));
61 public void testModel2EventBadOptions() {
62 final String[] EventArgs =
65 final String outputString = runModel2Event(EventArgs);
67 assertTrue(outputString.contains("usage: gen-model2event"));
71 public void testModel2EventHelp() {
72 final String[] EventArgs =
75 final String outputString = runModel2Event(EventArgs);
77 assertTrue(outputString.contains("usage: gen-model2event"));
81 public void testModel2EventVersion() {
82 final String[] EventArgs =
85 final String outputString = runModel2Event(EventArgs);
87 assertTrue(outputString.contains("gen-model2event"));
91 public void testModel2EventNoType() {
92 final String[] EventArgs =
93 { "-m", "src/test/resources/models/AvroModel.json" };
95 final String outputString = runModel2Event(EventArgs);
97 assertTrue(outputString.contains("gen-model2event: no event type given, cannot proceed (try -h for help)"));
101 public void testModel2EventBadType() {
102 final String[] EventArgs =
103 { "-m", "src/test/resources/models/AvroModel.json", "-t", "Zooby" };
105 final String outputString = runModel2Event(EventArgs);
107 assertTrue(outputString.contains("gen-model2event: unknown type <Zooby>, cannot proceed (try -h for help)"));
111 public void testModel2EventAadm() throws IOException {
112 testModel2EventModel("AADMPolicyModel");
116 public void testModel2EventAnomaly() {
117 testModel2EventModel("AnomalyDetectionPolicyModel");
121 public void testModel2EventAutoLearn() {
122 testModel2EventModel("AutoLearnPolicyModel");
126 public void testModel2EventMfp() {
127 testModel2EventModel("MyFirstPolicyModel");
131 public void testModel2EventSample() {
132 testModel2EventModel("SamplePolicyModelJAVASCRIPT");
136 * Run the application.
138 * @param eventArgs the command arguments
139 * @return a string containing the command output
141 private String runModel2Event(final String[] eventArgs) {
142 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
143 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
145 new Model2EventMain(eventArgs, new PrintStream(baosOut, true));
147 String outString = baosOut.toString();
148 String errString = baosErr.toString();
150 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
154 * Test Event generation.
156 * @param modelName the name of the model file
158 private void testModel2EventModel(String modelName) {
160 File tempFile = File.createTempFile(modelName, ".apex");
161 tempFile.deleteOnExit();
163 final String[] eventArgs0 =
164 { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" };
165 final String outputString0 = runModel2Event(eventArgs0);
167 assertTrue(outputString0.contains("type: stimuli"));
169 final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" };
170 final String outputString1 = runModel2Event(eventArgs1);
172 assertTrue(outputString1.contains("type: response"));
174 final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" };
175 final String outputString2 = runModel2Event(eventArgs2);
177 assertTrue(outputString2.contains("type: internal"));
178 } catch (Exception e) {
179 throw new ApexRuntimeException("test should not throw an exception", e);