19c399db8f67143cd7663c75178ebd3103e65d9b
[policy/models.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.policy.simulators;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import java.io.File;
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;
41
42 public class DmaapSimulatorTest {
43     private static final int MAX_WAIT_SEC = 5;
44     private static final String TOPIC = "MY-TOPIC";
45
46     /**
47      * Messages from the topic are placed here by the endpoint.
48      */
49     private BlockingQueue<String> queue;
50
51     @BeforeClass
52     public static void setUpBeforeClass() throws Exception {
53         TopicEndpointManager.getManager().shutdown();
54     }
55
56     /**
57      * Starts the simulator and the topic.
58      *
59      * @throws Exception if an error occurs
60      */
61     @Before
62     public void setUp() throws Exception {
63         assertNotNull(Util.buildDmaapSim());
64
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));
69
70         TopicParameterGroup topicConfig = new StandardCoder().decode(topicJson, TopicParameterGroup.class);
71
72         TopicEndpointManager.getManager().addTopics(topicConfig);
73         TopicEndpointManager.getManager().start();
74
75         queue = new LinkedBlockingQueue<>();
76     }
77
78     @After
79     public void tearDown() {
80         TopicEndpointManager.getManager().shutdown();
81         HttpServletServerFactoryInstance.getServerFactory().destroy();
82     }
83
84     @Test
85     public void test() throws InterruptedException {
86         TopicEndpointManager.getManager().getDmaapTopicSource(TOPIC)
87                         .register((infra, topic, event) -> queue.add(event));
88
89         DmaapTopicSink sink = TopicEndpointManager.getManager().getDmaapTopicSink(TOPIC);
90         assertThat(queue.poll(1, TimeUnit.SECONDS)).isNull();
91         sink.send("hello");
92         assertEquals("hello", queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));
93
94         sink.send("world");
95         assertEquals("world", queue.poll(MAX_WAIT_SEC, TimeUnit.SECONDS));
96     }
97 }