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;
29 import org.junit.Test;
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.apex.service.engine.main.ApexMain;
36 * This class tests the event generator.
38 public class EventGeneratorTest {
39 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
41 private final PrintStream stdout = System.out;
44 * Test event generation.
46 * @throws ApexException on Apex exceptions
49 public void testEventGeneration() throws ApexException {
50 EventGeneratorParameters pars = new EventGeneratorParameters();
51 pars.setBatchCount(1);
52 pars.setBatchSize(10);
54 EventGenerator eventGenerator = new EventGenerator(pars);
56 final String[] args = { "-rfr", "target", "-c",
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));
110 EventGenerator.main(null);
114 ThreadUtilities.sleep(1000);
115 final String outString = outContent.toString();
117 System.setOut(stdout);
119 assertTrue(outString.contains("Event generator started"));
120 assertTrue(outString.contains("Event generator shut down"));
124 public void testEventGeneratorOutfileGood() {
125 EventGeneratorParameters pars = new EventGeneratorParameters();
126 pars.setOutFile("target/statsOutFile.json");
128 EventGenerator generator = new EventGenerator(pars);
129 assertNotNull(generator);
131 generator.tearDown();
133 File outFile = new File("target/statsOutFile.json");
134 assertTrue(outFile.exists());
139 public void testEventGeneratorOutfileBad() {
140 EventGeneratorParameters pars = new EventGeneratorParameters();
141 pars.setOutFile("/I/Dont/Exist*");
143 EventGenerator generator = new EventGenerator(pars);
144 assertNotNull(generator);
146 System.setOut(new PrintStream(outContent));
148 generator.tearDown();
150 final String outString = outContent.toString();
151 System.setOut(stdout);
152 assertTrue(outString.contains("could not output statistics to file \"/I/Dont/Exist*\""));