2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-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.common.endpoints.listeners;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
32 import ch.qos.logback.classic.Level;
33 import ch.qos.logback.classic.Logger;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
40 import org.onap.policy.common.utils.coder.StandardCoderObject;
41 import org.onap.policy.common.utils.test.log.logback.ExtractAppender;
42 import org.slf4j.LoggerFactory;
44 public class MessageTypeDispatcherTest {
47 * Used to attach an appender to the class' logger.
49 private static final Logger logger = (Logger) LoggerFactory.getLogger(MessageTypeDispatcher.class);
50 private static final ExtractAppender appender = new ExtractAppender();
53 * Original logging level for the logger.
55 private static Level saveLevel;
57 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
58 private static final String TYPE_FIELD = "msg-type";
59 private static final String TOPIC = "my-topic";
60 private static final String TYPE1 = "msg-type-1";
61 private static final String TYPE2 = "msg-type-2";
63 private MessageTypeDispatcher primary;
65 private ScoListener<String> secondary1;
66 private ScoListener<String> secondary2;
69 * Initializes statics.
72 public static void setUpBeforeClass() {
73 saveLevel = logger.getLevel();
74 logger.setLevel(Level.INFO);
76 appender.setContext(logger.getLoggerContext());
81 public static void tearDownAfterClass() {
82 logger.setLevel(saveLevel);
87 * Initializes mocks and a listener.
90 @SuppressWarnings("unchecked")
92 appender.clearExtractions();
94 secondary1 = mock(ScoListener.class);
95 secondary2 = mock(ScoListener.class);
97 primary = new MessageTypeDispatcher(TYPE_FIELD);
101 public void tearDown() {
102 logger.detachAppender(appender);
106 public void testRegister_testUnregister() {
107 primary.register(TYPE1, secondary1);
108 primary.register(TYPE2, secondary2);
110 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
111 verify(secondary1).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
112 verify(secondary2, never()).onTopicEvent(any(), any(), any());
114 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
115 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
116 verify(secondary2, never()).onTopicEvent(any(), any(), any());
118 primary.unregister(TYPE1);
119 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
120 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
121 verify(secondary2, never()).onTopicEvent(any(), any(), any());
123 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE2));
124 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
125 verify(secondary2).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
128 primary.unregister(TYPE1);
130 // unregister second type
131 primary.unregister(TYPE2);
132 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
133 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE2));
134 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
135 verify(secondary2, times(1)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
139 public void testOnTopicEvent() {
140 primary.register(TYPE1, secondary1);
142 logger.addAppender(appender);
145 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
146 verify(secondary1).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
149 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
150 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
152 assertFalse(appender.getExtracted().toString().contains("unable to extract"));
153 assertFalse(appender.getExtracted().toString().contains("discarding event of type"));
156 appender.clearExtractions();
157 primary.onTopicEvent(INFRA, TOPIC, "{}");
158 assertTrue(appender.getExtracted().toString().contains("unable to extract"));
159 verify(secondary1, times(2)).onTopicEvent(any(), any(), any());
162 appender.clearExtractions();
163 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE2));
164 assertTrue(appender.getExtracted().toString().contains("discarding event of type"));
165 verify(secondary1, times(2)).onTopicEvent(any(), any(), any());
169 * Makes a JSON message of the given type.
171 * @param msgType the message type
172 * @return a JSON message of the given type
174 private String makeMessage(String msgType) {
175 String json = "{'" + TYPE_FIELD + "':'" + msgType + "', 'abc':'def'}";
176 return json.replace('\'', '"');