e37ea963c40485069e4afcb38b9ce4619db20d56
[policy/apex-pdp.git] /
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 =
59             { "-rfr", "target", "-c", "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 =
80             { "-zzz" };
81
82         EventGenerator.main(args);
83
84         final String outString = outContent.toString();
85
86         System.setOut(stdout);
87
88         assertTrue(outString.contains("Start of event generator failed: Unrecognized option: -zzz"));
89     }
90
91     @Test
92     public void testEventGeneratorHelp() {
93         System.setOut(new PrintStream(outContent));
94
95         final String[] args = {
96             "-h"
97         };
98
99         EventGenerator.main(args);
100
101         final String outString = outContent.toString();
102
103         System.setOut(stdout);
104
105         assertTrue(outString.contains("outputs the usage of this command"));
106     }
107
108     @Test
109     public void testEventGeneratorStart() {
110
111         System.setOut(new PrintStream(outContent));
112
113         (new Thread() {
114             public void run() {
115                 EventGenerator.main(null);
116             }
117            }).start();
118
119         ThreadUtilities.sleep(1000);
120         final String outString = outContent.toString();
121
122         System.setOut(stdout);
123
124         assertTrue(outString.contains("Event generator started"));
125         assertTrue(outString.contains("Event generator shut down"));
126     }
127
128     @Test
129     public void testEventGeneratorOutfileGood() {
130         EventGeneratorParameters pars =new EventGeneratorParameters();
131         pars.setOutFile("target/statsOutFile.json");
132
133         EventGenerator generator = new EventGenerator(pars);
134         assertNotNull(generator);
135
136         generator.tearDown();
137
138         File outFile = new File("target/statsOutFile.json");
139         assertTrue(outFile.exists());
140         outFile.delete();
141     }
142
143     @Test
144     public void testEventGeneratorOutfileBad() {
145         EventGeneratorParameters pars = new EventGeneratorParameters();
146         pars.setOutFile("/I/Dont/Exist");
147
148         EventGenerator generator = new EventGenerator(pars);
149         assertNotNull(generator);
150
151         System.setOut(new PrintStream(outContent));
152
153         generator.tearDown();
154
155         final String outString = outContent.toString();
156         System.setOut(stdout);
157
158         assertTrue(outString.contains("could not output statistics to file \"/I/Dont/Exist\""));
159     }
160 }