3044a1e44bcd8dd4d3ab620646b7b2cd00c1b5d2
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actorserviceprovider.topic;
23
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;
31
32 import java.util.Arrays;
33 import java.util.Map;
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;
45
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";
55
56     private static final String VALUEA_REQID = "hello";
57     private static final String VALUEA_SUBREQID = "world";
58
59     private static final String VALUEB_REQID = "bye";
60
61     private Forwarder forwarder1;
62     private Forwarder forwarder2;
63     private TopicListenerImpl topic;
64
65     @Mock
66     private BiConsumer<String, StandardCoderObject> listener1;
67
68     @Mock
69     private BiConsumer<String, StandardCoderObject> listener1b;
70
71     @Mock
72     private BiConsumer<String, StandardCoderObject> listener2;
73
74
75     /**
76      * Sets up.
77      */
78     @BeforeEach
79     void setUp() {
80         topic = new TopicListenerImpl();
81
82         forwarder1 = topic.addForwarder(new SelectorKey(KEY1));
83         forwarder2 = topic.addForwarder(new SelectorKey(KEY1), new SelectorKey(KEY2, SUBKEY));
84
85         assertNotNull(forwarder1);
86         assertNotNull(forwarder2);
87         assertNotSame(forwarder1, forwarder2);
88
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);
92     }
93
94     @Test
95     void testShutdown() {
96         // shut it down, which should clear all forwarders
97         topic.shutdown();
98
99         // should get a new forwarder now
100         Forwarder forwarder = topic.addForwarder(new SelectorKey(KEY1));
101         assertNotSame(forwarder1, forwarder);
102         assertNotSame(forwarder2, forwarder);
103
104         // new forwarder should be unchanged
105         assertSame(forwarder, topic.addForwarder(new SelectorKey(KEY1)));
106     }
107
108     @Test
109      void testAddForwarder() {
110         assertSame(forwarder1, topic.addForwarder(new SelectorKey(KEY1)));
111         assertSame(forwarder2, topic.addForwarder(new SelectorKey(KEY1), new SelectorKey(KEY2, SUBKEY)));
112     }
113
114     @Test
115      void testOnTopicEvent() {
116         /*
117          * send a message that should go to listener1 on forwarder1 and listener2 on
118          * forwarder2
119          */
120         String msg = makeMessage(Map.of(KEY1, VALUEA_REQID, KEY2, Map.of(SUBKEY, VALUEA_SUBREQID)));
121         topic.onTopicEvent(INFRA, MY_TOPIC, msg);
122
123         verify(listener1).accept(eq(msg), any());
124         verify(listener2).accept(eq(msg), any());
125
126         // not to listener1b
127         verify(listener1b, never()).accept(any(), any());
128
129         /*
130          * now send a message that should only go to listener1b on forwarder1
131          */
132         msg = makeMessage(Map.of(KEY1, VALUEB_REQID, KEY2, Map.of(SUBKEY, VALUEA_SUBREQID)));
133         topic.onTopicEvent(INFRA, MY_TOPIC, msg);
134
135         // should route to listener1 on forwarder1 and listener2 on forwarder2
136         verify(listener1b).accept(eq(msg), any());
137
138         // try one where the coder throws an exception
139         topic.onTopicEvent(INFRA, MY_TOPIC, "{invalid-json");
140
141         // no extra invocations
142         verify(listener1).accept(any(), any());
143         verify(listener1b).accept(any(), any());
144         verify(listener2).accept(any(), any());
145     }
146
147     /**
148      * Makes a message from a map.
149      */
150     private String makeMessage(Map<String, Object> map) {
151         try {
152             return coder.encode(map);
153         } catch (CoderException e) {
154             throw new IllegalArgumentException(e);
155         }
156     }
157 }