-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing vertx-web-validation module tests
- Loading branch information
1 parent
168efff
commit b404307
Showing
71 changed files
with
2,828 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...nced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/InterceptedResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import jakarta.ws.rs.ConstrainedTo; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.NameBinding; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.RuntimeType; | ||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.MultivaluedMap; | ||
import jakarta.ws.rs.ext.MessageBodyWriter; | ||
import jakarta.ws.rs.ext.Provider; | ||
import jakarta.ws.rs.ext.WriterInterceptor; | ||
import jakarta.ws.rs.ext.WriterInterceptorContext; | ||
|
||
@Path("/intercepted") | ||
public class InterceptedResource { | ||
|
||
/** | ||
* Interceptors write their message to this list, when they are invoked | ||
* It is a bit dumb way, but it is the easier to get indicators if interceptors were invoked to the client | ||
*/ | ||
public static List<String> interceptorMessages = new ArrayList<>(); | ||
|
||
@WithWriterInterceptor | ||
@GET | ||
public InterceptedString getInterceptedString() { | ||
return new InterceptedString("foo"); | ||
} | ||
|
||
@GET() | ||
@Path("/messages") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String getMessages() { | ||
StringBuilder outputMessage = new StringBuilder(); | ||
for (String string : interceptorMessages) { | ||
outputMessage.append(string); | ||
} | ||
return outputMessage.toString(); | ||
} | ||
|
||
public static class InterceptedString { | ||
public String name; | ||
|
||
public InterceptedString(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
/** | ||
* This annotation binds the providers to only intercept the method in this class. | ||
* Otherwise, they would be global and intercept all endpoints across the entire application. | ||
*/ | ||
@NameBinding | ||
@Target({ ElementType.METHOD, ElementType.TYPE }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface WithWriterInterceptor { | ||
|
||
} | ||
|
||
@Provider | ||
public static class InterceptedStringHandler implements MessageBodyWriter<InterceptedString> { | ||
@Override | ||
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return type == InterceptedString.class; | ||
} | ||
|
||
@Override | ||
public void writeTo(InterceptedString interceptedString, Class<?> type, Type genericType, Annotation[] annotations, | ||
MediaType mediaType, | ||
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) | ||
throws IOException, WebApplicationException { | ||
entityStream.write((interceptedString.name).getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} | ||
|
||
@Provider | ||
@WithWriterInterceptor | ||
public static class UnconstrainedWriterInterceptor implements WriterInterceptor { | ||
@Override | ||
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { | ||
InterceptedResource.interceptorMessages.add("Unconstrained interceptor "); | ||
context.proceed(); | ||
} | ||
} | ||
|
||
@Provider | ||
@ConstrainedTo(RuntimeType.CLIENT) | ||
@WithWriterInterceptor | ||
public static class ClientWriterInterceptor implements WriterInterceptor { | ||
|
||
@Override | ||
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { | ||
InterceptedResource.interceptorMessages.add("Client interceptor "); | ||
context.proceed(); | ||
} | ||
} | ||
|
||
@Provider | ||
@ConstrainedTo(RuntimeType.SERVER) | ||
@WithWriterInterceptor | ||
public static class ServerWriterInterceptor implements WriterInterceptor { | ||
|
||
@Override | ||
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { | ||
InterceptedResource.interceptorMessages.add("Server interceptor "); | ||
context.proceed(); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...d-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/SseEventUpdateResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.client.ClientBuilder; | ||
import jakarta.ws.rs.client.WebTarget; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.sse.OutboundSseEvent; | ||
import jakarta.ws.rs.sse.Sse; | ||
import jakarta.ws.rs.sse.SseEventSink; | ||
import jakarta.ws.rs.sse.SseEventSource; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
|
||
@Path("sse") | ||
public class SseEventUpdateResource { | ||
public static final String DATA_VALUE = "random data value"; | ||
|
||
@Context | ||
Sse sse; | ||
|
||
@GET | ||
@Path("client-update") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public Response clientUpdate() throws InterruptedException { | ||
String host = ConfigProvider.getConfig().getValue("quarkus.http.host", String.class); | ||
int port = ConfigProvider.getConfig().getValue("quarkus.http.port", Integer.class); | ||
List<String> receivedData = new CopyOnWriteArrayList<>(); | ||
|
||
WebTarget target = ClientBuilder.newClient().target("http://" + host + ":" + port + "/api/sse/server-update"); | ||
try (SseEventSource eventSource = SseEventSource.target(target).build()) { | ||
eventSource.register(ev -> { | ||
String event = "event: name=" + ev.getName() + " data={" + ev.readData() + "} and is empty: " + ev.isEmpty() | ||
+ "\n"; | ||
receivedData.add(event); | ||
}, thr -> { | ||
String event = "Error: " + thr.getMessage() + "\n" + Arrays.toString(thr.getStackTrace()); | ||
receivedData.add(event); | ||
}); | ||
|
||
CountDownLatch latch = new CountDownLatch(2); | ||
eventSource.open(); | ||
latch.await(1, TimeUnit.SECONDS); | ||
} | ||
return Response.ok(String.join("\n", receivedData)).build(); | ||
} | ||
|
||
@GET | ||
@Path("server-update") | ||
@Produces(MediaType.SERVER_SENT_EVENTS) | ||
public void updates(@Context SseEventSink eventSink) { | ||
eventSink.send(createEvent("NON EMPTY", DATA_VALUE)); | ||
eventSink.send(createEvent("EMPTY", "")); | ||
} | ||
|
||
private OutboundSseEvent createEvent(String name, String data) { | ||
return sse.newEventBuilder() | ||
.name(name) | ||
.data(data) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.