2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2022 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.simulators;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.util.concurrent.BlockingQueue;
30 import java.util.concurrent.LinkedBlockingQueue;
31 import java.util.concurrent.TimeUnit;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
37 import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSink;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
40 import org.onap.policy.common.utils.coder.StandardCoder;
42 public class DmaapSimulatorTest {
43 private static final int MAX_WAIT_SEC = 5;
44 private static final String TOPIC = "MY-TOPIC";
47 * Messages from the topic are placed here by the endpoint.
49 private BlockingQueue<String> queue;
52 public static void setUpBeforeClass() throws Exception {
53 TopicEndpointManager.getManager().shutdown();
57 * Starts the simulator and the topic.
59 * @throws Exception if an error occurs
62 public void setUp() throws Exception {
63 assertNotNull(Util.buildDmaapSim());
65 String topicJson = new String(Files.readAllBytes(
66 new File("src/test/resources/org/onap/policy/simulators/dmaap/TopicParameters.json").toPath()),
67 StandardCharsets.UTF_8);
68 topicJson = topicJson.replace("${port}", String.valueOf(Util.DMAAPSIM_SERVER_PORT));
70 TopicParameterGroup topicConfig = new StandardCoder().decode(topicJson, TopicParameterGroup.class);
72 TopicEndpointManager.getManager().addTopics(topicConfig);
73 TopicEndpointManager.getManager().start();
75 queue = new LinkedBlockingQueue<>();
79 public void tearDown() {
80 TopicEndpointManager.getManager().shutdown();
81 HttpServletServerFactoryInstance.getServerFactory().destroy();
85 public void test() throws InterruptedException {
86 TopicEndpointManager.getManager().getDmaapTopicSource(TOPIC)
87 .register((infra, topic, event) -> queue.add(event));
89 DmaapTopicSink sink = TopicEndpointManager.getManager().getDmaapTopicSink(TOPIC);
90 assertThat(queue.poll(1, TimeUnit.SECONDS)).isNull();
92 assertEquals("hello", queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));
95 assertEquals("world", queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));