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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event;
23 import java.util.Random;
26 * This class generates JSON event used for the test cases.
28 * @author Liam Fallon (liam.fallon@ericsson.com)
30 public class JSONEventGenerator {
31 private static int nextEventNo = 0;
33 public static String jsonEvents(final int eventCount) {
34 final StringBuilder builder = new StringBuilder();
36 for (int i = 0; i < eventCount; i++) {
40 builder.append(jsonEvent());
43 return builder.toString();
46 public static String jsonEvent() {
47 final Random rand = new Random();
49 final StringBuilder builder = new StringBuilder();
51 int nextEventNo = rand.nextInt(2);
52 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
53 final int nextMatchCase = rand.nextInt(4);
54 final float nextTestTemperature = rand.nextFloat() * 10000;
56 builder.append("{\n");
57 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
58 builder.append(" \"name\": \"" + eventName + "\",\n");
59 builder.append(" \"version\": \"0.0.1\",\n");
60 builder.append(" \"source\": \"test\",\n");
61 builder.append(" \"target\": \"apex\",\n");
62 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
63 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
64 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
65 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
68 return builder.toString();
71 public static String jsonEventNoName() {
72 final Random rand = new Random();
74 final StringBuilder builder = new StringBuilder();
76 int nextEventNo = rand.nextInt(2);
77 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
78 final int nextMatchCase = rand.nextInt(4);
79 final float nextTestTemperature = rand.nextFloat() * 10000;
81 builder.append("{\n");
82 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
83 builder.append(" \"namez\": \"" + eventName + "\",\n");
84 builder.append(" \"version\": \"0.0.1\",\n");
85 builder.append(" \"source\": \"test\",\n");
86 builder.append(" \"target\": \"apex\",\n");
87 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
88 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
89 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
90 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
93 return builder.toString();
96 public static String jsonEventBadName() {
97 final Random rand = new Random();
99 final StringBuilder builder = new StringBuilder();
101 int nextEventNo = rand.nextInt(2);
102 final int nextMatchCase = rand.nextInt(4);
103 final float nextTestTemperature = rand.nextFloat() * 10000;
105 builder.append("{\n");
106 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
107 builder.append(" \"name\": \"%%%%\",\n");
108 builder.append(" \"version\": \"0.0.1\",\n");
109 builder.append(" \"source\": \"test\",\n");
110 builder.append(" \"target\": \"apex\",\n");
111 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
112 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
113 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
114 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
117 return builder.toString();
120 public static String jsonEventNoExName() {
121 final Random rand = new Random();
123 final StringBuilder builder = new StringBuilder();
125 int nextEventNo = rand.nextInt(2);
126 final int nextMatchCase = rand.nextInt(4);
127 final float nextTestTemperature = rand.nextFloat() * 10000;
129 builder.append("{\n");
130 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
131 builder.append(" \"name\": \"I_DONT_EXIST\",\n");
132 builder.append(" \"source\": \"test\",\n");
133 builder.append(" \"target\": \"apex\",\n");
134 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
135 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
136 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
137 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
140 return builder.toString();
143 public static String jsonEventNoVersion() {
144 final Random rand = new Random();
146 final StringBuilder builder = new StringBuilder();
148 int nextEventNo = rand.nextInt(2);
149 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
150 final int nextMatchCase = rand.nextInt(4);
151 final float nextTestTemperature = rand.nextFloat() * 10000;
153 builder.append("{\n");
154 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
155 builder.append(" \"name\": \"" + eventName + "\",\n");
156 builder.append(" \"versiion\": \"0.0.1\",\n");
157 builder.append(" \"source\": \"test\",\n");
158 builder.append(" \"target\": \"apex\",\n");
159 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
160 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
161 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
162 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
165 return builder.toString();
168 public static String jsonEventBadVersion() {
169 final Random rand = new Random();
171 final StringBuilder builder = new StringBuilder();
173 int nextEventNo = rand.nextInt(2);
174 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
175 final int nextMatchCase = rand.nextInt(4);
176 final float nextTestTemperature = rand.nextFloat() * 10000;
178 builder.append("{\n");
179 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
180 builder.append(" \"name\": \"" + eventName + "\",\n");
181 builder.append(" \"version\": \"#####\",\n");
182 builder.append(" \"source\": \"test\",\n");
183 builder.append(" \"target\": \"apex\",\n");
184 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
185 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
186 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
187 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
190 return builder.toString();
193 public static String jsonEventNoExVersion() {
194 final Random rand = new Random();
196 final StringBuilder builder = new StringBuilder();
198 int nextEventNo = rand.nextInt(2);
199 final int nextMatchCase = rand.nextInt(4);
200 final float nextTestTemperature = rand.nextFloat() * 10000;
202 builder.append("{\n");
203 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
204 builder.append(" \"name\": \"Event0000\",\n");
205 builder.append(" \"version\": \"1.2.3\",\n");
206 builder.append(" \"source\": \"test\",\n");
207 builder.append(" \"target\": \"apex\",\n");
208 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
209 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
210 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
211 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
214 return builder.toString();
217 public static String jsonEventNoNamespace() {
218 final Random rand = new Random();
220 final StringBuilder builder = new StringBuilder();
222 int nextEventNo = rand.nextInt(2);
223 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
224 final int nextMatchCase = rand.nextInt(4);
225 final float nextTestTemperature = rand.nextFloat() * 10000;
227 builder.append("{\n");
228 builder.append(" \"nameSpacee\": \"org.onap.policy.apex.sample.events\",\n");
229 builder.append(" \"name\": \"" + eventName + "\",\n");
230 builder.append(" \"version\": \"0.0.1\",\n");
231 builder.append(" \"source\": \"test\",\n");
232 builder.append(" \"target\": \"apex\",\n");
233 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
234 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
235 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
236 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
239 return builder.toString();
242 public static String jsonEventBadNamespace() {
243 final Random rand = new Random();
245 final StringBuilder builder = new StringBuilder();
247 int nextEventNo = rand.nextInt(2);
248 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
249 final int nextMatchCase = rand.nextInt(4);
250 final float nextTestTemperature = rand.nextFloat() * 10000;
252 builder.append("{\n");
253 builder.append(" \"nameSpace\": \"hello.&&&&\",\n");
254 builder.append(" \"name\": \"" + eventName + "\",\n");
255 builder.append(" \"version\": \"0.0.1\",\n");
256 builder.append(" \"source\": \"test\",\n");
257 builder.append(" \"target\": \"apex\",\n");
258 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
259 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
260 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
261 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
264 return builder.toString();
267 public static String jsonEventNoExNamespace() {
268 final Random rand = new Random();
270 final StringBuilder builder = new StringBuilder();
272 int nextEventNo = rand.nextInt(2);
273 final int nextMatchCase = rand.nextInt(4);
274 final float nextTestTemperature = rand.nextFloat() * 10000;
276 builder.append("{\n");
277 builder.append(" \"nameSpace\": \"pie.in.the.sky\",\n");
278 builder.append(" \"name\": \"Event0000\",\n");
279 builder.append(" \"version\": \"0.0.1\",\n");
280 builder.append(" \"source\": \"test\",\n");
281 builder.append(" \"target\": \"apex\",\n");
282 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
283 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
284 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
285 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
288 return builder.toString();
291 public static String jsonEventNoSource() {
292 final Random rand = new Random();
294 final StringBuilder builder = new StringBuilder();
296 int nextEventNo = rand.nextInt(2);
297 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
298 final int nextMatchCase = rand.nextInt(4);
299 final float nextTestTemperature = rand.nextFloat() * 10000;
301 builder.append("{\n");
302 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
303 builder.append(" \"name\": \"" + eventName + "\",\n");
304 builder.append(" \"version\": \"0.0.1\",\n");
305 builder.append(" \"sourcee\": \"test\",\n");
306 builder.append(" \"target\": \"apex\",\n");
307 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
308 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
309 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
310 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
313 return builder.toString();
316 public static String jsonEventBadSource() {
317 final Random rand = new Random();
319 final StringBuilder builder = new StringBuilder();
321 int nextEventNo = rand.nextInt(2);
322 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
323 final int nextMatchCase = rand.nextInt(4);
324 final float nextTestTemperature = rand.nextFloat() * 10000;
326 builder.append("{\n");
327 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
328 builder.append(" \"name\": \"" + eventName + "\",\n");
329 builder.append(" \"version\": \"0.0.1\",\n");
330 builder.append(" \"source\": \"%!@**@!\",\n");
331 builder.append(" \"target\": \"apex\",\n");
332 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
333 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
334 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
335 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
338 return builder.toString();
341 public static String jsonEventNoTarget() {
342 final Random rand = new Random();
344 final StringBuilder builder = new StringBuilder();
346 int nextEventNo = rand.nextInt(2);
347 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
348 final int nextMatchCase = rand.nextInt(4);
349 final float nextTestTemperature = rand.nextFloat() * 10000;
351 builder.append("{\n");
352 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
353 builder.append(" \"name\": \"" + eventName + "\",\n");
354 builder.append(" \"version\": \"0.0.1\",\n");
355 builder.append(" \"source\": \"test\",\n");
356 builder.append(" \"targett\": \"apex\",\n");
357 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
358 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
359 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
360 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
363 return builder.toString();
366 public static String jsonEventBadTarget() {
367 final Random rand = new Random();
369 final StringBuilder builder = new StringBuilder();
371 int nextEventNo = rand.nextInt(2);
372 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
373 final int nextMatchCase = rand.nextInt(4);
374 final float nextTestTemperature = rand.nextFloat() * 10000;
376 builder.append("{\n");
377 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
378 builder.append(" \"name\": \"" + eventName + "\",\n");
379 builder.append(" \"version\": \"0.0.1\",\n");
380 builder.append(" \"source\": \"test\",\n");
381 builder.append(" \"target\": \"KNIO(*S)A(S)D\",\n");
382 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
383 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
384 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
385 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
388 return builder.toString();
391 public static String jsonEventMissingFields() {
392 final StringBuilder builder = new StringBuilder();
394 builder.append("{\n");
395 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
396 builder.append(" \"name\": \"Event0000\",\n");
397 builder.append(" \"version\": \"0.0.1\",\n");
398 builder.append(" \"source\": \"test\",\n");
399 builder.append(" \"target\": \"apex\"\n");
402 return builder.toString();
405 public static String jsonEventNullFields() {
406 final StringBuilder builder = new StringBuilder();
408 builder.append("{\n");
409 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
410 builder.append(" \"name\": \"Event0000\",\n");
411 builder.append(" \"version\": \"0.0.1\",\n");
412 builder.append(" \"source\": \"test\",\n");
413 builder.append(" \"target\": \"Apex\",\n");
414 builder.append(" \"TestSlogan\": null,\n");
415 builder.append(" \"TestMatchCase\": -1,\n");
416 builder.append(" \"TestTimestamp\": -1,\n");
417 builder.append(" \"TestTemperature\": -1.0\n");
420 return builder.toString();
423 public static void main(final String[] args) {
424 if (args.length != 1) {
425 System.err.println("usage EventGenerator #events");
431 eventCount = Integer.parseInt(args[0]);
432 } catch (final Exception e) {
433 System.err.println("usage EventGenerator #events");
438 System.out.println(jsonEvents(eventCount));
441 public static int getNextEventNo() {