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