it.txt.ens.authorisationService.test.IntegrationTest.java Source code

Java tutorial

Introduction

Here is the source code for it.txt.ens.authorisationService.test.IntegrationTest.java

Source

/***************************************************************************
 * Copyright 2012-2013 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the contract ICT-257367.
 *
 * Authors:
 *      Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *        Domenico Rotondi (TXT e-solutions SpA)
 **************************************************************************/
package it.txt.ens.authorisationService.test;

import static org.junit.Assert.assertNotNull;
import static org.ops4j.pax.exam.CoreOptions.cleanCaches;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;
import static org.ops4j.pax.exam.CoreOptions.systemPackage;
import it.txt.ens.authorisationService.osgi.ENSAuthorisationServiceMS;
import it.txt.ens.authorisationService.util.PropertiesKeys;

import java.io.File;
import java.io.FileInputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Properties;

import javax.inject.Inject;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.CoreOptions;
import org.ops4j.pax.exam.ExamSystem;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.TestContainer;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.junit.PaxExamServer;
import org.ops4j.pax.exam.spi.PaxExamRuntime;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerClass;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;

/**
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com)
 * @author Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com)
 *
 */
@RunWith(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class IntegrationTest {
    private static final String CONFIG_FILE_PROPERTY = "ens.test.config.path";
    private static final String TINKERPOP_STACK_VERSION = "2.2.0";
    private static final String ORIENT_VERSION = "1.3.0";
    @Inject
    private BundleContext context;
    @Inject
    private TestContainer exam;

    @org.ops4j.pax.exam.Configuration
    public Option[] config() {
        return options(mavenBundle("org.osgi", "org.osgi.compendium", "4.3.1"),
                mavenBundle("org.apache.felix", "org.apache.felix.configadmin", "1.6.0"),
                mavenBundle("javax.xml.stream", "com.springsource.javax.xml.stream", "1.0.1"),
                mavenBundle("org.apache.xerces", "com.springsource.org.apache.xerces", "2.9.1").noStart(),
                mavenBundle("org.apache.xmlcommons", "com.springsource.org.apache.xmlcommons", "1.3.4"),
                mavenBundle("com.sun.xml", "com.springsource.com.sun.xml.fastinfoset", "1.2.2"),
                mavenBundle("javax.activation", "com.springsource.javax.activation", "1.1.1"),
                mavenBundle("javax.xml.bind", "com.springsource.javax.xml.bind", "2.2.0"),
                mavenBundle("org.jvnet.staxex", "com.springsource.org.jvnet.staxex", "1.0.0"),
                mavenBundle("org.apache.xml", "com.springsource.org.apache.xml.resolver", "1.2.0"),
                mavenBundle("javax.xml.crypto", "com.springsource.javax.xml.crypto", "1.4.3"),
                mavenBundle("org.apache.xalan", "com.springsource.org.apache.xalan", "2.7.1"),
                mavenBundle("org.apache.xalan", "com.springsource.org.apache.xml.serializer", "2.7.1"),
                mavenBundle("org.bouncycastle", "com.springsource.org.bouncycastle.jce", "1.39.0"),
                mavenBundle("com.sun.xml", "com.springsource.com.sun.xml.bind", "2.2.0"),
                mavenBundle("org.apache.commons", "com.springsource.org.apache.commons.lang", "2.6.0"),
                mavenBundle("org.apache.commons", "com.springsource.org.apache.commons.io", "1.4.0"),
                mavenBundle("com.rabbitmq", "amqp-client", "3.0.2"),
                mavenBundle("org.apache.commons", "com.springsource.org.apache.commons.logging", "1.1.1"),
                mavenBundle("org.apache.commons", "com.springsource.org.apache.commons.codec", "1.6.0"),
                mavenBundle("org.apache.httpcomponents", "com.springsource.org.apache.httpcomponents.httpcore",
                        "4.2.1"),
                mavenBundle("org.apache.httpcomponents", "com.springsource.org.apache.httpcomponents.httpclient",
                        "4.2.1"),
                mavenBundle("org.springframework", "org.springframework.beans", "3.0.7.RELEASE"),
                mavenBundle("org.springframework", "org.springframework.core", "3.0.7.RELEASE"),
                mavenBundle("org.springframework.ldap", "org.springframework.ldap", "1.3.1.RELEASE"),
                mavenBundle("org.springframework", "org.springframework.transaction", "3.0.7.RELEASE"),
                //CapBAC bundles
                mavenBundle("org.apache.servicemix.bundles", "org.apache.servicemix.bundles.saxon", "9.3.0.11_1"),
                mavenBundle("it.txt.access.capability", "capability-commons", "3.0.0"),
                mavenBundle("it.txt.access.capability.schema", "capability-schema-commons", "1.0.0"),
                mavenBundle("it.txt.access.capability.schema", "capability-schema-creator", "1.0.1"),
                mavenBundle("it.txt.access.capability.schema", "capability-schema-verifier", "1.0.1"),
                mavenBundle("it.txt.access.capability.schema", "capability-schema-library", "1.0.1"),
                mavenBundle("it.txt.access.capability.libraries", "capability-libs-finder", "1.1.0"),
                //namespace bundles
                mavenBundle("javax.ws.rs", "com.springsource.javax.ws.rs", "1.0.0"),
                mavenBundle("edu.oswego.cs.concurrent", "com.springsource.edu.oswego.cs.dl.util.concurrent",
                        "1.3.4"),
                mavenBundle("org.apache.servicemix.bundles", "org.apache.servicemix.bundles.colt", "1.2.0_1"),
                mavenBundle("com.tinkerpop.osgi", "pipes", TINKERPOP_STACK_VERSION),
                mavenBundle("com.tinkerpop.blueprints.osgi", "blueprints-core", TINKERPOP_STACK_VERSION),
                mavenBundle("com.tinkerpop.blueprints.osgi", "blueprints-orient-graph", TINKERPOP_STACK_VERSION),
                mavenBundle("com.tinkerpop.gremlin.osgi", "gremlin-java", TINKERPOP_STACK_VERSION),
                mavenBundle("org.codehaus.jettison", "jettison", "1.3"),
                mavenBundle("org.codehaus.jackson", "jackson-core-asl", "1.8.5"),
                mavenBundle("org.codehaus.jackson", "jackson-mapper-asl", "1.8.5"),
                mavenBundle("org.codehaus.jackson", "jackson-jaxrs", "1.8.5"),
                mavenBundle("com.orientechnologies", "orientdb-core", ORIENT_VERSION),
                mavenBundle("com.orientechnologies", "orient-commons", ORIENT_VERSION).noStart(),
                mavenBundle("com.orientechnologies", "orientdb-enterprise", ORIENT_VERSION),
                mavenBundle("com.orientechnologies", "orientdb-client", ORIENT_VERSION).noStart(),
                mavenBundle("it.txt.ens.namespace", "ens-namespace-inquirer", "1.0.0"),
                //PDP Client
                mavenBundle("it.txt.access.capability.pdp", "capability-pdp-client", "1.0.0"),
                //RabbitMQ Management Client
                mavenBundle("com.google.code.gson", "gson", "2.2.2"),
                mavenBundle("org.restlet.osgi", "org.restlet", "2.1.1"),
                mavenBundle("org.restlet.osgi", "org.restlet.ext.json", "2.1.1"),
                //alternative: org.restlet.lib.org.json-2.0
                mavenBundle("org.json", "com.springsource.org.json", "1.0.0"),
                mavenBundle("it.txt.rabbitmq.management.rest", "rabbitmq-management-rest-client", "1.0.0"),
                //ENS bundles
                mavenBundle("it.txt.ens", "ens-schema", "2.0.0"), mavenBundle("it.txt.ens", "ens-core", "1.1.0"),
                mavenBundle("it.txt.ens", "ens-core-util", "1.0.1"),
                mavenBundle("it.txt.ens", "ens-core-impl", "1.0.2"),
                mavenBundle("it.txt.ens.server", "ens-authz-service", "1.0.0"),
                //TODO COMMENT WHEN YOU RELEASE THE MODULE
                systemProperty(CONFIG_FILE_PROPERTY).value(
                        "D:\\IoT@Work_DEV\\ENS\\ENS-Authorisation-Service\\src\\test\\resources\\ens-authz-service.properties"),
                //add publisher and subscriber test bundle
                systemPackage("com.sun.jndi.ldap"), systemProperty("pax.exam.osgi.unresolved.fail").value("false"),
                junitBundles(), cleanCaches(false));
    }

    @Test
    public void test1() {
        assertNotNull(exam);
    }

    @Test
    public void test2() throws Exception {

        //configure and start the ENS
        ServiceReference<ConfigurationAdmin> configAdminSR = context.getServiceReference(ConfigurationAdmin.class);
        assertNotNull("No service reference found for " + ConfigurationAdmin.class.getName(), configAdminSR);
        ConfigurationAdmin configAdmin = context.getService(configAdminSR);
        assertNotNull("No instance found for " + ConfigurationAdmin.class.getName(), configAdminSR);

        File file = new File(System.getProperty(CONFIG_FILE_PROPERTY));
        FileInputStream stream = new FileInputStream(file);
        Properties p = new Properties();
        try {
            p.load(stream);
        } finally {
            if (stream != null)
                stream.close();
        }
        Dictionary<String, Object> prop = new Hashtable<String, Object>(p.size());

        for (Object key : p.keySet()) {
            if (key.equals(PropertiesKeys.CONFIGURATION_FILE_PATH_ATTRIBUTE))
                prop.put((String) key, p.getProperty((String) key));
            else if (key.equals(PropertiesKeys.ENS_GUI_ATTRIBUTE))
                prop.put((String) key, Boolean.parseBoolean(p.getProperty((String) key)));
            else if (key.equals(PropertiesKeys.THREAD_POOL_SIZE_ATTRIBUTE))
                prop.put((String) key, Integer.parseInt(p.getProperty((String) key)));
            else
                continue;
        }
        Configuration config = configAdmin.getConfiguration(ENSAuthorisationServiceMS.SERVICE_PID);
        config.update(prop);

        Thread.sleep(60000);

        //delete the configuration
        config.delete();
    }

}