4da8c420168f93a2ceca038e58527ca7d8c8d088
[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.service.engine.event;
22
23 import java.util.Random;
24
25 /**
26  * @author Liam Fallon (liam.fallon@ericsson.com)
27  */
28 public class JSONEventGenerator {
29     private static int nextEventNo = 0;
30
31     public static String jsonEvents(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(jsonEvent());
39         }
40
41         return builder.toString();
42     }
43
44     public static String jsonEvent() {
45         final Random rand = new Random();
46
47         final StringBuilder builder = new StringBuilder();
48
49         int nextEventNo = rand.nextInt(2);
50         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
51         final int nextMatchCase = rand.nextInt(4);
52         final float nextTestTemperature = rand.nextFloat() * 10000;
53
54         builder.append("{\n");
55         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
56         builder.append("  \"name\": \"" + eventName + "\",\n");
57         builder.append("  \"version\": \"0.0.1\",\n");
58         builder.append("  \"source\": \"test\",\n");
59         builder.append("  \"target\": \"apex\",\n");
60         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
61         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
62         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
63         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
64         builder.append("}");
65
66         return builder.toString();
67     }
68
69     public static String jsonEventNoName() {
70         final Random rand = new Random();
71
72         final StringBuilder builder = new StringBuilder();
73
74         int nextEventNo = rand.nextInt(2);
75         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
76         final int nextMatchCase = rand.nextInt(4);
77         final float nextTestTemperature = rand.nextFloat() * 10000;
78
79         builder.append("{\n");
80         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
81         builder.append("  \"namez\": \"" + eventName + "\",\n");
82         builder.append("  \"version\": \"0.0.1\",\n");
83         builder.append("  \"source\": \"test\",\n");
84         builder.append("  \"target\": \"apex\",\n");
85         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
86         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
87         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
88         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
89         builder.append("}");
90
91         return builder.toString();
92     }
93
94     public static String jsonEventBadName() {
95         final Random rand = new Random();
96
97         final StringBuilder builder = new StringBuilder();
98
99         int nextEventNo = rand.nextInt(2);
100         final int nextMatchCase = rand.nextInt(4);
101         final float nextTestTemperature = rand.nextFloat() * 10000;
102
103         builder.append("{\n");
104         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
105         builder.append("  \"name\": \"%%%%\",\n");
106         builder.append("  \"version\": \"0.0.1\",\n");
107         builder.append("  \"source\": \"test\",\n");
108         builder.append("  \"target\": \"apex\",\n");
109         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
110         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
111         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
112         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
113         builder.append("}");
114
115         return builder.toString();
116     }
117
118     public static String jsonEventNoExName() {
119         final Random rand = new Random();
120
121         final StringBuilder builder = new StringBuilder();
122
123         int nextEventNo = rand.nextInt(2);
124         final int nextMatchCase = rand.nextInt(4);
125         final float nextTestTemperature = rand.nextFloat() * 10000;
126
127         builder.append("{\n");
128         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
129         builder.append("  \"name\": \"I_DONT_EXIST\",\n");
130         builder.append("  \"source\": \"test\",\n");
131         builder.append("  \"target\": \"apex\",\n");
132         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
133         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
134         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
135         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
136         builder.append("}");
137
138         return builder.toString();
139     }
140
141     public static String jsonEventNoVersion() {
142         final Random rand = new Random();
143
144         final StringBuilder builder = new StringBuilder();
145
146         int nextEventNo = rand.nextInt(2);
147         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
148         final int nextMatchCase = rand.nextInt(4);
149         final float nextTestTemperature = rand.nextFloat() * 10000;
150
151         builder.append("{\n");
152         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
153         builder.append("  \"name\": \"" + eventName + "\",\n");
154         builder.append("  \"versiion\": \"0.0.1\",\n");
155         builder.append("  \"source\": \"test\",\n");
156         builder.append("  \"target\": \"apex\",\n");
157         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
158         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
159         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
160         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
161         builder.append("}");
162
163         return builder.toString();
164     }
165
166     public static String jsonEventBadVersion() {
167         final Random rand = new Random();
168
169         final StringBuilder builder = new StringBuilder();
170
171         int nextEventNo = rand.nextInt(2);
172         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
173         final int nextMatchCase = rand.nextInt(4);
174         final float nextTestTemperature = rand.nextFloat() * 10000;
175
176         builder.append("{\n");
177         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
178         builder.append("  \"name\": \"" + eventName + "\",\n");
179         builder.append("  \"version\": \"#####\",\n");
180         builder.append("  \"source\": \"test\",\n");
181         builder.append("  \"target\": \"apex\",\n");
182         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
183         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
184         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
185         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
186         builder.append("}");
187
188         return builder.toString();
189     }
190
191     public static String jsonEventNoExVersion() {
192         final Random rand = new Random();
193
194         final StringBuilder builder = new StringBuilder();
195
196         int nextEventNo = rand.nextInt(2);
197         final int nextMatchCase = rand.nextInt(4);
198         final float nextTestTemperature = rand.nextFloat() * 10000;
199
200         builder.append("{\n");
201         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
202         builder.append("  \"name\": \"Event0000\",\n");
203         builder.append("  \"version\": \"1.2.3\",\n");
204         builder.append("  \"source\": \"test\",\n");
205         builder.append("  \"target\": \"apex\",\n");
206         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
207         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
208         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
209         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
210         builder.append("}");
211
212         return builder.toString();
213     }
214
215     public static String jsonEventNoNamespace() {
216         final Random rand = new Random();
217
218         final StringBuilder builder = new StringBuilder();
219
220         int nextEventNo = rand.nextInt(2);
221         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
222         final int nextMatchCase = rand.nextInt(4);
223         final float nextTestTemperature = rand.nextFloat() * 10000;
224
225         builder.append("{\n");
226         builder.append("  \"nameSpacee\": \"org.onap.policy.apex.sample.events\",\n");
227         builder.append("  \"name\": \"" + eventName + "\",\n");
228         builder.append("  \"version\": \"0.0.1\",\n");
229         builder.append("  \"source\": \"test\",\n");
230         builder.append("  \"target\": \"apex\",\n");
231         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
232         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
233         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
234         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
235         builder.append("}");
236
237         return builder.toString();
238     }
239
240     public static String jsonEventBadNamespace() {
241         final Random rand = new Random();
242
243         final StringBuilder builder = new StringBuilder();
244
245         int nextEventNo = rand.nextInt(2);
246         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
247         final int nextMatchCase = rand.nextInt(4);
248         final float nextTestTemperature = rand.nextFloat() * 10000;
249
250         builder.append("{\n");
251         builder.append("  \"nameSpace\": \"hello.&&&&\",\n");
252         builder.append("  \"name\": \"" + eventName + "\",\n");
253         builder.append("  \"version\": \"0.0.1\",\n");
254         builder.append("  \"source\": \"test\",\n");
255         builder.append("  \"target\": \"apex\",\n");
256         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
257         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
258         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
259         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
260         builder.append("}");
261
262         return builder.toString();
263     }
264
265     public static String jsonEventNoExNamespace() {
266         final Random rand = new Random();
267
268         final StringBuilder builder = new StringBuilder();
269
270         int nextEventNo = rand.nextInt(2);
271         final int nextMatchCase = rand.nextInt(4);
272         final float nextTestTemperature = rand.nextFloat() * 10000;
273
274         builder.append("{\n");
275         builder.append("  \"nameSpace\": \"pie.in.the.sky\",\n");
276         builder.append("  \"name\": \"Event0000\",\n");
277         builder.append("  \"version\": \"0.0.1\",\n");
278         builder.append("  \"source\": \"test\",\n");
279         builder.append("  \"target\": \"apex\",\n");
280         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
281         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
282         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
283         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
284         builder.append("}");
285
286         return builder.toString();
287     }
288
289     public static String jsonEventNoSource() {
290         final Random rand = new Random();
291
292         final StringBuilder builder = new StringBuilder();
293
294         int nextEventNo = rand.nextInt(2);
295         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
296         final int nextMatchCase = rand.nextInt(4);
297         final float nextTestTemperature = rand.nextFloat() * 10000;
298
299         builder.append("{\n");
300         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
301         builder.append("  \"name\": \"" + eventName + "\",\n");
302         builder.append("  \"version\": \"0.0.1\",\n");
303         builder.append("  \"sourcee\": \"test\",\n");
304         builder.append("  \"target\": \"apex\",\n");
305         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
306         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
307         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
308         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
309         builder.append("}");
310
311         return builder.toString();
312     }
313
314     public static String jsonEventBadSource() {
315         final Random rand = new Random();
316
317         final StringBuilder builder = new StringBuilder();
318
319         int nextEventNo = rand.nextInt(2);
320         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
321         final int nextMatchCase = rand.nextInt(4);
322         final float nextTestTemperature = rand.nextFloat() * 10000;
323
324         builder.append("{\n");
325         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
326         builder.append("  \"name\": \"" + eventName + "\",\n");
327         builder.append("  \"version\": \"0.0.1\",\n");
328         builder.append("  \"source\": \"%!@**@!\",\n");
329         builder.append("  \"target\": \"apex\",\n");
330         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
331         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
332         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
333         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
334         builder.append("}");
335
336         return builder.toString();
337     }
338
339     public static String jsonEventNoTarget() {
340         final Random rand = new Random();
341
342         final StringBuilder builder = new StringBuilder();
343
344         int nextEventNo = rand.nextInt(2);
345         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
346         final int nextMatchCase = rand.nextInt(4);
347         final float nextTestTemperature = rand.nextFloat() * 10000;
348
349         builder.append("{\n");
350         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
351         builder.append("  \"name\": \"" + eventName + "\",\n");
352         builder.append("  \"version\": \"0.0.1\",\n");
353         builder.append("  \"source\": \"test\",\n");
354         builder.append("  \"targett\": \"apex\",\n");
355         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
356         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
357         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
358         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
359         builder.append("}");
360
361         return builder.toString();
362     }
363
364     public static String jsonEventBadTarget() {
365         final Random rand = new Random();
366
367         final StringBuilder builder = new StringBuilder();
368
369         int nextEventNo = rand.nextInt(2);
370         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
371         final int nextMatchCase = rand.nextInt(4);
372         final float nextTestTemperature = rand.nextFloat() * 10000;
373
374         builder.append("{\n");
375         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
376         builder.append("  \"name\": \"" + eventName + "\",\n");
377         builder.append("  \"version\": \"0.0.1\",\n");
378         builder.append("  \"source\": \"test\",\n");
379         builder.append("  \"target\": \"KNIO(*S)A(S)D\",\n");
380         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
381         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
382         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
383         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
384         builder.append("}");
385
386         return builder.toString();
387     }
388
389     public static String jsonEventMissingFields() {
390         final StringBuilder builder = new StringBuilder();
391
392         builder.append("{\n");
393         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
394         builder.append("  \"name\": \"Event0000\",\n");
395         builder.append("  \"version\": \"0.0.1\",\n");
396         builder.append("  \"source\": \"test\",\n");
397         builder.append("  \"target\": \"apex\"\n");
398         builder.append("}");
399
400         return builder.toString();
401     }
402
403     public static String jsonEventNullFields() {
404         final StringBuilder builder = new StringBuilder();
405
406         builder.append("{\n");
407         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
408         builder.append("  \"name\": \"Event0000\",\n");
409         builder.append("  \"version\": \"0.0.1\",\n");
410         builder.append("  \"source\": \"test\",\n");
411         builder.append("  \"target\": \"Apex\",\n");
412         builder.append("  \"TestSlogan\": null,\n");
413         builder.append("  \"TestMatchCase\": -1,\n");
414         builder.append("  \"TestTimestamp\": -1,\n");
415         builder.append("  \"TestTemperature\": -1.0\n");
416         builder.append("}");
417
418         return builder.toString();
419     }
420
421     public static void main(final String[] args) {
422         if (args.length != 1) {
423             System.err.println("usage EventGenerator #events");
424             return;
425         }
426
427         int eventCount = 0;
428         try {
429             eventCount = Integer.parseInt(args[0]);
430         } catch (final Exception e) {
431             System.err.println("usage EventGenerator #events");
432             e.printStackTrace();
433             return;
434         }
435
436         System.out.println(jsonEvents(eventCount));
437     }
438
439     public static int getNextEventNo() {
440         return nextEventNo;
441     }
442 }