Stop using deprecated pax Configuration annotation. Move the code to use
[controller.git] / opendaylight / configuration / integrationtest / src / test / java / org / opendaylight / controller / configuration / internal / ConfigurationIT.java
1 package org.opendaylight.controller.configuration.internal;
2
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
6 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
7 import static org.ops4j.pax.exam.CoreOptions.options;
8 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
9 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
10
11 import java.io.File;
12
13 import javax.inject.Inject;
14
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.opendaylight.controller.configuration.IConfigurationService;
20 import org.opendaylight.controller.sal.utils.GlobalConstants;
21 import org.opendaylight.controller.sal.utils.Status;
22 import org.opendaylight.controller.sal.utils.StatusCode;
23 import org.ops4j.pax.exam.Option;
24 import org.ops4j.pax.exam.Configuration;
25 import org.ops4j.pax.exam.junit.PaxExam;
26 import org.ops4j.pax.exam.util.PathUtils;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceReference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @RunWith(PaxExam.class)
34 public class ConfigurationIT {
35     private final Logger logger = LoggerFactory
36             .getLogger(ConfigurationIT.class);
37     // get the OSGI bundle context
38     @Inject
39     private BundleContext bc;
40     private IConfigurationService configService = null;
41
42     // Configure the OSGi container
43     @Configuration
44     public Option[] config() {
45         return options(
46                 systemProperty("logback.configurationFile").value(
47                         "file:" + PathUtils.getBaseDir()
48                                 + "/src/test/resources/logback.xml"),
49                 // To start OSGi console for inspection remotely
50                 systemProperty("osgi.console").value("2401"),
51                 // Set the systemPackages (used by clustering)
52                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
53                 // List framework bundles
54                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console").versionAsInProject(),
55                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util").versionAsInProject(),
56                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services").versionAsInProject(),
57                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds").versionAsInProject(),
58                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command").versionAsInProject(),
59                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime").versionAsInProject(),
60                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell").versionAsInProject(),
61                 // List logger bundles
62                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
63                 mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),
64                 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),
65                 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(),
66                 mavenBundle("org.apache.commons", "commons-lang3").versionAsInProject(),
67                 mavenBundle("org.jboss.spec.javax.transaction", "jboss-transaction-api_1.1_spec").versionAsInProject(),
68                 mavenBundle("eclipselink", "javax.resource").versionAsInProject(),
69                 mavenBundle("org.apache.felix", "org.apache.felix.dependencymanager").versionAsInProject(),
70                 // List all the bundles on which the test case depends
71                 mavenBundle("org.opendaylight.controller", "sal").versionAsInProject(),
72                 mavenBundle("org.opendaylight.controller", "sal.implementation").versionAsInProject(),
73                 mavenBundle("org.opendaylight.controller", "protocol_plugins.stub").versionAsInProject(),
74                 // needed bundles by switchmanager
75                 mavenBundle("org.opendaylight.controller", "containermanager").versionAsInProject(),
76                 mavenBundle("org.opendaylight.controller", "containermanager.it.implementation").versionAsInProject(),
77                 // needed bundles by configuration
78                 mavenBundle("org.opendaylight.controller", "clustering.services").versionAsInProject(),
79                 mavenBundle("org.opendaylight.controller", "clustering.stub").versionAsInProject(),
80                 mavenBundle("org.opendaylight.controller", "configuration").versionAsInProject(),
81                 mavenBundle("org.opendaylight.controller", "configuration.implementation").versionAsInProject(),
82                 junitBundles());
83     }
84
85     private String stateToString(int state) {
86         switch (state) {
87         case Bundle.ACTIVE:
88             return "ACTIVE";
89         case Bundle.INSTALLED:
90             return "INSTALLED";
91         case Bundle.RESOLVED:
92             return "RESOLVED";
93         case Bundle.UNINSTALLED:
94             return "UNINSTALLED";
95         default:
96             return "Not CONVERTED";
97         }
98     }
99
100     @Before
101     public void areWeReadyForClustering() {
102         assertNotNull(bc);
103         boolean debugit = false;
104         Bundle b[] = bc.getBundles();
105         for (int i = 0; i < b.length; i++) {
106             int state = b[i].getState();
107             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
108                 logger.debug("Bundle:" + b[i].getSymbolicName() + " state:"
109                         + stateToString(state));
110                 debugit = true;
111             }
112         }
113         if (debugit) {
114             logger.debug("Do some debugging because some bundle is unresolved");
115         }
116
117         // Assert if true, if false we are good to go!
118         assertFalse(debugit);
119     }
120
121     @Before
122     public void areWeReadyForConfiguration() {
123         assertNotNull(bc);
124         boolean debugit = false;
125         Bundle b[] = bc.getBundles();
126         for (int i = 0; i < b.length; i++) {
127             int state = b[i].getState();
128             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
129                 logger.debug("Bundle:" + b[i].getSymbolicName() + " state:"
130                         + stateToString(state));
131                 debugit = true;
132             }
133         }
134         if (debugit) {
135             logger.debug("Do some debugging because some bundle is unresolved");
136         }
137
138         // Assert if true, if false we are good to go!
139         assertFalse(debugit);
140         ServiceReference r = bc.getServiceReference(IConfigurationService.class
141                 .getName());
142         if (r != null) {
143             this.configService = (IConfigurationService) bc.getService(r);
144         }
145         // If StatisticsManager is null, cannot run tests.
146         assertNotNull(this.configService);
147
148     }
149
150     @Test
151     public void saveConfiguration() {
152         File f = new File(GlobalConstants.STARTUPHOME.toString());
153         boolean success = f.exists();
154         if (!success) {
155             success = f.mkdirs();
156         }
157         if(success){
158             Status status = new Status(StatusCode.SUCCESS, null);
159             if (this.configService != null) {
160                 status = this.configService.saveConfigurations();
161             }
162             if (status.getCode().equals(StatusCode.SUCCESS)) {
163                 Assert.assertTrue("Successfully saved config for "
164                         + configService.getClass().getSimpleName(), status
165                         .getCode().equals(StatusCode.SUCCESS));
166             } else {
167                 Assert.assertFalse("Failed to save config for "
168                         + configService.getClass().getSimpleName(), status
169                         .getCode().equals(StatusCode.INTERNALERROR));
170             }
171         }
172     }
173
174 }