Search in sources :

Example 11 with MuleEventSpy

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

the class AbstractSecureClientTestCase method testOutbound.

@Test
public void testOutbound() throws Exception {
    expectException();
    String payload = "test-payload";
    MuleEventSpy spy = spyMessage(payload.getBytes());
    for (Code call : outboundCalls) {
        spy.clear();
        Request request = new Request(call);
        request.setURI(uri.resolve(getPath(call)));
        switch(call) {
            case DELETE:
            case GET:
                request.setUnintendedPayload();
                break;
            default:
                break;
        }
        request.setPayload("nothing important");
        CoapResponse response = client.advanced(request);
        assertNotNull("get gave no response", response);
        assertTrue("response indicates failure: " + response.getCode() + " msg: " + response.getResponseText(), response.isSuccess());
        assertArrayEquals("wrong payload in response", payload.getBytes(), response.getPayload());
        assertEquals("wrong number of spied events", 1, spy.getEvents().size());
        client.shutdown();
    }
}
Also used : CoapResponse(org.eclipse.californium.core.CoapResponse) Request(org.eclipse.californium.core.coap.Request) Code(org.eclipse.californium.core.coap.CoAP.Code) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Example 12 with MuleEventSpy

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

the class AbstractSecureClientTestCase method spyMessage.

/**
 * Create spy for requests with a message replacement
 * @return the spy
 */
private MuleEventSpy spyMessage(byte[] replacement) {
    MuleEventSpy spy = new MuleEventSpy("securityTest", null, replacement);
    spy.clear();
    return spy;
}
Also used : MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy)

Example 13 with MuleEventSpy

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

the class InsecureClientTest method testInsecureClientDelete.

@Test()
public void testInsecureClientDelete() throws Exception {
    MuleEventSpy spy = spyMessage();
    Code call = Code.DELETE;
    CoapClient client = getClient(getPath(call));
    Request request = new Request(call);
    CoapResponse response = client.advanced(request);
    assertNull("should not receive a response", response);
    assertSpy(spy);
    client.shutdown();
}
Also used : CoapResponse(org.eclipse.californium.core.CoapResponse) Request(org.eclipse.californium.core.coap.Request) Code(org.eclipse.californium.core.coap.CoAP.Code) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) CoapClient(org.eclipse.californium.core.CoapClient) Test(org.junit.Test)

Example 14 with MuleEventSpy

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

the class QueryTest method testSyncRequest.

/**
 * Test Sync request with query parameters.
 * @throws Exception should not happen in this test
 */
@Test
public void testSyncRequest() 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));
    Event result = flowRunner("request-sync").keepStreamsOpen().withPayload("nothing_important").withVariable("code", requestCode.toString()).withVariable("query", params).run();
    Message response = result.getMessage();
    // assertions...
    // assertTrue( "wrong response payload type", response.getPayload().getDataType().isCompatibleWith( DataType.BYTE_ARRAY ) );
    assertNotNull("no mule event", response);
    assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
    CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
    assertTrue("request failed", attributes.isSuccess());
    assertEquals("wrong request uri", expectedRequestUri(params), attributes.getRequestUri());
    CursorStreamProvider responsePayload = (CursorStreamProvider) TypedValue.unwrap(response.getPayload());
    assertArrayEquals("wrong response payload", expectedPayload(requestCode, params).getBytes(Defs.COAP_CHARSET), IOUtils.toByteArray(responsePayload.openCursor()));
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) 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 15 with MuleEventSpy

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

the class QueryTest method testRequestSyncWithDefaults1.

/**
 * Test Sync request with default query parameters.
 * @throws Exception should not happen in this test
 */
@Test
public void testRequestSyncWithDefaults1() 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("query1", "one"));
    expectedParams.add(new QueryParam("query2", "two"));
    expectedParams.add(new QueryParam("query3", null));
    Event result = flowRunner("request-sync-with-defaults").keepStreamsOpen().withPayload("nothing_important").withVariable("code", requestCode.toString()).withVariable("query", params).run();
    Message response = result.getMessage();
    // assertions...
    // assertTrue( "wrong response payload type", response.getPayload().getDataType().isCompatibleWith( DataType.BYTE_ARRAY ) );
    assertNotNull("no mule event", response);
    assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
    CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
    assertTrue("request failed", attributes.isSuccess());
    assertEquals("wrong request uri", expectedRequestUri(expectedParams), attributes.getRequestUri());
    CursorStreamProvider responsePayload = (CursorStreamProvider) TypedValue.unwrap(response.getPayload());
    assertArrayEquals("wrong response payload", expectedPayload(requestCode, expectedParams).getBytes(Defs.COAP_CHARSET), IOUtils.toByteArray(responsePayload.openCursor()));
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) 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)

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