2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2024 Nordix Foundation.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.events;
25 import java.util.Random;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The Class EventGenerator.
33 * @author Liam Fallon (liam.fallon@ericsson.com)
35 public class EventGenerator {
36 private static final Logger LOGGER = LoggerFactory.getLogger(EventGenerator.class);
39 private static int nextEventNo = 0;
44 * @param eventCount the event count
47 public static String xmlEvents(final int eventCount) {
48 final StringBuilder builder = new StringBuilder();
50 for (int i = 0; i < eventCount; i++) {
54 builder.append(xmlEvent());
57 return builder.toString();
63 * @param eventCount the event count
66 public static String jsonEvents(final int eventCount) {
67 final StringBuilder builder = new StringBuilder();
69 for (int i = 0; i < eventCount; i++) {
73 builder.append(jsonEvent());
76 return builder.toString();
84 public static String xmlEvent() {
85 final Random rand = new Random();
87 final StringBuilder builder = new StringBuilder();
89 int nextEventNo = rand.nextInt(2);
90 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
91 final int nextMatchCase = rand.nextInt(4);
92 final float nextTestTemperature = rand.nextFloat() * 10000;
94 builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
95 builder.append("<xmlApexEvent xmlns=\"http://www.onap.org/policy/apex-pdp/apexevent\">\n");
96 builder.append(" <name>").append(eventName).append("</name>\n");
97 builder.append(" <version>0.0.1</version>\n");
98 builder.append(" <nameSpace>org.onap.policy.apex.sample.events</nameSpace>\n");
99 builder.append(" <source>test</source>\n");
100 builder.append(" <target>apex</target>\n");
101 builder.append(" <data>\n");
102 builder.append(" <key>TestSlogan</key>\n");
104 builder.append(" <value>Test slogan for External Event").append(nextEventNo).append("</value>\n");
105 builder.append(" </data>\n");
106 builder.append(" <data>\n");
107 builder.append(" <key>TestMatchCase</key>\n");
108 builder.append(" <value>").append(nextMatchCase).append("</value>\n");
109 builder.append(" </data>\n");
110 builder.append(" <data>\n");
111 builder.append(" <key>TestTimestamp</key>\n");
112 builder.append(" <value>").append(System.currentTimeMillis()).append("</value>\n");
113 builder.append(" </data>\n");
114 builder.append(" <data>\n");
115 builder.append(" <key>TestTemperature</key>\n");
116 builder.append(" <value>").append(nextTestTemperature).append("</value>\n");
117 builder.append(" </data>\n");
118 builder.append("</xmlApexEvent>");
120 return builder.toString();
128 public static String jsonEvent() {
129 final Random rand = new Random();
131 final StringBuilder builder = new StringBuilder();
133 int nextEventNo = rand.nextInt(2);
134 final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
135 final int nextMatchCase = rand.nextInt(4);
136 final float nextTestTemperature = rand.nextFloat() * 10000;
138 builder.append("{\n");
139 builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n");
140 builder.append(" \"name\": \"").append(eventName).append("\",\n");
141 builder.append(" \"version\": \"0.0.1\",\n");
142 builder.append(" \"source\": \"test\",\n");
143 builder.append(" \"target\": \"apex\",\n");
145 builder.append(" \"TestSlogan\": \"Test slogan for External Event").append(nextEventNo).append("\",\n");
146 builder.append(" \"TestMatchCase\": ").append(nextMatchCase).append(",\n");
147 builder.append(" \"TestTimestamp\": ").append(System.currentTimeMillis()).append(",\n");
148 builder.append(" \"TestTemperature\": ").append(nextTestTemperature).append("\n");
151 return builder.toString();
157 * @param args the arguments
159 public static void main(final String[] args) {
160 if (args.length != 2) {
161 LOGGER.error("usage EventGenerator #events XML|JSON");
167 eventCount = Integer.parseInt(args[0]);
168 } catch (final Exception e) {
169 LOGGER.error("usage EventGenerator #events XML|JSON");
174 if (args[1].equalsIgnoreCase("XML")) {
175 LOGGER.info(xmlEvents(eventCount));
176 } else if (args[1].equalsIgnoreCase("JSON")) {
177 LOGGER.info(jsonEvents(eventCount));
179 LOGGER.error("usage EventGenerator #events XML|JSON");