2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 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.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import java.io.ByteArrayOutputStream;
29 import java.io.PrintStream;
30 import org.junit.Test;
31 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.service.engine.main.ApexMain;
37 * This class tests the event generator.
39 public class EventGeneratorTest {
40 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
42 private final PrintStream stdout = System.out;
45 * Test event generation.
47 * @throws ApexException on Apex exceptions
50 public void testEventGeneration() throws ApexException {
51 EventGeneratorParameters pars = new EventGeneratorParameters();
52 pars.setBatchCount(1);
53 pars.setBatchSize(10);
55 EventGenerator eventGenerator = new EventGenerator(pars);
56 final String[] args = { "-rfr", "target", "-p",
57 "target/examples/config/SampleDomain/REST2RESTJsonEventJavascript.json" };
59 final ApexMain apexMain = new ApexMain(args);
61 while (!eventGenerator.isFinished()) {
62 ThreadUtilities.sleep(200);
67 ThreadUtilities.sleep(5000);
68 eventGenerator.tearDown();
70 assertTrue(eventGenerator.getEventGenerationStats().contains("\"apexClient\": \"TOTAL\""));
74 public void testEventGeneratorBadParams() {
75 System.setOut(new PrintStream(outContent));
77 final String[] args = { "-zzz" };
79 EventGenerator.main(args);
81 final String outString = outContent.toString();
83 System.setOut(stdout);
85 assertTrue(outString.contains("Start of event generator failed: Unrecognized option: -zzz"));
89 public void testEventGeneratorHelp() {
90 System.setOut(new PrintStream(outContent));
92 final String[] args = { "-h" };
94 EventGenerator.main(args);
96 final String outString = outContent.toString();
98 System.setOut(stdout);
100 assertTrue(outString.contains("outputs the usage of this command"));
104 public void testEventGeneratorStart() {
106 System.setOut(new PrintStream(outContent));
111 EventGenerator.main(null);
115 ThreadUtilities.sleep(1000);
116 final String outString = outContent.toString();
118 System.setOut(stdout);
120 assertTrue(outString.contains("Event generator started"));
121 assertTrue(outString.contains("Event generator shut down"));
125 public void testEventGeneratorOutfileGood() {
126 EventGeneratorParameters pars = new EventGeneratorParameters();
127 pars.setOutFile("target/statsOutFile.json");
129 EventGenerator generator = new EventGenerator(pars);
130 assertNotNull(generator);
132 generator.tearDown();
134 File outFile = new File("target/statsOutFile.json");
135 assertTrue(outFile.exists());
140 public void testEventGeneratorOutfileBad() {
141 EventGeneratorParameters pars = new EventGeneratorParameters();
142 pars.setOutFile("/I/Dont/Exist*");
144 EventGenerator generator = new EventGenerator(pars);
145 assertNotNull(generator);
147 System.setOut(new PrintStream(outContent));
149 generator.tearDown();
151 final String outString = outContent.toString();
152 System.setOut(stdout);
153 assertTrue(outString.contains("could not output statistics to file \"/I/Dont/Exist*\""));