use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class ResponseWithExceptionTest 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 (expectClientError || expectServerError || 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());
if (expectClientError) {
assertEquals("wrong exception cause", e.getCause().getClass(), ClientErrorResponseException.class);
} else if (expectServerError) {
assertEquals("wrong exception cause", e.getCause().getClass(), ServerErrorResponseException.class);
} else {
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 PayloadForceTest method testPayload.
// TODO BlockwiseLayer calls responseExceedsMaxBodySize(response) but does debug logging only
// this way reason of canceling is not explicit (should log error)
// TODO add check for request isCancelled
/**
* Test CoAP request with request or response payload
* @throws Exception should not happen in this test
*/
@Test(timeout = 100000)
public void testPayload() throws Exception {
MuleEventSpy spy = new MuleEventSpy(flowName);
spy.clear();
flowRunner(flowName).withVariable("path", resourcePath).withPayload(Data.getContent(requestPayloadSize)).run();
assertEquals("spy has wrong number of events", 1, spy.getEvents().size());
Message response = (Message) spy.getEvents().get(0).getContent();
byte[] payload = (byte[]) response.getPayload().getValue();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
if (expectTooLarge) {
assertFalse("request should fail", attributes.isSuccess());
assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
} else {
assertTrue("request failed", attributes.isSuccess());
assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
assertEquals("wrong response size", expectedResponsePayloadSize.intValue(), (payload == null ? -1 : payload.length));
assertTrue("wrong response payload contents", Data.validateContent(payload, expectedResponsePayloadSize));
}
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class PayloadTest method testPayload.
// TODO BlockwiseLayer calls responseExceedsMaxBodySize(response) but does debug logging only
// this way reason of canceling is not explicit (should log error)
// TODO add check for request isCancelled
/**
* Test CoAP request with request or response payload
* @throws Exception should not happen in this test
*/
@Test(timeout = 100000)
public void testPayload() throws Exception {
MuleEventSpy spy = new MuleEventSpy(flowName);
spy.clear();
flowRunner(flowName).withVariable("path", resourcePath).withPayload(Data.getContent(requestPayloadSize)).run();
assertEquals("spy has wrong number of events", 1, spy.getEvents().size());
Message response = (Message) spy.getEvents().get(0).getContent();
byte[] payload = (byte[]) response.getPayload().getValue();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
if (expectFailure) {
assertFalse("request should fail", attributes.isSuccess());
assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
} else {
assertTrue("request failed", attributes.isSuccess());
assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
assertEquals("wrong response size", expectedResponsePayloadSize.intValue(), (payload == null ? -1 : payload.length));
assertTrue("wrong response payload contents", Data.validateContent(payload, expectedResponsePayloadSize));
}
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class ExceptionHandlingTest method testUnCatchedException.
/**
* Test uncatched exception in handler
* @throws Exception should not happen in this test
*/
@Test
public void testUnCatchedException() 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", "failing_handler").run();
Message response = result.getMessage();
// let handler do its asynchronous work
try {
await().atMost(1, TimeUnit.SECONDS).until(() -> {
return false;
});
} catch (ConditionTimeoutException e) {
// as expected
}
assertEquals("spy 2 has wrongfully been called", 0, spy2.getEvents().size());
assertEquals("spy has not been called once", 1, spy3.getEvents().size());
response = (Message) spy3.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());
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class AsyncResponseTest method testResponse.
/**
* Test CoAP request
* @throws Exception should not happen in this test
*/
@Test
public void testResponse() 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", "5683").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());
if (expectedResponseCode.name().startsWith("_")) {
assertNull("wrong response code", attributes.getResponseCode());
assertNull("wrong response payload", payload);
assertEquals("wrong success flag", false, attributes.isSuccess());
} else {
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());
}
}
Aggregations