Fix return building on policy get
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / topic / TopicPairTest.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.controlloop.actorserviceprovider.topic;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import java.util.Arrays;
34 import java.util.List;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
40 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
41 import org.onap.policy.common.endpoints.event.comm.TopicSink;
42 import org.onap.policy.common.endpoints.event.comm.TopicSource;
43
44 public class TopicPairTest {
45     private static final String UNKNOWN = "unknown";
46     private static final String MY_SOURCE = "pair-source";
47     private static final String MY_TARGET = "pair-target";
48     private static final String TEXT = "some text";
49
50     @Mock
51     private TopicSink publisher1;
52
53     @Mock
54     private TopicSink publisher2;
55
56     @Mock
57     private TopicSource subscriber1;
58
59     @Mock
60     private TopicSource subscriber2;
61
62     @Mock
63     private TopicEndpoint mgr;
64
65     private TopicPair pair;
66
67
68     /**
69      * Sets up.
70      */
71     @Before
72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74
75         when(mgr.getTopicSinks(MY_TARGET)).thenReturn(Arrays.asList(publisher1, publisher2));
76         when(mgr.getTopicSources(eq(Arrays.asList(MY_SOURCE)))).thenReturn(Arrays.asList(subscriber1, subscriber2));
77
78         when(publisher1.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.NOOP);
79         when(publisher2.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.UEB);
80
81         pair = new MyTopicPair(MY_SOURCE, MY_TARGET);
82
83         pair.start();
84     }
85
86     @Test
87     public void testTopicPair_testGetSource_testGetTarget() {
88         assertEquals(MY_SOURCE, pair.getSource());
89         assertEquals(MY_TARGET, pair.getTarget());
90
91         verify(mgr).getTopicSinks(anyString());
92         verify(mgr).getTopicSources(any());
93
94         // source not found
95         assertThatIllegalArgumentException().isThrownBy(() -> new MyTopicPair(UNKNOWN, MY_TARGET))
96                         .withMessageContaining("sources").withMessageContaining(UNKNOWN);
97
98         // target not found
99         assertThatIllegalArgumentException().isThrownBy(() -> new MyTopicPair(MY_SOURCE, UNKNOWN))
100                         .withMessageContaining("sinks").withMessageContaining(UNKNOWN);
101     }
102
103     @Test
104     public void testShutdown() {
105         pair.shutdown();
106         verify(subscriber1).unregister(pair);
107         verify(subscriber2).unregister(pair);
108     }
109
110     @Test
111     public void testStart() {
112         verify(subscriber1).register(pair);
113         verify(subscriber2).register(pair);
114     }
115
116     @Test
117     public void testStop() {
118         pair.stop();
119         verify(subscriber1).unregister(pair);
120         verify(subscriber2).unregister(pair);
121     }
122
123     @Test
124     public void testPublish() {
125         List<CommInfrastructure> infrastructures = pair.publish(TEXT);
126         assertEquals(Arrays.asList(CommInfrastructure.NOOP, CommInfrastructure.UEB), infrastructures);
127
128         verify(publisher1).send(TEXT);
129         verify(publisher2).send(TEXT);
130
131         // first one throws an exception - should have only published to the second
132         when(publisher1.send(any())).thenThrow(new IllegalStateException("expected exception"));
133
134         infrastructures = pair.publish(TEXT);
135         assertEquals(Arrays.asList(CommInfrastructure.UEB), infrastructures);
136
137         verify(publisher2, times(2)).send(TEXT);
138     }
139
140     @Test
141     public void testGetTopicEndpointManager() {
142         // setting "mgr" to null should cause it to use the superclass' method
143         mgr = null;
144         assertNotNull(pair.getTopicEndpointManager());
145     }
146
147
148     private class MyTopicPair extends TopicPair {
149         public MyTopicPair(String source, String target) {
150             super(source, target);
151         }
152
153         @Override
154         protected TopicEndpoint getTopicEndpointManager() {
155             return (mgr != null ? mgr : super.getTopicEndpointManager());
156         }
157     }
158 }