2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
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.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.simulators;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.verify;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
35 import org.onap.policy.common.endpoints.event.comm.TopicSink;
36 import org.onap.policy.common.endpoints.event.comm.TopicSource;
37 import org.onap.policy.common.utils.coder.StandardCoder;
39 @RunWith(MockitoJUnitRunner.class)
40 public class TopicServerTest {
41 private static final String MY_TOPIC = "my-topic";
42 private static final String TEXT = "hello";
43 private static final String RESPONSE = "world";
46 private TopicSink sink;
48 private TopicSource source;
50 private MyServer server;
57 server = new MyServer();
61 public void testConstructor() {
62 verify(source).register(server);
66 public void testShutdown() {
68 verify(source).unregister(server);
72 public void testOnTopicEvent() {
73 server.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, "{\"text\": \"hello\"}");
74 verify(sink).send(RESPONSE);
78 * Tests onTopicEvent() when the coder throws an exception.
81 public void testOnTopicEventException() {
82 assertThatIllegalArgumentException()
83 .isThrownBy(() -> server.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, "{invalid json"));
85 verify(sink, never()).send(any());
89 * Tests onTopicEvent() when there is no response.
92 public void testOnTopicEventNoResponse() {
93 server = new MyServer() {
95 protected String process(MyRequest request) {
100 server.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, "{\"text\": \"bye-bye\"}");
102 verify(sink, never()).send(any());
106 private class MyRequest {
110 private class MyServer extends TopicServer<MyRequest> {
112 super(sink, source, new StandardCoder(), MyRequest.class);
116 protected String process(MyRequest request) {
117 assertEquals(TEXT, request.text);