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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.tools.model.generator.model2event;
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;
29 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import org.junit.Test;
36 * Test the Model2Event utility.
38 public class Model2EventTest {
40 public void testModel2Event() {
41 final String[] EventArgs =
44 assertThatCode(() -> Model2EventMain.main(EventArgs)).doesNotThrowAnyException();
49 public void testModel2EventNoOptions() {
50 final String[] EventArgs = new String[]
53 final String outputString = runModel2Event(EventArgs);
55 assertTrue(outputString.contains("gen-model2event: no model file given, cannot proceed (try -h for help)"));
59 public void testModel2EventBadOptions() {
60 assertThat(runModel2Event(new String[] {"-zabbu"})).contains("usage: gen-model2event");
64 public void testModel2EventHelp() {
65 assertThat(runModel2Event(new String[] {"-h"})).contains("usage: gen-model2event");
69 public void testModel2EventVersion() {
70 assertThat(runModel2Event(new String[] {"-v"})).contains("gen-model2event").doesNotContain("usage:");
74 public void testModel2EventNoType() {
75 final String[] EventArgs =
76 { "-m", "src/test/resources/models/AvroModel.json" };
78 final String outputString = runModel2Event(EventArgs);
80 assertTrue(outputString.contains("gen-model2event: no event type given, cannot proceed (try -h for help)"));
84 public void testModel2EventBadType() {
85 final String[] EventArgs =
86 { "-m", "src/test/resources/models/AvroModel.json", "-t", "Zooby" };
88 final String outputString = runModel2Event(EventArgs);
90 assertTrue(outputString.contains("gen-model2event: unknown type <Zooby>, cannot proceed (try -h for help)"));
94 public void testModel2EventAadm() throws IOException {
95 testModel2EventModel("AADMPolicyModel");
99 public void testModel2EventAnomaly() throws IOException {
100 testModel2EventModel("AnomalyDetectionPolicyModel");
104 public void testModel2EventAutoLearn() throws IOException {
105 testModel2EventModel("AutoLearnPolicyModel");
109 public void testModel2EventMfp() throws IOException {
110 testModel2EventModel("MyFirstPolicyModel");
114 public void testModel2EventSample() throws IOException {
115 testModel2EventModel("SamplePolicyModelJAVASCRIPT");
119 * Run the application.
121 * @param eventArgs the command arguments
122 * @return a string containing the command output
124 private String runModel2Event(final String[] eventArgs) {
125 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
126 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
128 new Model2EventMain(eventArgs, new PrintStream(baosOut, true));
130 String outString = baosOut.toString();
131 String errString = baosErr.toString();
133 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
137 * Test Event generation.
139 * @param modelName the name of the model file
141 private void testModel2EventModel(String modelName) throws IOException {
142 File tempFile = File.createTempFile(modelName, ".apex");
143 tempFile.deleteOnExit();
145 final String[] eventArgs0 =
146 { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" };
147 final String outputString0 = runModel2Event(eventArgs0);
149 assertTrue(outputString0.contains("type: stimuli"));
151 final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" };
152 final String outputString1 = runModel2Event(eventArgs1);
154 assertTrue(outputString1.contains("type: response"));
156 final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" };
157 final String outputString2 = runModel2Event(eventArgs2);
159 assertTrue(outputString2.contains("type: internal"));