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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event;
23 import java.util.Random;
26 * @author Liam Fallon (liam.fallon@ericsson.com)
28 public class JSONEventGenerator {
29 private static int nextEventNo = 0;
31 public static String jsonEvents(final int eventCount) {
32 final StringBuilder builder = new StringBuilder();
34 for (int i = 0; i < eventCount; i++) {
38 builder.append(jsonEvent());
41 return builder.toString();
44 public static String jsonEvent() {
45 final Random rand = new Random();
47 final StringBuilder builder = new StringBuilder();
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;
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");
66 return builder.toString();
69 public static String jsonEventNoName() {
70 final Random rand = new Random();
72 final StringBuilder builder = new StringBuilder();
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;
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");
91 return builder.toString();
94 public static String jsonEventBadName() {
95 final Random rand = new Random();
97 final StringBuilder builder = new StringBuilder();
99 int nextEventNo = rand.nextInt(2);
100 final int nextMatchCase = rand.nextInt(4);
101 final float nextTestTemperature = rand.nextFloat() * 10000;
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");
115 return builder.toString();
118 public static String jsonEventNoExName() {
119 final Random rand = new Random();
121 final StringBuilder builder = new StringBuilder();
123 int nextEventNo = rand.nextInt(2);
124 final int nextMatchCase = rand.nextInt(4);
125 final float nextTestTemperature = rand.nextFloat() * 10000;
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");
138 return builder.toString();
141 public static String jsonEventNoVersion() {
142 final Random rand = new Random();
144 final StringBuilder builder = new StringBuilder();
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;
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");
163 return builder.toString();
166 public static String jsonEventBadVersion() {
167 final Random rand = new Random();
169 final StringBuilder builder = new StringBuilder();
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;
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");
188 return builder.toString();
191 public static String jsonEventNoExVersion() {
192 final Random rand = new Random();
194 final StringBuilder builder = new StringBuilder();
196 int nextEventNo = rand.nextInt(2);
197 final int nextMatchCase = rand.nextInt(4);
198 final float nextTestTemperature = rand.nextFloat() * 10000;
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");
212 return builder.toString();
215 public static String jsonEventNoNamespace() {
216 final Random rand = new Random();
218 final StringBuilder builder = new StringBuilder();
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;
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");
237 return builder.toString();
240 public static String jsonEventBadNamespace() {
241 final Random rand = new Random();
243 final StringBuilder builder = new StringBuilder();
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;
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");
262 return builder.toString();
265 public static String jsonEventNoExNamespace() {
266 final Random rand = new Random();
268 final StringBuilder builder = new StringBuilder();
270 int nextEventNo = rand.nextInt(2);
271 final int nextMatchCase = rand.nextInt(4);
272 final float nextTestTemperature = rand.nextFloat() * 10000;
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");
286 return builder.toString();
289 public static String jsonEventNoSource() {
290 final Random rand = new Random();
292 final StringBuilder builder = new StringBuilder();
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;
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");
311 return builder.toString();
314 public static String jsonEventBadSource() {
315 final Random rand = new Random();
317 final StringBuilder builder = new StringBuilder();
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;
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");
336 return builder.toString();
339 public static String jsonEventNoTarget() {
340 final Random rand = new Random();
342 final StringBuilder builder = new StringBuilder();
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;
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");
361 return builder.toString();
364 public static String jsonEventBadTarget() {
365 final Random rand = new Random();
367 final StringBuilder builder = new StringBuilder();
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;
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");
386 return builder.toString();
389 public static String jsonEventMissingFields() {
390 final StringBuilder builder = new StringBuilder();
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");
400 return builder.toString();
403 public static String jsonEventNullFields() {
404 final StringBuilder builder = new StringBuilder();
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");
418 return builder.toString();
421 public static void main(final String[] args) {
422 if (args.length != 1) {
423 System.err.println("usage EventGenerator #events");
429 eventCount = Integer.parseInt(args[0]);
430 } catch (final Exception e) {
431 System.err.println("usage EventGenerator #events");
436 System.out.println(jsonEvents(eventCount));
439 public static int getNextEventNo() {