2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.Matchers.any;
26 import static org.mockito.Matchers.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.endpoints.listeners.MessageTypeDispatcher;
41 import org.onap.policy.common.endpoints.listeners.ScoListener;
42 import org.onap.policy.common.utils.coder.StandardCoderObject;
43 import org.onap.policy.common.utils.test.log.logback.ExtractAppender;
44 import org.slf4j.LoggerFactory;
46 public class MessageTypeDispatcherTest {
49 * Used to attach an appender to the class' logger.
51 private static final Logger logger = (Logger) LoggerFactory.getLogger(MessageTypeDispatcher.class);
52 private static final ExtractAppender appender = new ExtractAppender();
55 * Original logging level for the logger.
57 private static Level saveLevel;
59 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
60 private static final String TYPE_FIELD = "msg-type";
61 private static final String TOPIC = "my-topic";
62 private static final String TYPE1 = "msg-type-1";
63 private static final String TYPE2 = "msg-type-2";
65 private MessageTypeDispatcher primary;
67 private ScoListener<String> secondary1;
68 private ScoListener<String> secondary2;
71 * Initializes statics.
74 public static void setUpBeforeClass() {
75 saveLevel = logger.getLevel();
76 logger.setLevel(Level.INFO);
78 appender.setContext(logger.getLoggerContext());
83 public static void tearDownAfterClass() {
84 logger.setLevel(saveLevel);
89 * Initializes mocks and a listener.
92 @SuppressWarnings("unchecked")
94 appender.clearExtractions();
96 secondary1 = mock(ScoListener.class);
97 secondary2 = mock(ScoListener.class);
99 primary = new MessageTypeDispatcher(TYPE_FIELD);
103 public void tearDown() {
104 logger.detachAppender(appender);
108 public void testRegister_testUnregister() {
109 primary.register(TYPE1, secondary1);
110 primary.register(TYPE2, secondary2);
112 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
113 verify(secondary1).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
114 verify(secondary2, never()).onTopicEvent(any(), any(), any());
116 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
117 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
118 verify(secondary2, never()).onTopicEvent(any(), any(), any());
120 primary.unregister(TYPE1);
121 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
122 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
123 verify(secondary2, never()).onTopicEvent(any(), any(), any());
125 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE2));
126 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
127 verify(secondary2).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
130 primary.unregister(TYPE1);
132 // unregister second type
133 primary.unregister(TYPE2);
134 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
135 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE2));
136 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
137 verify(secondary2, times(1)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
141 public void testOnTopicEvent() {
142 primary.register(TYPE1, secondary1);
144 logger.addAppender(appender);
147 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
148 verify(secondary1).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
151 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE1));
152 verify(secondary1, times(2)).onTopicEvent(eq(INFRA), eq(TOPIC), any(StandardCoderObject.class));
154 assertFalse(appender.getExtracted().toString().contains("unable to extract"));
155 assertFalse(appender.getExtracted().toString().contains("discarding event of type"));
158 appender.clearExtractions();
159 primary.onTopicEvent(INFRA, TOPIC, "{}");
160 assertTrue(appender.getExtracted().toString().contains("unable to extract"));
161 verify(secondary1, times(2)).onTopicEvent(any(), any(), any());
164 appender.clearExtractions();
165 primary.onTopicEvent(INFRA, TOPIC, makeMessage(TYPE2));
166 assertTrue(appender.getExtracted().toString().contains("discarding event of type"));
167 verify(secondary1, times(2)).onTopicEvent(any(), any(), any());
171 * Makes a JSON message of the given type.
173 * @param msgType the message type
174 * @return a JSON message of the given type
176 private String makeMessage(String msgType) {
177 String json = "{'" + TYPE_FIELD + "':'" + msgType + "', 'abc':'def'}";
178 return json.replace('\'', '"');