2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2022 AT&T Intellectual Property. 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.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.simulators;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import java.util.concurrent.BlockingQueue;
28 import java.util.concurrent.LinkedBlockingQueue;
29 import java.util.concurrent.TimeUnit;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
35 import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSink;
36 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
37 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
38 import org.onap.policy.common.utils.coder.StandardCoder;
40 public class DmaapSimulatorTest {
41 private static final int MAX_WAIT_SEC = 5;
42 private static final String TOPIC = "MY-TOPIC";
45 * Messages from the topic are placed here by the endpoint.
47 private BlockingQueue<String> queue;
50 public static void setUpBeforeClass() throws Exception {
51 TopicEndpointManager.getManager().shutdown();
55 * Starts the simulator and the topic.
57 * @throws Exception if an error occurs
60 public void setUp() throws Exception {
61 assertNotNull(Util.buildDmaapSim());
63 String topicJson = new String(Files.readAllBytes(
64 new File("src/test/resources/org/onap/policy/simulators/dmaap/TopicParameters.json").toPath()),
65 StandardCharsets.UTF_8);
66 topicJson = topicJson.replace("${port}", String.valueOf(Util.DMAAPSIM_SERVER_PORT));
68 TopicParameterGroup topicConfig = new StandardCoder().decode(topicJson, TopicParameterGroup.class);
70 TopicEndpointManager.getManager().addTopics(topicConfig);
71 TopicEndpointManager.getManager().start();
73 queue = new LinkedBlockingQueue<>();
77 public void tearDown() {
78 TopicEndpointManager.getManager().shutdown();
79 HttpServletServerFactoryInstance.getServerFactory().destroy();
83 public void test() throws InterruptedException {
84 TopicEndpointManager.getManager().getDmaapTopicSource(TOPIC)
85 .register((infra, topic, event) -> queue.add(event));
87 DmaapTopicSink sink = TopicEndpointManager.getManager().getDmaapTopicSink(TOPIC);
89 assertEquals("hello", queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));
92 assertEquals("world", queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));