Search in sources :

Example 6 with MuleEventSpy

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
}
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 7 with MuleEventSpy

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));
}
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 8 with MuleEventSpy

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
    }
}
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) ResponseException(nl.teslanet.mule.connectors.coap.api.error.ResponseException) SocketException(java.net.SocketException) Test(org.junit.Test)

Example 9 with MuleEventSpy

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());
}
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 10 with MuleEventSpy

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);
}
Also used : CoapResponse(org.eclipse.californium.core.CoapResponse) Request(org.eclipse.californium.core.coap.Request) 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