org.eclipse.swordfish.internal.resolver.backend.base.impl.HttpClientProxyTest.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.swordfish.internal.resolver.backend.base.impl.HttpClientProxyTest.java

Source

/*******************************************************************************
 * Copyright (c) 2009 SOPERA GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     SOPERA GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.swordfish.internal.resolver.backend.base.impl;

import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import static org.junit.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.URIException;
import org.easymock.EasyMock;
import org.easymock.IArgumentMatcher;
import org.eclipse.swordfish.core.SwordfishException;
import org.eclipse.swordfish.core.resolver.backend.ClientRequest;
import org.eclipse.swordfish.core.resolver.backend.ClientResponse;
import org.eclipse.swordfish.core.resolver.backend.RegistryProxy;
import org.eclipse.swordfish.core.resolver.backend.ProxyConstants.Method;
import org.eclipse.swordfish.core.resolver.backend.ProxyConstants.Status;
import org.eclipse.swordfish.internal.resolver.backend.base.RegistryProxyFactory;
import org.eclipse.swordfish.internal.resolver.backend.base.wsdl.WSDLList;
import org.junit.Before;
import org.junit.Test;

/**
 *
 */
public class HttpClientProxyTest {

    private static final String DESCRIPTION_URL = "http://localhost:9001/some-url";

    private static final String RESPONSE_WSDL_LIST = "<wsdlList>" + "<url>sample_1.wsdl</url>"
            + "<url>sample_2.wsdl</url>" + "</wsdlList>";

    private RegistryProxy proxy;

