001/*
002 * (C) Copyright 2013 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Vladimir Pasquier
016 */
017package org.nuxeo.ecm.automation.test;
018
019import java.io.IOException;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.automation.client.Session;
024import org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient;
025import org.nuxeo.runtime.test.runner.FeaturesRunner;
026import org.nuxeo.runtime.test.runner.SimpleFeature;
027
028import com.google.inject.Binder;
029import com.google.inject.Provider;
030import com.google.inject.Scopes;
031
032/**
033 * Feature to test automation client against an Nuxeo Automation Server running in a remote JVM.
034 *
035 * @since 5.7
036 */
037public class RemoteAutomationServerFeature extends SimpleFeature {
038
039    protected static final Log log = LogFactory.getLog(RemoteAutomationServerFeature.class);
040
041    protected static final String TEST_AUTOMATION_URL = "TEST_AUTOMATION_URL";
042
043    protected HttpAutomationClient client;
044
045    protected Session session;
046
047    protected String automationUrl;
048
049    public RemoteAutomationServerFeature() {
050        automationUrl = System.getenv(TEST_AUTOMATION_URL);
051        if (automationUrl == null) {
052            automationUrl = "http://localhost:8080/nuxeo/site/automation";
053            log.info("Could not find " + TEST_AUTOMATION_URL + " environment variable: fallback to: " + automationUrl);
054        } else {
055            log.info("Testing against: " + automationUrl);
056        }
057    }
058
059    @Override
060    public void afterRun(FeaturesRunner runner) throws Exception {
061        if (client != null) {
062            client.shutdown();
063            client = null;
064            session = null;
065        }
066        super.afterRun(runner);
067    }
068
069    @Override
070    public void configure(FeaturesRunner runner, Binder binder) {
071        super.configure(runner, binder);
072        binder.bind(HttpAutomationClient.class).toProvider(new Provider<HttpAutomationClient>() {
073            @Override
074            public HttpAutomationClient get() {
075                if (client == null) {
076                    client = new HttpAutomationClient(automationUrl);
077                }
078                return client;
079            }
080        }).in(Scopes.SINGLETON);
081        binder.bind(Session.class).toProvider(new Provider<Session>() {
082            @Override
083            public Session get() {
084                if (client == null) {
085                    client = new HttpAutomationClient(automationUrl);
086                }
087                if (session == null) {
088                    try {
089                        session = client.getSession("Administrator", "Administrator");
090                    } catch (IOException e) {
091                        throw new RuntimeException(e);
092                    }
093                }
094                return session;
095            }
096        }).in(Scopes.SINGLETON);
097    }
098
099}