Search in sources :

Example 1 with MuleEventSpy

use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.

the class ObserveTest method testPermanentObserve.

/**
 * Test permanent observe
 * @throws Exception should not happen in this test
 */
@Test(timeout = 100000L)
public void testPermanentObserve() throws Exception {
    final MuleEventSpy spy = new MuleEventSpy("permanent");
    spy.clear();
    // let asynchronous work happen
    pauze();
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        Event result = flowRunner("do_put_permanent").withPayload(contents.get(i)).run();
        Message response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    await().atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size();
    });
    int obsOffset = 0;
    for (int i = 0; i < spy.getEvents().size(); i++) {
        Message response = (Message) spy.getEvents().get(i).getContent();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        if (i == 0)
            obsOffset = attributes.getOptions().getObserve().intValue();
        assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload());
        assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
        assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
        assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
    }
}
Also used : Message(org.mule.runtime.api.message.Message) 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 2 with MuleEventSpy

use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.

the class ObserveTest method testTemporaryObserve.

/**
 * Test temporary observe
 * @throws Exception should not happen in this test
 */
@Test(timeout = 100000L)
public void testTemporaryObserve() throws Exception {
    Event result;
    Message response;
    MuleEventSpy spy = new MuleEventSpy("temporary");
    spy.clear();
    // let asynchronous work happen
    pauze();
    assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("1st series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    assertEquals("unexpected observation after 1st test series", 0, spy.getEvents().size());
    result = flowRunner("start_temporary").withPayload("nothing_important").run();
    response = result.getMessage();
    await("missing observation start response").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1;
    });
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("2nd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    await("number of observation after 2nd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size();
    });
    result = flowRunner("stop_temporary").withPayload("nothing_important").run();
    response = result.getMessage();
    await("observation stop response").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size() + 1;
    });
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("3rd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    await("number of observation after 3rd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size() + 1;
    });
    int obsOffset = 0;
    for (int i = 1; i < contents.size(); i++) {
        response = (Message) spy.getEvents().get(i).getContent();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        if (i == 1)
            obsOffset = attributes.getOptions().getObserve().intValue() - 1;
        assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload().getValue());
        assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
        assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
        assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
    }
}
Also used : Message(org.mule.runtime.api.message.Message) 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 3 with MuleEventSpy

use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.

the class ObserveTest method testTemporaryObserveNon.

/**
 * Test temporary observe using NON requests
 * @throws Exception should not happen in this test
 */
@Test(timeout = 100000L)
public void testTemporaryObserveNon() throws Exception {
    Event result;
    Message response;
    MuleEventSpy spy = new MuleEventSpy("temporary");
    spy.clear();
    // let asynchronous work happen
    pauze();
    assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("1st series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    assertEquals("unexpected observation after 1st test series", 0, spy.getEvents().size());
    result = flowRunner("start_temporary").withPayload("nothing_important").withVariable("msgType", "NON_CONFIRMABLE").run();
    response = result.getMessage();
    await("missing observation start response").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1;
    });
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("2nd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    await("number of observation after 2nd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size();
    });
    result = flowRunner("stop_temporary").withPayload("nothing_important").withVariable("msgType", "NON_CONFIRMABLE").run();
    response = result.getMessage();
    await("observation stop response").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size() + 1;
    });
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("3rd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    await("number of observation after 3rd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size() + 1;
    });
    int obsOffset = 0;
    for (int i = 1; i < contents.size(); i++) {
        response = (Message) spy.getEvents().get(i).getContent();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        if (i == 1)
            obsOffset = attributes.getOptions().getObserve().intValue() - 1;
        assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload().getValue());
        assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
        assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
        assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
    }
}
Also used : Message(org.mule.runtime.api.message.Message) 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 4 with MuleEventSpy

use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.

the class AsyncBasicTest method testAsyncRequest.

/**
 * Test Async request
 * @throws Exception should not happen in this test
 */
@Test(timeout = 20000L)
public void testAsyncRequest() throws Exception {
    MuleEventSpy spy = new MuleEventSpy("async-handler");
    spy.clear();
    Event result = flowRunner(flowName).withPayload("nothing_important").run();
    Message response = result.getMessage();
    assertTrue("wrong response payload", response.getPayload().getDataType().isCompatibleWith(DataType.STRING));
    assertEquals("wrong response payload", "nothing_important", (String) response.getPayload().getValue());
    // let handler do its asynchronous work
    await().atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1;
    });
    // assertions...
    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", expectedRequestCode.name(), attributes.getRequestCode());
    assertEquals("wrong request uri", expectedRequestUri, attributes.getRequestUri());
    assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
    assertArrayEquals("wrong response payload", (expectedPayload == null ? null : expectedPayload.getBytes(Defs.COAP_CHARSET)), (byte[]) response.getPayload().getValue());
}
Also used : Message(org.mule.runtime.api.message.Message) 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 5 with MuleEventSpy

use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.

the class AsyncDynamicUriBasicTest method testAsyncRequest.

/**
 * Test Async request
 * @throws Exception should not happen in this test
 */
@Test(timeout = 15000L)
public void testAsyncRequest() throws Exception {
    MuleEventSpy spy = new MuleEventSpy("handler_spy");
    spy.clear();
    Event result = flowRunner(flowName).withPayload("nothing_important").withVariable("code", requestCode).withVariable("host", host).withVariable("port", port).withVariable("path", path).run();
    Message response = result.getMessage();
    assertTrue("wrong response payload", response.getPayload().getDataType().isCompatibleWith(DataType.STRING));
    assertEquals("wrong response payload", "nothing_important", (String) response.getPayload().getValue());
    // let handler do its asynchronous work
    await().atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1;
    });
    // assertions
    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) 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)

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