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