You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2017/03/31 11:52:52 UTC

cxf git commit: Adding WebClient Failover test

Repository: cxf
Updated Branches:
  refs/heads/master a304f27b8 -> 18ac71fc4


Adding WebClient Failover test


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/18ac71fc
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/18ac71fc
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/18ac71fc

Branch: refs/heads/master
Commit: 18ac71fc402b0cd912bbf2849c44f438f63384f9
Parents: a304f27
Author: Colm O hEigeartaigh <co...@apache.org>
Authored: Fri Mar 31 12:52:41 2017 +0100
Committer: Colm O hEigeartaigh <co...@apache.org>
Committed: Fri Mar 31 12:52:41 2017 +0100

----------------------------------------------------------------------
 .../jaxrs/failover/FailoverBookServer.java      | 43 ++++++++++
 .../jaxrs/failover/FailoverBookStore.java       | 47 +++++++++++
 .../jaxrs/failover/FailoverWebClientTest.java   | 83 ++++++++++++++++++++
 .../cxf/systest/jaxrs/failover/cxf-service.xml  | 75 ++++++++++++++++++
 4 files changed, 248 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/18ac71fc/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverBookServer.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverBookServer.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverBookServer.java
new file mode 100644
index 0000000..34e99b3
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverBookServer.java
@@ -0,0 +1,43 @@
+/**
+ * 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.jaxrs.failover;
+
+import java.net.URL;
+
+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 FailoverBookServer extends AbstractBusTestServerBase {
+    protected void run()  {
+        URL busFile = Server.class.getResource("cxf-service.xml");
+        Bus busLocal = new SpringBusFactory().createBus(busFile);
+        BusFactory.setDefaultBus(busLocal);
+        setBus(busLocal);
+
+        try {
+            new Server();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/18ac71fc/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverBookStore.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverBookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverBookStore.java
new file mode 100644
index 0000000..29b5432
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverBookStore.java
@@ -0,0 +1,47 @@
+/**
+ * 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.jaxrs.failover;
+
+
+import javax.ws.rs.GET;
+import javax.ws.rs.NotFoundException;
+import javax.ws.rs.Path;
+
+import org.apache.cxf.systest.jaxrs.Book;
+
+/**
+ * Just allow one invocation + then return 404
+ */
+@Path("/bookstore")
+public class FailoverBookStore {
+
+    private int counter;
+
+    @GET
+    @Path("/")
+    public Book getBookRoot() {
+        if (counter != 0) {
+            throw new NotFoundException();
+        }
+        counter++;
+        return new Book("root", 124L);
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cxf/blob/18ac71fc/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverWebClientTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverWebClientTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverWebClientTest.java
new file mode 100644
index 0000000..eb7269f
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverWebClientTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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.jaxrs.failover;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.cxf.clustering.FailoverFeature;
+import org.apache.cxf.clustering.SequentialStrategy;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.systest.jaxrs.Book;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * A test for failover using a WebClient object
+ */
+public class FailoverWebClientTest extends AbstractBusClientServerTestBase {
+    static final String PORT1 = allocatePort(FailoverBookServer.class);
+    static final String PORT2 = allocatePort(FailoverBookServer.class, 2);
+    static final String PORT3 = allocatePort(FailoverBookServer.class, 3);
+
+
+    @BeforeClass
+    public static void startServers() throws Exception {
+        AbstractResourceInfo.clearAllMaps();
+        assertTrue("server did not launch correctly",
+                   launchServer(FailoverBookServer.class, true));
+        createStaticBus();
+    }
+
+    @Test
+    public void testEchoXmlBookQuery() throws Exception {
+        String address = "http://localhost:" + PORT1 + "/bookstore";
+
+        FailoverFeature failoverFeature = new FailoverFeature();
+        SequentialStrategy strategy = new SequentialStrategy();
+        List<String> addresses = new ArrayList<>();
+        addresses.add("http://localhost:" + PORT2 + "/bookstore");
+        addresses.add("http://localhost:" + PORT3 + "/bookstore");
+        strategy.setAlternateAddresses(addresses);
+        failoverFeature.setStrategy(strategy);
+
+        WebClient webClient = WebClient.create(address, null,
+                                               Collections.singletonList(failoverFeature),
+                                               null).accept("application/xml");
+        // Should hit PORT1
+        Book b = webClient.get(Book.class);
+        assertEquals(124L, b.getId());
+        assertEquals("root", b.getName());
+
+        // Should failover to PORT2
+        webClient.get(Book.class);
+        assertEquals(124L, b.getId());
+        assertEquals("root", b.getName());
+
+        // Should failover to PORT3
+        webClient.get(Book.class);
+        assertEquals(124L, b.getId());
+        assertEquals("root", b.getName());
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cxf/blob/18ac71fc/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/failover/cxf-service.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/failover/cxf-service.xml b/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/failover/cxf-service.xml
new file mode 100644
index 0000000..f0f5747
--- /dev/null
+++ b/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/failover/cxf-service.xml
@@ -0,0 +1,75 @@
+<!--
+ 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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+  xmlns:cxf="http://cxf.apache.org/core"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xmlns:sec="http://cxf.apache.org/configuration/security"
+  xmlns:http="http://cxf.apache.org/transports/http/configuration"
+  xmlns:httpj="http://cxf.apache.org/transports/http-jetty/configuration"
+  xmlns:jaxws="http://cxf.apache.org/jaxws"
+  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
+  xsi:schemaLocation="
+            http://cxf.apache.org/core
+            http://cxf.apache.org/schemas/core.xsd
+            http://cxf.apache.org/configuration/security
+            http://cxf.apache.org/schemas/configuration/security.xsd
+            http://cxf.apache.org/jaxws
+            http://cxf.apache.org/schemas/jaxws.xsd
+            http://cxf.apache.org/jaxrs
+            http://cxf.apache.org/schemas/jaxrs.xsd
+            http://cxf.apache.org/transports/http/configuration
+            http://cxf.apache.org/schemas/configuration/http-conf.xsd
+            http://cxf.apache.org/transports/http-jetty/configuration
+            http://cxf.apache.org/schemas/configuration/http-jetty.xsd
+            http://www.springframework.org/schema/beans
+            http://www.springframework.org/schema/beans/spring-beans.xsd">
+   
+   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
+   
+   <cxf:bus>
+        <cxf:features>
+            <cxf:logging/>
+        </cxf:features>
+   </cxf:bus>
+   
+   <bean id="serviceBean1" class="org.apache.cxf.systest.jaxrs.failover.FailoverBookStore"/>
+   
+   <jaxrs:server address="http://localhost:${testutil.ports.FailoverBookServer}/">
+       <jaxrs:serviceBeans>
+          <ref bean="serviceBean1"/>
+       </jaxrs:serviceBeans>
+   </jaxrs:server>
+   
+   <bean id="serviceBean2" class="org.apache.cxf.systest.jaxrs.failover.FailoverBookStore"/>
+   
+   <jaxrs:server address="http://localhost:${testutil.ports.FailoverBookServer.2}/">
+       <jaxrs:serviceBeans>
+          <ref bean="serviceBean2"/>
+       </jaxrs:serviceBeans>
+   </jaxrs:server>
+   
+   <bean id="serviceBean3" class="org.apache.cxf.systest.jaxrs.failover.FailoverBookStore"/>
+   
+   <jaxrs:server address="http://localhost:${testutil.ports.FailoverBookServer.3}/">
+       <jaxrs:serviceBeans>
+          <ref bean="serviceBean3"/>
+       </jaxrs:serviceBeans>
+   </jaxrs:server>
+  
+</beans>