Search in sources :

Example 16 with MuleEventSpy

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

the class QueryTest method testFixedAsyncRequest.

/**
 * Test Async request with fixed query parameters.
 * @throws Exception should not happen in this test
 */
@Test
public void testFixedAsyncRequest() throws Exception {
    MuleEventSpy spy = new MuleEventSpy("query-handler");
    spy.clear();
    LinkedList<QueryParam> params = new LinkedList<>();
    params.add(new QueryParam("query1", "one"));
    params.add(new QueryParam("query2", "two"));
    params.add(new QueryParam("query3", null));
    LinkedList<QueryParam> expectedParams = new LinkedList<>();
    expectedParams.add(new QueryParam("test3", "three"));
    expectedParams.add(new QueryParam("test4", "four"));
    expectedParams.add(new QueryParam("novalue2", null));
    Event result = flowRunner("request-async-fixed").withPayload("nothing_important").withVariable("code", requestCode.toString()).withVariable("query", params).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();
    assertTrue("error response", attributes.isSuccess());
    assertEquals("wrong request uri", expectedRequestUri(expectedParams), attributes.getRequestUri());
    assertArrayEquals("wrong response payload", expectedPayload(requestCode, expectedParams).getBytes(Defs.COAP_CHARSET), (byte[]) response.getPayload().getValue());
}
Also used : Message(org.mule.runtime.api.message.Message) AbstractQueryParam(nl.teslanet.mule.connectors.coap.api.query.AbstractQueryParam) QueryParam(nl.teslanet.mule.connectors.coap.api.query.QueryParam) Event(org.mule.runtime.api.event.Event) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 17 with MuleEventSpy

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

the class QueryTest method testRequestAsyncFixedWithDefaults.

/**
 * Test Async request with fixed and default query parameters.
 * @throws Exception should not happen in this test
 */
@Test
public void testRequestAsyncFixedWithDefaults() throws Exception {
    MuleEventSpy spy = new MuleEventSpy("query-handler");
    spy.clear();
    LinkedList<QueryParam> params = new LinkedList<>();
    params.add(new QueryParam("query1", "one"));
    params.add(new QueryParam("query2", "two"));
    params.add(new QueryParam("query3", null));
    LinkedList<QueryParam> expectedParams = new LinkedList<>();
    expectedParams.add(new QueryParam("test1", "one"));
    expectedParams.add(new QueryParam("test2", "two"));
    expectedParams.add(new QueryParam("novalue1", null));
    expectedParams.add(new QueryParam("test3", "three"));
    expectedParams.add(new QueryParam("test4", "four"));
    expectedParams.add(new QueryParam("novalue2", null));
    Event result = flowRunner("request-async-fixed-with-defaults").withPayload("nothing_important").withVariable("code", requestCode.toString()).withVariable("query", params).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();
    assertTrue("error response", attributes.isSuccess());
    assertEquals("wrong request uri", expectedRequestUri(expectedParams), attributes.getRequestUri());
    assertArrayEquals("wrong response payload", expectedPayload(requestCode, expectedParams).getBytes(Defs.COAP_CHARSET), (byte[]) response.getPayload().getValue());
}
Also used : Message(org.mule.runtime.api.message.Message) AbstractQueryParam(nl.teslanet.mule.connectors.coap.api.query.AbstractQueryParam) QueryParam(nl.teslanet.mule.connectors.coap.api.query.QueryParam) Event(org.mule.runtime.api.event.Event) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 18 with MuleEventSpy

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

the class VirtualHostTestCase method testDefaultVirtualHostOverrideRequest.

/**
 * Test inbound property
 * @throws Exception should not happen in this test
 */
@Test
public void testDefaultVirtualHostOverrideRequest() throws Exception {
    MuleEventSpy spy = new MuleEventSpy("6768");
    spy.clear();
    Event result = flowRunner("do_request_to_virtual_host_override").withPayload("nothing_important").withVariable("code", requestCode.name()).withVariable("host", host).withVariable("port", port).withVariable("endpointHost", endpointHost).withVariable("endpointPort", endpointPort).withVariable("path", path).run();
    Message response = result.getMessage();
    assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
    CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
    assertEquals("wrong response code", expectedResponseCode.name(), attributes.getResponseCode());
    MuleEventSpy.Event event = spy.getEvents().get(0);
    assertEquals("wrong options class", OptionSet.class, event.getContent().getClass());
    OptionSet options = (OptionSet) event.getContent();
    assertEquals("wrong uri host", host, options.getUriHost());
    assertEquals("wrong uri port", port, options.getUriPort());
}
Also used : Message(org.mule.runtime.api.message.Message) Event(org.mule.runtime.api.event.Event) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) OptionSet(org.eclipse.californium.core.coap.OptionSet) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Example 19 with MuleEventSpy

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

the class VirtualHostTestCase method testDefaultVirtualHostRequest.

/**
 * Test inbound property
 * @throws Exception should not happen in this test
 */
@Test
public void testDefaultVirtualHostRequest() throws Exception {
    MuleEventSpy spy = new MuleEventSpy("6767");
    spy.clear();
    Event result = flowRunner("do_request_to_virtual_host").withPayload("nothing_important").withVariable("code", requestCode.name()).withVariable("host", host).withVariable("port", port).withVariable("endpointHost", endpointHost).withVariable("endpointPort", endpointPort).withVariable("path", path).run();
    Message response = result.getMessage();
    assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
    CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
    assertEquals("wrong response code", expectedResponseCode.name(), attributes.getResponseCode());
    MuleEventSpy.Event event = spy.getEvents().get(0);
    assertEquals("wrong options class", OptionSet.class, event.getContent().getClass());
    OptionSet options = (OptionSet) event.getContent();
    assertEquals("wrong uri host", host, options.getUriHost());
    assertEquals("wrong uri port", port, options.getUriPort());
}
Also used : Message(org.mule.runtime.api.message.Message) Event(org.mule.runtime.api.event.Event) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) OptionSet(org.eclipse.californium.core.coap.OptionSet) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Example 20 with MuleEventSpy

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

the class MulticastIPv4Test method testMulticast.

@Test(timeout = 20000L)
public void testMulticast() throws Exception {
    MultiCoapHandler handler = new MultiCoapHandler();
    // spyRequestMessage();
    MuleEventSpy spy = new MuleEventSpy(spyId);
    spy.clear();
    // spyActivated.set( false );
    client.useLateNegotiation();
    Request request = new Request(requestCode, Type.NON);
    if (unintendedPayload)
        request.setUnintendedPayload();
    request.setPayload(Data.getContent(contentSize));
    client.advanced(handler, request);
    while (handler.waitOn(15000)) ;
    validateInboundResponse(handler.getResponse(), spy);
}
Also used : 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