2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.aai.util;
\r
23 import ch.qos.logback.classic.Level;
\r
24 import ch.qos.logback.classic.Logger;
\r
25 import ch.qos.logback.classic.PatternLayout;
\r
26 import ch.qos.logback.classic.spi.ILoggingEvent;
\r
27 import ch.qos.logback.core.AppenderBase;
\r
28 import org.apache.commons.lang.ObjectUtils;
\r
29 import org.junit.Before;
\r
30 import org.junit.Ignore;
\r
31 import org.junit.Rule;
\r
32 import org.junit.Test;
\r
33 import org.openecomp.aai.dbmap.AAIGraph;
\r
34 import org.openecomp.aai.logging.ErrorLogHelper;
\r
35 import org.powermock.api.mockito.PowerMockito;
\r
36 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
37 import org.powermock.modules.agent.PowerMockAgent;
\r
38 import org.powermock.modules.junit4.rule.PowerMockRule;
\r
39 import org.slf4j.LoggerFactory;
\r
41 import javax.servlet.ServletContextEvent;
\r
42 import java.util.ArrayList;
\r
43 import java.util.List;
\r
45 import static org.junit.Assert.assertFalse;
\r
46 import static org.junit.Assert.assertTrue;
\r
48 @PrepareForTest({AAIGraph.class, AAIConfig.class, ErrorLogHelper.class})
\r
49 public class AAIAppServletContextListenerTest {
\r
52 public PowerMockRule rule = new PowerMockRule();
\r
55 PowerMockAgent.initializeIfNeeded();
\r
58 private ServletContextEvent arg;
\r
59 private AAIAppServletContextListener listener;
\r
65 @PrepareForTest({AAIGraph.class, AAIConfig.class, ErrorLogHelper.class})
\r
66 public void initialize(){
\r
67 arg = PowerMockito.mock(ServletContextEvent.class);
\r
68 PowerMockito.mockStatic(AAIGraph.class);
\r
69 PowerMockito.mockStatic(AAIConfig.class);
\r
70 PowerMockito.mockStatic(ErrorLogHelper.class);
\r
72 listener = new AAIAppServletContextListener();
\r
77 * Test contextDestroyed.
\r
79 @Test(expected = NullPointerException.class)
\r
81 public void testContextDestroyed(){
\r
82 listener.contextDestroyed(arg);
\r
83 assertTrue(logContains(Level.DEBUG, "AAI Server shutdown"));
\r
84 assertTrue(logContains(Level.INFO, "AAI graph shutdown"));
\r
88 * Test contextInitialized.
\r
92 public void testContextInitialized(){
\r
93 listener.contextInitialized(arg);
\r
94 assertFalse(logContains(Level.DEBUG, "Loading aaiconfig.properties"));
\r
95 assertFalse(logContains(Level.DEBUG, "Loading error.properties"));
\r
96 assertFalse(logContains(Level.DEBUG, "Loading graph database"));
\r
97 assertFalse(logContains(Level.INFO, "AAI Server initialization"));
\r
102 * Helper method to check if a String appears in the desired log level.
\r
104 * @param level Log level to use
\r
105 * @param expected String to search for
\r
106 * @return True if search String is found, false otherwise
\r
108 private boolean logContains(Level level, String expected) {
\r
109 String actual[] = RecordingAppender.messages();
\r
110 for (String log : actual) {
\r
111 if (log.contains(level.toString()) && log.contains(expected))
\r
118 * Set logging level, and initialize log-appender.
\r
120 private void configureLog() {
\r
121 org.slf4j.Logger rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
\r
122 rootLogger.debug("debug");
\r
124 // rootLogger.addAppender(RecordingAppender.appender(new PatternLayout()));
\r
131 * Appender class that appends log messages to a String List when some logging event occurs
\r
133 class RecordingAppender extends AppenderBase<ILoggingEvent> {
\r
134 private static List<String> messages = new ArrayList<String>();
\r
135 private static RecordingAppender appender = new RecordingAppender();
\r
136 private PatternLayout patternLayout;
\r
138 private RecordingAppender() {
\r
143 * @param patternLayout Pattern to format log message
\r
144 * @return Current appender
\r
146 public static RecordingAppender appender(PatternLayout patternLayout) {
\r
147 appender.patternLayout = patternLayout;
\r
153 protected void append(ILoggingEvent event) {
\r
154 messages.add(patternLayout.doLayout(event));
\r
157 public void close() {}
\r
159 public boolean requiresLayout() {
\r
164 * @return Return logs as a String array
\r
166 public static String[] messages() {
\r
167 return (String[]) messages.toArray(new String[messages.size()]);
\r
171 * Clear the message container
\r
173 private void clear() {
\r