Search in sources :

Example 26 with MuleEventSpy

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

the class AbstractOutboundPropertyTestcase method mockMessage.

/**
 * Mock that sets the outbound property in the Mule flow
 * @param propertyName name of the outbound property to set
 * @param propertyValue value to set on the property
 */
private MuleEventSpy mockMessage(String propertyName, Object propertyValue) {
    MuleEventSpy spy = new MuleEventSpy(propertyName, propertyName, propertyValue);
    spy.clear();
    return spy;
}
Also used : MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy)

Example 27 with MuleEventSpy

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

the class AsyncMulticastTest method testAsyncRequest.

/**
 * Test Async request
 * @throws Exception should not happen in this test
 */
@Test
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
    if (expectedResponseCode == null) {
        Instant start = Instant.now();
        await().atMost(20, TimeUnit.SECONDS).until(() -> {
            return Instant.now().isAfter(start.plusSeconds(15L));
        });
        assertEquals("should not receive response on multicast", 0, spy.getEvents().size());
    } else {
        await().atMost(15, TimeUnit.SECONDS).until(() -> {
            return spy.getEvents().size() == 1;
        });
        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, (byte[]) response.getPayload().getValue());
    }
}
Also used : Message(org.mule.runtime.api.message.Message) Instant(java.time.Instant) 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 28 with MuleEventSpy

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

the class ObserveTest method testObserveReregistration1.

/**
 * Test observe re-registration after max_age
 * @throws Exception should not happen in this test
 */
@Test(timeout = 100000L)
public void testObserveReregistration1() throws Exception {
    @SuppressWarnings("unused") Event result;
    MuleEventSpy spy = new MuleEventSpy("maxage1_nonotify");
    spy.clear();
    // let asynchronous work happen
    pauze();
    // check clean start
    assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
    result = flowRunner("start_maxage1_nonotify").withPayload("nothing_important").run();
    // five notifications expected, period= max_age + notificationReregistrationBackoff per notification, plus margin
    pauze(5000 + 500 + 100);
    result = flowRunner("stop_maxage1_nonotify").withPayload("nothing_important").run();
    // GET observe=0 response + notification= 1 + 5
    await("number of notifications received 1").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1 + 5 + 1;
    });
    // wait some more
    pauze(5000 + 500 + 100);
    // stop result is also handled -> 1+5+1 times
    await("number of notifications received 2").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1 + 5 + 1;
    });
}
Also used : Event(org.mule.runtime.api.event.Event) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Example 29 with MuleEventSpy

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

the class ObserveTest method testObserveReregistration4.

/**
 * Test observe re-registration after max_age
 * @throws Exception should not happen in this test
 */
@Test(timeout = 100000L)
public void testObserveReregistration4() throws Exception {
    @SuppressWarnings("unused") Event result;
    MuleEventSpy spy = new MuleEventSpy("maxage4_nonotify");
    spy.clear();
    // let asynchronous work happen
    pauze();
    // check clean start
    assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
    result = flowRunner("start_maxage4_nonotify").withPayload("nothing_important").run();
    // five notifications expected, period= max_age + notificationReregistrationBackoff per notification, plus margin
    pauze(5 * (4 * 1000 + 100) + 500);
    result = flowRunner("stop_maxage4_nonotify").withPayload("nothing_important").run();
    // GET observe=0 response + notification= 1 + 5
    await("number of notifications received 1").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1 + 5 + 1;
    });
    // five notifications expected, period= max_age + notificationReregistrationBackoff per notification, plus margin
    pauze(5 * (4 * 1000 + 100) + 500);
    // GET observe=0 response + notification= 1 + 5
    await("number of notifications received 2").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1 + 5 + 1;
    });
}
Also used : Event(org.mule.runtime.api.event.Event) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Example 30 with MuleEventSpy

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

the class ObserveTest method testObserveNotifications.

// TODO warn when not existing observe is stopped
/**
 * Test observe notifications at max_age
 * @throws Exception should not happen in this test
 */
@Test(timeout = 100000L)
public void testObserveNotifications() throws Exception {
    @SuppressWarnings("unused") Event result;
    MuleEventSpy spy = new MuleEventSpy("maxage1");
    spy.clear();
    // let asynchronous work happen
    pauze();
    assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
    result = flowRunner("start_maxage1").withPayload("nothing_important").run();
    pauze(5500);
    result = flowRunner("stop_maxage1").withPayload("nothing_important").run();
    // GET observe=0 response + notifications= 1+5+1
    await("number of notifications received").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1 + 5 + 1;
    });
    pauze(5500);
    // GET observe=0 response + notifications= 1+5
    // stop result is also handled -> 1+5+1 times
    await("number of notifications received").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1 + 5 + 1;
    });
}
Also used : Event(org.mule.runtime.api.event.Event) 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