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.SimpleFeature;
029
030import com.google.inject.Binder;
031import com.google.inject.Provider;
032import com.google.inject.Scopes;
033
034/**
035 * Feature to test automation client against an Nuxeo Automation Server running in a remote JVM.
036 *
037 * @since 5.7
038 */
039public class RemoteAutomationServerFeature extends SimpleFeature {
040
041    protected static final Log log = LogFactory.getLog(RemoteAutomationServerFeature.class);
042
043    protected static final String TEST_AUTOMATION_URL = "TEST_AUTOMATION_URL";
044
045    protected HttpAutomationClient client;
046
047    protected Session session;
048
049    protected String automationUrl;
050
051    public RemoteAutomationServerFeature() {
052        automationUrl = System.getenv(TEST_AUTOMATION_URL);
053        if (automationUrl == null) {
054            automationUrl = "http://localhost:8080/nuxeo/site/automation";
055            log.info("Could not find " + TEST_AUTOMATION_URL + " environment variable: fallback to: " + automationUrl);
056        } else {
057            log.info("Testing against: " + automationUrl);
058        }
059    }
060
061    @Override
062    public void afterRun(FeaturesRunner runner) throws Exception {
063        if (client != null) {
064            client.shutdown();
065            client = null;
066            session = null;
067        }
068        super.afterRun(runner);
069    }
070
071    @Override
072    public void configure(FeaturesRunner runner, Binder binder) {
073        super.configure(runner, binder);
074        binder.bind(HttpAutomationClient.class).toProvider(new Provider<HttpAutomationClient>() {
075            @Override
076            public HttpAutomationClient get() {
077                if (client == null) {
078                    client = new HttpAutomationClient(automationUrl);
079                }
080                return client;
081            }
082        }).in(Scopes.SINGLETON);
083        binder.bind(Session.class).toProvider(new Provider<Session>() {
084            @Override
085            public Session get() {
086                if (client == null) {
087                    client = new HttpAutomationClient(automationUrl);
088                }
089                if (session == null) {
090                    try {
091                        session = client.getSession("Administrator", "Administrator");
092                    } catch (IOException e) {
093                        throw new RuntimeException(e);
094                    }
095                }
096                return session;
097            }
098        }).in(Scopes.SINGLETON);
099    }
100
101}