001/*
002 * (C) Copyright 2006-2010 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.test;
020
021import java.io.IOException;
022import java.util.function.Supplier;
023
024import javax.inject.Inject;
025
026import org.nuxeo.ecm.automation.client.Session;
027import org.nuxeo.ecm.automation.client.adapters.AsyncSession;
028import org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient;
029import org.nuxeo.ecm.core.test.DetectThreadDeadlocksFeature;
030import org.nuxeo.ecm.webengine.test.WebEngineFeature;
031import org.nuxeo.runtime.test.runner.Features;
032import org.nuxeo.runtime.test.runner.FeaturesRunner;
033import org.nuxeo.runtime.test.runner.RunnerFeature;
034import org.nuxeo.runtime.test.runner.ServletContainerFeature;
035
036import com.google.inject.Binder;
037import com.google.inject.Scopes;
038
039/**
040 * Shortcut to deploy bundles required by automation in your test
041 *
042 * @since 5.7
043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
044 */
045@Features({ DetectThreadDeadlocksFeature.class, WebEngineFeature.class, AutomationServerFeature.class })
046public class EmbeddedAutomationServerFeature implements RunnerFeature {
047
048    protected static final int HTTP_CONNECTION_TIMEOUT = 60000; // 60 seconds
049
050    @Inject
051    protected ServletContainerFeature servletContainerFeature;
052
053    protected HttpAutomationClient client;
054
055    protected Session session;
056
057    @Override
058    public void afterRun(FeaturesRunner runner) {
059        if (client != null) {
060            client.shutdown();
061            client = null;
062            session = null;
063        }
064    }
065
066    @Override
067    public void configure(FeaturesRunner runner, Binder binder) {
068        binder.bind(HttpAutomationClient.class).toProvider(() -> {
069            if (client == null) {
070                client = getHttpAutomationClient();
071            }
072            return client;
073        }).in(Scopes.SINGLETON);
074        binder.bind(Session.class).toProvider(() -> {
075            if (client == null) {
076                client = getHttpAutomationClient();
077            }
078            if (session == null) {
079                try {
080                    session = client.getSession("Administrator", "Administrator");
081                } catch (IOException e) {
082                    throw new RuntimeException(e);
083                }
084            }
085            return session;
086        }).in(Scopes.SINGLETON);
087        binder.bind(AsyncSession.class).toProvider(() -> {
088            if (client == null) {
089                client = getHttpAutomationClient();
090            }
091            if (session == null) {
092                try {
093                    session = client.getSession("Administrator", "Administrator");
094                } catch (IOException e) {
095                    throw new RuntimeException(e);
096                }
097            }
098            return session.getAdapter(AsyncSession.class);
099        }).in(Scopes.SINGLETON);
100    }
101
102    protected HttpAutomationClient getHttpAutomationClient() {
103        // port must be supplied dynamically because it may change after hot-reload of services
104        Supplier<String> urlSupplier = () -> "http://localhost:" + servletContainerFeature.getPort() + "/automation/";
105        HttpAutomationClient client = new HttpAutomationClient(urlSupplier, HTTP_CONNECTION_TIMEOUT);
106        // Deactivate global operation registry cache to allow tests using this
107        // feature in a test suite to deploy different set of operations
108        client.setSharedRegistryExpirationDelay(0);
109        return client;
110    }
111
112}