Merge "Bug fixes in models simulators"
[policy/models.git] / models-interactions / model-simulators / src / test / java / org / onap / policy / simulators / TopicServerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.simulators;
22
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;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.common.endpoints.event.comm.TopicSink;
35 import org.onap.policy.common.endpoints.event.comm.TopicSource;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37
38 public class TopicServerTest {
39     private static final String MY_TOPIC = "my-topic";
40     private static final String TEXT = "hello";
41     private static final String RESPONSE = "world";
42
43     @Mock
44     private TopicSink sink;
45     @Mock
46     private TopicSource source;
47
48     private MyServer server;
49
50     /**
51      * Sets up.
52      */
53     @Before
54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56
57         server = new MyServer();
58     }
59
60     @Test
61     public void testConstructor() {
62         verify(source).register(server);
63     }
64
65     @Test
66     public void testShutdown() {
67         server.shutdown();
68         verify(source).unregister(server);
69     }
70
71     @Test
72     public void testOnTopicEvent() {
73         server.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, "{\"text\": \"hello\"}");
74         verify(sink).send(RESPONSE);
75     }
76
77     /**
78      * Tests onTopicEvent() when the coder throws an exception.
79      */
80     @Test
81     public void testOnTopicEventException() {
82         assertThatIllegalArgumentException()
83                         .isThrownBy(() -> server.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, "{invalid json"));
84
85         verify(sink, never()).send(any());
86     }
87
88     /**
89      * Tests onTopicEvent() when there is no response.
90      */
91     @Test
92     public void testOnTopicEventNoResponse() {
93         server = new MyServer() {
94             @Override
95             protected String process(MyRequest request) {
96                 return null;
97             }
98         };
99
100         server.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, "{\"text\": \"bye-bye\"}");
101
102         verify(sink, never()).send(any());
103     }
104
105
106     private class MyRequest {
107         private String text;
108     }
109
110     private class MyServer extends TopicServer<MyRequest> {
111         public MyServer() {
112             super(sink, source, new StandardCoder(), MyRequest.class);
113         }
114
115         @Override
116         protected String process(MyRequest request) {
117             assertEquals(TEXT, request.text);
118             return RESPONSE;
119         }
120     }
121 }