Adding UI extensibility
[aai/sparky-be.git] / src / test / java / org / onap / aai / 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.onap.aai.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   /*
94    * (non-Javadoc)
95    * 
96    * @see ch.qos.logback.core.spi.LifeCycle#start()
97    */
98   @Override
99   public void start() {}
100
101   /*
102    * (non-Javadoc)
103    * 
104    * @see ch.qos.logback.core.spi.LifeCycle#stop()
105    */
106   @Override
107   public void stop() {}
108
109   @Override
110   public boolean isStarted() {
111     // TODO Auto-generated method stub
112     System.out.println("isStarted");
113     return false;
114   }
115
116   @Override
117   public void setContext(Context context) {
118     // TODO Auto-generated method stub
119     System.out.println("setContext");
120
121   }
122
123   @Override
124   public Context getContext() {
125     // TODO Auto-generated method stub
126     System.out.println("getContext");
127     return null;
128   }
129
130   /*
131    * (non-Javadoc)
132    * 
133    * @see ch.qos.logback.core.spi.ContextAware#addStatus(ch.qos.logback.core.status.Status)
134    */
135   @Override
136   public void addStatus(Status status) {
137     // TODO Auto-generated method stub
138     System.out.println("addStatus");
139   }
140
141   /*
142    * (non-Javadoc)
143    * 
144    * @see ch.qos.logback.core.spi.ContextAware#addInfo(java.lang.String)
145    */
146   @Override
147   public void addInfo(String msg) {
148     // TODO Auto-generated method stub
149
150   }
151
152   /*
153    * (non-Javadoc)
154    * 
155    * @see ch.qos.logback.core.spi.ContextAware#addInfo(java.lang.String, java.lang.Throwable)
156    */
157   @Override
158   public void addInfo(String msg, Throwable ex) {
159     // TODO Auto-generated method stub
160
161   }
162
163   /*
164    * (non-Javadoc)
165    * 
166    * @see ch.qos.logback.core.spi.ContextAware#addWarn(java.lang.String)
167    */
168   @Override
169   public void addWarn(String msg) {
170     // TODO Auto-generated method stub
171
172   }
173
174   /*
175    * (non-Javadoc)
176    * 
177    * @see ch.qos.logback.core.spi.ContextAware#addWarn(java.lang.String, java.lang.Throwable)
178    */
179   @Override
180   public void addWarn(String msg, Throwable ex) {
181     // TODO Auto-generated method stub
182
183   }
184
185   /*
186    * (non-Javadoc)
187    * 
188    * @see ch.qos.logback.core.spi.ContextAware#addError(java.lang.String)
189    */
190   @Override
191   public void addError(String msg) {
192     // TODO Auto-generated method stub
193
194   }
195
196   /*
197    * (non-Javadoc)
198    * 
199    * @see ch.qos.logback.core.spi.ContextAware#addError(java.lang.String, java.lang.Throwable)
200    */
201   @Override
202   public void addError(String msg, Throwable ex) {
203     // TODO Auto-generated method stub
204
205   }
206
207   /*
208    * (non-Javadoc)
209    * 
210    * @see ch.qos.logback.core.spi.FilterAttachable#addFilter(ch.qos.logback.core.filter.Filter)
211    */
212   @Override
213   public void addFilter(Filter newFilter) {
214     // TODO Auto-generated method stub
215
216   }
217
218   /*
219    * (non-Javadoc)
220    * 
221    * @see ch.qos.logback.core.spi.FilterAttachable#clearAllFilters()
222    */
223   @Override
224   public void clearAllFilters() {
225     // TODO Auto-generated method stub
226
227   }
228
229   @Override
230   public List getCopyOfAttachedFiltersList() {
231     // TODO Auto-generated method stub
232     return null;
233   }
234
235   /*
236    * (non-Javadoc)
237    * 
238    * @see ch.qos.logback.core.spi.FilterAttachable#getFilterChainDecision(java.lang.Object)
239    */
240   @Override
241   public FilterReply getFilterChainDecision(Object event) {
242     // TODO Auto-generated method stub
243     return null;
244   }
245
246   @Override
247   public String getName() {
248     // TODO Auto-generated method stub
249     System.out.println("getName");
250     return "MOCK";
251   }
252
253   /*
254    * (non-Javadoc)
255    * 
256    * @see ch.qos.logback.core.Appender#doAppend(java.lang.Object)
257    */
258   @Override
259   public void doAppend(Object event) throws LogbackException {
260     // TODO Auto-generated method stub
261     // System.out.println("doAppend(), event = " + event);
262     // System.out.println("event class = " + event.getClass().getSimpleName());
263     capturedLogs.add((LoggingEvent) event);
264   }
265
266   @Override
267   public void setName(String name) {
268     // TODO Auto-generated method stub
269     System.out.println("setName() name = " + name);
270
271   }
272
273 }