org.apache.karaf.looptc.LoopTcTest.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.karaf.looptc.LoopTcTest.java

Source

package org.apache.karaf.looptc; /******************************************************************************
                                 * * This data and information is proprietary to, and a valuable trade secret
                                 * * of, Basis Technology Corp.  It is given in confidence by Basis Technology
                                 * * and may only be used as permitted under the license agreement under which
                                 * * it has been distributed, and in no other way.
                                 * *
                                 * * Copyright (c) 2015 Basis Technology Corporation All rights reserved.
                                 * *
                                 * * The technical data and information provided herein are provided with
                                 * * `limited rights', and the computer software provided herein is provided
                                 * * with `restricted rights' as those terms are defined in DAR and ASPR
                                 * * 7-104.9(a).
                                 ******************************************************************************/

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.karaf.features.BootFinished;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.karaf.options.LogLevelOption;
import org.ops4j.pax.exam.options.MavenUrlReference;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerClass;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperties;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;
import static org.ops4j.pax.exam.CoreOptions.when;
//import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.debugConfiguration;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;

/**
 * Configure the front end, no tracker, and a worker with the dummy sdk.
 *
 */
@RunWith(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class LoopTcTest {

    @Inject
    BootFinished bootFinished;

    private int workerPort;
    private int frontEndPort;
    private CloseableHttpClient httpClient;

    @Configuration
    public static Option[] paxConfiguration() {
        String basedir = System.getProperty("basedir", ".");
        String version = System.getProperty("project.version");
        String karafVersion = System.getProperty("karaf.version");
        MavenUrlReference karafStandardRepo = maven().groupId("org.apache.karaf.features").artifactId("standard")
                .version(karafVersion).classifier("features").type("xml");
        MavenUrlReference cxfRepo = maven().groupId("org.apache.cxf.karaf").artifactId("apache-cxf")
                .versionAsInProject().classifier("features").type("xml");

        return new Option[] {
                mavenBundle().groupId("org.apache.httpcomponents").artifactId("httpclient-osgi")
                        .versionAsInProject(),
                mavenBundle().groupId("org.apache.httpcomponents").artifactId("httpcore-osgi").versionAsInProject(),

                //configureConsole().ignoreLocalConsole(),
                keepRuntimeFolder(), logLevel(LogLevelOption.LogLevel.DEBUG),
                editConfigurationFilePut("etc/org.ops4j.pax.web.cfg", "org.osgi.service.http.port",
                        System.getProperty("karaf.port")),
                //editConfigurationFilePut("etc/org.ops4j.pax.url.mvn.cfg", "org.ops4j.pax.url.mvn.defaultLocalRepoAsRemote", "true"),
                editConfigurationFilePut("etc/org.ops4j.pax.logging.cfg",
                        "log4j.category.org.ops4j.pax.url.mvn.internal.AetherBasedResolver", "ERROR"),
                karafDistributionConfiguration().karafVersion(karafVersion).name("Apache Karaf")
                        .frameworkUrl(maven().groupId("org.apache.karaf").artifactId("apache-karaf").type("zip")
                                .versionAsInProject())
                        .unpackDirectory(new File(basedir, "target/pax")),
                features(karafStandardRepo, "http"), features(cxfRepo, "cxf"),
                features(
                        maven("org.apache.karaf", "tc-service").classifier("features").type("xml").version(version),
                        "tc-service"),
                when(Boolean.getBoolean("karaf.debug")).useOptions(debugConfiguration()), junitBundles(),
                systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
                systemProperty("org.ops4j.pax.exam.rbc.rmi.host").value("localhost"),
                propagateSystemProperties("test.worker.port", "test.frontend.port") };
    }

    @Before
    public void before() {
        workerPort = Integer.parseInt(System.getProperty("test.worker.port"));
        frontEndPort = Integer.parseInt(System.getProperty("test.frontend.port"));
        httpClient = HttpClients.createDefault();
        System.out.format("Ports: front end %d worker %d%n", frontEndPort, workerPort);
    }

    @After
    public void after() throws IOException {
        httpClient.close();
    }

    private String frontEndUrl(String resource) {
        return String.format("http://localhost:%d/%s", frontEndPort, resource);
    }

    @Test
    public void testFrontEndPing() throws Exception {
        String url = frontEndUrl("ping");
        HttpGet httpGet = new HttpGet(url);
        httpClient.execute(httpGet, httpResponse -> {
            assertEquals(200, httpResponse.getStatusLine().getStatusCode());
            return null;
        });
    }
}