@Override public Publisher<HandlerResult> handle( ServerHttpRequest request, ServerHttpResponse response, Object handler) { HttpHandler httpHandler = (HttpHandler) handler; Publisher<Void> publisher = httpHandler.handle(request, response); return Streams.wrap(publisher).map(aVoid -> null); }
@Test public void list() { ListOrganizationsResponse page1 = new ListOrganizationsResponse() .withResource( new ListOrganizationsResponseResource() .withMetadata(new Metadata().withId("test-id-1")) .withEntity(new ListOrganizationsResponseEntity().withName("test-name-1"))) .withTotalPages(2); when(this.cloudFoundryClient.organizations().list(new ListOrganizationsRequest().withPage(1))) .thenReturn(Publishers.just(page1)); ListOrganizationsResponse page2 = new ListOrganizationsResponse() .withResource( new ListOrganizationsResponseResource() .withMetadata(new Metadata().withId("test-id-2")) .withEntity(new ListOrganizationsResponseEntity().withName("test-name-2"))) .withTotalPages(2); when(this.cloudFoundryClient.organizations().list(new ListOrganizationsRequest().withPage(2))) .thenReturn(Publishers.just(page2)); List<Organization> expected = Arrays.asList( new Organization("test-id-1", "test-name-1"), new Organization("test-id-2", "test-name-2")); List<Organization> actual = Streams.wrap(this.organizations.list()).toList().poll(); assertEquals(expected, actual); }
@Test public void decode() throws InterruptedException { Stream<ByteBuffer> source = Streams.just(Buffer.wrap("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}").byteBuffer()); List<Object> results = Streams.wrap(decoder.decode(source, ResolvableType.forClass(Pojo.class), null)) .toList() .await(); assertEquals(1, results.size()); assertEquals("foofoo", ((Pojo) results.get(0)).getFoo()); }
@Test public void decodeMultipleChunksToArray() throws InterruptedException { JsonObjectDecoder decoder = new JsonObjectDecoder(true); Stream<ByteBuffer> source = Streams.just( Buffer.wrap("[{\"foo\": \"foofoo\", \"bar\"").byteBuffer(), Buffer.wrap(": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]") .byteBuffer()); List<String> results = Streams.wrap(decoder.decode(source, null, null)) .map( chunk -> { byte[] b = new byte[chunk.remaining()]; chunk.get(b); return new String(b, StandardCharsets.UTF_8); }) .toList() .await(); assertEquals(2, results.size()); assertEquals("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}", results.get(0)); assertEquals("{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}", results.get(1)); }
private HttpServer<Buffer, Buffer> createProtocolListener() throws Exception { final Stream<Buffer> stream = Streams.wrap(processor) .window(flushCount, flushTime, TimeUnit.SECONDS) .flatMap( new Function<Stream<Buffer>, Publisher<Buffer>>() { @Override public Publisher<Buffer> apply(Stream<Buffer> t) { return t.reduce( new Buffer(), new BiFunction<Buffer, Buffer, Buffer>() { @Override public Buffer apply(Buffer prev, Buffer next) { return prev.append(next); } }); } }) .process(RingBufferWorkProcessor.<Buffer>create("gpfdist-sink-worker", 8192, false)); HttpServer<Buffer, Buffer> httpServer = NetStreams.httpServer( new Function<HttpServerSpec<Buffer, Buffer>, HttpServerSpec<Buffer, Buffer>>() { @Override public HttpServerSpec<Buffer, Buffer> apply(HttpServerSpec<Buffer, Buffer> server) { return server.codec(new GpfdistCodec()).listen(port); } }); httpServer.get( "/data", new ReactorChannelHandler<Buffer, Buffer, HttpChannel<Buffer, Buffer>>() { @Override public Publisher<Void> apply(HttpChannel<Buffer, Buffer> request) { request.responseHeaders().removeTransferEncodingChunked(); request.addResponseHeader("Content-type", "text/plain"); request.addResponseHeader("Expires", "0"); request.addResponseHeader("X-GPFDIST-VERSION", "Spring XD"); request.addResponseHeader("X-GP-PROTO", "1"); request.addResponseHeader("Cache-Control", "no-cache"); request.addResponseHeader("Connection", "close"); return request .writeWith( stream .take(batchCount) .timeout(batchTimeout, TimeUnit.SECONDS, Streams.<Buffer>empty()) .concatWith(Streams.just(Buffer.wrap(new byte[0])))) .capacity(1l); } }); httpServer.start().awaitSuccess(); log.info("Server running using address=[" + httpServer.getListenAddress() + "]"); localPort = httpServer.getListenAddress().getPort(); return httpServer; }
private HandlerMethod toHandlerMethod(Publisher<?> handlerPublisher) throws InterruptedException { assertNotNull(handlerPublisher); List<?> handlerList = Streams.wrap(handlerPublisher).toList().await(5, TimeUnit.SECONDS); assertEquals(1, handlerList.size()); return (HandlerMethod) handlerList.get(0); }