Fix broken SouthboundIT test
[ovsdb.git] / southbound / southbound-it / src / test / java / org / opendaylight / ovsdb / southbound / it / AbstractConfigTestBase.java
1 /*
2  * Copyright (c) 2015 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.ovsdb.southbound.it;
9
10 import static org.ops4j.pax.exam.CoreOptions.maven;
11 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
12 //import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
13 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
14 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
15
16 import com.google.common.collect.ObjectArrays;
17
18 import java.io.File;
19 import java.lang.management.ManagementFactory;
20 import java.util.Calendar;
21
22 import javax.management.InstanceNotFoundException;
23
24 import org.junit.Rule;
25 import org.junit.internal.AssumptionViolatedException;
26 import org.junit.rules.TestRule;
27 import org.junit.rules.TestWatcher;
28 import org.junit.runner.Description;
29 import org.opendaylight.controller.config.api.ConfigRegistry;
30 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
31 import org.ops4j.pax.exam.Configuration;
32 import org.ops4j.pax.exam.Option;
33 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
34 import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
35 import org.ops4j.pax.exam.options.MavenUrlReference;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public abstract class AbstractConfigTestBase {
40     private static final Logger LOG = LoggerFactory.getLogger(AbstractConfigTestBase.class);
41
42     /*
43      * Wait up to 10s for our configured module to come up
44      */
45     private static final int MODULE_TIMEOUT = 10000;
46
47     public abstract String getModuleName();
48
49     public abstract String getInstanceName();
50
51     public abstract MavenUrlReference getFeatureRepo();
52
53     public abstract String getFeatureName();
54
55     public Option[] getLoggingOptions() {
56         Option[] options = new Option[] {
57                 editConfigurationFilePut(SouthboundITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
58                         logConfiguration(AbstractConfigTestBase.class),
59                         LogLevel.INFO.name())
60         };
61         return options;
62     }
63
64     public String logConfiguration(Class<?> klazz) {
65         return "log4j.logger." + klazz.getPackage().getName();
66     }
67
68     public Option[] getFeaturesOptions() {
69         return new Option[]{};
70     }
71
72     public Option[] getPropertiesOptions() {
73         return new Option[]{};
74     }
75
76     public MavenArtifactUrlReference getKarafDistro() {
77         /*MavenArtifactUrlReference karafUrl = maven()
78                 .groupId("org.opendaylight.controller")
79                 .artifactId("opendaylight-karaf-empty")
80                 .version("1.5.0-SNAPSHOT")
81                 .type("zip");*/
82         MavenArtifactUrlReference karafUrl = maven()
83                 .groupId("org.opendaylight.ovsdb")
84                 .artifactId("southbound-karaf")
85                 .version("1.1.0-SNAPSHOT")
86                 .type("zip");
87         return karafUrl;
88     }
89
90     @Configuration
91     public Option[] config() {
92         Option[] options = new Option[] {
93                 // KarafDistributionOption.debugConfiguration("5005", true),
94                 karafDistributionConfiguration()
95                         .frameworkUrl(getKarafDistro())
96                         .unpackDirectory(new File("target/exam"))
97                         .useDeployFolder(false),
98                 keepRuntimeFolder(),
99                 //features(getFeatureRepo() , getFeatureName())
100         };
101         options = ObjectArrays.concat(options, getFeaturesOptions(), Option.class);
102         options = ObjectArrays.concat(options, getLoggingOptions(), Option.class);
103         options = ObjectArrays.concat(options, getPropertiesOptions(), Option.class);
104         return options;
105     }
106
107     public void setup() throws Exception {
108         LOG.info("Module: {} Instance: {} attempting to configure.",
109                 getModuleName(),getInstanceName());
110         Calendar start = Calendar.getInstance();
111         ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory
112                 .getPlatformMBeanServer());
113         for (int timer = 0;timer < MODULE_TIMEOUT;timer++) {
114             try {
115                 configRegistryClient.lookupConfigBean(getModuleName(), getInstanceName());
116                 Thread.sleep(1);
117             } catch (InstanceNotFoundException e) {
118                 if (timer >= MODULE_TIMEOUT) {
119                     throw e;
120                 }
121             } catch (InterruptedException e) {
122                 LOG.error("Exception: ",e);
123             }
124         }
125         Calendar stop = Calendar.getInstance();
126         LOG.info("Module: {} Instance: {} configured after {} ms",
127                 getModuleName(),getInstanceName(),
128                 stop.getTimeInMillis() - start.getTimeInMillis());
129     }
130
131     @Rule
132     public TestRule watcher = new TestWatcher() {
133         @Override
134         protected void starting(Description description) {
135             LOG.info("TestWatcher: Starting test:\n{}", description.getDisplayName());
136         }
137
138         @Override
139         protected void finished(Description description) {
140             LOG.info("TestWatcher: Finished test:\n{}", description.getDisplayName());
141         }
142
143         @Override
144         protected void succeeded(Description description) {
145             LOG.info("TestWatcher: Test succeeded:\n{}", description.getDisplayName());
146         }
147
148         @Override
149         protected void failed(Throwable ex, Description description) {
150             LOG.info("TestWatcher: Test failed:\n{} ", description.getDisplayName(), ex);
151         }
152
153         @Override
154         protected void skipped(AssumptionViolatedException ex, Description description) {
155             LOG.info("TestWatcher: Test skipped:\n{} ", description.getDisplayName(), ex);
156         }
157     };
158 }