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