Convert junit4 to junit5
[policy/apex-pdp.git] / testsuites / integration / integration-uservice-test / src / test / java / org / onap / policy / apex / testsuites / integration / uservice / adapt / events / EventGenerator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2024 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.events;
24
25 import java.util.Random;
26 import lombok.Getter;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The Class EventGenerator.
32  *
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  */
35 public class EventGenerator {
36     private static final Logger LOGGER = LoggerFactory.getLogger(EventGenerator.class);
37
38     @Getter
39     private static int nextEventNo = 0;
40
41     /**
42      * Xml events.
43      *
44      * @param eventCount the event count
45      * @return the string
46      */
47     public static String xmlEvents(final int eventCount) {
48         final StringBuilder builder = new StringBuilder();
49
50         for (int i = 0; i < eventCount; i++) {
51             if (i > 0) {
52                 builder.append("\n");
53             }
54             builder.append(xmlEvent());
55         }
56
57         return builder.toString();
58     }
59
60     /**
61      * Json events.
62      *
63      * @param eventCount the event count
64      * @return the string
65      */
66     public static String jsonEvents(final int eventCount) {
67         final StringBuilder builder = new StringBuilder();
68
69         for (int i = 0; i < eventCount; i++) {
70             if (i > 0) {
71                 builder.append("\n");
72             }
73             builder.append(jsonEvent());
74         }
75
76         return builder.toString();
77     }
78
79     /**
80      * Xml event.
81      *
82      * @return the string
83      */
84     public static String xmlEvent() {
85         final Random rand = new Random();
86
87         final StringBuilder builder = new StringBuilder();
88
89         int nextEventNo = rand.nextInt(2);
90         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
91         final int nextMatchCase = rand.nextInt(4);
92         final float nextTestTemperature = rand.nextFloat() * 10000;
93
94         builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
95         builder.append("<xmlApexEvent xmlns=\"http://www.onap.org/policy/apex-pdp/apexevent\">\n");
96         builder.append("  <name>").append(eventName).append("</name>\n");
97         builder.append("  <version>0.0.1</version>\n");
98         builder.append("  <nameSpace>org.onap.policy.apex.sample.events</nameSpace>\n");
99         builder.append("  <source>test</source>\n");
100         builder.append("  <target>apex</target>\n");
101         builder.append("  <data>\n");
102         builder.append("    <key>TestSlogan</key>\n");
103         nextEventNo += 1;
104         builder.append("    <value>Test slogan for External Event").append(nextEventNo).append("</value>\n");
105         builder.append("  </data>\n");
106         builder.append("  <data>\n");
107         builder.append("    <key>TestMatchCase</key>\n");
108         builder.append("    <value>").append(nextMatchCase).append("</value>\n");
109         builder.append("  </data>\n");
110         builder.append("  <data>\n");
111         builder.append("    <key>TestTimestamp</key>\n");
112         builder.append("    <value>").append(System.currentTimeMillis()).append("</value>\n");
113         builder.append("  </data>\n");
114         builder.append("  <data>\n");
115         builder.append("    <key>TestTemperature</key>\n");
116         builder.append("    <value>").append(nextTestTemperature).append("</value>\n");
117         builder.append("  </data>\n");
118         builder.append("</xmlApexEvent>");
119
120         return builder.toString();
121     }
122
123     /**
124      * Json event.
125      *
126      * @return the string
127      */
128     public static String jsonEvent() {
129         final Random rand = new Random();
130
131         final StringBuilder builder = new StringBuilder();
132
133         int nextEventNo = rand.nextInt(2);
134         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
135         final int nextMatchCase = rand.nextInt(4);
136         final float nextTestTemperature = rand.nextFloat() * 10000;
137
138         builder.append("{\n");
139         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
140         builder.append("  \"name\": \"").append(eventName).append("\",\n");
141         builder.append("  \"version\": \"0.0.1\",\n");
142         builder.append("  \"source\": \"test\",\n");
143         builder.append("  \"target\": \"apex\",\n");
144         nextEventNo += 1;
145         builder.append("  \"TestSlogan\": \"Test slogan for External Event").append(nextEventNo).append("\",\n");
146         builder.append("  \"TestMatchCase\": ").append(nextMatchCase).append(",\n");
147         builder.append("  \"TestTimestamp\": ").append(System.currentTimeMillis()).append(",\n");
148         builder.append("  \"TestTemperature\": ").append(nextTestTemperature).append("\n");
149         builder.append("}");
150
151         return builder.toString();
152     }
153
154     /**
155      * The main method.
156      *
157      * @param args the arguments
158      */
159     public static void main(final String[] args) {
160         if (args.length != 2) {
161             LOGGER.error("usage EventGenerator #events XML|JSON");
162             return;
163         }
164
165         int eventCount;
166         try {
167             eventCount = Integer.parseInt(args[0]);
168         } catch (final Exception e) {
169             LOGGER.error("usage EventGenerator #events XML|JSON");
170             e.printStackTrace();
171             return;
172         }
173
174         if (args[1].equalsIgnoreCase("XML")) {
175             LOGGER.info(xmlEvents(eventCount));
176         } else if (args[1].equalsIgnoreCase("JSON")) {
177             LOGGER.info(jsonEvents(eventCount));
178         } else {
179             LOGGER.error("usage EventGenerator #events XML|JSON");
180         }
181     }
182 }