2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-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.testsuites.performance.benchmark.eventgenerator;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
26 import java.io.ByteArrayOutputStream;
28 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;
34 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.EventGenerator;
35 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.EventGeneratorParameters;
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);
58 final String[] args = { "-rfr", "target", "-c",
59 "target/examples/config/SampleDomain/REST2RESTJsonEventJavascript.json" };
61 final ApexMain apexMain = new ApexMain(args);
63 while (!eventGenerator.isFinished()) {
64 ThreadUtilities.sleep(200);
69 ThreadUtilities.sleep(5000);
70 eventGenerator.tearDown();
72 assertTrue(eventGenerator.getEventGenerationStats().contains("\"apexClient\": \"TOTAL\""));
76 public void testEventGeneratorBadParams() {
77 System.setOut(new PrintStream(outContent));
79 final String[] args = { "-zzz" };
81 EventGenerator.main(args);
83 final String outString = outContent.toString();
85 System.setOut(stdout);
87 assertTrue(outString.contains("Start of event generator failed: Unrecognized option: -zzz"));
91 public void testEventGeneratorHelp() {
92 System.setOut(new PrintStream(outContent));
94 final String[] args = { "-h" };
96 EventGenerator.main(args);
98 final String outString = outContent.toString();
100 System.setOut(stdout);
102 assertTrue(outString.contains("outputs the usage of this command"));
106 public void testEventGeneratorStart() {
108 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*");
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 assertTrue(outString.contains("could not output statistics to file \"/I/Dont/Exist*\""));