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;
36 * @param eventCount the event count
39 public static String jsonEvents(final int eventCount) {
40 final StringBuilder builder = new StringBuilder();
42 for (int i = 0; i < eventCount; i++) {
46 builder.append(jsonEvent());
49 return builder.toString();
57 public static String jsonEvent() {
58 final Random rand = new Random();
60 final StringBuilder builder = new StringBuilder();
62 int nextEventNo = rand.nextInt(2);
63 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
64 final int nextMatchCase = rand.nextInt(4);
65 final float nextTestTemperature = rand.nextFloat() * 10000;
67 builder.append("{\n");
68 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
69 builder.append(" \"name\": \"" + eventName + "\",\n");
70 builder.append(" \"version\": \"0.0.1\",\n");
71 builder.append(" \"source\": \"test\",\n");
72 builder.append(" \"target\": \"apex\",\n");
73 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
74 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
75 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
76 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
79 return builder.toString();
87 public static String jsonEventNoName() {
88 final Random rand = new Random();
90 final StringBuilder builder = new StringBuilder();
92 int nextEventNo = rand.nextInt(2);
93 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
94 final int nextMatchCase = rand.nextInt(4);
95 final float nextTestTemperature = rand.nextFloat() * 10000;
97 builder.append("{\n");
98 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
99 builder.append(" \"namez\": \"" + eventName + "\",\n");
100 builder.append(" \"version\": \"0.0.1\",\n");
101 builder.append(" \"source\": \"test\",\n");
102 builder.append(" \"target\": \"apex\",\n");
103 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
104 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
105 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
106 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
109 return builder.toString();
113 * Json event bad name.
117 public static String jsonEventBadName() {
118 final Random rand = new Random();
120 final StringBuilder builder = new StringBuilder();
122 int nextEventNo = rand.nextInt(2);
123 final int nextMatchCase = rand.nextInt(4);
124 final float nextTestTemperature = rand.nextFloat() * 10000;
126 builder.append("{\n");
127 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
128 builder.append(" \"name\": \"%%%%\",\n");
129 builder.append(" \"version\": \"0.0.1\",\n");
130 builder.append(" \"source\": \"test\",\n");
131 builder.append(" \"target\": \"apex\",\n");
132 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
133 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
134 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
135 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
138 return builder.toString();
142 * Json event no ex name.
146 public static String jsonEventNoExName() {
147 final Random rand = new Random();
149 final StringBuilder builder = new StringBuilder();
151 int nextEventNo = rand.nextInt(2);
152 final int nextMatchCase = rand.nextInt(4);
153 final float nextTestTemperature = rand.nextFloat() * 10000;
155 builder.append("{\n");
156 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
157 builder.append(" \"name\": \"I_DONT_EXIST\",\n");
158 builder.append(" \"source\": \"test\",\n");
159 builder.append(" \"target\": \"apex\",\n");
160 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
161 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
162 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
163 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
166 return builder.toString();
170 * Json event no version.
174 public static String jsonEventNoVersion() {
175 final Random rand = new Random();
177 final StringBuilder builder = new StringBuilder();
179 int nextEventNo = rand.nextInt(2);
180 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
181 final int nextMatchCase = rand.nextInt(4);
182 final float nextTestTemperature = rand.nextFloat() * 10000;
184 builder.append("{\n");
185 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
186 builder.append(" \"name\": \"" + eventName + "\",\n");
187 builder.append(" \"versiion\": \"0.0.1\",\n");
188 builder.append(" \"source\": \"test\",\n");
189 builder.append(" \"target\": \"apex\",\n");
190 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
191 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
192 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
193 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
196 return builder.toString();
200 * Json event bad version.
204 public static String jsonEventBadVersion() {
205 final Random rand = new Random();
207 final StringBuilder builder = new StringBuilder();
209 int nextEventNo = rand.nextInt(2);
210 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
211 final int nextMatchCase = rand.nextInt(4);
212 final float nextTestTemperature = rand.nextFloat() * 10000;
214 builder.append("{\n");
215 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
216 builder.append(" \"name\": \"" + eventName + "\",\n");
217 builder.append(" \"version\": \"#####\",\n");
218 builder.append(" \"source\": \"test\",\n");
219 builder.append(" \"target\": \"apex\",\n");
220 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
221 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
222 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
223 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
226 return builder.toString();
230 * Json event no ex version.
234 public static String jsonEventNoExVersion() {
235 final Random rand = new Random();
237 final StringBuilder builder = new StringBuilder();
239 int nextEventNo = rand.nextInt(2);
240 final int nextMatchCase = rand.nextInt(4);
241 final float nextTestTemperature = rand.nextFloat() * 10000;
243 builder.append("{\n");
244 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
245 builder.append(" \"name\": \"Event0000\",\n");
246 builder.append(" \"version\": \"1.2.3\",\n");
247 builder.append(" \"source\": \"test\",\n");
248 builder.append(" \"target\": \"apex\",\n");
249 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
250 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
251 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
252 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
255 return builder.toString();
259 * Json event no namespace.
263 public static String jsonEventNoNamespace() {
264 final Random rand = new Random();
266 final StringBuilder builder = new StringBuilder();
268 int nextEventNo = rand.nextInt(2);
269 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
270 final int nextMatchCase = rand.nextInt(4);
271 final float nextTestTemperature = rand.nextFloat() * 10000;
273 builder.append("{\n");
274 builder.append(" \"nameSpacee\": \"org.onap.policy.apex.sample.events\",\n");
275 builder.append(" \"name\": \"" + eventName + "\",\n");
276 builder.append(" \"version\": \"0.0.1\",\n");
277 builder.append(" \"source\": \"test\",\n");
278 builder.append(" \"target\": \"apex\",\n");
279 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
280 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
281 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
282 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
285 return builder.toString();
289 * Json event bad namespace.
293 public static String jsonEventBadNamespace() {
294 final Random rand = new Random();
296 final StringBuilder builder = new StringBuilder();
298 int nextEventNo = rand.nextInt(2);
299 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
300 final int nextMatchCase = rand.nextInt(4);
301 final float nextTestTemperature = rand.nextFloat() * 10000;
303 builder.append("{\n");
304 builder.append(" \"nameSpace\": \"hello.&&&&\",\n");
305 builder.append(" \"name\": \"" + eventName + "\",\n");
306 builder.append(" \"version\": \"0.0.1\",\n");
307 builder.append(" \"source\": \"test\",\n");
308 builder.append(" \"target\": \"apex\",\n");
309 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
310 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
311 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
312 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
315 return builder.toString();
319 * Json event no ex namespace.
323 public static String jsonEventNoExNamespace() {
324 final Random rand = new Random();
326 final StringBuilder builder = new StringBuilder();
328 int nextEventNo = rand.nextInt(2);
329 final int nextMatchCase = rand.nextInt(4);
330 final float nextTestTemperature = rand.nextFloat() * 10000;
332 builder.append("{\n");
333 builder.append(" \"nameSpace\": \"pie.in.the.sky\",\n");
334 builder.append(" \"name\": \"Event0000\",\n");
335 builder.append(" \"version\": \"0.0.1\",\n");
336 builder.append(" \"source\": \"test\",\n");
337 builder.append(" \"target\": \"apex\",\n");
338 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
339 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
340 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
341 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
344 return builder.toString();
348 * Json event no source.
352 public static String jsonEventNoSource() {
353 final Random rand = new Random();
355 final StringBuilder builder = new StringBuilder();
357 int nextEventNo = rand.nextInt(2);
358 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
359 final int nextMatchCase = rand.nextInt(4);
360 final float nextTestTemperature = rand.nextFloat() * 10000;
362 builder.append("{\n");
363 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
364 builder.append(" \"name\": \"" + eventName + "\",\n");
365 builder.append(" \"version\": \"0.0.1\",\n");
366 builder.append(" \"sourcee\": \"test\",\n");
367 builder.append(" \"target\": \"apex\",\n");
368 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
369 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
370 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
371 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
374 return builder.toString();
378 * Json event bad source.
382 public static String jsonEventBadSource() {
383 final Random rand = new Random();
385 final StringBuilder builder = new StringBuilder();
387 int nextEventNo = rand.nextInt(2);
388 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
389 final int nextMatchCase = rand.nextInt(4);
390 final float nextTestTemperature = rand.nextFloat() * 10000;
392 builder.append("{\n");
393 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
394 builder.append(" \"name\": \"" + eventName + "\",\n");
395 builder.append(" \"version\": \"0.0.1\",\n");
396 builder.append(" \"source\": \"%!@**@!\",\n");
397 builder.append(" \"target\": \"apex\",\n");
398 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
399 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
400 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
401 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
404 return builder.toString();
408 * Json event no target.
412 public static String jsonEventNoTarget() {
413 final Random rand = new Random();
415 final StringBuilder builder = new StringBuilder();
417 int nextEventNo = rand.nextInt(2);
418 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
419 final int nextMatchCase = rand.nextInt(4);
420 final float nextTestTemperature = rand.nextFloat() * 10000;
422 builder.append("{\n");
423 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
424 builder.append(" \"name\": \"" + eventName + "\",\n");
425 builder.append(" \"version\": \"0.0.1\",\n");
426 builder.append(" \"source\": \"test\",\n");
427 builder.append(" \"targett\": \"apex\",\n");
428 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
429 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
430 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
431 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
434 return builder.toString();
438 * Json event bad target.
442 public static String jsonEventBadTarget() {
443 final Random rand = new Random();
445 final StringBuilder builder = new StringBuilder();
447 int nextEventNo = rand.nextInt(2);
448 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
449 final int nextMatchCase = rand.nextInt(4);
450 final float nextTestTemperature = rand.nextFloat() * 10000;
452 builder.append("{\n");
453 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
454 builder.append(" \"name\": \"" + eventName + "\",\n");
455 builder.append(" \"version\": \"0.0.1\",\n");
456 builder.append(" \"source\": \"test\",\n");
457 builder.append(" \"target\": \"KNIO(*S)A(S)D\",\n");
458 builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
459 builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n");
460 builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
461 builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n");
464 return builder.toString();
468 * Json event missing fields.
472 public static String jsonEventMissingFields() {
473 final StringBuilder builder = new StringBuilder();
475 builder.append("{\n");
476 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
477 builder.append(" \"name\": \"Event0000\",\n");
478 builder.append(" \"version\": \"0.0.1\",\n");
479 builder.append(" \"source\": \"test\",\n");
480 builder.append(" \"target\": \"apex\"\n");
483 return builder.toString();
487 * Json event null fields.
491 public static String jsonEventNullFields() {
492 final StringBuilder builder = new StringBuilder();
494 builder.append("{\n");
495 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
496 builder.append(" \"name\": \"Event0000\",\n");
497 builder.append(" \"version\": \"0.0.1\",\n");
498 builder.append(" \"source\": \"test\",\n");
499 builder.append(" \"target\": \"Apex\",\n");
500 builder.append(" \"TestSlogan\": null,\n");
501 builder.append(" \"TestMatchCase\": -1,\n");
502 builder.append(" \"TestTimestamp\": -1,\n");
503 builder.append(" \"TestTemperature\": -1.0\n");
506 return builder.toString();
512 * @param args the arguments
514 public static void main(final String[] args) {
515 if (args.length != 1) {
516 System.err.println("usage EventGenerator #events");
522 eventCount = Integer.parseInt(args[0]);
523 } catch (final Exception e) {
524 System.err.println("usage EventGenerator #events");
529 System.out.println(jsonEvents(eventCount));
533 * Gets the next event no.
535 * @return the next event no
537 public static int getNextEventNo() {