001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Vladimir Pasquier
018 */
019package org.nuxeo.ecm.automation.test;
020
021import java.io.IOException;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.automation.client.Session;
026import org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient;
027import org.nuxeo.runtime.test.runner.FeaturesRunner;
028import org.nuxeo.runtime.test.runner.RunnerFeature;
029
030import com.google.inject.Binder;
031import com.google.inject.Scopes;
032
033/**
034 * Feature to test automation client against an Nuxeo Automation Server running in a remote JVM.
035 *
036 * @since 5.7
037 */
038public class RemoteAutomationServerFeature implements RunnerFeature {
039
040    protected static final Log log = LogFactory.getLog(RemoteAutomationServerFeature.class);
041
042    protected static final String TEST_AUTOMATION_URL = "TEST_AUTOMATION_URL";
043
044    protected HttpAutomationClient client;
045
046    protected Session session;
047
048    protected String automationUrl;
049
050    public RemoteAutomationServerFeature() {
051        automationUrl = System.getenv(TEST_AUTOMATION_URL);
052        if (automationUrl == null) {
053            automationUrl = "http://localhost:8080/nuxeo/site/automation";
054            log.info("Could not find " + TEST_AUTOMATION_URL + " environment variable: fallback to: " + automationUrl);
055        } else {
056            log.info("Testing against: " + automationUrl);
057        }
058    }
059
060    @Override
061    public void afterRun(FeaturesRunner runner) {
062        if (client != null) {
063            client.shutdown();
064            client = null;
065            session = null;
066        }
067    }
068
069    @Override
070    public void configure(FeaturesRunner runner, Binder binder) {
071        binder.bind(HttpAutomationClient.class).toProvider(() -> {
072            if (client == null) {
073                client = new HttpAutomationClient(automationUrl);
074            }
075            return client;
076        }).in(Scopes.SINGLETON);
077        binder.bind(Session.class).toProvider(() -> {
078            if (client == null) {
079                client = new HttpAutomationClient(automationUrl);
080            }
081            if (session == null) {
082                try {
083                    session = client.getSession("Administrator", "Administrator");
084                } catch (IOException e) {
085                    throw new RuntimeException(e);
086                }
087            }
088            return session;
089        }).in(Scopes.SINGLETON);
090    }
091
092}