[CXF-6833] Adding a HelloWorld server test
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/6c324421 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/6c324421 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/6c324421 Branch: refs/heads/master-jaxrs-2.1 Commit: 6c324421a358919b4ecb3cfdd8a2e0e16ff81724 Parents: a51bce9 Author: Sergey Beryozkin <[email protected]> Authored: Fri Apr 29 13:41:39 2016 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Fri Apr 29 13:41:39 2016 +0100 ---------------------------------------------------------------------- systests/jaxrs/pom.xml | 5 ++ .../jaxrs/reactive/JAXRSReactiveTest.java | 47 ++++++++++++++ .../jaxrs/reactive/ObservableWriter.java | 66 ++++++++++++++++++++ .../systest/jaxrs/reactive/ReactiveServer.java | 61 ++++++++++++++++++ .../systest/jaxrs/reactive/ReactiveService.java | 42 +++++++++++++ 5 files changed, 221 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/6c324421/systests/jaxrs/pom.xml ---------------------------------------------------------------------- diff --git a/systests/jaxrs/pom.xml b/systests/jaxrs/pom.xml index 8a2fc9e..034b5f3 100644 --- a/systests/jaxrs/pom.xml +++ b/systests/jaxrs/pom.xml @@ -82,6 +82,11 @@ <groupId>org.glassfish</groupId> <artifactId>javax.json</artifactId> </dependency> + <dependency> + <groupId>io.reactivex</groupId> + <artifactId>rxjava</artifactId> + <version>1.1.3</version> + </dependency> <!-- <dependency> <groupId>org.apache.bval</groupId> http://git-wip-us.apache.org/repos/asf/cxf/blob/6c324421/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/JAXRSReactiveTest.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/JAXRSReactiveTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/JAXRSReactiveTest.java new file mode 100644 index 0000000..0241fed --- /dev/null +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/JAXRSReactiveTest.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.reactive; + +import org.apache.cxf.jaxrs.client.WebClient; +import org.apache.cxf.jaxrs.model.AbstractResourceInfo; +import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class JAXRSReactiveTest extends AbstractBusClientServerTestBase { + public static final String PORT = ReactiveServer.PORT; + @BeforeClass + public static void startServers() throws Exception { + AbstractResourceInfo.clearAllMaps(); + assertTrue("server did not launch correctly", + launchServer(ReactiveServer.class, true)); + createStaticBus(); + } + @Test + public void testGetHelloWorldText() throws Exception { + String address = "http://localhost:" + PORT + "/reactive/text"; + WebClient wc = WebClient.create(address); + String text = wc.accept("text/plain").get(String.class); + assertEquals("Hello, world!", text); + } + + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/6c324421/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ObservableWriter.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ObservableWriter.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ObservableWriter.java new file mode 100644 index 0000000..57e7aec --- /dev/null +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ObservableWriter.java @@ -0,0 +1,66 @@ +/** + * 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.reactive; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; + +import org.apache.cxf.jaxrs.utils.ExceptionUtils; +import org.apache.cxf.jaxrs.utils.HttpUtils; + +import rx.Observable; + +public class ObservableWriter implements MessageBodyWriter<Observable<String>> { + + @Override + public long getSize(Observable<String> arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) { + // TODO Auto-generated method stub + return -1; + } + + @Override + public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) { + return true; + } + + @Override + public void writeTo(Observable<String> obs, Class<?> cls, Type t, Annotation[] anns, MediaType mt, + MultivaluedMap<String, Object> headers, OutputStream os) + throws IOException, WebApplicationException { + final String enc = HttpUtils.getSetEncoding(mt, headers, StandardCharsets.UTF_8.name()); + obs.subscribe(s -> writeToOutputStream(s, os, enc)); + } + + private static void writeToOutputStream(String s, OutputStream os, String enc) { + try { + os.write(s.getBytes(enc)); + } catch (IOException ex) { + throw ExceptionUtils.toInternalServerErrorException(ex, null); + } + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/6c324421/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveServer.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveServer.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveServer.java new file mode 100644 index 0000000..b4e3f54 --- /dev/null +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveServer.java @@ -0,0 +1,61 @@ +/** + * 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.reactive; + +import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; +import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider; +import org.apache.cxf.testutil.common.AbstractBusTestServerBase; + +public class ReactiveServer extends AbstractBusTestServerBase { + public static final String PORT = allocatePort(ReactiveServer.class); + + org.apache.cxf.endpoint.Server server; + public ReactiveServer() { + } + + protected void run() { + JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); + sf.setProvider(new ObservableWriter()); + sf.setResourceClasses(ReactiveService.class); + sf.setResourceProvider(ReactiveService.class, + new SingletonResourceProvider(new ReactiveService(), true)); + sf.setAddress("http://localhost:" + PORT + "/"); + server = sf.create(); + } + + public void tearDown() throws Exception { + server.stop(); + server.destroy(); + server = null; + } + + public static void main(String[] args) { + try { + ReactiveServer s = new ReactiveServer(); + s.start(); + } catch (Exception ex) { + ex.printStackTrace(); + System.exit(-1); + } finally { + System.out.println("done!"); + } + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/6c324421/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveService.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveService.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveService.java new file mode 100644 index 0000000..87ab35a --- /dev/null +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveService.java @@ -0,0 +1,42 @@ +/** + * 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.reactive; + + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +import rx.Observable; + + +@Path("/reactive") +public class ReactiveService { + + @GET + @Produces("text/plain") + @Path("text") + public Observable<String> getText() { + return Observable.just("Hello, world!"); + } + +} + +
