org.springframework.protobuf.Msg Java Examples

The following examples show how to use org.springframework.protobuf.Msg. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CancelWithoutDemandCodecTests.java    From spring-analysis-note with MIT License 7 votes vote down vote up
@Test // gh-22731
public void cancelWithProtobufDecoder() throws InterruptedException {
	ProtobufDecoder decoder = new ProtobufDecoder();

	Mono<DataBuffer> input = Mono.fromCallable(() -> {
		Msg msg = Msg.newBuilder().setFoo("Foo").build();
		byte[] bytes = msg.toByteArray();
		DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
		buffer.write(bytes);
		return buffer;
	});

	Flux<Message> messages = decoder.decode(input, ResolvableType.forType(Msg.class),
			new MimeType("application", "x-protobuf"), Collections.emptyMap());
	ZeroDemandMessageSubscriber subscriber = new ZeroDemandMessageSubscriber();
	messages.subscribe(subscriber);
	subscriber.cancel();
}
 
Example #2
Source File: ProtobufHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeJsonWithGoogleProtobuf() throws IOException {
	this.converter = new ProtobufHttpMessageConverter(
			new ProtobufHttpMessageConverter.ProtobufJavaUtilSupport(null, null),
			this.extensionRegistry);
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
	this.converter.write(this.testMsg, contentType, outputMessage);

	assertEquals(contentType, outputMessage.getHeaders().getContentType());

	final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
	assertFalse("body is empty", body.isEmpty());

	Msg.Builder builder = Msg.newBuilder();
	JsonFormat.parser().merge(body, builder);
	assertEquals(this.testMsg, builder.build());

	assertNull(outputMessage.getHeaders().getFirst(
			ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER));
	assertNull(outputMessage.getHeaders().getFirst(
			ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER));
}
 
Example #3
Source File: ProtobufHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeJsonWithJavaFormat() throws IOException {
	this.converter = new ProtobufHttpMessageConverter(
			new ProtobufHttpMessageConverter.ProtobufJavaFormatSupport(),
			this.extensionRegistry);
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
	this.converter.write(this.testMsg, contentType, outputMessage);

	assertEquals(contentType, outputMessage.getHeaders().getContentType());

	final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
	assertFalse("body is empty", body.isEmpty());

	Msg.Builder builder = Msg.newBuilder();
	JsonFormat.parser().merge(body, builder);
	assertEquals(this.testMsg, builder.build());

	assertNull(outputMessage.getHeaders().getFirst(
			ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER));
	assertNull(outputMessage.getHeaders().getFirst(
			ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER));
}
 
Example #4
Source File: ProtobufHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeJsonWithGoogleProtobuf() throws IOException {
	this.converter = new ProtobufHttpMessageConverter(
			new ProtobufHttpMessageConverter.ProtobufJavaUtilSupport(null, null),
			this.extensionRegistry);
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = MediaType.APPLICATION_JSON;
	this.converter.write(this.testMsg, contentType, outputMessage);

	assertEquals(contentType, outputMessage.getHeaders().getContentType());

	final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
	assertFalse("body is empty", body.isEmpty());

	Msg.Builder builder = Msg.newBuilder();
	JsonFormat.parser().merge(body, builder);
	assertEquals(this.testMsg, builder.build());

	assertNull(outputMessage.getHeaders().getFirst(
			ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER));
	assertNull(outputMessage.getHeaders().getFirst(
			ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER));
}
 
Example #5
Source File: ProtobufHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeProtobuf() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
	this.converter.write(this.testMsg, contentType, outputMessage);
	assertEquals(contentType, outputMessage.getHeaders().getContentType());
	assertTrue(outputMessage.getBodyAsBytes().length > 0);
	Message result = Msg.parseFrom(outputMessage.getBodyAsBytes());
	assertEquals(this.testMsg, result);

	String messageHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
	assertEquals("Msg", messageHeader);
	String schemaHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
	assertEquals("sample.proto", schemaHeader);
}
 
