94210e4738d50eb63a6e783cd29108ce282e22e9
[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  * This class generates JSON event used for the test cases.
27  * 
28  * @author Liam Fallon (liam.fallon@ericsson.com)
29  */
30 public class JSONEventGenerator {
31     private static int nextEventNo = 0;
32
33     public static String jsonEvents(final int eventCount) {
34         final StringBuilder builder = new StringBuilder();
35
36         for (int i = 0; i < eventCount; i++) {
37             if (i > 0) {
38                 builder.append("\n");
39             }
40             builder.append(jsonEvent());
41         }
42
43         return builder.toString();
44     }
45
46     public static String jsonEvent() {
47         final Random rand = new Random();
48
49         final StringBuilder builder = new StringBuilder();
50
51         int nextEventNo = rand.nextInt(2);
52         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
53         final int nextMatchCase = rand.nextInt(4);
54         final float nextTestTemperature = rand.nextFloat() * 10000;
55
56         builder.append("{\n");
57         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
58         builder.append("  \"name\": \"" + eventName + "\",\n");
59         builder.append("  \"version\": \"0.0.1\",\n");
60         builder.append("  \"source\": \"test\",\n");
61         builder.append("  \"target\": \"apex\",\n");
62         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
63         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
64         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
65         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
66         builder.append("}");
67
68         return builder.toString();
69     }
70
71     public static String jsonEventNoName() {
72         final Random rand = new Random();
73
74         final StringBuilder builder = new StringBuilder();
75
76         int nextEventNo = rand.nextInt(2);
77         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
78         final int nextMatchCase = rand.nextInt(4);
79         final float nextTestTemperature = rand.nextFloat() * 10000;
80
81         builder.append("{\n");
82         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
83         builder.append("  \"namez\": \"" + eventName + "\",\n");
84         builder.append("  \"version\": \"0.0.1\",\n");
85         builder.append("  \"source\": \"test\",\n");
86         builder.append("  \"target\": \"apex\",\n");
87         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
88         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
89         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
90         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
91         builder.append("}");
92
93         return builder.toString();
94     }
95
96     public static String jsonEventBadName() {
97         final Random rand = new Random();
98
99         final StringBuilder builder = new StringBuilder();
100
101         int nextEventNo = rand.nextInt(2);
102         final int nextMatchCase = rand.nextInt(4);
103         final float nextTestTemperature = rand.nextFloat() * 10000;
104
105         builder.append("{\n");
106         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
107         builder.append("  \"name\": \"%%%%\",\n");
108         builder.append("  \"version\": \"0.0.1\",\n");
109         builder.append("  \"source\": \"test\",\n");
110         builder.append("  \"target\": \"apex\",\n");
111         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
112         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
113         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
114         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
115         builder.append("}");
116
117         return builder.toString();
118     }
119
120     public static String jsonEventNoExName() {
121         final Random rand = new Random();
122
123         final StringBuilder builder = new StringBuilder();
124
125         int nextEventNo = rand.nextInt(2);
126         final int nextMatchCase = rand.nextInt(4);
127         final float nextTestTemperature = rand.nextFloat() * 10000;
128
129         builder.append("{\n");
130         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
131         builder.append("  \"name\": \"I_DONT_EXIST\",\n");
132         builder.append("  \"source\": \"test\",\n");
133         builder.append("  \"target\": \"apex\",\n");
134         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
135         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
136         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
137         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
138         builder.append("}");
139
140         return builder.toString();
141     }
142
143     public static String jsonEventNoVersion() {
144         final Random rand = new Random();
145
146         final StringBuilder builder = new StringBuilder();
147
148         int nextEventNo = rand.nextInt(2);
149         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
150         final int nextMatchCase = rand.nextInt(4);
151         final float nextTestTemperature = rand.nextFloat() * 10000;
152
153         builder.append("{\n");
154         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
155         builder.append("  \"name\": \"" + eventName + "\",\n");
156         builder.append("  \"versiion\": \"0.0.1\",\n");
157         builder.append("  \"source\": \"test\",\n");
158         builder.append("  \"target\": \"apex\",\n");
159         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
160         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
161         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
162         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
163         builder.append("}");
164
165         return builder.toString();
166     }
167
168     public static String jsonEventBadVersion() {
169         final Random rand = new Random();
170
171         final StringBuilder builder = new StringBuilder();
172
173         int nextEventNo = rand.nextInt(2);
174         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
175         final int nextMatchCase = rand.nextInt(4);
176         final float nextTestTemperature = rand.nextFloat() * 10000;
177
178         builder.append("{\n");
179         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
180         builder.append("  \"name\": \"" + eventName + "\",\n");
181         builder.append("  \"version\": \"#####\",\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 jsonEventNoExVersion() {
194         final Random rand = new Random();
195
196         final StringBuilder builder = new StringBuilder();
197
198         int nextEventNo = rand.nextInt(2);
199         final int nextMatchCase = rand.nextInt(4);
200         final float nextTestTemperature = rand.nextFloat() * 10000;
201
202         builder.append("{\n");
203         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
204         builder.append("  \"name\": \"Event0000\",\n");
205         builder.append("  \"version\": \"1.2.3\",\n");
206         builder.append("  \"source\": \"test\",\n");
207         builder.append("  \"target\": \"apex\",\n");
208         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
209         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
210         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
211         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
212         builder.append("}");
213
214         return builder.toString();
215     }
216
217     public static String jsonEventNoNamespace() {
218         final Random rand = new Random();
219
220         final StringBuilder builder = new StringBuilder();
221
222         int nextEventNo = rand.nextInt(2);
223         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
224         final int nextMatchCase = rand.nextInt(4);
225         final float nextTestTemperature = rand.nextFloat() * 10000;
226
227         builder.append("{\n");
228         builder.append("  \"nameSpacee\": \"org.onap.policy.apex.sample.events\",\n");
229         builder.append("  \"name\": \"" + eventName + "\",\n");
230         builder.append("  \"version\": \"0.0.1\",\n");
231         builder.append("  \"source\": \"test\",\n");
232         builder.append("  \"target\": \"apex\",\n");
233         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
234         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
235         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
236         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
237         builder.append("}");
238
239         return builder.toString();
240     }
241
242     public static String jsonEventBadNamespace() {
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\": \"hello.&&&&\",\n");
254         builder.append("  \"name\": \"" + eventName + "\",\n");
255         builder.append("  \"version\": \"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     public static String jsonEventNoExNamespace() {
268         final Random rand = new Random();
269
270         final StringBuilder builder = new StringBuilder();
271
272         int nextEventNo = rand.nextInt(2);
273         final int nextMatchCase = rand.nextInt(4);
274         final float nextTestTemperature = rand.nextFloat() * 10000;
275
276         builder.append("{\n");
277         builder.append("  \"nameSpace\": \"pie.in.the.sky\",\n");
278         builder.append("  \"name\": \"Event0000\",\n");
279         builder.append("  \"version\": \"0.0.1\",\n");
280         builder.append("  \"source\": \"test\",\n");
281         builder.append("  \"target\": \"apex\",\n");
282         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
283         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
284         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
285         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
286         builder.append("}");
287
288         return builder.toString();
289     }
290
291     public static String jsonEventNoSource() {
292         final Random rand = new Random();
293
294         final StringBuilder builder = new StringBuilder();
295
296         int nextEventNo = rand.nextInt(2);
297         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
298         final int nextMatchCase = rand.nextInt(4);
299         final float nextTestTemperature = rand.nextFloat() * 10000;
300
301         builder.append("{\n");
302         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
303         builder.append("  \"name\": \"" + eventName + "\",\n");
304         builder.append("  \"version\": \"0.0.1\",\n");
305         builder.append("  \"sourcee\": \"test\",\n");
306         builder.append("  \"target\": \"apex\",\n");
307         builder.append("  \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n");
308         builder.append("  \"TestMatchCase\": " + nextMatchCase + ",\n");
309         builder.append("  \"TestTimestamp\": " + System.currentTimeMillis() + ",\n");
310         builder.append("  \"TestTemperature\": " + nextTestTemperature + "\n");
311         builder.append("}");
312
313         return builder.toString();
314     }
315
316     public static String jsonEventBadSource() {
317         final Random rand = new Random();
318
319         final StringBuilder builder = new StringBuilder();
320
321         int nextEventNo = rand.nextInt(2);
322         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
323         final int nextMatchCase = rand.nextInt(4);
324         final float nextTestTemperature = rand.nextFloat() * 10000;
325
326         builder.append("{\n");
327         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
328         builder.append("  \"name\": \"" + eventName + "\",\n");
329         builder.append("  \"version\": \"0.0.1\",\n");
330         builder.append("  \"source\": \"%!@**@!\",\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 jsonEventNoTarget() {
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("  \"source\": \"test\",\n");
356         builder.append("  \"targett\": \"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 jsonEventBadTarget() {
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\": \"test\",\n");
381         builder.append("  \"target\": \"KNIO(*S)A(S)D\",\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 jsonEventMissingFields() {
392         final StringBuilder builder = new StringBuilder();
393
394         builder.append("{\n");
395         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
396         builder.append("  \"name\": \"Event0000\",\n");
397         builder.append("  \"version\": \"0.0.1\",\n");
398         builder.append("  \"source\": \"test\",\n");
399         builder.append("  \"target\": \"apex\"\n");
400         builder.append("}");
401
402         return builder.toString();
403     }
404
405     public static String jsonEventNullFields() {
406         final StringBuilder builder = new StringBuilder();
407
408         builder.append("{\n");
409         builder.append("  \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
410         builder.append("  \"name\": \"Event0000\",\n");
411         builder.append("  \"version\": \"0.0.1\",\n");
412         builder.append("  \"source\": \"test\",\n");
413         builder.append("  \"target\": \"Apex\",\n");
414         builder.append("  \"TestSlogan\": null,\n");
415         builder.append("  \"TestMatchCase\": -1,\n");
416         builder.append("  \"TestTimestamp\": -1,\n");
417         builder.append("  \"TestTemperature\": -1.0\n");
418         builder.append("}");
419
420         return builder.toString();
421     }
422
423     public static void main(final String[] args) {
424         if (args.length != 1) {
425             System.err.println("usage EventGenerator #events");
426             return;
427         }
428
429         int eventCount = 0;
430         try {
431             eventCount = Integer.parseInt(args[0]);
432         } catch (final Exception e) {
433             System.err.println("usage EventGenerator #events");
434             e.printStackTrace();
435             return;
436         }
437
438         System.out.println(jsonEvents(eventCount));
439     }
440
441     public static int getNextEventNo() {
442         return nextEventNo;
443     }
444 }