@Test public void readResourcePositionAndTakeUntil() throws Exception { Resource resource = new ClassPathResource("DataBufferUtilsTests.txt", getClass()); Flux<DataBuffer> flux = DataBufferUtils.read(resource, 3, this.bufferFactory, 3); flux = DataBufferUtils.takeUntilByteCount(flux, 5); StepVerifier.create(flux) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("ba")) .expectComplete() .verify(Duration.ofSeconds(5)); }
private void verifyWrittenData(Flux<DataBuffer> writeResult) throws IOException { StepVerifier.create(writeResult) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("baz")) .consumeNextWith(stringConsumer("qux")) .expectComplete() .verify(Duration.ofSeconds(3)); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foobarbazqux", result); }
@Override @Test public void decode() { Flux<DataBuffer> input = Flux.concat( dataBuffer(this.fooBytes), dataBuffer(this.barBytes)); testDecodeAll(input, ByteBuffer.class, step -> step .consumeNextWith(expectByteBuffer(ByteBuffer.wrap(this.fooBytes))) .consumeNextWith(expectByteBuffer(ByteBuffer.wrap(this.barBytes))) .verifyComplete()); }
@Test public void writeString() { Flux<String> source = Flux.just("foo", "bar"); testWrite(source, outputMessage, String.class); StepVerifier.create(outputMessage.getBody()) .consumeNextWith(stringConsumer("data:")) .consumeNextWith(stringConsumer("foo\n")) .consumeNextWith(stringConsumer("\n")) .consumeNextWith(stringConsumer("data:")) .consumeNextWith(stringConsumer("bar\n")) .consumeNextWith(stringConsumer("\n")) .expectComplete() .verify(); }
@Test public void takeUntilByteCountExact() { Flux<DataBuffer> source = Flux.concat( deferStringBuffer("foo"), deferStringBuffer("bar"), deferStringBuffer("baz") ); Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(source, 6L); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@Test public void skipUntilByteCount() { Flux<DataBuffer> source = Flux.concat( deferStringBuffer("foo"), deferStringBuffer("bar"), deferStringBuffer("baz") ); Flux<DataBuffer> result = DataBufferUtils.skipUntilByteCount(source, 5L); StepVerifier.create(result) .consumeNextWith(stringConsumer("r")) .consumeNextWith(stringConsumer("baz")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@Test public void encodeAsStream() throws Exception { Pojo pojo1 = new Pojo("foo", "bar"); Pojo pojo2 = new Pojo("foofoo", "barbar"); Pojo pojo3 = new Pojo("foofoofoo", "barbarbar"); Flux<Pojo> input = Flux.just(pojo1, pojo2, pojo3); ResolvableType type = ResolvableType.forClass(Pojo.class); testEncodeAll(input, type, step -> step .consumeNextWith(expect(pojo1, Pojo.class)) .consumeNextWith(expect(pojo2, Pojo.class)) .consumeNextWith(expect(pojo3, Pojo.class)) .verifyComplete(), STREAM_SMILE_MIME_TYPE, null); }
@Test public void decodeErrorAalto() { Flux<DataBuffer> source = Flux.concat( stringBuffer("<pojo>"), Flux.error(new RuntimeException())); Flux<XMLEvent> events = this.decoder.decode(source, null, null, Collections.emptyMap()); StepVerifier.create(events) .consumeNextWith(e -> assertTrue(e.isStartDocument())) .consumeNextWith(e -> assertStartElement(e, "pojo")) .expectError(RuntimeException.class) .verify(); }
@Override public void encode() throws Exception { Flux<DataBuffer> input = Flux.just(this.fooBytes, this.barBytes) .flatMap(bytes -> Mono.defer(() -> { DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(bytes.length); dataBuffer.write(bytes); return Mono.just(dataBuffer); })); testEncodeAll(input, DataBuffer.class, step -> step .consumeNextWith(expectBytes(this.fooBytes)) .consumeNextWith(expectBytes(this.barBytes)) .verifyComplete()); }
@Test public void writeServerSentEvent() { ServerSentEvent<?> event = ServerSentEvent.builder().data("bar").id("c42").event("foo") .comment("bla\nbla bla\nbla bla bla").retry(Duration.ofMillis(123L)).build(); Mono<ServerSentEvent> source = Mono.just(event); testWrite(source, outputMessage, ServerSentEvent.class); StepVerifier.create(outputMessage.getBody()) .consumeNextWith(stringConsumer("id:c42\nevent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:")) .consumeNextWith(stringConsumer("bar\n")) .consumeNextWith(stringConsumer("\n")) .expectComplete() .verify(); }
@Override public void encode() { Flux<CharSequence> input = Flux.just(this.foo, this.bar); testEncodeAll(input, CharSequence.class, step -> step .consumeNextWith(expectString(this.foo)) .consumeNextWith(expectString(this.bar)) .verifyComplete()); }
@Test public void encodeStream() { Flux<Message> input = Flux.just(this.msg1, this.msg2); testEncodeAll(input, Msg.class, step -> step .consumeNextWith(expect(this.msg1)) .consumeNextWith(expect(this.msg2)) .verifyComplete()); }
@Override @Test public void encode() { Flux<ByteBuffer> input = Flux.just(this.fooBytes, this.barBytes) .map(ByteBuffer::wrap); testEncodeAll(input, ByteBuffer.class, step -> step .consumeNextWith(expectBytes(this.fooBytes)) .consumeNextWith(expectBytes(this.barBytes)) .verifyComplete()); } }
@Test public void takeUntilByteCount() { Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount( Flux.just(stringBuffer("foo"), stringBuffer("bar")), 5L); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("ba")) .expectComplete() .verify(Duration.ofSeconds(5)); }
private void verifyReadData(Flux<DataBuffer> buffers) { StepVerifier.create(buffers) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("baz")) .consumeNextWith(stringConsumer("qux")) .expectComplete() .verify(Duration.ofSeconds(3)); }
@Override public void decode() { Flux<DataBuffer> input = Flux.just( this.bufferFactory.wrap(this.fooBytes), this.bufferFactory.wrap(this.barBytes)); testDecodeAll(input, DataBuffer.class, step -> step .consumeNextWith(expectDataBuffer(this.fooBytes)) .consumeNextWith(expectDataBuffer(this.barBytes)) .verifyComplete()); }
@Override public void encode() throws Exception { Flux<Object> input = Flux.just(new Pojo("foo", "bar"), new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar")); testEncodeAll(input, ResolvableType.forClass(Pojo.class), step -> step .consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}\n")) .consumeNextWith(expectString("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n")) .consumeNextWith(expectString("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n")) .verifyComplete(), APPLICATION_STREAM_JSON, null); }
@Test public void readByteArrayResourcePositionAndTakeUntil() throws Exception { Resource resource = new ByteArrayResource("foobarbazqux" .getBytes()); Flux<DataBuffer> flux = DataBufferUtils.read(resource, 3, this.bufferFactory, 3); flux = DataBufferUtils.takeUntilByteCount(flux, 5); StepVerifier.create(flux) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("ba")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@Override @Test public void decode() { Flux<DataBuffer> input = Flux.concat( dataBuffer(this.fooBytes), dataBuffer(this.barBytes)); testDecodeAll(input, byte[].class, step -> step .consumeNextWith(expectBytes(this.fooBytes)) .consumeNextWith(expectBytes(this.barBytes)) .verifyComplete()); }
@Override public void encode() { Flux<byte[]> input = Flux.just(this.fooBytes, this.barBytes); testEncodeAll(input, byte[].class, step -> step .consumeNextWith(expectBytes(this.fooBytes)) .consumeNextWith(expectBytes(this.barBytes)) .verifyComplete()); } }