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