Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / sparky / util / CaptureLoggerAppender.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
7 * All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12
13 *      http://www.apache.org/licenses/LICENSE-2.0
14
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
21
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.openecomp.sparky.util;
27
28 import java.util.ArrayList;
29 import java.util.Deque;
30 import java.util.List;
31 import java.util.concurrent.ConcurrentLinkedDeque;
32
33 import ch.qos.logback.classic.spi.LoggingEvent;
34 import ch.qos.logback.core.Appender;
35 import ch.qos.logback.core.Context;
36 import ch.qos.logback.core.LogbackException;
37 import ch.qos.logback.core.filter.Filter;
38 import ch.qos.logback.core.spi.FilterReply;
39 import ch.qos.logback.core.status.Status;
40
41 /**
42  * A test class used to provide a concrete log stub of the Log4j API interface. The goal is to
43  * transparently capture logging paths so we can add log validation during the junit validation
44  * without post-analyzing on-disk logs.
45  * 
46  * @author DAVEA
47  *
48  */
49 @SuppressWarnings("rawtypes")
50 public class CaptureLoggerAppender implements Appender {
51
52   private Deque<LoggingEvent> capturedLogs;
53
54   /**
55    * Instantiates a new capture logger appender.
56    */
57   public CaptureLoggerAppender() {
58     capturedLogs = new ConcurrentLinkedDeque<LoggingEvent>();
59   }
60
61   /**
62    * Drain all logs.
63    *
64    * @return the list
65    */
66   public List<LoggingEvent> drainAllLogs() {
67     List<LoggingEvent> loggingEvents = new ArrayList<LoggingEvent>();
68
69     LoggingEvent event = null;
70
71     while (capturedLogs.peek() != null) {
72       event = capturedLogs.pop();
73       loggingEvents.add(event);
74     }
75
76     return loggingEvents;
77   }
78
79   /**
80    * Clears the capture logs double-ended queue and returns the size of the queue before it was
81    * cleared.
82    * 
83    * @return int numCapturedLogs
84    */
85   public int clearAllLogs() {
86     int numCapturedLogs = capturedLogs.size();
87     capturedLogs.clear();
88     return numCapturedLogs;
89   }
90
91
92
93   /* (non-Javadoc)
94    * @see ch.qos.logback.core.spi.LifeCycle#start()
95    */
96   @Override
97   public void start() {}
98
99   /* (non-Javadoc)
100    * @see ch.qos.logback.core.spi.LifeCycle#stop()
101    */
102   @Override
103   public void stop() {}
104
105   @Override
106   public boolean isStarted() {
107     // TODO Auto-generated method stub
108     System.out.println("isStarted");
109     return false;
110   }
111
112   @Override
113   public void setContext(Context context) {
114     // TODO Auto-generated method stub
115     System.out.println("setContext");
116
117   }
118
119   @Override
120   public Context getContext() {
121     // TODO Auto-generated method stub
122     System.out.println("getContext");
123     return null;
124   }
125
126   /* (non-Javadoc)
127    * @see ch.qos.logback.core.spi.ContextAware#addStatus(ch.qos.logback.core.status.Status)
128    */
129   @Override
130   public void addStatus(Status status) {
131     // TODO Auto-generated method stub
132     System.out.println("addStatus");
133   }
134
135   /* (non-Javadoc)
136    * @see ch.qos.logback.core.spi.ContextAware#addInfo(java.lang.String)
137    */
138   @Override
139   public void addInfo(String msg) {
140     // TODO Auto-generated method stub
141
142   }
143
144   /* (non-Javadoc)
145    * @see ch.qos.logback.core.spi.ContextAware#addInfo(java.lang.String, java.lang.Throwable)
146    */
147   @Override
148   public void addInfo(String msg, Throwable ex) {
149     // TODO Auto-generated method stub
150
151   }
152
153   /* (non-Javadoc)
154    * @see ch.qos.logback.core.spi.ContextAware#addWarn(java.lang.String)
155    */
156   @Override
157   public void addWarn(String msg) {
158     // TODO Auto-generated method stub
159
160   }
161
162   /* (non-Javadoc)
163    * @see ch.qos.logback.core.spi.ContextAware#addWarn(java.lang.String, java.lang.Throwable)
164    */
165   @Override
166   public void addWarn(String msg, Throwable ex) {
167     // TODO Auto-generated method stub
168
169   }
170
171   /* (non-Javadoc)
172    * @see ch.qos.logback.core.spi.ContextAware#addError(java.lang.String)
173    */
174   @Override
175   public void addError(String msg) {
176     // TODO Auto-generated method stub
177
178   }
179
180   /* (non-Javadoc)
181    * @see ch.qos.logback.core.spi.ContextAware#addError(java.lang.String, java.lang.Throwable)
182    */
183   @Override
184   public void addError(String msg, Throwable ex) {
185     // TODO Auto-generated method stub
186
187   }
188
189   /* (non-Javadoc)
190    * @see ch.qos.logback.core.spi.FilterAttachable#addFilter(ch.qos.logback.core.filter.Filter)
191    */
192   @Override
193   public void addFilter(Filter newFilter) {
194     // TODO Auto-generated method stub
195
196   }
197
198   /* (non-Javadoc)
199    * @see ch.qos.logback.core.spi.FilterAttachable#clearAllFilters()
200    */
201   @Override
202   public void clearAllFilters() {
203     // TODO Auto-generated method stub
204
205   }
206
207   @Override
208   public List getCopyOfAttachedFiltersList() {
209     // TODO Auto-generated method stub
210     return null;
211   }
212
213   /* (non-Javadoc)
214    * @see ch.qos.logback.core.spi.FilterAttachable#getFilterChainDecision(java.lang.Object)
215    */
216   @Override
217   public FilterReply getFilterChainDecision(Object event) {
218     // TODO Auto-generated method stub
219     return null;
220   }
221
222   @Override
223   public String getName() {
224     // TODO Auto-generated method stub
225     System.out.println("getName");
226     return "MOCK";
227   }
228
229   /* (non-Javadoc)
230    * @see ch.qos.logback.core.Appender#doAppend(java.lang.Object)
231    */
232   @Override
233   public void doAppend(Object event) throws LogbackException {
234     // TODO Auto-generated method stub
235     // System.out.println("doAppend(), event = " + event);
236     // System.out.println("event class = " + event.getClass().getSimpleName());
237     capturedLogs.add((LoggingEvent) event);
238   }
239
240   @Override
241   public void setName(String name) {
242     // TODO Auto-generated method stub
243     System.out.println("setName() name = " + name);
244
245   }
246
247 }