9573d1f66e5b225d5bc9fee0a286e436cff1f150
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / util / AAIAppServletContextListenerTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.util;
23
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;
41
42 import javax.servlet.ServletContextEvent;
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import static org.junit.Assert.assertFalse;
47 import static org.junit.Assert.assertTrue;
48
49 @PrepareForTest({AAIGraph.class, AAIConfig.class, ErrorLogHelper.class})
50 public class AAIAppServletContextListenerTest {
51         
52         @Rule
53         public PowerMockRule rule = new PowerMockRule();
54         
55         static {
56              PowerMockAgent.initializeIfNeeded();
57          }
58         
59         private ServletContextEvent arg; 
60         private AAIAppServletContextListener listener;
61                 
62         /**
63          * Initialize.
64          */
65         @Before
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);
72         
73                 listener = new AAIAppServletContextListener();
74                 configureLog();
75         }
76         
77                 /**
78                  * Test contextDestroyed.
79                  */
80                 @Test(expected = NullPointerException.class)
81                 @Ignore
82                 public void testContextDestroyed(){
83                         listener.contextDestroyed(arg);
84                         assertTrue(logContains(Level.DEBUG, "AAI Server shutdown"));
85                         assertTrue(logContains(Level.INFO, "AAI graph shutdown"));
86                 }
87                 
88                 /**
89                  * Test contextInitialized.
90                  */
91                 @Test
92                 //@Ignore
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"));
99                 }
100                 
101                 
102                 /**
103                  * Helper method to check if a String appears in the desired log level.
104                  *
105                  * @param level Log level to use
106                  * @param expected String to search for
107                  * @return True if search String is found, false otherwise
108                  */
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))
113                                         return true;
114                         }
115                         return false;
116                 }
117
118                 /**
119                  * Set logging level, and initialize log-appender.
120                  */
121                 private void configureLog() {
122                         org.slf4j.Logger rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
123                         rootLogger.debug("debug");
124                 //      rootLogger.();
125                 //      rootLogger.addAppender(RecordingAppender.appender(new PatternLayout()));
126                 }
127
128 }
129
130
131 /**
132  * Appender class that appends log messages to a String List when some logging event occurs
133  */
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;
138
139         private RecordingAppender() {
140                 super();
141         }
142
143         /**
144          * @param patternLayout Pattern to format log message
145          * @return Current appender 
146          */
147         public static RecordingAppender appender(PatternLayout patternLayout) {
148                 appender.patternLayout = patternLayout;
149                 appender.clear();
150                 return appender;
151         }
152
153         @Override
154         protected void append(ILoggingEvent event) {
155                 messages.add(patternLayout.doLayout(event));
156         }
157
158         public void close() {}
159
160         public boolean requiresLayout() {
161                 return false;
162         }
163
164         /**
165          * @return Return logs as a String array
166          */
167         public static String[] messages() {
168                 return (String[]) messages.toArray(new String[messages.size()]);
169         }
170
171         /**
172          * Clear the message container
173          */
174         private void clear() {
175                 messages.clear();
176         }
177         
178 }