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