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.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertTrue;
27 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import org.junit.Test;
34 * Test the Model2Event utility.
36 public class Model2EventTest {
38 public void testModel2Event() {
39 final String[] EventArgs =
42 assertThatCode(() -> Model2EventMain.main(EventArgs)).doesNotThrowAnyException();
47 public void testModel2EventNoOptions() {
48 final String[] EventArgs = new String[]
51 final String outputString = runModel2Event(EventArgs);
53 assertTrue(outputString.contains("gen-model2event: no model file given, cannot proceed (try -h for help)"));
57 public void testModel2EventBadOptions() {
58 final String[] EventArgs =
61 final String outputString = runModel2Event(EventArgs);
63 assertTrue(outputString.contains("usage: gen-model2event"));
67 public void testModel2EventHelp() {
68 final String[] EventArgs =
71 final String outputString = runModel2Event(EventArgs);
73 assertTrue(outputString.contains("usage: gen-model2event"));
77 public void testModel2EventVersion() {
78 final String[] EventArgs =
81 final String outputString = runModel2Event(EventArgs);
83 assertTrue(outputString.contains("gen-model2event"));
87 public void testModel2EventNoType() {
88 final String[] EventArgs =
89 { "-m", "src/test/resources/models/AvroModel.json" };
91 final String outputString = runModel2Event(EventArgs);
93 assertTrue(outputString.contains("gen-model2event: no event type given, cannot proceed (try -h for help)"));
97 public void testModel2EventBadType() {
98 final String[] EventArgs =
99 { "-m", "src/test/resources/models/AvroModel.json", "-t", "Zooby" };
101 final String outputString = runModel2Event(EventArgs);
103 assertTrue(outputString.contains("gen-model2event: unknown type <Zooby>, cannot proceed (try -h for help)"));
107 public void testModel2EventAadm() throws IOException {
108 testModel2EventModel("AADMPolicyModel");
112 public void testModel2EventAnomaly() throws IOException {
113 testModel2EventModel("AnomalyDetectionPolicyModel");
117 public void testModel2EventAutoLearn() throws IOException {
118 testModel2EventModel("AutoLearnPolicyModel");
122 public void testModel2EventMfp() throws IOException {
123 testModel2EventModel("MyFirstPolicyModel");
127 public void testModel2EventSample() throws IOException {
128 testModel2EventModel("SamplePolicyModelJAVASCRIPT");
132 * Run the application.
134 * @param eventArgs the command arguments
135 * @return a string containing the command output
137 private String runModel2Event(final String[] eventArgs) {
138 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
139 final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
141 new Model2EventMain(eventArgs, new PrintStream(baosOut, true));
143 String outString = baosOut.toString();
144 String errString = baosErr.toString();
146 return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
150 * Test Event generation.
152 * @param modelName the name of the model file
154 private void testModel2EventModel(String modelName) throws IOException {
155 File tempFile = File.createTempFile(modelName, ".apex");
156 tempFile.deleteOnExit();
158 final String[] eventArgs0 =
159 { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" };
160 final String outputString0 = runModel2Event(eventArgs0);
162 assertTrue(outputString0.contains("type: stimuli"));
164 final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" };
165 final String outputString1 = runModel2Event(eventArgs1);
167 assertTrue(outputString1.contains("type: response"));
169 final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" };
170 final String outputString2 = runModel2Event(eventArgs2);
172 assertTrue(outputString2.contains("type: internal"));