dcbafba07a4a8a52d9486f6608c24e78047478ce
[policy/apex-pdp.git] / testsuites / performance / performance-benchmark-test / src / test / java / org / onap / policy / apex / testsuites / performance / benchmark / eventgenerator / EventGeneratorTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.File;
28 import java.io.PrintStream;
29
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;
36
37 /**
38  * This class tests the event generator.
39  */
40 public class EventGeneratorTest {
41     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
42
43     private final PrintStream stdout = System.out;
44
45     /**
46      * Test event generation.
47      *
48      * @throws ApexException on Apex exceptions
49      */
50     @Test
51     public void testEventGeneration() throws ApexException {
52         EventGeneratorParameters pars = new EventGeneratorParameters();
53         pars.setBatchCount(1);
54         pars.setBatchSize(10);
55
56         EventGenerator eventGenerator = new EventGenerator(pars);
57
58         final String[] args = { "-rfr", "target", "-c",
59             "target/examples/config/SampleDomain/REST2RESTJsonEventJavascript.json" };
60
61         final ApexMain apexMain = new ApexMain(args);
62
63         while (!eventGenerator.isFinished()) {
64             ThreadUtilities.sleep(200);
65         }
66
67         apexMain.shutdown();
68
69         ThreadUtilities.sleep(5000);
70         eventGenerator.tearDown();
71
72         assertTrue(eventGenerator.getEventGenerationStats().contains("\"apexClient\": \"TOTAL\""));
73     }
74
75     @Test
76     public void testEventGeneratorBadParams() {
77         System.setOut(new PrintStream(outContent));
78
79         final String[] args = { "-zzz" };
80
81         EventGenerator.main(args);
82
83         final String outString = outContent.toString();
84
85         System.setOut(stdout);
86
87         assertTrue(outString.contains("Start of event generator failed: Unrecognized option: -zzz"));
88     }
89
90     @Test
91     public void testEventGeneratorHelp() {
92         System.setOut(new PrintStream(outContent));
93
94         final String[] args = { "-h" };
95
96         EventGenerator.main(args);
97
98         final String outString = outContent.toString();
99
100         System.setOut(stdout);
101
102         assertTrue(outString.contains("outputs the usage of this command"));
103     }
104
105     @Test
106     public void testEventGeneratorStart() {
107
108         System.setOut(new PrintStream(outContent));
109
110         (new Thread() {
111             public void run() {
112                 EventGenerator.main(null);
113             }
114         }).start();
115
116         ThreadUtilities.sleep(1000);
117         final String outString = outContent.toString();
118
119         System.setOut(stdout);
120
121         assertTrue(outString.contains("Event generator started"));
122         assertTrue(outString.contains("Event generator shut down"));
123     }
124
125     @Test
126     public void testEventGeneratorOutfileGood() {
127         EventGeneratorParameters pars = new EventGeneratorParameters();
128         pars.setOutFile("target/statsOutFile.json");
129
130         EventGenerator generator = new EventGenerator(pars);
131         assertNotNull(generator);
132
133         generator.tearDown();
134
135         File outFile = new File("target/statsOutFile.json");
136         assertTrue(outFile.exists());
137         outFile.delete();
138     }
139
140     @Test
141     public void testEventGeneratorOutfileBad() {
142         EventGeneratorParameters pars = new EventGeneratorParameters();
143         pars.setOutFile("/I/Dont/Exist*");
144
145         EventGenerator generator = new EventGenerator(pars);
146         assertNotNull(generator);
147
148         System.setOut(new PrintStream(outContent));
149
150         generator.tearDown();
151
152         final String outString = outContent.toString();
153         System.setOut(stdout);
154         assertTrue(outString.contains("could not output statistics to file \"/I/Dont/Exist*\""));
155     }
156 }