2 * ============LICENSE_START=======================================================
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
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.common.endpoints.listeners;
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;
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;
45 class MessageTypeDispatcherTest {
48 * Used to attach an appender to the class' logger.
50 private static final Logger logger = (Logger) LoggerFactory.getLogger(MessageTypeDispatcher.class);
51 private static final ExtractAppender appender = new ExtractAppender();
54 * Original logging level for the logger.
56 private static Level saveLevel;
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";
64 private MessageTypeDispatcher primary;
66 private ScoListener<String> secondary1;
67 private ScoListener<String> secondary2;
70 * Initializes statics.
73 public static void setUpBeforeClass() {
74 saveLevel = logger.getLevel();
75 logger.setLevel(Level.INFO);
77 appender.setContext(logger.getLoggerContext());
82 public static void tearDownAfterClass() {
83 logger.setLevel(saveLevel);
88 * Initializes mocks and a listener.
91 @SuppressWarnings("unchecked")
93 appender.clearExtractions();
95 secondary1 = mock(ScoListener.class);
96 secondary2 = mock(ScoListener.class);
98 primary = new MessageTypeDispatcher(TYPE_FIELD);
102 public void tearDown() {
103 logger.detachAppender(appender);
107 void testRegister_testUnregister() {
108 primary.register(TYPE1, secondary1);
109 primary.register(TYPE2, secondary2);
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());
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());
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());
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));
129 primary.unregister(TYPE1);
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));
140 void testOnTopicEvent() {
141 primary.register(TYPE1, secondary1);
143 logger.addAppender(appender);
146 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
147 verify(secondary1).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
150 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
151 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
153 assertFalse(appender.getExtracted().toString().contains("unable to extract"));
154 assertFalse(appender.getExtracted().toString().contains("discarding event of 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());
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());
170 * Makes a JSON message of the given type.
172 * @param msgType the message type
173 * @return a JSON message of the given type
175 private String makeMessage(String msgType) {
176 String json = "{'" + TYPE_FIELD + "':'" + msgType + "', 'abc':'def'}";
177 return json.replace('\'', '"');