Search in sources :

Example 31 with MuleEventSpy

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
    }
}
Also used : ServerErrorResponseException(nl.teslanet.mule.connectors.coap.api.error.ServerErrorResponseException) Message(org.mule.runtime.api.message.Message) ResponseException(nl.teslanet.mule.connectors.coap.api.error.ResponseException) ServerErrorResponseException(nl.teslanet.mule.connectors.coap.api.error.ServerErrorResponseException) ClientErrorResponseException(nl.teslanet.mule.connectors.coap.api.error.ClientErrorResponseException) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) ResponseException(nl.teslanet.mule.connectors.coap.api.error.ResponseException) ServerErrorResponseException(nl.teslanet.mule.connectors.coap.api.error.ServerErrorResponseException) ClientErrorResponseException(nl.teslanet.mule.connectors.coap.api.error.ClientErrorResponseException) SocketException(java.net.SocketException) Test(org.junit.Test)

Example 32 with MuleEventSpy

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));
    }
}
Also used : Message(org.mule.runtime.api.message.Message) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Example 33 with MuleEventSpy

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));
    }
}
Also used : Message(org.mule.runtime.api.message.Message) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Example 34 with MuleEventSpy

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());
}
Also used : Message(org.mule.runtime.api.message.Message) ConditionTimeoutException(org.awaitility.core.ConditionTimeoutException) Event(org.mule.runtime.api.event.Event) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Example 35 with MuleEventSpy

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());
    }
}
Also used : Message(org.mule.runtime.api.message.Message) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Aggregations

MuleEventSpy (nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy)50 Test (org.junit.Test)46 Message (org.mule.runtime.api.message.Message)30 CoAPResponseAttributes (nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes)29 Event (org.mule.runtime.api.event.Event)24 Request (org.eclipse.californium.core.coap.Request)13 CoapResponse (org.eclipse.californium.core.CoapResponse)12 LinkedList (java.util.LinkedList)10 AbstractQueryParam (nl.teslanet.mule.connectors.coap.api.query.AbstractQueryParam)10 QueryParam (nl.teslanet.mule.connectors.coap.api.query.QueryParam)10 Code (org.eclipse.californium.core.coap.CoAP.Code)6 CursorStreamProvider (org.mule.runtime.api.streaming.bytes.CursorStreamProvider)5 CoapClient (org.eclipse.californium.core.CoapClient)4 SocketException (java.net.SocketException)3 OptionSet (org.eclipse.californium.core.coap.OptionSet)3 ResponseException (nl.teslanet.mule.connectors.coap.api.error.ResponseException)2 ConditionTimeoutException (org.awaitility.core.ConditionTimeoutException)2 Instant (java.time.Instant)1 CoAPRequestAttributes (nl.teslanet.mule.connectors.coap.api.CoAPRequestAttributes)1 ClientErrorResponseException (nl.teslanet.mule.connectors.coap.api.error.ClientErrorResponseException)1