use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class AsyncNoResponseTest method testNoResponse.
/**
* Test CoAP request that receives no response.
* @throws Exception should not happen in this test
*/
@Test
public void testNoResponse() throws Exception {
MuleEventSpy spy = new MuleEventSpy("handler_spy");
spy.clear();
flowRunner("do_request").withPayload("nothing_important").withVariable("code", requestCode).withVariable("host", "127.0.0.1").withVariable("port", "999").withVariable("path", resourcePath).run();
// let handler do its asynchronous work
await().atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == 1;
});
Message response = (Message) spy.getEvents().get(0).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
byte[] payload = (byte[]) (response.getPayload().getValue());
assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
assertEquals("wrong response payload", expectedResponsePayload, payload);
assertFalse("wrong success flag", attributes.isSuccess());
// TODO test for property clienterror, servererror
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class DynamicUriBasicTest method testRequest.
/**
* Test CoAP request
* @throws Exception should not happen in this test
*/
@Test
public void testRequest() throws Exception {
MuleEventSpy spy = new MuleEventSpy("do_request");
spy.clear();
flowRunner(flowName).withVariable("code", requestCode).withVariable("host", host).withVariable("port", port).withVariable("path", path).withPayload("nothing_important").run();
Message response = (Message) spy.getEvents().get(0).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("wrong request code", requestCode, attributes.getRequestCode());
assertEquals("wrong request uri", expectedRequestUri, attributes.getRequestUri());
assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
byte[] responsePayload = (byte[]) TypedValue.unwrap(response.getPayload());
assertEquals("wrong response payload", expectedPayload, responsePayload == null ? "" : new String(responsePayload, Defs.COAP_CHARSET));
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class ResponseTest method testResponse.
/**
* Test CoAP request
* @throws Exception should not happen in this test
*/
@Test
public void testResponse() throws Exception {
MuleEventSpy spy = new MuleEventSpy("do_request");
spy.clear();
if (expectedResponseCode.name().startsWith("_")) {
Exception e = assertThrows(Exception.class, () -> flowRunner("do_request").withPayload("nothing_important").withVariable("code", requestCode).withVariable("host", "127.0.0.1").withVariable("port", "5683").withVariable("path", resourcePath).run());
assertEquals("wrong exception message", "CoAP Client { config } failed to execute request.", e.getMessage());
assertEquals("wrong exception cause", e.getCause().getClass(), ResponseException.class);
} else {
flowRunner("do_request").withPayload("nothing_important").withVariable("code", requestCode).withVariable("host", "127.0.0.1").withVariable("port", "5683").withVariable("path", resourcePath).run();
assertEquals("spy has not been called once", 1, spy.getEvents().size());
Message response = (Message) spy.getEvents().get(0).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
byte[] payload = (byte[]) (response.getPayload().getValue());
assertEquals("wrong response code", expectedResponseCode.name(), attributes.getResponseCode());
assertEquals("wrong response payload", expectedResponsePayload, new String(payload));
assertEquals("wrong success flag", ResponseCode.isSuccess(expectedResponseCode), attributes.isSuccess());
// TODO test for property clienterror, servererror
}
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class ExceptionHandlingTest method testCatchedException.
/**
* Test catching exception in handler
* @throws Exception should not happen in this test
*/
@Test
public void testCatchedException() throws Exception {
MuleEventSpy spy1 = new MuleEventSpy("spy-me1");
spy1.clear();
MuleEventSpy spy2 = new MuleEventSpy("spy-me2");
spy2.clear();
MuleEventSpy spy3 = new MuleEventSpy("spy-me3");
spy3.clear();
Event result = flowRunner(flowName).withPayload("nothing_important").withVariable("code", expectedRequestCode.name()).withVariable("host", host).withVariable("port", port).withVariable("path", path).withVariable("handler", "catching_handler").run();
Message response = result.getMessage();
// let handler do its asynchronous work
await().atMost(10, TimeUnit.SECONDS).until(() -> {
return spy1.getEvents().size() == 1 && spy2.getEvents().size() == 1;
});
response = (Message) spy1.getEvents().get(0).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("wrong request code", expectedRequestCode.name(), attributes.getRequestCode());
assertEquals("wrong request uri", expectedRequestUri, attributes.getRequestUri());
assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
assertArrayEquals("wrong response payload", expectedPayload, (byte[]) response.getPayload().getValue());
response = (Message) spy2.getEvents().get(0).getContent();
// message containing message!!
response = (Message) response.getPayload().getValue();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("wrong request code", expectedRequestCode.name(), attributes.getRequestCode());
assertEquals("wrong request uri", expectedRequestUri, attributes.getRequestUri());
assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
assertArrayEquals("wrong response payload", expectedPayload, (byte[]) response.getPayload().getValue());
// wait some more time and see that spy 3 is really not called
try {
await().atMost(1, TimeUnit.SECONDS);
} catch (ConditionTimeoutException e) {
// as expected
}
assertEquals("spy 3 has wrongfully been called", 0, spy3.getEvents().size());
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class PayloadTest method testOutboundRequestEarlyNegotiation.
@Test(timeout = 20000L)
public void testOutboundRequestEarlyNegotiation() throws URISyntaxException, ConnectorException, IOException {
// mockResponseMessage();
MuleEventSpy spy = new MuleEventSpy(spyId, null, Data.getContent(contentSize));
spy.clear();
client.useEarlyNegotiation(32);
Request request = new Request(requestCode);
if (unintendedPayload)
request.setUnintendedPayload();
request.setPayload("nothing important");
CoapResponse response = client.advanced(request);
validateOutboundResponse(response, spy);
}
Aggregations