0417b73318716c6dd5b33ec43aa91c3938b138eb
[policy/apex-pdp.git] /
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  * ================================================================================
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.integration.uservice.adapt.events;
23
24 import java.util.Random;
25 import lombok.Getter;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The Class EventGenerator.
31  *
32  * @author Liam Fallon (liam.fallon@ericsson.com)
33  */
34 public class EventGenerator {
35     private static final Logger LOGGER = LoggerFactory.getLogger(EventGenerator.class);
36
37     @Getter
38     private static int nextEventNo = 0;
39
40     /**
41      * Xml events.
42      *
43      * @param eventCount the event count
44      * @return the string
45      */
46     public static String xmlEvents(final int eventCount) {
47         final StringBuilder builder = new StringBuilder();
48
49         for (int i = 0; i < eventCount; i++) {
50             if (i > 0) {
51                 builder.append("\n");
52             }
53             builder.append(xmlEvent());
54         }
55
56         return builder.toString();
57     }
58
59     /**
60      * Json events.
61      *
62      * @param eventCount the event count
63      * @return the string
64      */
65     public static String jsonEvents(final int eventCount) {
66         final StringBuilder builder = new StringBuilder();
67
68         for (int i = 0; i < eventCount; i++) {
69             if (i > 0) {
70                 builder.append("\n");
71             }
72             builder.append(jsonEvent());
73         }
74
75         return builder.toString();
76     }
77
78     /**
79      * Xml event.
80      *
81      * @return the string
82      */
83     public static String xmlEvent() {
84         final Random rand = new Random();
85
86         final StringBuilder builder = new StringBuilder();
87
88         int nextEventNo = rand.nextInt(2);
89         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
90         final int nextMatchCase = rand.nextInt(4);
91         final float nextTestTemperature = rand.nextFloat() * 10000;
92
93         builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
94         builder.append("<xmlApexEvent xmlns=\"http://www.onap.org/policy/apex-pdp/apexevent\">\n");
95
96         builder.append("  <name>" + eventName + "</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         builder.append("    <value>Test slogan for External Event" + (nextEventNo++) + "</value>\n");
104         builder.append("  </data>\n");
105         builder.append("  <data>\n");
106         builder.append("    <key>TestMatchCase</key>\n");
107         builder.append("    <value>" + nextMatchCase + "</value>\n");
108         builder.append("  </data>\n");
109         builder.append("  <data>\n");
110         builder.append("    <key>TestTimestamp</key>\n");
111         builder.append("    <value>" + System.currentTimeMillis() + "</value>\n");
112         builder.append("  </data>\n");
113         builder.append("  <data>\n");
114         builder.append("    <key>TestTemperature</key>\n");
115         builder.append("    <value>" + nextTestTemperature + "</value>\n");
116         builder.append("  </data>\n");
117         builder.append("</xmlApexEvent>");
118
119         return builder.toString();
120     }
121
122     /**
123      * Json event.
124      *
125      * @return the string
126      */
127     public static String jsonEvent() {
128         final Random rand = new Random();
129
130         final StringBuilder builder = new StringBuilder();
131
132         int nextEventNo = rand.nextInt(2);
133         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
134         final int nextMatchCase = rand.nextInt(4);
135         final float nextTestTemperature = rand.nextFloat() * 10000;
136
137         builder.append("{\n");
138         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
139         builder.append("  \"name\": \"" + eventName + "\",\n");
140         builder.append("  \"version\": \"0.0.1\",\n");
141         builder.append("  \"source\": \"test\",\n");
142         builder.append("  \"target\": \"apex\",\n");
143         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
144         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
145         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
146         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
147         builder.append("}");
148
149         return builder.toString();
150     }
151
152     /**
153      * Json event no name.
154      *
155      * @return the string
156      */
157     public static String jsonEventNoName() {
158         final Random rand = new Random();
159
160         final StringBuilder builder = new StringBuilder();
161
162         int nextEventNo = rand.nextInt(2);
163         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
164         final int nextMatchCase = rand.nextInt(4);
165         final float nextTestTemperature = rand.nextFloat() * 10000;
166
167         builder.append("{\n");
168         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
169         builder.append("  \"namez\": \"" + eventName + "\",\n");
170         builder.append("  \"version\": \"0.0.1\",\n");
171         builder.append("  \"source\": \"test\",\n");
172         builder.append("  \"target\": \"apex\",\n");
173         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
174         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
175         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
176         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
177         builder.append("}");
178
179         return builder.toString();
180     }
181
182     /**
183      * Json event bad name.
184      *
185      * @return the string
186      */
187     public static String jsonEventBadName() {
188         final Random rand = new Random();
189
190         final StringBuilder builder = new StringBuilder();
191
192         int nextEventNo = rand.nextInt(2);
193         final int nextMatchCase = rand.nextInt(4);
194         final float nextTestTemperature = rand.nextFloat() * 10000;
195
196         builder.append("{\n");
197         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
198         builder.append("  \"name\": \"%%%%\",\n");
199         builder.append("  \"version\": \"0.0.1\",\n");
200         builder.append("  \"source\": \"test\",\n");
201         builder.append("  \"target\": \"apex\",\n");
202         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
203         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
204         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
205         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
206         builder.append("}");
207
208         return builder.toString();
209     }
210
211     /**
212      * Json event no ex name.
213      *
214      * @return the string
215      */
216     public static String jsonEventNoExName() {
217         final Random rand = new Random();
218
219         final StringBuilder builder = new StringBuilder();
220
221         int nextEventNo = rand.nextInt(2);
222         final int nextMatchCase = rand.nextInt(4);
223         final float nextTestTemperature = rand.nextFloat() * 10000;
224
225         builder.append("{\n");
226         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
227         builder.append("  \"name\": \"I_DONT_EXIST\",\n");
228         builder.append("  \"source\": \"test\",\n");
229         builder.append("  \"target\": \"apex\",\n");
230         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
231         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
232         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
233         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
234         builder.append("}");
235
236         return builder.toString();
237     }
238
239     /**
240      * Json event no version.
241      *
242      * @return the string
243      */
244     public static String jsonEventNoVersion() {
245         final Random rand = new Random();
246
247         final StringBuilder builder = new StringBuilder();
248
249         int nextEventNo = rand.nextInt(2);
250         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
251         final int nextMatchCase = rand.nextInt(4);
252         final float nextTestTemperature = rand.nextFloat() * 10000;
253
254         builder.append("{\n");
255         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
256         builder.append("  \"name\": \"" + eventName + "\",\n");
257         builder.append("  \"versiion\": \"0.0.1\",\n");
258         builder.append("  \"source\": \"test\",\n");
259         builder.append("  \"target\": \"apex\",\n");
260         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
261         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
262         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
263         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
264         builder.append("}");
265
266         return builder.toString();
267     }
268
269     /**
270      * Json event bad version.
271      *
272      * @return the string
273      */
274     public static String jsonEventBadVersion() {
275         final Random rand = new Random();
276
277         final StringBuilder builder = new StringBuilder();
278
279         int nextEventNo = rand.nextInt(2);
280         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
281         final int nextMatchCase = rand.nextInt(4);
282         final float nextTestTemperature = rand.nextFloat() * 10000;
283
284         builder.append("{\n");
285         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
286         builder.append("  \"name\": \"" + eventName + "\",\n");
287         builder.append("  \"version\": \"#####\",\n");
288         builder.append("  \"source\": \"test\",\n");
289         builder.append("  \"target\": \"apex\",\n");
290         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
291         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
292         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
293         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
294         builder.append("}");
295
296         return builder.toString();
297     }
298
299     /**
300      * Json event no ex version.
301      *
302      * @return the string
303      */
304     public static String jsonEventNoExVersion() {
305         final Random rand = new Random();
306
307         final StringBuilder builder = new StringBuilder();
308
309         int nextEventNo = rand.nextInt(2);
310         final int nextMatchCase = rand.nextInt(4);
311         final float nextTestTemperature = rand.nextFloat() * 10000;
312
313         builder.append("{\n");
314         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
315         builder.append("  \"name\": \"Event0000\",\n");
316         builder.append("  \"version\": \"1.2.3\",\n");
317         builder.append("  \"source\": \"test\",\n");
318         builder.append("  \"target\": \"apex\",\n");
319         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
320         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
321         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
322         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
323         builder.append("}");
324
325         return builder.toString();
326     }
327
328     /**
329      * Json event no namespace.
330      *
331      * @return the string
332      */
333     public static String jsonEventNoNamespace() {
334         final Random rand = new Random();
335
336         final StringBuilder builder = new StringBuilder();
337
338         int nextEventNo = rand.nextInt(2);
339         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
340         final int nextMatchCase = rand.nextInt(4);
341         final float nextTestTemperature = rand.nextFloat() * 10000;
342
343         builder.append("{\n");
344         builder.append("  \"nameSpacee\": \"org.onap.policy.apex.sample.events\",\n");
345         builder.append("  \"name\": \"" + eventName + "\",\n");
346         builder.append("  \"version\": \"0.0.1\",\n");
347         builder.append("  \"source\": \"test\",\n");
348         builder.append("  \"target\": \"apex\",\n");
349         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
350         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
351         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
352         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
353         builder.append("}");
354
355         return builder.toString();
356     }
357
358     /**
359      * Json event bad namespace.
360      *
361      * @return the string
362      */
363     public static String jsonEventBadNamespace() {
364         final Random rand = new Random();
365
366         final StringBuilder builder = new StringBuilder();
367
368         int nextEventNo = rand.nextInt(2);
369         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
370         final int nextMatchCase = rand.nextInt(4);
371         final float nextTestTemperature = rand.nextFloat() * 10000;
372
373         builder.append("{\n");
374         builder.append("  \"nameSpace\": \"hello.&&&&\",\n");
375         builder.append("  \"name\": \"" + eventName + "\",\n");
376         builder.append("  \"version\": \"0.0.1\",\n");
377         builder.append("  \"source\": \"test\",\n");
378         builder.append("  \"target\": \"apex\",\n");
379         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
380         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
381         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
382         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
383         builder.append("}");
384
385         return builder.toString();
386     }
387
388     /**
389      * Json event no ex namespace.
390      *
391      * @return the string
392      */
393     public static String jsonEventNoExNamespace() {
394         final Random rand = new Random();
395
396         final StringBuilder builder = new StringBuilder();
397
398         int nextEventNo = rand.nextInt(2);
399         final int nextMatchCase = rand.nextInt(4);
400         final float nextTestTemperature = rand.nextFloat() * 10000;
401
402         builder.append("{\n");
403         builder.append("  \"nameSpace\": \"pie.in.the.sky\",\n");
404         builder.append("  \"name\": \"Event0000\",\n");
405         builder.append("  \"version\": \"0.0.1\",\n");
406         builder.append("  \"source\": \"test\",\n");
407         builder.append("  \"target\": \"apex\",\n");
408         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
409         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
410         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
411         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
412         builder.append("}");
413
414         return builder.toString();
415     }
416
417     /**
418      * Json event no source.
419      *
420      * @return the string
421      */
422     public static String jsonEventNoSource() {
423         final Random rand = new Random();
424
425         final StringBuilder builder = new StringBuilder();
426
427         int nextEventNo = rand.nextInt(2);
428         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
429         final int nextMatchCase = rand.nextInt(4);
430         final float nextTestTemperature = rand.nextFloat() * 10000;
431
432         builder.append("{\n");
433         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
434         builder.append("  \"name\": \"" + eventName + "\",\n");
435         builder.append("  \"version\": \"0.0.1\",\n");
436         builder.append("  \"sourcee\": \"test\",\n");
437         builder.append("  \"target\": \"apex\",\n");
438         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
439         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
440         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
441         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
442         builder.append("}");
443
444         return builder.toString();
445     }
446
447     /**
448      * Json event bad source.
449      *
450      * @return the string
451      */
452     public static String jsonEventBadSource() {
453         final Random rand = new Random();
454
455         final StringBuilder builder = new StringBuilder();
456
457         int nextEventNo = rand.nextInt(2);
458         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
459         final int nextMatchCase = rand.nextInt(4);
460         final float nextTestTemperature = rand.nextFloat() * 10000;
461
462         builder.append("{\n");
463         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
464         builder.append("  \"name\": \"" + eventName + "\",\n");
465         builder.append("  \"version\": \"0.0.1\",\n");
466         builder.append("  \"source\": \"%!@**@!\",\n");
467         builder.append("  \"target\": \"apex\",\n");
468         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
469         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
470         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
471         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
472         builder.append("}");
473
474         return builder.toString();
475     }
476
477     /**
478      * Json event no target.
479      *
480      * @return the string
481      */
482     public static String jsonEventNoTarget() {
483         final Random rand = new Random();
484
485         final StringBuilder builder = new StringBuilder();
486
487         int nextEventNo = rand.nextInt(2);
488         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
489         final int nextMatchCase = rand.nextInt(4);
490         final float nextTestTemperature = rand.nextFloat() * 10000;
491
492         builder.append("{\n");
493         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
494         builder.append("  \"name\": \"" + eventName + "\",\n");
495         builder.append("  \"version\": \"0.0.1\",\n");
496         builder.append("  \"source\": \"test\",\n");
497         builder.append("  \"targett\": \"apex\",\n");
498         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
499         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
500         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
501         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
502         builder.append("}");
503
504         return builder.toString();
505     }
506
507     /**
508      * Json event bad target.
509      *
510      * @return the string
511      */
512     public static String jsonEventBadTarget() {
513         final Random rand = new Random();
514
515         final StringBuilder builder = new StringBuilder();
516
517         int nextEventNo = rand.nextInt(2);
518         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
519         final int nextMatchCase = rand.nextInt(4);
520         final float nextTestTemperature = rand.nextFloat() * 10000;
521
522         builder.append("{\n");
523         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
524         builder.append("  \"name\": \"" + eventName + "\",\n");
525         builder.append("  \"version\": \"0.0.1\",\n");
526         builder.append("  \"source\": \"test\",\n");
527         builder.append("  \"target\": \"KNIO(*S)A(S)D\",\n");
528         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
529         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
530         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
531         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
532         builder.append("}");
533
534         return builder.toString();
535     }
536
537     /**
538      * Json event missing fields.
539      *
540      * @return the string
541      */
542     public static String jsonEventMissingFields() {
543         final StringBuilder builder = new StringBuilder();
544
545         builder.append("{\n");
546         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
547         builder.append("  \"name\": \"Event0000\",\n");
548         builder.append("  \"version\": \"0.0.1\",\n");
549         builder.append("  \"source\": \"test\",\n");
550         builder.append("  \"target\": \"apex\"\n");
551         builder.append("}");
552
553         return builder.toString();
554     }
555
556     /**
557      * Json event null fields.
558      *
559      * @return the string
560      */
561     public static String jsonEventNullFields() {
562         final StringBuilder builder = new StringBuilder();
563
564         builder.append("{\n");
565         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
566         builder.append("  \"name\": \"Event0000\",\n");
567         builder.append("  \"version\": \"0.0.1\",\n");
568         builder.append("  \"source\": \"test\",\n");
569         builder.append("  \"target\": \"Apex\",\n");
570         builder.append("  \"TestSlogan\": null,\n");
571         builder.append("  \"TestMatchCase\": -1,\n");
572         builder.append("  \"TestTimestamp\": -1,\n");
573         builder.append("  \"TestTemperature\": -1.0\n");
574         builder.append("}");
575
576         return builder.toString();
577     }
578
579     /**
580      * The main method.
581      *
582      * @param args the arguments
583      */
584     public static void main(final String[] args) {
585         if (args.length != 2) {
586             LOGGER.error("usage EventGenerator #events XML|JSON");
587             return;
588         }
589
590         int eventCount = 0;
591         try {
592             eventCount = Integer.parseInt(args[0]);
593         } catch (final Exception e) {
594             LOGGER.error("usage EventGenerator #events XML|JSON");
595             e.printStackTrace();
596             return;
597         }
598
599         if (args[1].equalsIgnoreCase("XML")) {
600             LOGGER.info(xmlEvents(eventCount));
601         } else if (args[1].equalsIgnoreCase("JSON")) {
602             LOGGER.info(jsonEvents(eventCount));
603         } else {
604             LOGGER.error("usage EventGenerator #events XML|JSON");
605             return;
606         }
607     }
608 }