    @Before
    public void setUp() {
        proxy = new HttpClientProxy();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testCallProxyWithNullArgument() {
        proxy.get(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testPassRequestWithWrongMethod() {
        ClientRequest request = RegistryProxyFactory.getInstance().createRequest();
        request.setMethod(Method.POST);
        proxy.get(request);
    }

    @Test
    public void testPassNonInitializedRequest() throws Exception {
        HttpClientProxy.class.cast(proxy).setClient(new HttpClientStub());
        ClientRequest request = RegistryProxyFactory.getInstance().createRequest();

        try {
            proxy.get(request);
            fail("invocation of get() method should fail.");
        } catch (IllegalArgumentException e) {
            // expected
        }

        try {
            request.setMethod(Method.GET);
            proxy.get(request);
            fail("invocation of get() method should fail.");
        } catch (IllegalArgumentException e) {
            // expected
        }

        request.setURI(new URI(DESCRIPTION_URL));
        // when all required properties are set invocation will succeed
        proxy.get(request);
    }

    @Test
    public void testRetrieveWSDLList() throws Exception {
        Map<String, String> props = new HashMap<String, String>();
        props.put("property1", "value1");
        props.put("property2", "value2");
        props.put("property3", "value3");

        ClientRequest request = RegistryProxyFactory.getInstance().createRequest();
        request.setURI(new URI(DESCRIPTION_URL));
        request.setMethod(Method.GET);
        request.setEntityType(WSDLList.class);
        request.setProperties(props);

        HttpMethod httpMethodMock = createNiceMock(HttpMethod.class);

        HttpClientProxyStub proxyStub = new HttpClientProxyStub();
        proxyStub.setClient(new HttpClientStub());
        proxyStub.setMethod(httpMethodMock);

        httpMethodMock.setURI(uriMatches(DESCRIPTION_URL, convertToQuery(props)));
        expectLastCall().atLeastOnce();
        httpMethodMock.getResponseBodyAsString();
        expectLastCall().andReturn(RESPONSE_WSDL_LIST).once();
        replay(httpMethodMock);

        ClientResponse response = proxyStub.get(request);
        assertNotNull("ClientResponse must not be null.", response);
        assertNotNull("Response status must be set.", response.getStatus());
        assertEquals(Status.SUCCESS, response.getStatus());

        assertNotNull("Response object must be set.", response.getEntity());
        assertEquals(WSDLList.class, response.getEntity().getClass());
        List<String> wsdls = WSDLList.class.cast(response.getEntity()).getUrl();

        assertNotNull(wsdls);
        assertEquals(2, wsdls.size());

        verify(httpMethodMock);
    }

    @Test
    public void testRetrieveWsdl() throws Exception {
        ClientRequest request = RegistryProxyFactory.getInstance().createRequest();
        request.setURI(new URI(DESCRIPTION_URL));

        HttpMethod httpMethodMock = createNiceMock(HttpMethod.class);

        HttpClientProxyStub proxyStub = new HttpClientProxyStub();
        proxyStub.setClient(new HttpClientStub());
        proxyStub.setMethod(httpMethodMock);

        httpMethodMock.getResponseBodyAsString();
        expectLastCall().andReturn("wsdl contents").once();
        replay(httpMethodMock);

        ClientResponse response = proxyStub.get(request);
        assertNotNull("ClientResponse must not be null.", response);
        assertNotNull("Response status must be set.", response.getStatus());
        assertEquals(Status.SUCCESS, response.getStatus());

        assertNotNull("Response object must be set.", response.getEntity());
        assertEquals("wsdl contents", response.getEntity());

        verify(httpMethodMock);
    }

    @Test
    public void testWSDLRetrievalFails() throws Exception {
        ClientRequest request = RegistryProxyFactory.getInstance().createRequest();
        request.setURI(new URI(DESCRIPTION_URL));

        HttpClientStub clientStub = new HttpClientStub();
        HttpClientProxy.class.cast(proxy).setClient(clientStub);

        clientStub.setExceptionClass(IOException.class);
        ClientResponse response = proxy.get(request);

        assertNotNull("ClientResponse must not be null.", response);
        assertNotNull("Response status must be set.", response.getStatus());
        assertEquals(Status.ERROR, response.getStatus());
        assertNotNull("Response object must be set.", response.getEntity());
        assertEquals(IOException.class, response.getEntity().getClass());

        clientStub.setExceptionClass(HttpException.class);
        response = proxy.get(request);

        assertNotNull("ClientResponse must not be null.", response);
        assertNotNull("Response status must be set.", response.getStatus());
        assertEquals(Status.ERROR, response.getStatus());
        assertNotNull("Response object must be set.", response.getEntity());
        assertEquals(HttpException.class, response.getEntity().getClass());
    }

    @Test
    public void testRetrievalFailsProxyThrewException() throws Exception {
        ClientRequest request = RegistryProxyFactory.getInstance().createRequest();
        request.setURI(new URI(DESCRIPTION_URL));

        HttpMethod httpMethodMock = createNiceMock(HttpMethod.class);

        HttpClientProxyStub proxyStub = new HttpClientProxyStub();
        proxyStub.setClient(new HttpClientStub());
        proxyStub.setMethod(httpMethodMock);

        httpMethodMock.getResponseBodyAsString();
        expectLastCall().andReturn("registry contents").once();
        replay(httpMethodMock);

        try {
            // setting any type other than WSDLList will lead to
            // unmarshalling failure and exception will be thrown
            request.setEntityType(String.class);
            proxyStub.get(request);
        } catch (SwordfishException e) {
            // expected
        }
        verify(httpMethodMock);
    }

    private String convertToQuery(Map<String, String> properties) throws Exception {
        HttpURL url = new HttpURL(DESCRIPTION_URL);
        String[] queryNames = properties.keySet().toArray(new String[0]);
        String[] queryValues = properties.values().toArray(new String[0]);
        url.setQuery(queryNames, queryValues);
        return url.getQuery();
    }

    private org.apache.commons.httpclient.URI uriMatches(String url, String query) {
        EasyMock.reportMatcher(new HttpClientURIMatcher(url, query));
        return null;
    }

    private class HttpClientURIMatcher implements IArgumentMatcher {

        private String url;
        private String query;
        private org.apache.commons.httpclient.URI actualUri;

        public HttpClientURIMatcher(String url, String query) {
            this.url = url;
            this.query = query;
        }

        public boolean matches(Object actual) {
            boolean matches = false;
            if (!(actual instanceof org.apache.commons.httpclient.URI)) {
                return false;
            }
            actualUri = org.apache.commons.httpclient.URI.class.cast(actual);

            try {
                matches = url.indexOf(actualUri.getScheme()) != -1 && url.indexOf(actualUri.getHost()) != -1
                        && url.indexOf(Integer.toString(actualUri.getPort())) != -1
                        && url.indexOf(actualUri.getPath()) != -1 && query.equals(actualUri.getQuery());
            } catch (URIException e) {
                throw new IllegalStateException(e);
            }
            return matches;
        }

        public void appendTo(StringBuffer buffer) {
            buffer.append("\nURL: ").append(url);
            buffer.append("\nQuery: ").append(query);
            buffer.append("\nhas been expected, but was");
            buffer.append("\n");
            buffer.append(actualUri);
            buffer.append("\n");
        }
    }

    private class HttpClientStub extends HttpClient {

        private Class<? extends Exception> exceptionClass;

        @Override
        public int executeMethod(HttpMethod method) throws IOException, HttpException {
            if (exceptionClass != null) {
                if (exceptionClass.equals(IOException.class)) {
                    throw new IOException();
                }
                if (exceptionClass.equals(HttpException.class)) {
                    throw new HttpException();
                }
            }
            return 200;
        }

        public void setExceptionClass(Class<? extends Exception> exceptionClass) {
            this.exceptionClass = exceptionClass;
        }
    }

    private class HttpClientProxyStub extends HttpClientProxy {

        private HttpMethod httpMethod;

        @Override
        protected HttpMethod getMethod(Method method) {
            return httpMethod;
        }

        public void setMethod(HttpMethod method) {
            this.httpMethod = method;
        }
    }

}