2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yang.logback.config;
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.fail;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17 import javax.management.ObjectName;
18 import org.junit.Before;
19 import org.junit.Ignore;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.api.ValidationException;
22 import org.opendaylight.controller.config.api.jmx.CommitStatus;
23 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
24 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
25 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
27 public class LogbackModuleTest extends AbstractConfigTest {
29 private static final String INSTANCE_NAME = "singleton";
31 private LogbackModuleFactory factory;
34 public void setUp() throws Exception {
36 factory = new LogbackModuleFactory();
37 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,factory));
41 public void testCreateBean() throws Exception {
43 CommitStatus status = createBeans(true, "target/rollingApp",
44 "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1, 5, "target/%i.log", "rolling",
45 "consoleName", "ALL", "logger1", "DEBUG", "FixedWindowRollingPolicy", 0, "FileAppender").commit();
47 assertBeanCount(1, factory.getImplementationName());
48 assertStatus(status, 1, 0, 0);
52 public void testReusingInstance() throws Exception {
53 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1, 5,
54 "target/%i.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG", "FixedWindowRollingPolicy", 0,
55 "FileAppender").commit();
57 assertBeanCount(1, factory.getImplementationName());
59 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
60 CommitStatus status = transaction.commit();
62 assertBeanCount(1, factory.getImplementationName());
63 assertStatus(status, 0, 0, 1);
67 public void testRecreateInstance() throws Exception {
68 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1, 5,
69 "target/%i.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG", "FixedWindowRollingPolicy", 0,
70 "FileAppender").commit();
72 assertBeanCount(1, LogbackModuleFactory.NAME);
73 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
75 ObjectName logback = transaction.lookupConfigBean(LogbackModuleFactory.NAME, "singleton");
76 LogbackModuleMXBean nwBean = transaction.newMXBeanProxy(logback, LogbackModuleMXBean.class);
77 CommitStatus status = transaction.commit();
78 assertBeanCount(1, LogbackModuleFactory.NAME);
80 assertStatus(status, 0, 0, 1);
84 public void testDestroyInstance() throws Exception {
85 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1, 5,
86 "target/%i.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG", "FixedWindowRollingPolicy", 0,
87 "FileAppender").commit();
88 assertBeanCount(1, factory.getImplementationName());
90 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
91 transaction.destroyModule(factory.getImplementationName(), INSTANCE_NAME);
92 CommitStatus status = transaction.commit();
94 assertBeanCount(0, factory.getImplementationName());
95 assertStatus(status, 0, 0, 0);
100 public void testValidation1() throws Exception {
102 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1, 5,
103 "target/%i.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG", "FixedWindowRollingPolicy",
104 0, "FileAppender").commit();
106 } catch (final ValidationException e) {
107 assertThat(e.getFailedValidations().toString(), containsString("FileName is null"));
112 public void testValidation2() throws Exception {
114 createBeans(true, "target/rollingApp", null, "30MB", 1, 5, "target/%i.log", "rolling", "consoleName",
115 "ALL", "logger1", "DEBUG", "FixedWindowRollingPolicy", 0, "FileAppender").commit();
117 } catch (final ValidationException e) {
118 assertThat(e.getFailedValidations().toString(), containsString("EncoderPattern is null"));
123 public void testValidation4() throws Exception {
125 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", null, 1, 5,
126 "target/%i.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG", "FixedWindowRollingPolicy",
127 0, "FileAppender").commit();
129 } catch (final ValidationException e) {
130 assertThat(e.getFailedValidations().toString(), containsString("MaxFileSize is null"));
135 public void testValidation6() throws Exception {
137 createBeans(true, "", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1, 5, "target/%i.log",
138 "rolling", "consoleName", "ALL", "logger1", "DEBUG", "FixedWindowRollingPolicy", 0, "FileAppender")
141 } catch (final ValidationException e) {
142 assertThat(e.getFailedValidations().toString(), containsString("FileName needs to be set"));
147 public void testValidation7() throws Exception {
151 true, "target/rollingApp", "", "30MB", 1, 5, "target/%i.log", "rolling", "consoleName", "ALL", "logger1",
152 "DEBUG", "FixedWindowRollingPolicy", 0, "FileAppender").commit();
154 } catch (final ValidationException e) {
155 assertThat(e.getFailedValidations().toString(), containsString("EncoderPattern needs to be set"));
160 public void testValidation8() throws Exception {
162 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1, 5,
163 "target/%i.log", "rolling", "consoleName", "ALL", null, "DEBUG", "FixedWindowRollingPolicy", 0,
164 "FileAppender").commit();
166 } catch (final ValidationException e) {
167 assertThat(e.getFailedValidations().toString(), containsString("LoggerName is null"));
172 public void testValidation9() throws Exception {
174 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1, 5,
175 "target/%i.log", "rolling", "consoleName", "ALL", "", "DEBUG", "FixedWindowRollingPolicy", 0,
176 "FileAppender").commit();
178 } catch (final ValidationException e) {
179 assertThat(e.getFailedValidations().toString(), containsString("LoggerName needs to be set"));
184 public void testValidation10() throws Exception {
186 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", null,
187 5, "target/%i.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG",
188 "FixedWindowRollingPolicy", 0, "FileAppender").commit();
190 } catch (final ValidationException e) {
191 assertThat(e.getFailedValidations().toString(), containsString("MinIndex is null"));
196 public void testValidation11() throws Exception {
198 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1,
199 null, "target/%i.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG",
200 "FixedWindowRollingPolicy", 0, "FileAppender").commit();
202 } catch (final ValidationException e) {
203 assertThat(e.getFailedValidations().toString(), containsString("MaxIndex is null"));
208 public void testValidation12() throws Exception {
210 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1,
211 null, "target/%i.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG", null, 1, "FileAppender")
214 } catch (final ValidationException e) {
215 assertThat(e.getFailedValidations().toString(), containsString("RollingPolicyType is null"));
220 public void testValidation13() throws Exception {
222 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1,
223 null, "target/%i.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG", "", 1, "FileAppender")
226 } catch (final ValidationException e) {
227 assertThat(e.getFailedValidations().toString(), containsString("RollingPolicyType is not set"));
232 public void testValidation14() throws Exception {
234 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", 1,
235 null, "target/%i.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG", "RollingPolicy", 1,
236 "FileAppender").commit();
238 } catch (final ValidationException e) {
239 assertThat(e.getFailedValidations().toString(), containsString("RollingPolicyType is not supported"));
244 public void testTimeBasedRollingPolicy() throws Exception {
245 createBeans(true, "target/rollingApp", "%-4relative [%thread] %-5level %logger{35} - %msg%n", "30MB", null,
246 null, "target/%d.log", "rolling", "consoleName", "ALL", "logger1", "DEBUG", "TimeBasedRollingPolicy",
247 1, "FileAppender").commit();
250 private ConfigTransactionJMXClient createBeans(final Boolean isAppend, final String rollingFileName, final String encoderPattern,
251 final String maxFileSize, final Integer minIndex, final Integer maxIndex, final String fileNamePattern, final String rollingName,
252 final String consoleName, final String thresholdFilter, final String loggerName, final String level, final String rollingPolicyType,
253 final int maxHistory, final String fileAppName) throws Exception {
254 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
255 ObjectName nameRetrieved = transaction.lookupConfigBean(factory.getImplementationName(), INSTANCE_NAME);
256 LogbackModuleMXBean bean = transaction.newMXBeanProxy(nameRetrieved, LogbackModuleMXBean.class);
258 List<RollingFileAppenderTO> rollingAppenders = new ArrayList<>();
259 RollingFileAppenderTO rollingAppender = new RollingFileAppenderTO();
260 rollingAppender.setAppend(isAppend);
261 rollingAppender.setEncoderPattern(encoderPattern);
262 rollingAppender.setFileName(rollingFileName);
263 rollingAppender.setMaxFileSize(maxFileSize);
264 rollingAppender.setMaxIndex(maxIndex);
265 rollingAppender.setMinIndex(minIndex);
266 rollingAppender.setFileNamePattern(fileNamePattern);
267 rollingAppender.setName(rollingName);
268 rollingAppender.setRollingPolicyType(rollingPolicyType);
269 rollingAppender.setMaxHistory(maxHistory);
270 rollingAppenders.add(rollingAppender);
272 List<ConsoleAppenderTO> consoleAppenders = new ArrayList<>();
273 ConsoleAppenderTO consoleAppender = new ConsoleAppenderTO();
274 consoleAppender.setEncoderPattern(encoderPattern);
275 consoleAppender.setName(consoleName);
276 consoleAppender.setThresholdFilter(thresholdFilter);
277 consoleAppenders.add(consoleAppender);
279 List<FileAppenderTO> fileAppenders = new ArrayList<>();
280 FileAppenderTO fileAppender = new FileAppenderTO();
281 fileAppender.setName(fileAppName);
282 fileAppender.setAppend(isAppend);
283 fileAppender.setEncoderPattern(encoderPattern);
284 fileAppender.setFileName(rollingFileName);
285 fileAppenders.add(fileAppender);
287 List<LoggerTO> loggers = new ArrayList<>();
289 LoggerTO logger = new LoggerTO();
291 logger.setAppenders(Arrays.<String> asList());
293 logger.setLevel(level);
294 logger.setLoggerName(loggerName);
296 bean.setLoggerTO(loggers);
297 bean.setRollingFileAppenderTO(rollingAppenders);
298 bean.setConsoleAppenderTO(consoleAppenders);
299 bean.setFileAppenderTO(fileAppenders);
301 transaction.validateConfig();