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);
59 { "-rfr", "target", "-c", "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));
82 EventGenerator.main(args);
84 final String outString = outContent.toString();
86 System.setOut(stdout);
88 assertTrue(outString.contains("Start of event generator failed: Unrecognized option: -zzz"));
92 public void testEventGeneratorHelp() {
93 System.setOut(new PrintStream(outContent));
95 final String[] args = {
99 EventGenerator.main(args);
101 final String outString = outContent.toString();
103 System.setOut(stdout);
105 assertTrue(outString.contains("outputs the usage of this command"));
109 public void testEventGeneratorStart() {
111 System.setOut(new PrintStream(outContent));
115 EventGenerator.main(null);
119 ThreadUtilities.sleep(1000);
120 final String outString = outContent.toString();
122 System.setOut(stdout);
124 assertTrue(outString.contains("Event generator started"));
125 assertTrue(outString.contains("Event generator shut down"));
129 public void testEventGeneratorOutfileGood() {
130 EventGeneratorParameters pars =new EventGeneratorParameters();
131 pars.setOutFile("target/statsOutFile.json");
133 EventGenerator generator = new EventGenerator(pars);
134 assertNotNull(generator);
136 generator.tearDown();
138 File outFile = new File("target/statsOutFile.json");
139 assertTrue(outFile.exists());
144 public void testEventGeneratorOutfileBad() {
145 EventGeneratorParameters pars = new EventGeneratorParameters();
146 pars.setOutFile("/I/Dont/Exist");
148 EventGenerator generator = new EventGenerator(pars);
149 assertNotNull(generator);
151 System.setOut(new PrintStream(outContent));
153 generator.tearDown();
155 final String outString = outContent.toString();
156 System.setOut(stdout);
158 assertTrue(outString.contains("could not output statistics to file \"/I/Dont/Exist\""));