[POLICY-52] pdp-d: PolicyEngine junits
[policy/drools-pdp.git] / feature-eelf / src / test / java / org / openecomp / policy / drools / eelf / test / EElfTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-eelf
4  * ================================================================================
5  * Copyright (C) 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 package org.openecomp.policy.drools.eelf.test;
21
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.junit.Test;
29 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
30 import org.openecomp.policy.drools.eelf.EelfFeature;
31 import org.openecomp.policy.drools.system.Main;
32 import org.openecomp.policy.drools.system.PolicyEngine;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.att.eelf.configuration.Configuration;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFLogger.Level;
39 import com.att.eelf.configuration.EELFManager;
40
41 import ch.qos.logback.classic.LoggerContext;
42
43 /**
44  * Logger Tests
45  */
46 public class EElfTest {
47         
48         /**
49          * logback configuration location
50          */
51         public final static String LOGBACK_CONFIGURATION_FILE_DEFAULT = "src/main/feature/config/logback-eelf.xml";
52         
53         /**
54          * SLF4J Logger
55          */
56         private final Logger slf4jLogger = LoggerFactory.getLogger(EElfTest.class);
57         
58         /**
59          * get all loggers
60          * @return list of all loggers
61          */
62         protected List<String> loggers() {
63                 List<String> loggers = new ArrayList<String>();
64                 LoggerContext context = 
65                         (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();
66         for (org.slf4j.Logger logger: context.getLoggerList()) {
67                 loggers.add(logger.getName());
68         }
69         slf4jLogger.info(loggers.toString());
70                 return loggers;
71         }
72         
73         /**
74          * Assert Log Levels are the same between an EELF Logger and an SLF4J Logger
75          * 
76          * @param eelfLogger EELF Logger
77          * @param slf4jLogger SLF4J Logger
78          */
79         protected void assertLogLevels(EELFLogger eelfLogger, Logger slf4jLogger) {
80                 assertTrue(slf4jLogger.isDebugEnabled() == eelfLogger.isDebugEnabled());
81                 assertTrue(slf4jLogger.isInfoEnabled() == eelfLogger.isInfoEnabled());
82                 assertTrue(slf4jLogger.isErrorEnabled() == eelfLogger.isErrorEnabled());
83                 assertTrue(slf4jLogger.isWarnEnabled() == eelfLogger.isWarnEnabled());
84                 assertTrue(slf4jLogger.isTraceEnabled() == eelfLogger.isTraceEnabled());
85         }
86         
87         @Test
88         public void slf4jLog() {
89                 
90                 /* standard slf4j using defaults */
91                 
92                 slf4jLogger.info("slf4j info");
93                 
94                 List<String> loggers = loggers();       
95         
96                 assertFalse(loggers.contains(Configuration.DEBUG_LOGGER_NAME));
97                 assertFalse(loggers.contains(Configuration.AUDIT_LOGGER_NAME));
98                 assertFalse(loggers.contains(Configuration.ERROR_LOGGER_NAME));
99                 assertFalse(loggers.contains(Configuration.METRICS_LOGGER_NAME));
100                 
101                 /* set logback configuration */
102                 
103         System.setProperty(Main.LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY, 
104                            LOGBACK_CONFIGURATION_FILE_DEFAULT);
105         
106         /* set up eelf throuth common loggings library */
107         
108                 EelfFeature feature = new EelfFeature();
109                 feature.beforeBoot(PolicyEngine.manager, null);
110                 
111                 loggers = loggers();
112                 assertTrue(loggers.contains(Configuration.DEBUG_LOGGER_NAME));
113                 assertTrue(loggers.contains(Configuration.AUDIT_LOGGER_NAME));
114                 assertTrue(loggers.contains(Configuration.ERROR_LOGGER_NAME));
115                 assertTrue(loggers.contains(Configuration.METRICS_LOGGER_NAME));
116                 
117                 EELFLogger eelfAuditLogger = EELFManager.getInstance().getAuditLogger();
118                 Logger slf4jAuditLogger = org.slf4j.LoggerFactory.getLogger(Configuration.AUDIT_LOGGER_NAME);
119                 org.openecomp.policy.common.logging.flexlogger.Logger flexLogger = 
120                                                                                                 FlexLogger.getLogger(EElfTest.class, true);
121                 
122                 /* generate an error entry */
123                 
124                 Exception testException = new IllegalStateException("exception test");
125                 flexLogger.error("flex-logger exception", testException);
126                 EELFManager.getInstance().getErrorLogger().error("eelf-logger exception", testException);
127                 org.slf4j.LoggerFactory.getLogger(Configuration.ERROR_LOGGER_NAME).error("slf4j-logger", testException);
128
129                 
130                 /* generate an audit entry through all logs */
131                 
132                 flexLogger.audit("flexlogger audit");
133                 eelfAuditLogger.info("eelf audit");
134                 slf4jAuditLogger.info("slf4j audit");
135                 
136                 /* check log levels in eelf and standard slf4j  change in both directions */
137                 
138                 /* eelf initiated */
139                 eelfAuditLogger.setLevel(Level.ERROR);          
140                 assertLogLevels(eelfAuditLogger, slf4jAuditLogger);
141                 
142                 /* slf4j initiated */
143                 ((ch.qos.logback.classic.Logger) slf4jLogger).setLevel((ch.qos.logback.classic.Level.INFO));            
144                 assertLogLevels(eelfAuditLogger, slf4jAuditLogger);
145         }
146 }