2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.util;
24 import ch.qos.logback.classic.Level;
25 import ch.qos.logback.classic.Logger;
26 import ch.qos.logback.classic.PatternLayout;
27 import ch.qos.logback.classic.spi.ILoggingEvent;
28 import ch.qos.logback.core.AppenderBase;
29 import org.apache.commons.lang.ObjectUtils;
30 import org.junit.Before;
31 import org.junit.Ignore;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.onap.aai.dbmap.AAIGraph;
35 import org.onap.aai.logging.ErrorLogHelper;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.agent.PowerMockAgent;
39 import org.powermock.modules.junit4.rule.PowerMockRule;
40 import org.slf4j.LoggerFactory;
42 import javax.servlet.ServletContextEvent;
43 import java.util.ArrayList;
44 import java.util.List;
46 import static org.junit.Assert.assertFalse;
47 import static org.junit.Assert.assertTrue;
49 @PrepareForTest({AAIGraph.class, AAIConfig.class, ErrorLogHelper.class})
50 public class AAIAppServletContextListenerTest {
53 public PowerMockRule rule = new PowerMockRule();
56 PowerMockAgent.initializeIfNeeded();
59 private ServletContextEvent arg;
60 private AAIAppServletContextListener listener;
66 @PrepareForTest({AAIGraph.class, AAIConfig.class, ErrorLogHelper.class})
67 public void initialize(){
68 arg = PowerMockito.mock(ServletContextEvent.class);
69 PowerMockito.mockStatic(AAIGraph.class);
70 PowerMockito.mockStatic(AAIConfig.class);
71 PowerMockito.mockStatic(ErrorLogHelper.class);
73 listener = new AAIAppServletContextListener();
78 * Test contextDestroyed.
80 @Test(expected = NullPointerException.class)
82 public void testContextDestroyed(){
83 listener.contextDestroyed(arg);
84 assertTrue(logContains(Level.DEBUG, "AAI Server shutdown"));
85 assertTrue(logContains(Level.INFO, "AAI graph shutdown"));
89 * Test contextInitialized.
93 public void testContextInitialized(){
94 listener.contextInitialized(arg);
95 assertFalse(logContains(Level.DEBUG, "Loading aaiconfig.properties"));
96 assertFalse(logContains(Level.DEBUG, "Loading error.properties"));
97 assertFalse(logContains(Level.DEBUG, "Loading graph database"));
98 assertFalse(logContains(Level.INFO, "AAI Server initialization"));
103 * Helper method to check if a String appears in the desired log level.
105 * @param level Log level to use
106 * @param expected String to search for
107 * @return True if search String is found, false otherwise
109 private boolean logContains(Level level, String expected) {
110 String actual[] = RecordingAppender.messages();
111 for (String log : actual) {
112 if (log.contains(level.toString()) && log.contains(expected))
119 * Set logging level, and initialize log-appender.
121 private void configureLog() {
122 org.slf4j.Logger rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
123 rootLogger.debug("debug");
125 // rootLogger.addAppender(RecordingAppender.appender(new PatternLayout()));
132 * Appender class that appends log messages to a String List when some logging event occurs
134 class RecordingAppender extends AppenderBase<ILoggingEvent> {
135 private static List<String> messages = new ArrayList<String>();
136 private static RecordingAppender appender = new RecordingAppender();
137 private PatternLayout patternLayout;
139 private RecordingAppender() {
144 * @param patternLayout Pattern to format log message
145 * @return Current appender
147 public static RecordingAppender appender(PatternLayout patternLayout) {
148 appender.patternLayout = patternLayout;
154 protected void append(ILoggingEvent event) {
155 messages.add(patternLayout.doLayout(event));
158 public void close() {}
160 public boolean requiresLayout() {
165 * @return Return logs as a String array
167 public static String[] messages() {
168 return (String[]) messages.toArray(new String[messages.size()]);
172 * Clear the message container
174 private void clear() {