Итак, я попытался реализовать ResponseDefinitionTransformer, это мой текущий код:
public class Stub extends ResponseDefinitionTransformer {
FileOutputStream fos;
String path;
File file;
byte[] message;
boolean value;
@Override
public ResponseDefinition transform(Request rqst, ResponseDefinition rd, FileSource fs, Parameters prmtrs) {
message = rqst.getBody();
path = "C:/Users/xxx/Documents/NetBeansProjects/WeatherApplication/xmlFile.xml";
file = new File(path);
try {
FileOutputStream fos = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException ex) {
}
try {
fos.write(message);
} catch (IOException ex) {
}
try {
fos.flush();
} catch (IOException ex) {
}
System.out.println("File Written Successfully");
System.out.println("File Created and Written to");
value = validateXMLSchema("shiporder.xsd", "xmlFile.xml");
}
if(value){
return new ResponseDefinitionBuilder()
.withHeader("Content-Type", "application/xml")
.withStatus(200)
.withBody("Message Matched")
.build();
}else{
return new ResponseDefinitionBuilder()
.withHeader("Content-Type", "application/xml")
.withStatus(404)
.withBody("Message not Matched")
.build();
}
}
@Override
public String getName() {
return "example";
}
И затем в основном методе, это мой код, я получаю сообщение об ошибке в бите ".withTransformer". Я не уверен, как подключить ResponseDefintionTransformer к моей фактической заглушке. Кроме того, я не должен вызывать метод переопределения? Как он узнает запрос URL? Разве мне не нужно передавать это URL?
public static void main(String[] args) throws IOException, InterruptedException, SAXException, JAXBException, org.xml.sax.SAXException, UnsupportedOperationException {
Stub sub = new Stub();
WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options().port(8080));
wireMockServer.start();
stubFor(any(urlEqualTo("/user/test")).atPriority(1)
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/xml")
.withBody("XML RECIEVED")
.withTransformer("example")
)
);
}