BUG 2839: Config: remove dependencies on commons-io
[controller.git] / opendaylight / config / logback-config / src / test / java / org / opendaylight / controller / config / yang / logback / config / LogbackModuleWithInitialConfigurationTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.yang.logback.config;
9
10 import static org.junit.Assert.assertEquals;
11
12 import ch.qos.logback.classic.LoggerContext;
13 import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
14 import ch.qos.logback.classic.joran.JoranConfigurator;
15 import ch.qos.logback.classic.spi.ILoggingEvent;
16 import ch.qos.logback.core.joran.spi.JoranException;
17 import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
18 import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
19 import com.google.common.collect.Lists;
20 import java.io.File;
21 import java.io.IOException;
22 import java.lang.management.ManagementFactory;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import javax.management.InstanceAlreadyExistsException;
27 import javax.management.InstanceNotFoundException;
28 import javax.management.JMX;
29 import javax.management.MalformedObjectNameException;
30 import javax.management.ObjectName;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.opendaylight.controller.config.api.ConflictingVersionException;
34 import org.opendaylight.controller.config.api.ValidationException;
35 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
36 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
37 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
38 import org.opendaylight.controller.config.util.ConfigTransactionClient;
39 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
40 import org.slf4j.LoggerFactory;
41
42 public class LogbackModuleWithInitialConfigurationTest extends AbstractConfigTest {
43
44     private LogbackModuleFactory factory;
45
46     @Before
47     public void setUp() throws IOException, ClassNotFoundException {
48
49         factory = new LogbackModuleFactory();
50         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,factory));
51     }
52
53     /**
54      * Tests that initial configuration was changed. Changed attributes:
55      * location, fileName, duplicateInsertTries. Added new FileAppender.
56      */
57     @Test
58     public void test() throws Exception {
59
60         createBeans();
61
62         ConfigTransactionClient transaction = configRegistryClient.createTransaction();
63
64         LogbackModuleMXBean bean = JMX.newMXBeanProxy(ManagementFactory.getPlatformMBeanServer(),
65                 transaction.lookupConfigBean("logback", "singleton"), LogbackModuleMXBean.class);
66         assertEquals(1, bean.getConsoleAppenderTO().size());
67         assertEquals(1, bean.getRollingFileAppenderTO().size());
68         assertEquals(0, bean.getFileAppenderTO().size());
69         assertEquals(1, bean.getLoggerTO().size());
70
71         RollingFileAppenderTO rolling = new RollingFileAppenderTO();
72         RollingFileAppenderTO old = bean.getRollingFileAppenderTO().get(0);
73         rolling.setAppend(old.getAppend());
74         rolling.setEncoderPattern(old.getEncoderPattern());
75         rolling.setRollingPolicyType(old.getRollingPolicyType());
76         rolling.setFileName("target/logFile1.log");
77         rolling.setFileNamePattern("target/%i.log");
78         rolling.setMaxFileSize(old.getMaxFileSize());
79         rolling.setMinIndex(old.getMinIndex());
80         rolling.setMaxIndex(old.getMaxIndex());
81         rolling.setName("FILE");
82
83         ConsoleAppenderTO console = new ConsoleAppenderTO();
84         console.setEncoderPattern("%date %level [%thread] %logger{10} %msg%n");
85         console.setName("SYSTEM");
86         console.setThresholdFilter("DEBUG");
87
88         FileAppenderTO file = new FileAppenderTO();
89         file.setName("FILE_APPENDER");
90         file.setAppend(true);
91         file.setEncoderPattern("%-4relative [%thread] %-5level %logger{35} - %msg%n");
92         file.setFileName("target/testFile.log");
93
94         bean.setConsoleAppenderTO(Lists.newArrayList(console));
95         bean.setRollingFileAppenderTO(Lists.newArrayList(rolling));
96         bean.setFileAppenderTO(Lists.newArrayList(file));
97
98         LoggerTO logger = new LoggerTO();
99         logger.setLevel("INFO");
100         logger.setLoggerName("logger");
101         logger.setAppenders(Lists.newArrayList("SYSTEM"));
102
103         LoggerTO fileLogger = new LoggerTO();
104         fileLogger.setLevel("DEBUG");
105         fileLogger.setLoggerName("fileLogger");
106         fileLogger.setAppenders(Lists.newArrayList("FILE_APPENDER"));
107
108         List<LoggerTO> loggers = Lists.newArrayList(logger, fileLogger);
109         bean.setLoggerTO(loggers);
110
111         transaction.commit();
112
113         LogbackModuleMXBean logback = configRegistryClient.newMXBeanProxy(
114                 ObjectNameUtil.createReadOnlyModuleON("logback", "singleton"), LogbackModuleMXBean.class);
115
116         List<RollingFileAppenderTO> rollingList = logback.getRollingFileAppenderTO();
117         assertEquals(1, rollingList.size());
118
119         RollingFileAppenderTO rollingApp = rollingList.get(0);
120         assertEquals(rollingApp.getFileName(), "target/logFile1.log");
121         assertEquals(rollingApp.getName(), "FILE");
122
123         List<ConsoleAppenderTO> consoleList = logback.getConsoleAppenderTO();
124         assertEquals(1, consoleList.size());
125
126         ConsoleAppenderTO consoleApp = consoleList.get(0);
127         assertEquals(consoleApp.getThresholdFilter(), "DEBUG");
128         assertEquals(consoleApp.getName(), "SYSTEM");
129
130         List<FileAppenderTO> fileList = logback.getFileAppenderTO();
131         assertEquals(1, fileList.size());
132
133         FileAppenderTO fileApp = fileList.get(0);
134         assertEquals(fileApp.getFileName(), "target/testFile.log");
135         assertEquals(fileApp.getName(), "FILE_APPENDER");
136
137         loggers = logback.getLoggerTO();
138         assertEquals(2, loggers.size());
139         assertEquals("logger", loggers.get(0).getLoggerName());
140         assertEquals("fileLogger", loggers.get(1).getLoggerName());
141
142     }
143
144     public ObjectName createBeans() throws JoranException, InstanceAlreadyExistsException, IOException,
145             MalformedObjectNameException, InstanceNotFoundException, ValidationException, ConflictingVersionException {
146
147         LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
148
149         JoranConfigurator configurator = new JoranConfigurator();
150         configurator.setContext(lc);
151         configurator.doConfigure("src/test/resources/simple_config_logback.xml");
152         File f = new File("target/it");
153         if (f.exists()) {
154             cleanDirectory(f);
155         }
156         ch.qos.logback.classic.Logger logger = lc.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
157         ch.qos.logback.core.rolling.RollingFileAppender<ILoggingEvent> fileAppender = (ch.qos.logback.core.rolling.RollingFileAppender<ILoggingEvent>) logger
158                 .getAppender("VARLOGFILE");
159         fileAppender.start();
160
161         ch.qos.logback.core.ConsoleAppender<ILoggingEvent> consoleAppender = (ch.qos.logback.core.ConsoleAppender<ILoggingEvent>) logger
162                 .getAppender("STDOUT");
163         consoleAppender.start();
164         List<RollingFileAppenderTO> rollingAppenders = new ArrayList<>();
165         RollingFileAppenderTO rollingApp = new RollingFileAppenderTO();
166         rollingApp.setAppend(fileAppender.isAppend());
167         PatternLayoutEncoder enc = (PatternLayoutEncoder) fileAppender.getEncoder();
168         rollingApp.setEncoderPattern(enc.getPattern());
169         rollingApp.setFileName(fileAppender.getFile());
170         FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) fileAppender.getRollingPolicy();
171         rollingApp.setRollingPolicyType("FixedWindowRollingPolicy");
172         rollingApp.setMaxIndex(rollingPolicy.getMaxIndex());
173         rollingApp.setMinIndex(rollingPolicy.getMinIndex());
174         SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = (SizeBasedTriggeringPolicy<ILoggingEvent>) fileAppender
175                 .getTriggeringPolicy();
176         rollingApp.setMaxFileSize(triggeringPolicy.getMaxFileSize());
177         rollingApp.setName(fileAppender.getName());
178         rollingApp.setFileNamePattern(rollingPolicy.getFileNamePattern());
179         rollingAppenders.add(rollingApp);
180
181         assertEquals(rollingApp.getFileName(), "target/osgi.log");
182         assertEquals(rollingApp.getMaxFileSize(), "50MB");
183         assertEquals(rollingApp.getName(), "VARLOGFILE");
184
185         List<ConsoleAppenderTO> consoleAppenders = new ArrayList<>();
186         ConsoleAppenderTO consoleApp = new ConsoleAppenderTO();
187         enc = (PatternLayoutEncoder) consoleAppender.getEncoder();
188         consoleApp.setEncoderPattern(enc.getPattern());
189         consoleApp.setName(consoleAppender.getName());
190         consoleApp.setThresholdFilter("ALL");
191         consoleAppenders.add(consoleApp);
192
193         List<FileAppenderTO> fileAppenders = new ArrayList<>();
194
195         List<LoggerTO> loggersDTOs = new ArrayList<>();
196         LoggerTO log = new LoggerTO();
197         log.setAppenders(Arrays.asList(fileAppender.getName(), consoleApp.getName()));
198
199         log.setLevel(logger.getLevel().toString());
200         log.setLoggerName(logger.getName());
201         loggersDTOs.add(log);
202
203         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
204         ObjectName nameRetrieved = transaction.lookupConfigBean(factory.getImplementationName(), LogbackModuleFactory.INSTANCE_NAME);
205         LogbackModuleMXBean bean = transaction.newMXBeanProxy(nameRetrieved, LogbackModuleMXBean.class);
206
207         bean.setLoggerTO(loggersDTOs);
208         bean.setRollingFileAppenderTO(rollingAppenders);
209         bean.setConsoleAppenderTO(consoleAppenders);
210         bean.setFileAppenderTO(fileAppenders);
211
212         transaction.commit();
213
214         return nameRetrieved;
215     }
216 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.