Add working integration-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 void setExtras() {}
56
57     public Option[] getLoggingOptions() {
58         Option[] options = new Option[] {
59                 editConfigurationFilePut(SouthboundITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
60                         logConfiguration(AbstractConfigTestBase.class),
61                         LogLevel.INFO.name())
62         };
63         return options;
64     }
65
66     public String logConfiguration(Class<?> klazz) {
67         return "log4j.logger." + klazz.getPackage().getName();
68     }
69
70     public Option[] getFeaturesOptions() {
71         return new Option[]{};
72     }
73
74     public Option[] getPropertiesOptions() {
75         return new Option[]{};
76     }
77
78     public MavenArtifactUrlReference getKarafDistro() {
79         /*MavenArtifactUrlReference karafUrl = maven()
80                 .groupId("org.opendaylight.controller")
81                 .artifactId("opendaylight-karaf-empty")
82                 .version("1.5.0-SNAPSHOT")
83                 .type("zip");*/
84         MavenArtifactUrlReference karafUrl = maven()
85                 .groupId("org.opendaylight.ovsdb")
86                 //.artifactId("southbound-karaf")
87                 .artifactId("karaf")
88                 .version("1.1.0-SNAPSHOT")
89                 .type("zip");
90         return karafUrl;
91     }
92
93     @Configuration
94     public Option[] config() {
95         setExtras();
96         Option[] options = new Option[] {
97                 // KarafDistributionOption.debugConfiguration("5005", true),
98                 karafDistributionConfiguration()
99                         .frameworkUrl(getKarafDistro())
100                         .unpackDirectory(new File("target/exam"))
101                         .useDeployFolder(false),
102                 keepRuntimeFolder(),
103                 //features(getFeatureRepo() , getFeatureName())
104         };
105         //options = ObjectArrays.concat(options, getFeaturesOptions(), Option.class);
106         options = ObjectArrays.concat(options, getLoggingOptions(), Option.class);
107         options = ObjectArrays.concat(options, getPropertiesOptions(), Option.class);
108         LOG.info("options: {}", options);
109         return options;
110     }
111
112     public void setup() throws Exception {
113         LOG.info("Module: {} Instance: {} attempting to configure.",
114                 getModuleName(),getInstanceName());
115         Calendar start = Calendar.getInstance();
116         ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory
117                 .getPlatformMBeanServer());
118         for (int timer = 0;timer < MODULE_TIMEOUT;timer++) {
119             try {
120                 configRegistryClient.lookupConfigBean(getModuleName(), getInstanceName());
121                 Thread.sleep(1);
122             } catch (InstanceNotFoundException e) {
123                 if (timer >= MODULE_TIMEOUT) {
124                     throw e;
125                 }
126             } catch (InterruptedException e) {
127                 LOG.error("Exception: ",e);
128             }
129         }
130         Calendar stop = Calendar.getInstance();
131         LOG.info("Module: {} Instance: {} configured after {} ms",
132                 getModuleName(),getInstanceName(),
133                 stop.getTimeInMillis() - start.getTimeInMillis());
134     }
135
136     @Rule
137     public TestRule watcher = new TestWatcher() {
138         @Override
139         protected void starting(Description description) {
140             LOG.info("TestWatcher: Starting test:\n{}", description.getDisplayName());
141         }
142
143         @Override
144         protected void finished(Description description) {
145             LOG.info("TestWatcher: Finished test:\n{}", description.getDisplayName());
146         }
147
148         @Override
149         protected void succeeded(Description description) {
150             LOG.info("TestWatcher: Test succeeded:\n{}", description.getDisplayName());
151         }
152
153         @Override
154         protected void failed(Throwable ex, Description description) {
155             LOG.info("TestWatcher: Test failed:\n{} ", description.getDisplayName(), ex);
156         }
157
158         @Override
159         protected void skipped(AssumptionViolatedException ex, Description description) {
160             LOG.info("TestWatcher: Test skipped:\n{} ", description.getDisplayName(), ex);
161         }
162     };
163 }