2 * ============LICENSE_START=======================================================
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
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.controlloop.actorserviceprovider.topic;
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;
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;
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";
51 private TopicSink publisher1;
54 private TopicSink publisher2;
57 private TopicSource subscriber1;
60 private TopicSource subscriber2;
63 private TopicEndpoint mgr;
65 private TopicPair pair;
73 MockitoAnnotations.initMocks(this);
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));
78 when(publisher1.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.NOOP);
79 when(publisher2.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.UEB);
81 pair = new MyTopicPair(MY_SOURCE, MY_TARGET);
87 public void testTopicPair_testGetSource_testGetTarget() {
88 assertEquals(MY_SOURCE, pair.getSource());
89 assertEquals(MY_TARGET, pair.getTarget());
91 verify(mgr).getTopicSinks(anyString());
92 verify(mgr).getTopicSources(any());
95 assertThatIllegalArgumentException().isThrownBy(() -> new MyTopicPair(UNKNOWN, MY_TARGET))
96 .withMessageContaining("sources").withMessageContaining(UNKNOWN);
99 assertThatIllegalArgumentException().isThrownBy(() -> new MyTopicPair(MY_SOURCE, UNKNOWN))
100 .withMessageContaining("sinks").withMessageContaining(UNKNOWN);
104 public void testShutdown() {
106 verify(subscriber1).unregister(pair);
107 verify(subscriber2).unregister(pair);
111 public void testStart() {
112 verify(subscriber1).register(pair);
113 verify(subscriber2).register(pair);
117 public void testStop() {
119 verify(subscriber1).unregister(pair);
120 verify(subscriber2).unregister(pair);
124 public void testPublish() {
125 List<CommInfrastructure> infrastructures = pair.publish(TEXT);
126 assertEquals(Arrays.asList(CommInfrastructure.NOOP, CommInfrastructure.UEB), infrastructures);
128 verify(publisher1).send(TEXT);
129 verify(publisher2).send(TEXT);
131 // first one throws an exception - should have only published to the second
132 when(publisher1.send(any())).thenThrow(new IllegalStateException("expected exception"));
134 infrastructures = pair.publish(TEXT);
135 assertEquals(Arrays.asList(CommInfrastructure.UEB), infrastructures);
137 verify(publisher2, times(2)).send(TEXT);
141 public void testGetTopicEndpointManager() {
142 // setting "mgr" to null should cause it to use the superclass' method
144 assertNotNull(pair.getTopicEndpointManager());
148 private class MyTopicPair extends TopicPair {
149 public MyTopicPair(String source, String target) {
150 super(source, target);
154 protected TopicEndpoint getTopicEndpointManager() {
155 return (mgr != null ? mgr : super.getTopicEndpointManager());