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