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