Example #6
Source File: ProtobufDecoderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void decodeChunksToMono() {
	byte[] full = this.testMsg1.toByteArray();
	byte[] chunk1 = Arrays.copyOfRange(full, 0, full.length / 2);
	byte[] chunk2 = Arrays.copyOfRange(full, chunk1.length, full.length);

	Flux<DataBuffer> input = Flux.just(chunk1, chunk2)
			.flatMap(bytes -> Mono.defer(() -> {
				DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(bytes.length);
				dataBuffer.write(bytes);
				return Mono.just(dataBuffer);
			}));

	testDecodeToMono(input, Msg.class, step -> step
			.expectNext(this.testMsg1)
			.verifyComplete());
}
 
Example #7
Source File: ProtobufJsonFormatHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
	this.converter.write(this.testMsg, contentType, outputMessage);
	assertEquals(contentType, outputMessage.getHeaders().getContentType());
	assertTrue(outputMessage.getBodyAsBytes().length > 0);
	Message result = Msg.parseFrom(outputMessage.getBodyAsBytes());
	assertEquals(this.testMsg, result);

	String messageHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
	assertEquals("Msg", messageHeader);
	String schemaHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
	assertEquals("sample.proto", schemaHeader);
}
 
Example #8
Source File: ProtobufDecoderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Test
public void decode() {
	Flux<DataBuffer> input = Flux.just(this.testMsg1, this.testMsg2)
			.flatMap(msg -> Mono.defer(() -> {
				DataBuffer buffer = this.bufferFactory.allocateBuffer();
				try {
					msg.writeDelimitedTo(buffer.asOutputStream());
					return Mono.just(buffer);
				}
				catch (IOException e) {
					release(buffer);
					return Mono.error(e);
				}
			}));

	testDecodeAll(input, Msg.class, step -> step
			.expectNext(this.testMsg1)
			.expectNext(this.testMsg2)
			.verifyComplete());
}
 
Example #9
Source File: ProtobufEncoderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@Test
public void encode() {
	Mono<Message> input = Mono.just(this.msg1);

	testEncodeAll(input, Msg.class, step -> step
			.consumeNextWith(dataBuffer -> {
				try {
					assertEquals(this.msg1, Msg.parseFrom(dataBuffer.asInputStream()));

				}
				catch (IOException ex) {
					throw new UncheckedIOException(ex);
				}
				finally {
					DataBufferUtils.release(dataBuffer);
				}
			})
			.verifyComplete());
}
 
Example #10
Source File: ProtobufDecoderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void decodeChunksToMono() {
	byte[] full = this.testMsg1.toByteArray();
	byte[] chunk1 = Arrays.copyOfRange(full, 0, full.length / 2);
	byte[] chunk2 = Arrays.copyOfRange(full, chunk1.length, full.length);

	Flux<DataBuffer> input = Flux.just(chunk1, chunk2)
			.flatMap(bytes -> Mono.defer(() -> {
				DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(bytes.length);
				dataBuffer.write(bytes);
				return Mono.just(dataBuffer);
			}));

	testDecodeToMono(input, Msg.class, step -> step
			.expectNext(this.testMsg1)
			.verifyComplete());
}
 
Example #11
Source File: ProtobufDecoderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@Test
public void decode() {
	Flux<DataBuffer> input = Flux.just(this.testMsg1, this.testMsg2)
			.flatMap(msg -> Mono.defer(() -> {
				DataBuffer buffer = this.bufferFactory.allocateBuffer();
				try {
					msg.writeDelimitedTo(buffer.asOutputStream());
					return Mono.just(buffer);
				}
				catch (IOException e) {
					release(buffer);
					return Mono.error(e);
				}
			}));

	testDecodeAll(input, Msg.class, step -> step
			.expectNext(this.testMsg1)
			.expectNext(this.testMsg2)
			.verifyComplete());
}
 
Example #12
Source File: ProtobufEncoderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Test
public void encode() {
	Mono<Message> input = Mono.just(this.msg1);

	testEncodeAll(input, Msg.class, step -> step
			.consumeNextWith(dataBuffer -> {
				try {
					assertEquals(this.msg1, Msg.parseFrom(dataBuffer.asInputStream()));

				}
				catch (IOException ex) {
					throw new UncheckedIOException(ex);
				}
				finally {
					DataBufferUtils.release(dataBuffer);
				}
			})
			.verifyComplete());
}
 
Example #13
Source File: ProtobufJsonFormatHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
	this.converter.write(this.testMsg, contentType, outputMessage);
	assertEquals(contentType, outputMessage.getHeaders().getContentType());
	assertTrue(outputMessage.getBodyAsBytes().length > 0);
	Message result = Msg.parseFrom(outputMessage.getBodyAsBytes());
	assertEquals(this.testMsg, result);

	String messageHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
	assertEquals("Msg", messageHeader);
	String schemaHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
	assertEquals("sample.proto", schemaHeader);
}
 
Example #14
Source File: ProtobufHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeJsonWithJavaFormat() throws IOException {
	this.converter = new ProtobufHttpMessageConverter(
			new ProtobufHttpMessageConverter.ProtobufJavaFormatSupport(),
			this.extensionRegistry);
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
	this.converter.write(this.testMsg, contentType, outputMessage);

	assertEquals(contentType, outputMessage.getHeaders().getContentType());

	final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
	assertFalse("body is empty", body.isEmpty());

	Msg.Builder builder = Msg.newBuilder();
	JsonFormat.parser().merge(body, builder);
	assertEquals(this.testMsg, builder.build());

	assertNull(outputMessage.getHeaders().getFirst(
			ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER));
	assertNull(outputMessage.getHeaders().getFirst(
			ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER));
}
 
Example #15
Source File: ProtobufHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeProtobuf() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
	this.converter.write(this.testMsg, contentType, outputMessage);
	assertEquals(contentType, outputMessage.getHeaders().getContentType());
	assertTrue(outputMessage.getBodyAsBytes().length > 0);
	Message result = Msg.parseFrom(outputMessage.getBodyAsBytes());
	assertEquals(this.testMsg, result);

	String messageHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
	assertEquals("Msg", messageHeader);
	String schemaHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
	assertEquals("sample.proto", schemaHeader);
}
 
Example #16
Source File: ProtobufDecoderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void exceedMaxSize() {
	this.decoder.setMaxMessageSize(1);
	Mono<DataBuffer> input = dataBuffer(this.testMsg1);

	testDecode(input, Msg.class, step -> step
			.verifyError(DecodingException.class));
}
 
Example #17
Source File: ProtobufDecoderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Mono<DataBuffer> dataBuffer(Msg msg) {
	return Mono.defer(() -> {
		byte[] bytes = msg.toByteArray();
		DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
		buffer.write(bytes);
		return Mono.just(buffer);
	});
}
 
Example #18
Source File: ProtobufDecoderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void decodeSplitChunks() {


	Flux<DataBuffer> input = Flux.just(this.testMsg1, this.testMsg2)
			.flatMap(msg -> Mono.defer(() -> {
				DataBuffer buffer = this.bufferFactory.allocateBuffer();
				try {
					msg.writeDelimitedTo(buffer.asOutputStream());
					return Mono.just(buffer);
				}
				catch (IOException e) {
					release(buffer);
					return Mono.error(e);
				}
			}))
			.flatMap(buffer -> {
				int len = buffer.readableByteCount() / 2;
				Flux<DataBuffer> result = Flux.just(
						DataBufferUtils.retain(buffer.slice(0, len)),
						DataBufferUtils
								.retain(buffer.slice(len, buffer.readableByteCount() - len))
				);
				release(buffer);
				return result;
			});

	testDecode(input, Msg.class, step -> step
			.expectNext(this.testMsg1)
			.expectNext(this.testMsg2)
			.verifyComplete());
}
 
Example #19
Source File: ProtobufEncoderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Test
public void canEncode() {
	assertTrue(this.encoder.canEncode(forClass(Msg.class), null));
	assertTrue(this.encoder.canEncode(forClass(Msg.class), PROTOBUF_MIME_TYPE));
	assertTrue(this.encoder.canEncode(forClass(Msg.class), MediaType.APPLICATION_OCTET_STREAM));
	assertFalse(this.encoder.canEncode(forClass(Msg.class), MediaType.APPLICATION_JSON));
	assertFalse(this.encoder.canEncode(forClass(Object.class), PROTOBUF_MIME_TYPE));
}
 
Example #20
Source File: ProtobufHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void canRead() {
	assertTrue(this.converter.canRead(Msg.class, null));
	assertTrue(this.converter.canRead(Msg.class, ProtobufHttpMessageConverter.PROTOBUF));
	assertTrue(this.converter.canRead(Msg.class, MediaType.APPLICATION_JSON));
	assertTrue(this.converter.canRead(Msg.class, MediaType.APPLICATION_XML));
	assertTrue(this.converter.canRead(Msg.class, MediaType.TEXT_PLAIN));
	// only supported as an output format
	assertFalse(this.converter.canRead(Msg.class, MediaType.TEXT_HTML));
}
 
Example #21
Source File: ProtobufHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void canWrite() {
	assertTrue(this.converter.canWrite(Msg.class, null));
	assertTrue(this.converter.canWrite(Msg.class, ProtobufHttpMessageConverter.PROTOBUF));
	assertTrue(this.converter.canWrite(Msg.class, MediaType.APPLICATION_JSON));
	assertTrue(this.converter.canWrite(Msg.class, MediaType.APPLICATION_XML));
	assertTrue(this.converter.canWrite(Msg.class, MediaType.TEXT_PLAIN));
	assertTrue(this.converter.canWrite(Msg.class, MediaType.TEXT_HTML));
}
 
Example #22
Source File: ProtobufJsonFormatHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void readNoContentType() throws IOException {
	byte[] body = this.testMsg.toByteArray();
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
	Message result = this.converter.read(Msg.class, inputMessage);
	assertEquals(this.testMsg, result);
}
 
Example #23
Source File: ProtobufJsonFormatHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void read() throws IOException {
	byte[] body = this.testMsg.toByteArray();
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
	inputMessage.getHeaders().setContentType(ProtobufHttpMessageConverter.PROTOBUF);
	Message result = this.converter.read(Msg.class, inputMessage);
	assertEquals(this.testMsg, result);
}
 
Example #24
Source File: ProtobufJsonFormatHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void canWrite() {
	assertTrue(this.converter.canWrite(Msg.class, null));
	assertTrue(this.converter.canWrite(Msg.class, ProtobufHttpMessageConverter.PROTOBUF));
	assertTrue(this.converter.canWrite(Msg.class, MediaType.APPLICATION_JSON));
	assertTrue(this.converter.canWrite(Msg.class, MediaType.TEXT_PLAIN));
}
 
Example #25
Source File: ProtobufJsonFormatHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void canRead() {
	assertTrue(this.converter.canRead(Msg.class, null));
	assertTrue(this.converter.canRead(Msg.class, ProtobufHttpMessageConverter.PROTOBUF));
	assertTrue(this.converter.canRead(Msg.class, MediaType.APPLICATION_JSON));
	assertTrue(this.converter.canRead(Msg.class, MediaType.TEXT_PLAIN));
}
 
Example #26
Source File: ProtobufJsonFormatHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.registryInitializer = mock(ExtensionRegistryInitializer.class);
	this.extensionRegistry = mock(ExtensionRegistry.class);
	this.converter = new ProtobufJsonFormatHttpMessageConverter(
			JsonFormat.parser(), JsonFormat.printer(), this.registryInitializer);
	this.testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
}
 
Example #27
Source File: ProtobufHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void read() throws IOException {
	byte[] body = this.testMsg.toByteArray();
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
	inputMessage.getHeaders().setContentType(ProtobufHttpMessageConverter.PROTOBUF);
	Message result = this.converter.read(Msg.class, inputMessage);
	assertEquals(this.testMsg, result);
}
 
Example #28
Source File: ProtobufHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void readNoContentType() throws IOException {
	byte[] body = this.testMsg.toByteArray();
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
	Message result = this.converter.read(Msg.class, inputMessage);
	assertEquals(this.testMsg, result);
}
 
Example #29
Source File: ProtobufHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
	this.converter.write(this.testMsg, contentType, outputMessage);
	assertEquals(contentType, outputMessage.getHeaders().getContentType());
	assertTrue(outputMessage.getBodyAsBytes().length > 0);
	Message result = Msg.parseFrom(outputMessage.getBodyAsBytes());
	assertEquals(this.testMsg, result);

	String messageHeader = outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
	assertEquals("Msg", messageHeader);
	String schemaHeader = outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
	assertEquals("sample.proto", schemaHeader);
}
 
Example #30
Source File: ProtobufEncoderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}