08860b013473de7195cd3f48f4f2f5e45477bb1d
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2021 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.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.PrintStream;
31 import org.junit.Test;
32 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
33 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
34 import org.onap.policy.apex.service.engine.main.ApexMain;
35
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         final String[] args = { "-rfr", "target", "-p",
58             "target/examples/config/SampleDomain/REST2RESTJsonEventJavascript.json" };
59
60         final ApexMain apexMain = new ApexMain(args);
61
62         while (!eventGenerator.isFinished()) {
63             ThreadUtilities.sleep(200);
64         }
65
66         apexMain.shutdown();
67
68         ThreadUtilities.sleep(5000);
69         eventGenerator.tearDown();
70
71         assertTrue(eventGenerator.getEventGenerationStats().contains("\"apexClient\": \"TOTAL\""));
72     }
73
74     @Test
75     public void testEventGeneratorBadParams() {
76         System.setOut(new PrintStream(outContent));
77
78         final String[] args = { "-zzz" };
79
80         EventGenerator.main(args);
81
82         final String outString = outContent.toString();
83
84         System.setOut(stdout);
85
86         assertTrue(outString.contains("Start of event generator failed: Unrecognized option: -zzz"));
87     }
88
89     @Test
90     public void testEventGeneratorHelp() {
91         System.setOut(new PrintStream(outContent));
92
93         final String[] args = { "-h" };
94
95         EventGenerator.main(args);
96
97         final String outString = outContent.toString();
98
99         System.setOut(stdout);
100
101         assertTrue(outString.contains("outputs the usage of this command"));
102     }
103
104     @Test
105     public void testEventGeneratorStart() {
106
107         System.setOut(new PrintStream(outContent));
108
109         (new Thread() {
110             @Override
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\0");
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         assertThat(outString).contains("could not output statistics to file \"/I/Dont/Exist\0\"");
155     }
156 }