95dd9f53145bf28e01b13539249ddf6b509fa186
[policy/common.git] /
1 /*--
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 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.common.endpoints.listeners;
23
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32
33 import ch.qos.logback.classic.Level;
34 import ch.qos.logback.classic.Logger;
35 import org.junit.jupiter.api.AfterAll;
36 import org.junit.jupiter.api.AfterEach;
37 import org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
41 import org.onap.policy.common.utils.coder.StandardCoderObject;
42 import org.onap.policy.common.utils.test.log.logback.ExtractAppender;
43 import org.slf4j.LoggerFactory;
44
45 class MessageTypeDispatcherTest {
46
47     /**
48      * Used to attach an appender to the class' logger.
49      */
50     private static final Logger logger = (Logger) LoggerFactory.getLogger(MessageTypeDispatcher.class);
51     private static final ExtractAppender appender = new ExtractAppender();
52
53     /**
54      * Original logging level for the logger.
55      */
56     private static Level saveLevel;
57
58     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
59     private static final String TYPE_FIELD = "msg-type";
60     private static final String TOPIC = "my-topic";
61     private static final String TYPE1 = "msg-type-1";
62     private static final String TYPE2 = "msg-type-2";
63
64     private MessageTypeDispatcher primary;
65
66     private ScoListener<String> secondary1;
67     private ScoListener<String> secondary2;
68
69     /**
70      * Initializes statics.
71      */
72     @BeforeAll
73     public static void setUpBeforeClass() {
74         saveLevel = logger.getLevel();
75         logger.setLevel(Level.INFO);
76
77         appender.setContext(logger.getLoggerContext());
78         appender.start();
79     }
80
81     @AfterAll
82     public static void tearDownAfterClass() {
83         logger.setLevel(saveLevel);
84         appender.stop();
85     }
86
87     /**
88      * Initializes mocks and a listener.
89      */
90     @BeforeEach
91     @SuppressWarnings("unchecked")
92     public void setUp() {
93         appender.clearExtractions();
94
95         secondary1 = mock(ScoListener.class);
96         secondary2 = mock(ScoListener.class);
97
98         primary = new MessageTypeDispatcher(TYPE_FIELD);
99     }
100
101     @AfterEach
102     public void tearDown() {
103         logger.detachAppender(appender);
104     }
105
106     @Test
107     void testRegister_testUnregister() {
108         primary.register(TYPE1, secondary1);
109         primary.register(TYPE2, secondary2);
110
111         primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
112         verify(secondary1).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
113         verify(secondary2, never()).onTopicEvent(any(), any(), any());
114
115         primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
116         verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
117         verify(secondary2, never()).onTopicEvent(any(), any(), any());
118
119         primary.unregister(TYPE1);
120         primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
121         verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
122         verify(secondary2, never()).onTopicEvent(any(), any(), any());
123
124         primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE2));
125         verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
126         verify(secondary2).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
127
128         // unregister again
129         primary.unregister(TYPE1);
130
131         // unregister second type
132         primary.unregister(TYPE2);
133         primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
134         primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE2));
135         verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
136         verify(secondary2, times(1)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
137     }
138
139     @Test
140     void testOnTopicEvent() {
141         primary.register(TYPE1, secondary1);
142
143         logger.addAppender(appender);
144
145         // success
146         primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
147         verify(secondary1).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
148
149         // repeat
150         primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
151         verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
152
153         assertFalse(appender.getExtracted().toString().contains("unable to extract"));
154         assertFalse(appender.getExtracted().toString().contains("discarding event of type"));
155
156         // no message type
157         appender.clearExtractions();
158         primary.onTopicEvent(INFRA, TOPIC, "{}");
159         assertTrue(appender.getExtracted().toString().contains("unable to extract"));
160         verify(secondary1, times(2)).onTopicEvent(any(), any(), any());
161
162         // unknown type
163         appender.clearExtractions();
164         primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE2));
165         assertTrue(appender.getExtracted().toString().contains("discarding event of type"));
166         verify(secondary1, times(2)).onTopicEvent(any(), any(), any());
167     }
168
169     /**
170      * Makes a JSON message of the given type.
171      *
172      * @param msgType the message type
173      * @return a JSON message of the given type
174      */
175     private String makeMessage(String msgType) {
176         String json = "{'" + TYPE_FIELD + "':'" + msgType + "', 'abc':'def'}";
177         return json.replace('\'', '"');
178     }
179 }