2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actorserviceprovider.topic;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertNotSame;
26 import static org.junit.jupiter.api.Assertions.assertSame;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
32 import java.util.Arrays;
34 import java.util.function.BiConsumer;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.TestInstance;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.Mock;
40 import org.mockito.junit.jupiter.MockitoExtension;
41 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.common.utils.coder.StandardCoderObject;
46 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
47 @ExtendWith(MockitoExtension.class)
48 class TopicListenerImplTest {
49 private static final StandardCoder coder = new StandardCoder();
50 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
51 private static final String MY_TOPIC = "my-topic";
52 private static final String KEY1 = "requestId";
53 private static final String KEY2 = "container";
54 private static final String SUBKEY = "subRequestId";
56 private static final String VALUEA_REQID = "hello";
57 private static final String VALUEA_SUBREQID = "world";
59 private static final String VALUEB_REQID = "bye";
61 private Forwarder forwarder1;
62 private Forwarder forwarder2;
63 private TopicListenerImpl topic;
66 private BiConsumer<String, StandardCoderObject> listener1;
69 private BiConsumer<String, StandardCoderObject> listener1b;
72 private BiConsumer<String, StandardCoderObject> listener2;
80 topic = new TopicListenerImpl();
82 forwarder1 = topic.addForwarder(new SelectorKey(KEY1));
83 forwarder2 = topic.addForwarder(new SelectorKey(KEY1), new SelectorKey(KEY2, SUBKEY));
85 assertNotNull(forwarder1);
86 assertNotNull(forwarder2);
87 assertNotSame(forwarder1, forwarder2);
89 forwarder1.register(Arrays.asList(VALUEA_REQID), listener1);
90 forwarder1.register(Arrays.asList(VALUEB_REQID), listener1b);
91 forwarder2.register(Arrays.asList(VALUEA_REQID, VALUEA_SUBREQID), listener2);
96 // shut it down, which should clear all forwarders
99 // should get a new forwarder now
100 Forwarder forwarder = topic.addForwarder(new SelectorKey(KEY1));
101 assertNotSame(forwarder1, forwarder);
102 assertNotSame(forwarder2, forwarder);
104 // new forwarder should be unchanged
105 assertSame(forwarder, topic.addForwarder(new SelectorKey(KEY1)));
109 void testAddForwarder() {
110 assertSame(forwarder1, topic.addForwarder(new SelectorKey(KEY1)));
111 assertSame(forwarder2, topic.addForwarder(new SelectorKey(KEY1), new SelectorKey(KEY2, SUBKEY)));
115 void testOnTopicEvent() {
117 * send a message that should go to listener1 on forwarder1 and listener2 on
120 String msg = makeMessage(Map.of(KEY1, VALUEA_REQID, KEY2, Map.of(SUBKEY, VALUEA_SUBREQID)));
121 topic.onTopicEvent(INFRA, MY_TOPIC, msg);
123 verify(listener1).accept(eq(msg), any());
124 verify(listener2).accept(eq(msg), any());
127 verify(listener1b, never()).accept(any(), any());
130 * now send a message that should only go to listener1b on forwarder1
132 msg = makeMessage(Map.of(KEY1, VALUEB_REQID, KEY2, Map.of(SUBKEY, VALUEA_SUBREQID)));
133 topic.onTopicEvent(INFRA, MY_TOPIC, msg);
135 // should route to listener1 on forwarder1 and listener2 on forwarder2
136 verify(listener1b).accept(eq(msg), any());
138 // try one where the coder throws an exception
139 topic.onTopicEvent(INFRA, MY_TOPIC, "{invalid-json");
141 // no extra invocations
142 verify(listener1).accept(any(), any());
143 verify(listener1b).accept(any(), any());
144 verify(listener2).accept(any(), any());
148 * Makes a message from a map.
150 private String makeMessage(Map<String, Object> map) {
152 return coder.encode(map);
153 } catch (CoderException e) {
154 throw new IllegalArgumentException(e);