You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2021/06/12 02:32:52 UTC

[cxf] 01/02: Adding test cases for HTTPConduit handlin 503/404 w/o IO exceptions (#814)

This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch 3.4.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 477de799ea90cb619f7e2c268f838a05c95c8e9b
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Fri Jun 11 18:01:04 2021 -0400

    Adding test cases for HTTPConduit handlin 503/404 w/o IO exceptions (#814)
    
    (cherry picked from commit 136925ad0d2092ae00f2840573abf516decc3802)
---
 .../apache/cxf/systest/http/BadGreeterImpl.java    |  62 +++++++++
 .../org/apache/cxf/systest/http/BadServer.java     |  59 +++++++++
 .../systest/http/HTTPConduitIoExceptionsTest.java  | 147 +++++++++++++++++++++
 3 files changed, 268 insertions(+)

diff --git a/systests/transports/src/test/java/org/apache/cxf/systest/http/BadGreeterImpl.java b/systests/transports/src/test/java/org/apache/cxf/systest/http/BadGreeterImpl.java
new file mode 100644
index 0000000..ef9bd1d
--- /dev/null
+++ b/systests/transports/src/test/java/org/apache/cxf/systest/http/BadGreeterImpl.java
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.http;
+
+import java.util.logging.Logger;
+
+import javax.jws.WebService;
+
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.hello_world.Greeter;
+
+
+@WebService(serviceName = "SOAPService",
+            endpointInterface = "org.apache.hello_world.Greeter",
+            targetNamespace = "http://apache.org/hello_world")
+public class BadGreeterImpl implements Greeter {
+
+    private static final Logger LOG =
+        LogUtils.getLogger(BadGreeterImpl.class,
+                           null,
+                           BadGreeterImpl.class.getPackage().getName());
+
+    public BadGreeterImpl() {
+    }
+
+    public String greetMe(String me) {
+        LOG.info("Executing operation greetMe");
+        return failWith(404, "Not found: " + me);
+    }
+
+    public String sayHi() {
+        LOG.info("Executing operation sayHi");
+        return failWith(503, "Go away");
+    }
+
+    public void pingMe() {
+    }
+    
+    private String failWith(int status, String message) {
+        final Fault f = new Fault(new RuntimeException(message));
+        f.setStatusCode(status);
+        throw f;
+    }
+}
diff --git a/systests/transports/src/test/java/org/apache/cxf/systest/http/BadServer.java b/systests/transports/src/test/java/org/apache/cxf/systest/http/BadServer.java
new file mode 100644
index 0000000..0212c1b
--- /dev/null
+++ b/systests/transports/src/test/java/org/apache/cxf/systest/http/BadServer.java
@@ -0,0 +1,59 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.http;
+
+import javax.xml.ws.Endpoint;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.bus.spring.SpringBusFactory;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+
+public class BadServer extends AbstractBusTestServerBase {
+    public static final String PORT = allocatePort(BadServer.class);
+
+    Endpoint ep;
+
+    @Override
+    protected void run() {
+        Bus bus = new SpringBusFactory().createBus();
+        BusFactory.setDefaultBus(bus);
+        Object implementor = new BadGreeterImpl();
+        String address = "http://localhost:" + PORT + "/Mortimer";
+        ep = Endpoint.publish(address, implementor);
+    }
+    @Override
+    public void tearDown() {
+        ep.stop();
+        ep = null;
+    }
+
+    public static void main(String[] args) {
+        try {
+            // System.out.println("!!!!start");
+            BadServer s = new BadServer();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        }
+    }
+
+}
diff --git a/systests/transports/src/test/java/org/apache/cxf/systest/http/HTTPConduitIoExceptionsTest.java b/systests/transports/src/test/java/org/apache/cxf/systest/http/HTTPConduitIoExceptionsTest.java
new file mode 100644
index 0000000..da12052
--- /dev/null
+++ b/systests/transports/src/test/java/org/apache/cxf/systest/http/HTTPConduitIoExceptionsTest.java
@@ -0,0 +1,147 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.http;
+
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.xml.namespace.QName;
+import javax.xml.ws.WebServiceException;
+import javax.xml.ws.soap.SOAPFaultException;
+
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.cxf.transport.http.HTTPConduit;
+import org.apache.cxf.transport.http.HTTPException;
+import org.apache.hello_world.Greeter;
+import org.apache.hello_world.services.SOAPService;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
+
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class HTTPConduitIoExceptionsTest extends AbstractBusClientServerTestBase {
+    @Rule public final ExpectedException exception = ExpectedException.none();
+
+    private final QName serviceName =
+        new QName("http://apache.org/hello_world", "SOAPService");
+      
+    private final QName mortimerQ =
+        new QName("http://apache.org/hello_world", "Mortimer");
+
+    public HTTPConduitIoExceptionsTest() {
+    }
+
+
+    @BeforeClass
+    public static void startServers() throws Exception {
+        assertTrue("server did not launch correctly", launchServer(BadServer.class, true));
+    }
+
+    @Test
+    public void testNoIoExceptions() throws Exception {
+        final Greeter greeter = getGreeter();
+
+        try (Client client = (Client)greeter) {
+            client.getRequestContext().put(HTTPConduit.NO_IO_EXCEPTIONS, true);
+            
+            exception.expect(SOAPFaultException.class);
+            exception.expectMessage("Go away");
+            
+            greeter.sayHi();
+        }
+    }
+
+    @Test
+    public void testServiceUnavailable() throws Exception {
+        final Greeter greeter = getGreeter();
+
+        exception.expect(WebServiceException.class);
+        exception.expectCause(new TypeSafeMatcher<Throwable>() {
+            private final String message = "HTTP response '503: Service Unavailable' when "
+                + "communicating with http://localhost:" + BadServer.PORT + "/Mortimer";
+
+            @Override
+            public void describeTo(Description description) {
+                description
+                    .appendValue(HTTPException.class)
+                    .appendText(" and message ")
+                    .appendValue(message);
+            }
+
+            @Override
+            protected boolean matchesSafely(Throwable item) {
+                return item instanceof HTTPException && item.getMessage().equals(message);
+            }
+        });
+        
+        
+        greeter.sayHi();
+    }
+
+    @Test
+    public void testNotFound() throws Exception {
+        final Greeter greeter = getGreeter();
+
+        exception.expect(WebServiceException.class);
+        exception.expectCause(new TypeSafeMatcher<Throwable>() {
+            private final String message = "HTTP response '404: Not Found' when "
+                + "communicating with http://localhost:" + BadServer.PORT + "/Mortimer";
+
+            @Override
+            public void describeTo(Description description) {
+                description
+                    .appendValue(HTTPException.class)
+                    .appendText(" and message ")
+                    .appendValue(message);
+            }
+
+            @Override
+            protected boolean matchesSafely(Throwable item) {
+                return item instanceof HTTPException && item.getMessage().equals(message);
+            }
+        });
+        
+        
+        greeter.greetMe("Test");
+    }
+    
+    private Greeter getGreeter() throws MalformedURLException {
+        URL wsdl = getClass().getResource("greeting.wsdl");
+        assertNotNull("WSDL is null", wsdl);
+
+        SOAPService service = new SOAPService(wsdl, serviceName);
+        assertNotNull("Service is null", service);
+
+        Greeter mortimer = service.getPort(mortimerQ, Greeter.class);
+        assertNotNull("Port is null", mortimer);
+        updateAddressPort(mortimer, BadServer.PORT);
+
+        return mortimer;
+    }
+}
+