Base projects and parents for IT tests.
[controller.git] / opendaylight / config / config-it-base / src / main / java / org / opendaylight / controller / config / it / base / AbstractConfigTestBase.java
1 package org.opendaylight.controller.config.it.base;
2
3 import static org.ops4j.pax.exam.CoreOptions.maven;
4 import static org.ops4j.pax.exam.CoreOptions.when;
5 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
6 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
7 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
8 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
9
10 import com.google.common.base.Stopwatch;
11
12 import java.io.File;
13 import java.lang.management.ManagementFactory;
14 import java.util.concurrent.TimeUnit;
15
16 import javax.management.ObjectName;
17
18 import org.junit.Before;
19 import org.junit.Rule;
20 import org.junit.internal.AssumptionViolatedException;
21 import org.junit.rules.TestRule;
22 import org.junit.rules.TestWatcher;
23 import org.junit.runner.Description;
24 import org.opendaylight.controller.config.api.ConfigRegistry;
25 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
26 import org.ops4j.pax.exam.Configuration;
27 import org.ops4j.pax.exam.Option;
28 import org.ops4j.pax.exam.karaf.options.KarafDistributionOption;
29 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
30 import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
31 import org.ops4j.pax.exam.options.MavenUrlReference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public abstract class AbstractConfigTestBase {
36
37     private static final String PAX_EXAM_UNPACK_DIRECTORY = "target/exam";
38     private static final String KARAF_DEBUG_PORT = "5005";
39     private static final String KARAF_DEBUG_PROP = "karaf.debug";
40     private static final String KEEP_UNPACK_DIRECTORY_PROP = "karaf.keep.unpack";
41     private static final Logger LOG = LoggerFactory.getLogger(AbstractConfigTestBase.class);
42     public static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
43
44     /*
45      * Default values for karaf distro version, type, groupId, and artifactId
46      */
47     private static final String KARAF_DISTRO_VERSION = "3.0.2";
48     private static final String KARAF_DISTRO_TYPE = "zip";
49     private static final String KARAF_DISTRO_ARTIFACTID = "apache-karaf";
50     private static final String KARAF_DISTRO_GROUPID = "org.apache.karaf";
51
52     /*
53      * Property names to override defaults for karaf distro artifactId, groupId, version, and type
54      */
55     private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
56     private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
57     private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
58     private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
59
60     /*
61      * Wait up to 10s for our configured module to come up
62      */
63     private static final int MODULE_TIMEOUT_MILLIS = 60000;
64
65     public abstract String getModuleName();
66
67     public abstract String getInstanceName();
68
69     public abstract MavenUrlReference getFeatureRepo();
70
71     public abstract String getFeatureName();
72
73     public Option getLoggingOption() {
74         Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
75                         logConfiguration(AbstractConfigTestBase.class),
76                         LogLevel.INFO.name());
77         return option;
78     }
79
80     public String logConfiguration(Class<?> klazz) {
81         return "log4j.logger." + klazz.getPackage().getName();
82     }
83
84     public String getKarafDistro() {
85         String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP,KARAF_DISTRO_GROUPID);
86         String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP,KARAF_DISTRO_ARTIFACTID);
87         String version = System.getProperty(KARAF_DISTRO_VERSION_PROP,KARAF_DISTRO_VERSION);
88         String type = System.getProperty(KARAF_DISTRO_TYPE_PROP,KARAF_DISTRO_TYPE);
89         MavenArtifactUrlReference karafUrl = maven()
90                 .groupId(groupId)
91                 .artifactId(artifactId)
92                 .version(version)
93                 .type(type);
94         return karafUrl.getURL();
95     }
96
97     @Configuration
98     public Option[] config() {
99         Option[] options = new Option[] {
100                 when(Boolean.getBoolean(KARAF_DEBUG_PROP))
101                         .useOptions(KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)),
102                 karafDistributionConfiguration().frameworkUrl(getKarafDistro())
103                         .unpackDirectory(new File(PAX_EXAM_UNPACK_DIRECTORY))
104                         .useDeployFolder(false),
105                 when(Boolean.getBoolean(KEEP_UNPACK_DIRECTORY_PROP)).useOptions(keepRuntimeFolder()),
106                 features(getFeatureRepo(), getFeatureName()),
107                 getLoggingOption()};
108         return options;
109     }
110
111     @Before
112     public void setup() throws Exception {
113         LOG.info("Module: {} Instance: {} attempting to configure.",
114                 getModuleName(),getInstanceName());
115         Stopwatch stopWatch = Stopwatch.createStarted();
116         ObjectName objectName = null;
117         for(int i = 0;i<MODULE_TIMEOUT_MILLIS;i++) {
118             try {
119                 ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory
120                         .getPlatformMBeanServer());
121                 objectName = configRegistryClient.lookupConfigBean(getModuleName(), getInstanceName());
122                 LOG.info("Module: {} Instance: {} ObjectName: {}.",
123                         getModuleName(),getInstanceName(),objectName);
124                 break;
125             } catch (Exception e) {
126                 if(i<MODULE_TIMEOUT_MILLIS) {
127                     Thread.sleep(1);
128                     continue;
129                 } else {
130                     throw e;
131                 }
132             }
133         }
134         if(objectName != null) {
135             LOG.info("Module: {} Instance: {} configured after {} ms",
136                 getModuleName(),getInstanceName(),
137                 stopWatch.elapsed(TimeUnit.MILLISECONDS));
138         } else {
139             throw new RuntimeException("NOT FOUND Module: " +getModuleName() + " Instance: " + getInstanceName() +
140                     " configured after " + stopWatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
141         }
142     }
143
144     @Rule
145     public TestRule watcher = new TestWatcher() {
146
147         @Override
148         protected void starting(Description description) {
149             LOG.info("TestWatcher: Starting test: {}", description.getDisplayName());
150         }
151
152         @Override
153         protected void finished(Description description) {
154             LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());
155         }
156
157         @Override
158         protected void succeeded(Description description) {
159             LOG.info("TestWatcher: Test succeeded: {}", description.getDisplayName());
160         }
161
162         @Override
163         protected void failed(Throwable ex, Description description) {
164             LOG.info("TestWatcher: Test failed: {}", description.getDisplayName(), ex);
165         }
166
167         @Override
168         protected void skipped(AssumptionViolatedException ex, Description description) {
169             LOG.info("TestWatcher: Test skipped: {} ", description.getDisplayName(), ex);
170         }
171     };
172
173 }

©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.