2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
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.testsuites.performance.benchmark.eventgenerator;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
28 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
31 import org.junit.Test;
32 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
33 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
34 import org.onap.policy.apex.service.engine.main.ApexMain;
38 * This class tests the event generator.
40 public class EventGeneratorTest {
41 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
43 private final PrintStream stdout = System.out;
46 * Test event generation.
48 * @throws ApexException on Apex exceptions
51 public void testEventGeneration() throws ApexException {
52 EventGeneratorParameters pars = new EventGeneratorParameters();
53 pars.setBatchCount(1);
54 pars.setBatchSize(10);
56 EventGenerator eventGenerator = new EventGenerator(pars);
57 final String[] args = { "-rfr", "target", "-p",
58 "target/examples/config/SampleDomain/REST2RESTJsonEventJavascript.json" };
60 final ApexMain apexMain = new ApexMain(args);
62 while (!eventGenerator.isFinished()) {
63 ThreadUtilities.sleep(200);
68 ThreadUtilities.sleep(5000);
69 eventGenerator.tearDown();
71 assertTrue(eventGenerator.getEventGenerationStats().contains("\"apexClient\": \"TOTAL\""));
75 public void testEventGeneratorBadParams() {
76 System.setOut(new PrintStream(outContent));
78 final String[] args = { "-zzz" };
80 EventGenerator.main(args);
82 final String outString = outContent.toString();
84 System.setOut(stdout);
86 assertTrue(outString.contains("Start of event generator failed: Unrecognized option: -zzz"));
90 public void testEventGeneratorHelp() {
91 System.setOut(new PrintStream(outContent));
93 final String[] args = { "-h" };
95 EventGenerator.main(args);
97 final String outString = outContent.toString();
99 System.setOut(stdout);
101 assertTrue(outString.contains("outputs the usage of this command"));
105 public void testEventGeneratorStart() {
107 System.setOut(new PrintStream(outContent));
112 EventGenerator.main(null);
116 ThreadUtilities.sleep(1000);
117 final String outString = outContent.toString();
119 System.setOut(stdout);
121 assertTrue(outString.contains("Event generator started"));
122 assertTrue(outString.contains("Event generator shut down"));
126 public void testEventGeneratorOutfileGood() {
127 EventGeneratorParameters pars = new EventGeneratorParameters();
128 pars.setOutFile("target/statsOutFile.json");
130 EventGenerator generator = new EventGenerator(pars);
131 assertNotNull(generator);
133 generator.tearDown();
135 File outFile = new File("target/statsOutFile.json");
136 assertTrue(outFile.exists());
141 public void testEventGeneratorOutfileBad() {
142 EventGeneratorParameters pars = new EventGeneratorParameters();
143 pars.setOutFile("/I/Dont/Exist\0");
145 EventGenerator generator = new EventGenerator(pars);
146 assertNotNull(generator);
148 System.setOut(new PrintStream(outContent));
150 generator.tearDown();
152 final String outString = outContent.toString();
153 System.setOut(stdout);
154 assertThat(outString).contains("could not output statistics to file \"/I/Dont/Exist\0\"");