|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * The contents of this file are subject to the terms of either the GNU |
| 7 | + * General Public License Version 2 only ("GPL") or the Common Development |
| 8 | + * and Distribution License("CDDL") (collectively, the "License"). You |
| 9 | + * may not use this file except in compliance with the License. You can |
| 10 | + * obtain a copy of the License at |
| 11 | + * http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 | + * or packager/legal/LICENSE.txt. See the License for the specific |
| 13 | + * language governing permissions and limitations under the License. |
| 14 | + * |
| 15 | + * When distributing the software, include this License Header Notice in each |
| 16 | + * file and include the License file at packager/legal/LICENSE.txt. |
| 17 | + * |
| 18 | + * GPL Classpath Exception: |
| 19 | + * Oracle designates this particular file as subject to the "Classpath" |
| 20 | + * exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 | + * file that accompanied this code. |
| 22 | + * |
| 23 | + * Modifications: |
| 24 | + * If applicable, add the following below the License Header, with the fields |
| 25 | + * enclosed by brackets [] replaced by your own identifying information: |
| 26 | + * "Portions Copyright [year] [name of copyright owner]" |
| 27 | + * |
| 28 | + * Contributor(s): |
| 29 | + * If you wish your version of this file to be governed by only the CDDL or |
| 30 | + * only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 | + * elects to include this software in this distribution under the [CDDL or GPL |
| 32 | + * Version 2] license." If you don't indicate a single choice of license, a |
| 33 | + * recipient has the option to distribute your version of this file under |
| 34 | + * either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 | + * its licensees as provided above. However, if you add GPL Version 2 code |
| 36 | + * and therefore, elected the GPL Version 2 license, then the option applies |
| 37 | + * only if the new code is made subject to such option by the copyright |
| 38 | + * holder. |
| 39 | + */ |
| 40 | + |
| 41 | +package org.glassfish.jersey.tests.e2e.server; |
| 42 | + |
| 43 | +import java.util.concurrent.CompletableFuture; |
| 44 | +import java.util.concurrent.CompletionStage; |
| 45 | +import java.util.concurrent.ExecutorService; |
| 46 | +import java.util.concurrent.Executors; |
| 47 | + |
| 48 | +import javax.ws.rs.GET; |
| 49 | +import javax.ws.rs.Path; |
| 50 | +import javax.ws.rs.WebApplicationException; |
| 51 | +import javax.ws.rs.core.Application; |
| 52 | +import javax.ws.rs.core.Response; |
| 53 | + |
| 54 | +import org.glassfish.jersey.server.ResourceConfig; |
| 55 | +import org.glassfish.jersey.test.JerseyTest; |
| 56 | + |
| 57 | +import org.junit.Test; |
| 58 | +import static org.hamcrest.CoreMatchers.is; |
| 59 | +import static org.junit.Assert.assertThat; |
| 60 | + |
| 61 | +/** |
| 62 | + * @author Pavel Bucek (pavel.bucek at oracle.com) |
| 63 | + */ |
| 64 | +public class CompletionStageTest extends JerseyTest { |
| 65 | + |
| 66 | + static final String ENTITY = "entity"; |
| 67 | + // delay of async operations in seconds. |
| 68 | + static final int DELAY = 1; |
| 69 | + |
| 70 | + @Override |
| 71 | + protected Application configure() { |
| 72 | + return new ResourceConfig(CompletionStageResource.class); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testGetCompleted() { |
| 77 | + Response response = target("cs/completed").request().get(); |
| 78 | + |
| 79 | + assertThat(response.getStatus(), is(200)); |
| 80 | + assertThat(response.readEntity(String.class), is(ENTITY)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testGetException400() { |
| 85 | + Response response = target("cs/exception400").request().get(); |
| 86 | + |
| 87 | + assertThat(response.getStatus(), is(400)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testGetException405() { |
| 92 | + Response response = target("cs/exception405").request().get(); |
| 93 | + |
| 94 | + assertThat(response.getStatus(), is(405)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testGetCancelled() { |
| 99 | + Response response = target("cs/cancelled").request().get(); |
| 100 | + |
| 101 | + assertThat(response.getStatus(), is(503)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testGetCompletedAsync() { |
| 106 | + Response response = target("cs/completedAsync").request().get(); |
| 107 | + |
| 108 | + assertThat(response.getStatus(), is(200)); |
| 109 | + assertThat(response.readEntity(String.class), is(ENTITY)); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testGetException400Async() { |
| 114 | + Response response = target("cs/exception400Async").request().get(); |
| 115 | + |
| 116 | + assertThat(response.getStatus(), is(400)); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testGetException405Async() { |
| 121 | + Response response = target("cs/exception405Async").request().get(); |
| 122 | + |
| 123 | + assertThat(response.getStatus(), is(405)); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testGetCancelledAsync() { |
| 128 | + Response response = target("cs/cancelledAsync").request().get(); |
| 129 | + |
| 130 | + assertThat(response.getStatus(), is(503)); |
| 131 | + } |
| 132 | + |
| 133 | + @Path("/cs") |
| 134 | + public static class CompletionStageResource { |
| 135 | + |
| 136 | + private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool(); |
| 137 | + |
| 138 | + @GET |
| 139 | + @Path("/completed") |
| 140 | + public CompletionStage<String> getCompleted() { |
| 141 | + return CompletableFuture.completedFuture(ENTITY); |
| 142 | + } |
| 143 | + |
| 144 | + @GET |
| 145 | + @Path("/exception400") |
| 146 | + public CompletionStage<String> getException400() { |
| 147 | + CompletableFuture<String> cs = new CompletableFuture<>(); |
| 148 | + cs.completeExceptionally(new WebApplicationException(400)); |
| 149 | + |
| 150 | + return cs; |
| 151 | + } |
| 152 | + |
| 153 | + @GET |
| 154 | + @Path("/exception405") |
| 155 | + public CompletionStage<String> getException405() { |
| 156 | + CompletableFuture<String> cs = new CompletableFuture<>(); |
| 157 | + cs.completeExceptionally(new WebApplicationException(405)); |
| 158 | + |
| 159 | + return cs; |
| 160 | + } |
| 161 | + |
| 162 | + @GET |
| 163 | + @Path("/cancelled") |
| 164 | + public CompletionStage<String> getCancelled() { |
| 165 | + CompletableFuture<String> cs = new CompletableFuture<>(); |
| 166 | + cs.cancel(true); |
| 167 | + |
| 168 | + return cs; |
| 169 | + } |
| 170 | + |
| 171 | + @GET |
| 172 | + @Path("/completedAsync") |
| 173 | + public CompletionStage<String> getCompletedAsync() { |
| 174 | + CompletableFuture<String> cs = new CompletableFuture<>(); |
| 175 | + delaySubmit(() -> cs.complete(ENTITY)); |
| 176 | + return cs; |
| 177 | + } |
| 178 | + |
| 179 | + @GET |
| 180 | + @Path("/exception400Async") |
| 181 | + public CompletionStage<String> getException400Async() { |
| 182 | + CompletableFuture<String> cs = new CompletableFuture<>(); |
| 183 | + delaySubmit(() -> cs.completeExceptionally(new WebApplicationException(400))); |
| 184 | + |
| 185 | + return cs; |
| 186 | + } |
| 187 | + |
| 188 | + @GET |
| 189 | + @Path("/exception405Async") |
| 190 | + public CompletionStage<String> getException405Async() { |
| 191 | + CompletableFuture<String> cs = new CompletableFuture<>(); |
| 192 | + delaySubmit(() -> cs.completeExceptionally(new WebApplicationException(405))); |
| 193 | + |
| 194 | + return cs; |
| 195 | + } |
| 196 | + |
| 197 | + @GET |
| 198 | + @Path("/cancelledAsync") |
| 199 | + public CompletionStage<String> getCancelledAsync() { |
| 200 | + CompletableFuture<String> cs = new CompletableFuture<>(); |
| 201 | + delaySubmit(() -> cs.cancel(true)); |
| 202 | + |
| 203 | + return cs; |
| 204 | + } |
| 205 | + |
| 206 | + private void delaySubmit(Runnable runnable) { |
| 207 | + EXECUTOR_SERVICE.submit(() -> { |
| 208 | + try { |
| 209 | + Thread.sleep(DELAY * 1000); |
| 210 | + } catch (InterruptedException e) { |
| 211 | + // ignore |
| 212 | + } |
| 213 | + |
| 214 | + runnable.run(); |
| 215 | + }); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments