Removes Gradle plugin's src/test/resources/contracts checking; fixes gh-1848
This commit is contained in:
@@ -16,10 +16,16 @@
|
||||
|
||||
package com.example.fraud;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.core.publisher.EmitterProcessor;
|
||||
|
||||
import org.springframework.cloud.stream.function.StreamBridge;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@@ -27,14 +33,32 @@ class MessageSender {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MessageSender.class);
|
||||
|
||||
private final EmitterProcessor<String> emitterProcessor;
|
||||
private final StreamBridge streamBridge;
|
||||
|
||||
MessageSender(EmitterProcessor<String> emitterProcessor) {
|
||||
this.emitterProcessor = emitterProcessor;
|
||||
private final byte[] expectedOutput;
|
||||
|
||||
MessageSender(StreamBridge streamBridge) {
|
||||
this.streamBridge = streamBridge;
|
||||
this.expectedOutput = forFile("/contracts/messaging/output.pdf");
|
||||
}
|
||||
|
||||
public void emit() {
|
||||
log.info("Emitting the message");
|
||||
this.emitterProcessor.onNext("{\"id\":\"99\",\"temperature\":\"123.45\"}");
|
||||
this.streamBridge.send("sensor_data-out-0", "{\"id\":\"99\",\"temperature\":\"123.45\"}");
|
||||
}
|
||||
|
||||
public void emitBytes() {
|
||||
log.info("Emitting the message");
|
||||
this.streamBridge.send("my_output-out-0", this.expectedOutput);
|
||||
}
|
||||
|
||||
private byte[] forFile(String relative) {
|
||||
URL resource = MessageSender.class.getResource(relative);
|
||||
try {
|
||||
return Files.readAllBytes(new File(resource.toURI()).toPath());
|
||||
}
|
||||
catch (IOException | URISyntaxException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.fraud;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("my_output")
|
||||
class MyProcessor implements Function<byte[], byte[]> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MyProcessor.class);
|
||||
|
||||
private final byte[] expectedInput;
|
||||
|
||||
private final byte[] expectedOutput;
|
||||
|
||||
MyProcessor() {
|
||||
this.expectedInput = forFile("/contracts/messaging/input.pdf");
|
||||
this.expectedOutput = forFile("/contracts/messaging/output.pdf");
|
||||
}
|
||||
|
||||
private byte[] forFile(String relative) {
|
||||
URL resource = MyProcessor.class.getResource(relative);
|
||||
try {
|
||||
return Files.readAllBytes(new File(resource.toURI()).toPath());
|
||||
}
|
||||
catch (IOException | URISyntaxException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] apply(byte[] payload) {
|
||||
log.info("Got the message!");
|
||||
if (!Arrays.equals(payload, this.expectedInput)) {
|
||||
log.error("Input payload size is [" + payload.length + "] and the expected one is [" + this.expectedInput.length + "]");
|
||||
throw new IllegalStateException("Wrong input");
|
||||
}
|
||||
return this.expectedOutput;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
spring.cloud.function.definition=my_output;sensor_data
|
||||
spring.cloud.stream.bindings.sensor_data-out-0.destination=sensor_data
|
||||
spring.cloud.stream.bindings.my_output-in-0.contentType=application/octet-stream
|
||||
spring.cloud.stream.bindings.my_output-in-0.destination=bytes_input
|
||||
spring.cloud.stream.bindings.my_output-out-0.contentType=application/octet-stream
|
||||
spring.cloud.stream.bindings.my_output-out-0.destination=bytes_output
|
||||
server.port=0
|
||||
server.port=0
|
||||
|
||||
@@ -42,7 +42,7 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
public abstract class MessagingBase {
|
||||
|
||||
@Autowired
|
||||
MessageSender poller;
|
||||
MessageSender messageSender;
|
||||
|
||||
@Autowired
|
||||
WebApplicationContext context;
|
||||
@@ -53,7 +53,11 @@ public abstract class MessagingBase {
|
||||
}
|
||||
|
||||
public void createSensorData() {
|
||||
poller.emit();
|
||||
messageSender.emit();
|
||||
}
|
||||
|
||||
public void createBinaryPayload() {
|
||||
messageSender.emitBytes();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
||||
Binary file not shown.
@@ -21,7 +21,7 @@ import org.springframework.cloud.contract.spec.Contract
|
||||
Contract.make {
|
||||
label("positive")
|
||||
input {
|
||||
triggeredBy("hashCode()")
|
||||
triggeredBy("createBinaryPayload()")
|
||||
}
|
||||
outputMessage {
|
||||
sentTo("bytes_output")
|
||||
|
||||
Reference in New Issue
Block a user