Java tutorial
/*************************************************************************** * Copyright 2012 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 research area ICT-2009.1.3 * Internet of Things and enterprise environments. * * Authors: * Salvatore Piccione (TXT e-solutions SpA) * * Contributors: * Domenico Rotondi (TXT e-solutions SpA) **************************************************************************/ package it.txt.ens.core.impl.osgi.test; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.ops4j.pax.exam.CoreOptions.junitBundles; import static org.ops4j.pax.exam.CoreOptions.mavenBundle; import static org.ops4j.pax.exam.OptionUtils.expand; import it.txt.ens.core.ENSAuthzServiceConnectionParameters; import it.txt.ens.core.ENSResource; import it.txt.ens.core.KeystoreParameters; import it.txt.ens.core.X509CertificateRetrievalParameters; import it.txt.ens.core.factory.ENSAuthzServiceConnectionParametersFactory; import it.txt.ens.core.factory.ENSResourceFactory; import it.txt.ens.core.factory.KeystoreParametersFactory; import it.txt.ens.core.factory.URIBuildingException; import it.txt.ens.core.factory.X509CertificateRetrievalParametersFactory; import it.txt.ens.core.impl.BasicENSAuthzServiceConnectionParameters; import it.txt.ens.core.impl.BasicENSAuthzServiceConnectionParameters.BasicENSConnectionParametersFactory; import it.txt.ens.core.impl.BasicENSResource; import it.txt.ens.core.impl.BasicENSResource.BasicENSResourceFactory; import it.txt.ens.core.impl.BasicKeystoreParameters; import it.txt.ens.core.impl.BasicKeystoreParameters.BasicKeystoreParametersFactory; import it.txt.ens.core.impl.BasicX509CertificateRetrievalParameters; import it.txt.ens.core.impl.BasicX509CertificateRetrievalParameters.BasicX509CertificateRetrievalParametersFactory; import it.txt.ens.core.impl.osgi.BasicENSConnectionParametersFactorySF; import it.txt.ens.core.impl.osgi.BasicENSResourceFactorySF; import it.txt.ens.core.impl.osgi.BasicKeystoreParametersFactorySF; import it.txt.ens.core.impl.osgi.BasicX509CertificateRetrievalParametersFactorySF; import it.txt.ens.core.osgi.FilterAttributes; import java.io.File; import javax.inject.Inject; import org.junit.Test; import org.junit.runner.RunWith; import org.ops4j.pax.exam.Option; import org.ops4j.pax.exam.junit.Configuration; import org.ops4j.pax.exam.junit.ExamReactorStrategy; import org.ops4j.pax.exam.junit.JUnit4TestRunner; import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory; import org.ops4j.pax.exam.util.Filter; import org.osgi.framework.BundleContext; /** * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com) * @contributor Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com) * */ @RunWith(JUnit4TestRunner.class) @ExamReactorStrategy(AllConfinedStagedReactorFactory.class) public class BasicENSResourceOsgiTest { @Inject private BundleContext context; @Inject @Filter("(" + FilterAttributes.ENS_RESOURCE_IMPLEMENTATION_KEY + "=" + BasicENSResourceFactorySF.IMPLEMENTATION_ID + ")") private ENSResourceFactory resourcefactory; @Inject @Filter("(" + FilterAttributes.ENS_AUTHZ_SERVICE_CONN_PARAMS_IMPLEMENTATION_KEY + "=" + BasicENSConnectionParametersFactorySF.IMPLEMENTATION_ID + ")") private ENSAuthzServiceConnectionParametersFactory connParamsFactory; @Inject @Filter("(" + FilterAttributes.KEYSTORE_PARAMETERS_FACTORY_IMPLEMENTATION_KEY + "=" + BasicKeystoreParametersFactorySF.IMPLEMENTATION_ID + ")") private KeystoreParametersFactory keystoreParamsFactory; @Inject @Filter("(" + FilterAttributes.X509CER_RETRIEVAL_PARAMS_FACTORY_IMPLEMENTATION_KEY + "=" + BasicX509CertificateRetrievalParametersFactorySF.IMPLEMENTATION_ID + ")") private X509CertificateRetrievalParametersFactory x509ParamsFactory; @Configuration public static Option[] configure() { return expand( // this does't work in newer Pax release because the Pax Runner is deprecated // frameworks( // felix(), equinox(), knopflerfish()), mavenBundle("it.txt.ens", "ens-core", "1.0.0"), mavenBundle("it.txt.ens", "ens-core-impl", "1.0.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.apache.commons", "com.springsource.org.apache.commons.codec", "1.6.0"), junitBundles() ); } @Test public void testBasicENSResourceFactoryRetrivalAndUsage() { System.out.println("Testing retrieval and usage of an instance of the factory " + BasicENSResourceFactory.class.getName()); assertNotNull(resourcefactory); try { ENSResource r = resourcefactory.create("myHost", "myNamespace", "my.pattern"); assertNotNull("The resource has not been created."); assertTrue( "The factory is not an instance of " + BasicENSResourceFactory.class.getName() + ". Actual class: " + resourcefactory.getClass().getName(), resourcefactory instanceof BasicENSResourceFactory); assertTrue("The resource is not an instance of " + BasicENSResource.class.getName() + ". Actual class: " + r.getClass().getName(), r instanceof BasicENSResource); System.out.println("TEST SUCCEEDED"); } catch (IllegalArgumentException e) { e.printStackTrace(); fail(e.getMessage()); } catch (URIBuildingException e) { e.printStackTrace(); fail(e.getMessage()); } System.out.println(); } @Test public void testBasicENSConnectionParametersFactoryRetrievalAndUsage() { System.out.println("Testing retrieval and usage of an instance of the factory " + BasicENSConnectionParametersFactory.class.getName()); assertNotNull(connParamsFactory); ENSAuthzServiceConnectionParameters p = connParamsFactory.create("mySubjectID", "myAccessToken", "myDestinationName"); assertNotNull("The connection parameters container has not been created", p); assertTrue( "The factory is not an instance of " + BasicENSConnectionParametersFactory.class.getName() + ". Actual class: " + connParamsFactory.getClass().getName(), connParamsFactory instanceof BasicENSConnectionParametersFactory); assertTrue("The connection parameters container is not an instance of " + BasicENSAuthzServiceConnectionParameters.class.getName() + ". Actual class: " + p.getClass().getName(), p instanceof BasicENSAuthzServiceConnectionParameters); System.out.println("TEST SUCEEDED"); System.out.println(); } @Test public void testBasicKeystoreParametersFactoryRetrievalAndUsage() { System.out.println("Testing retrieval and usage of an instance of the factory " + BasicKeystoreParametersFactory.class.getName()); assertNotNull(keystoreParamsFactory); KeystoreParameters p = keystoreParamsFactory.create(new File("D:/"), "myPassword".toCharArray()); assertNotNull("The keystore parameters container has not been created", p); assertTrue( "The factory is not an instance of " + BasicKeystoreParametersFactory.class.getName() + ". Actual class: " + keystoreParamsFactory.getClass().getName(), keystoreParamsFactory instanceof BasicKeystoreParametersFactory); assertTrue("The keystore parameters container is not an instance of " + BasicKeystoreParameters.class.getName() + ". Actual class: " + p.getClass().getName(), p instanceof BasicKeystoreParameters); System.out.println("TEST SUCEEDED"); System.out.println(); } @Test public void testBasicX509CertificateRetrievalParametersFactoryRetrievalAndUsage() { System.out.println("Testing retrieval and usage of an instance of the factory " + BasicX509CertificateRetrievalParametersFactory.class.getName()); assertNotNull(x509ParamsFactory); X509CertificateRetrievalParameters p = x509ParamsFactory.create("myCertificateSubject", "myPrivateKeyPassword".toCharArray()); assertNotNull("The X.509 certificate retrieval parameters container has not been created", p); assertTrue( "The factory is not an instance of " + BasicX509CertificateRetrievalParametersFactory.class.getName() + ". Actual class: " + x509ParamsFactory.getClass().getName(), x509ParamsFactory instanceof BasicX509CertificateRetrievalParametersFactory); assertTrue("The X.509 certificate parameters parameters container is not an instance of " + BasicX509CertificateRetrievalParameters.class.getName() + ". Actual class: " + p.getClass().getName(), p instanceof BasicX509CertificateRetrievalParameters); System.out.println("TEST SUCEEDED"); System.out.println(); } }