Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / sparky / util / LogValidator.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.List;
29
30 import org.slf4j.LoggerFactory;
31
32 import ch.qos.logback.classic.Level;
33 import ch.qos.logback.classic.spi.LoggingEvent;
34
35 /**
36  * The Class LogValidator.
37  */
38 public class LogValidator {
39
40   protected CaptureLoggerAppender logger = null;
41
42   /**
43    * Initialize logger.
44    *
45    * @param level the level
46    */
47   @SuppressWarnings("unchecked")
48   public void initializeLogger(Level level) {
49     ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
50         .getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
51     root.detachAndStopAllAppenders();
52     logger = new CaptureLoggerAppender();
53     root.setLevel(level);
54     root.addAppender(logger);
55   }
56
57   public CaptureLoggerAppender getLogger() {
58     return logger;
59   }
60
61   /**
62    * Dump and count logs.
63    *
64    * @param logToConsole the log to console
65    * @return the int
66    */
67   public int dumpAndCountLogs(boolean logToConsole) {
68
69     List<LoggingEvent> logs = logger.drainAllLogs();
70
71     if (logs == null) {
72       return 0;
73     }
74
75     if (logToConsole) {
76       for (LoggingEvent e : logs) {
77         System.out.println(e);
78       }
79     }
80
81     return logs.size();
82
83   }
84
85 }