APEX standalone support for ToscaPolicy format
[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  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
29 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
35
36 /**
37  * This class tests the event generator.
38  */
39 public class EventGeneratorTest {
40     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
41
42     private final PrintStream stdout = System.out;
43
44     /**
45      * Test event generation.
46      *
47      * @throws ApexException on Apex exceptions
48      */
49     @Test
50     public void testEventGeneration() throws ApexException {
51         EventGeneratorParameters pars = new EventGeneratorParameters();
52         pars.setBatchCount(1);
53         pars.setBatchSize(10);
54
55         EventGenerator eventGenerator = new EventGenerator(pars);
56         final String[] args = { "-rfr", "target", "-p",
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             @Override
110             public void run() {
111                 EventGenerator.main(null);
112             }
113         }).start();
114
115         ThreadUtilities.sleep(1000);
116         final String outString = outContent.toString();
117
118         System.setOut(stdout);
119
120         assertTrue(outString.contains("Event generator started"));
121         assertTrue(outString.contains("Event generator shut down"));
122     }
123
124     @Test
125     public void testEventGeneratorOutfileGood() {
126         EventGeneratorParameters pars = new EventGeneratorParameters();
127         pars.setOutFile("target/statsOutFile.json");
128
129         EventGenerator generator = new EventGenerator(pars);
130         assertNotNull(generator);
131
132         generator.tearDown();
133
134         File outFile = new File("target/statsOutFile.json");
135         assertTrue(outFile.exists());
136         outFile.delete();
137     }
138
139     @Test
140     public void testEventGeneratorOutfileBad() {
141         EventGeneratorParameters pars = new EventGeneratorParameters();
142         pars.setOutFile("/I/Dont/Exist*");
143
144         EventGenerator generator = new EventGenerator(pars);
145         assertNotNull(generator);
146
147         System.setOut(new PrintStream(outContent));
148
149         generator.tearDown();
150
151         final String outString = outContent.toString();
152         System.setOut(stdout);
153         assertTrue(outString.contains("could not output statistics to file \"/I/Dont/Exist*\""));
154     }
155 }