4c2ed5c88936bdb911c4458493d7351bbbee80cd
[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.apps.uservice.test.adapt.events;
22
23 import java.util.Random;
24
25 /**
26  * @author Liam Fallon (liam.fallon@ericsson.com)
27  */
28 public class EventGenerator {
29     private static int nextEventNo = 0;
30
31     public static String xmlEvents(final int eventCount) {
32         final StringBuilder builder = new StringBuilder();
33
34         for (int i = 0; i < eventCount; i++) {
35             if (i > 0) {
36                 builder.append("\n");
37             }
38             builder.append(xmlEvent());
39         }
40
41         return builder.toString();
42     }
43
44     public static String jsonEvents(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(jsonEvent());
52         }
53
54         return builder.toString();
55     }
56
57     public static String xmlEvent() {
58         final Random rand = new Random();
59
60         final StringBuilder builder = new StringBuilder();
61
62         int nextEventNo = rand.nextInt(2);
63         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
64         final int nextMatchCase = rand.nextInt(4);
65         final float nextTestTemperature = rand.nextFloat() * 10000;
66
67         builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
68         builder.append("<xmlApexEvent xmlns=\"http://www.onap.org/policy/apex-pdp/apexevent\">\n");
69
70         builder.append("  <name>" + eventName + "</name>\n");
71         builder.append("  <version>0.0.1</version>\n");
72         builder.append("  <nameSpace>org.onap.policy.apex.sample.events</nameSpace>\n");
73         builder.append("  <source>test</source>\n");
74         builder.append("  <target>apex</target>\n");
75         builder.append("  <data>\n");
76         builder.append("    <key>TestSlogan</key>\n");
77         builder.append("    <value>Test slogan for External Event" + (nextEventNo++) + "</value>\n");
78         builder.append("  </data>\n");
79         builder.append("  <data>\n");
80         builder.append("    <key>TestMatchCase</key>\n");
81         builder.append("    <value>" + nextMatchCase + "</value>\n");
82         builder.append("  </data>\n");
83         builder.append("  <data>\n");
84         builder.append("    <key>TestTimestamp</key>\n");
85         builder.append("    <value>" + System.currentTimeMillis() + "</value>\n");
86         builder.append("  </data>\n");
87         builder.append("  <data>\n");
88         builder.append("    <key>TestTemperature</key>\n");
89         builder.append("    <value>" + nextTestTemperature + "</value>\n");
90         builder.append("  </data>\n");
91         builder.append("</xmlApexEvent>");
92
93         return builder.toString();
94     }
95
96     public static String jsonEvent() {
97         final Random rand = new Random();
98
99         final StringBuilder builder = new StringBuilder();
100
101         int nextEventNo = rand.nextInt(2);
102         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
103         final int nextMatchCase = rand.nextInt(4);
104         final float nextTestTemperature = rand.nextFloat() * 10000;
105
106         builder.append("{\n");
107         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
108         builder.append("  \"name\": \"" + eventName + "\",\n");
109         builder.append("  \"version\": \"0.0.1\",\n");
110         builder.append("  \"source\": \"test\",\n");
111         builder.append("  \"target\": \"apex\",\n");
112         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
113         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
114         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
115         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
116         builder.append("}");
117
118         return builder.toString();
119     }
120
121     public static String jsonEventNoName() {
122         final Random rand = new Random();
123
124         final StringBuilder builder = new StringBuilder();
125
126         int nextEventNo = rand.nextInt(2);
127         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
128         final int nextMatchCase = rand.nextInt(4);
129         final float nextTestTemperature = rand.nextFloat() * 10000;
130
131         builder.append("{\n");
132         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
133         builder.append("  \"namez\": \"" + eventName + "\",\n");
134         builder.append("  \"version\": \"0.0.1\",\n");
135         builder.append("  \"source\": \"test\",\n");
136         builder.append("  \"target\": \"apex\",\n");
137         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
138         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
139         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
140         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
141         builder.append("}");
142
143         return builder.toString();
144     }
145
146     public static String jsonEventBadName() {
147         final Random rand = new Random();
148
149         final StringBuilder builder = new StringBuilder();
150
151         int nextEventNo = rand.nextInt(2);
152         final int nextMatchCase = rand.nextInt(4);
153         final float nextTestTemperature = rand.nextFloat() * 10000;
154
155         builder.append("{\n");
156         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
157         builder.append("  \"name\": \"%%%%\",\n");
158         builder.append("  \"version\": \"0.0.1\",\n");
159         builder.append("  \"source\": \"test\",\n");
160         builder.append("  \"target\": \"apex\",\n");
161         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
162         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
163         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
164         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
165         builder.append("}");
166
167         return builder.toString();
168     }
169
170     public static String jsonEventNoExName() {
171         final Random rand = new Random();
172
173         final StringBuilder builder = new StringBuilder();
174
175         int nextEventNo = rand.nextInt(2);
176         final int nextMatchCase = rand.nextInt(4);
177         final float nextTestTemperature = rand.nextFloat() * 10000;
178
179         builder.append("{\n");
180         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
181         builder.append("  \"name\": \"I_DONT_EXIST\",\n");
182         builder.append("  \"source\": \"test\",\n");
183         builder.append("  \"target\": \"apex\",\n");
184         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
185         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
186         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
187         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
188         builder.append("}");
189
190         return builder.toString();
191     }
192
193     public static String jsonEventNoVersion() {
194         final Random rand = new Random();
195
196         final StringBuilder builder = new StringBuilder();
197
198         int nextEventNo = rand.nextInt(2);
199         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
200         final int nextMatchCase = rand.nextInt(4);
201         final float nextTestTemperature = rand.nextFloat() * 10000;
202
203         builder.append("{\n");
204         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
205         builder.append("  \"name\": \"" + eventName + "\",\n");
206         builder.append("  \"versiion\": \"0.0.1\",\n");
207         builder.append("  \"source\": \"test\",\n");
208         builder.append("  \"target\": \"apex\",\n");
209         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
210         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
211         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
212         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
213         builder.append("}");
214
215         return builder.toString();
216     }
217
218     public static String jsonEventBadVersion() {
219         final Random rand = new Random();
220
221         final StringBuilder builder = new StringBuilder();
222
223         int nextEventNo = rand.nextInt(2);
224         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
225         final int nextMatchCase = rand.nextInt(4);
226         final float nextTestTemperature = rand.nextFloat() * 10000;
227
228         builder.append("{\n");
229         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
230         builder.append("  \"name\": \"" + eventName + "\",\n");
231         builder.append("  \"version\": \"#####\",\n");
232         builder.append("  \"source\": \"test\",\n");
233         builder.append("  \"target\": \"apex\",\n");
234         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
235         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
236         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
237         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
238         builder.append("}");
239
240         return builder.toString();
241     }
242
243     public static String jsonEventNoExVersion() {
244         final Random rand = new Random();
245
246         final StringBuilder builder = new StringBuilder();
247
248         int nextEventNo = rand.nextInt(2);
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\": \"Event0000\",\n");
255         builder.append("  \"version\": \"1.2.3\",\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     public static String jsonEventNoNamespace() {
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("  \"nameSpacee\": \"org.onap.policy.apex.sample.events\",\n");
279         builder.append("  \"name\": \"" + eventName + "\",\n");
280         builder.append("  \"version\": \"0.0.1\",\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     public static String jsonEventBadNamespace() {
293         final Random rand = new Random();
294
295         final StringBuilder builder = new StringBuilder();
296
297         int nextEventNo = rand.nextInt(2);
298         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
299         final int nextMatchCase = rand.nextInt(4);
300         final float nextTestTemperature = rand.nextFloat() * 10000;
301
302         builder.append("{\n");
303         builder.append("  \"nameSpace\": \"hello.&&&&\",\n");
304         builder.append("  \"name\": \"" + eventName + "\",\n");
305         builder.append("  \"version\": \"0.0.1\",\n");
306         builder.append("  \"source\": \"test\",\n");
307         builder.append("  \"target\": \"apex\",\n");
308         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
309         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
310         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
311         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
312         builder.append("}");
313
314         return builder.toString();
315     }
316
317     public static String jsonEventNoExNamespace() {
318         final Random rand = new Random();
319
320         final StringBuilder builder = new StringBuilder();
321
322         int nextEventNo = rand.nextInt(2);
323         final int nextMatchCase = rand.nextInt(4);
324         final float nextTestTemperature = rand.nextFloat() * 10000;
325
326         builder.append("{\n");
327         builder.append("  \"nameSpace\": \"pie.in.the.sky\",\n");
328         builder.append("  \"name\": \"Event0000\",\n");
329         builder.append("  \"version\": \"0.0.1\",\n");
330         builder.append("  \"source\": \"test\",\n");
331         builder.append("  \"target\": \"apex\",\n");
332         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
333         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
334         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
335         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
336         builder.append("}");
337
338         return builder.toString();
339     }
340
341     public static String jsonEventNoSource() {
342         final Random rand = new Random();
343
344         final StringBuilder builder = new StringBuilder();
345
346         int nextEventNo = rand.nextInt(2);
347         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
348         final int nextMatchCase = rand.nextInt(4);
349         final float nextTestTemperature = rand.nextFloat() * 10000;
350
351         builder.append("{\n");
352         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
353         builder.append("  \"name\": \"" + eventName + "\",\n");
354         builder.append("  \"version\": \"0.0.1\",\n");
355         builder.append("  \"sourcee\": \"test\",\n");
356         builder.append("  \"target\": \"apex\",\n");
357         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
358         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
359         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
360         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
361         builder.append("}");
362
363         return builder.toString();
364     }
365
366     public static String jsonEventBadSource() {
367         final Random rand = new Random();
368
369         final StringBuilder builder = new StringBuilder();
370
371         int nextEventNo = rand.nextInt(2);
372         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
373         final int nextMatchCase = rand.nextInt(4);
374         final float nextTestTemperature = rand.nextFloat() * 10000;
375
376         builder.append("{\n");
377         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
378         builder.append("  \"name\": \"" + eventName + "\",\n");
379         builder.append("  \"version\": \"0.0.1\",\n");
380         builder.append("  \"source\": \"%!@**@!\",\n");
381         builder.append("  \"target\": \"apex\",\n");
382         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
383         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
384         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
385         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
386         builder.append("}");
387
388         return builder.toString();
389     }
390
391     public static String jsonEventNoTarget() {
392         final Random rand = new Random();
393
394         final StringBuilder builder = new StringBuilder();
395
396         int nextEventNo = rand.nextInt(2);
397         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
398         final int nextMatchCase = rand.nextInt(4);
399         final float nextTestTemperature = rand.nextFloat() * 10000;
400
401         builder.append("{\n");
402         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
403         builder.append("  \"name\": \"" + eventName + "\",\n");
404         builder.append("  \"version\": \"0.0.1\",\n");
405         builder.append("  \"source\": \"test\",\n");
406         builder.append("  \"targett\": \"apex\",\n");
407         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
408         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
409         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
410         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
411         builder.append("}");
412
413         return builder.toString();
414     }
415
416     public static String jsonEventBadTarget() {
417         final Random rand = new Random();
418
419         final StringBuilder builder = new StringBuilder();
420
421         int nextEventNo = rand.nextInt(2);
422         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
423         final int nextMatchCase = rand.nextInt(4);
424         final float nextTestTemperature = rand.nextFloat() * 10000;
425
426         builder.append("{\n");
427         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
428         builder.append("  \"name\": \"" + eventName + "\",\n");
429         builder.append("  \"version\": \"0.0.1\",\n");
430         builder.append("  \"source\": \"test\",\n");
431         builder.append("  \"target\": \"KNIO(*S)A(S)D\",\n");
432         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
433         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
434         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
435         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
436         builder.append("}");
437
438         return builder.toString();
439     }
440
441     public static String jsonEventMissingFields() {
442         final StringBuilder builder = new StringBuilder();
443
444         builder.append("{\n");
445         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
446         builder.append("  \"name\": \"Event0000\",\n");
447         builder.append("  \"version\": \"0.0.1\",\n");
448         builder.append("  \"source\": \"test\",\n");
449         builder.append("  \"target\": \"apex\"\n");
450         builder.append("}");
451
452         return builder.toString();
453     }
454
455     public static String jsonEventNullFields() {
456         final StringBuilder builder = new StringBuilder();
457
458         builder.append("{\n");
459         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
460         builder.append("  \"name\": \"Event0000\",\n");
461         builder.append("  \"version\": \"0.0.1\",\n");
462         builder.append("  \"source\": \"test\",\n");
463         builder.append("  \"target\": \"Apex\",\n");
464         builder.append("  \"TestSlogan\": null,\n");
465         builder.append("  \"TestMatchCase\": -1,\n");
466         builder.append("  \"TestTimestamp\": -1,\n");
467         builder.append("  \"TestTemperature\": -1.0\n");
468         builder.append("}");
469
470         return builder.toString();
471     }
472
473     public static void main(final String[] args) {
474         if (args.length != 2) {
475             System.err.println("usage EventGenerator #events XML|JSON");
476             return;
477         }
478
479         int eventCount = 0;
480         try {
481             eventCount = Integer.parseInt(args[0]);
482         } catch (final Exception e) {
483             System.err.println("usage EventGenerator #events XML|JSON");
484             e.printStackTrace();
485             return;
486         }
487
488         if (args[1].equalsIgnoreCase("XML")) {
489             System.out.println(xmlEvents(eventCount));
490         } else if (args[1].equalsIgnoreCase("JSON")) {
491             System.out.println(jsonEvents(eventCount));
492         } else {
493             System.err.println("usage EventGenerator #events XML|JSON");
494             return;
495         }
496     }
497
498     public static int getNextEventNo() {
499         return nextEventNo;
500     }
501 }