Search in sources :

Example 1 with Type

use of org.eclipse.californium.core.coap.CoAP.Type in project hono by eclipse.

the class ResourceTestBase method newCoapExchange.

static CoapExchange newCoapExchange(final Buffer payload, final Type requestType, final OptionSet options) {
    final Request request = mock(Request.class);
    when(request.getType()).thenReturn(requestType);
    when(request.isConfirmable()).thenReturn(requestType == Type.CON);
    when(request.getOptions()).thenReturn(options);
    final Exchange exchange = new Exchange(request, Origin.REMOTE, mock(Executor.class));
    final CoapExchange coapExchange = mock(CoapExchange.class);
    when(coapExchange.advanced()).thenReturn(exchange);
    Optional.ofNullable(payload).ifPresent(b -> when(coapExchange.getRequestPayload()).thenReturn(b.getBytes()));
    when(coapExchange.getRequestOptions()).thenReturn(options);
    when(coapExchange.getQueryParameter(anyString())).thenAnswer(invocation -> {
        final String key = invocation.getArgument(0);
        return options.getUriQuery().stream().map(param -> param.split("=", 2)).filter(keyValue -> key.equals(keyValue[0])).findFirst().map(keyValue -> keyValue.length < 2 ? Boolean.TRUE.toString() : keyValue[1]).orElse(null);
    });
    return coapExchange;
}
Also used : CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Exchange(org.eclipse.californium.core.network.Exchange) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Exchange(org.eclipse.californium.core.network.Exchange) HashMap(java.util.HashMap) Constants(org.eclipse.hono.util.Constants) Origin(org.eclipse.californium.core.network.Exchange.Origin) MessagingType(org.eclipse.hono.util.MessagingType) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TelemetryExecutionContext(org.eclipse.hono.util.TelemetryExecutionContext) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Type(org.eclipse.californium.core.coap.CoAP.Type) Executor(java.util.concurrent.Executor) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) Request(org.eclipse.californium.core.coap.Request) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Endpoint(org.eclipse.californium.core.network.Endpoint) Optional(java.util.Optional) OptionSet(org.eclipse.californium.core.coap.OptionSet) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Executor(java.util.concurrent.Executor) Request(org.eclipse.californium.core.coap.Request) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange)

Example 2 with Type

use of org.eclipse.californium.core.coap.CoAP.Type in project hono by eclipse.

the class CoapProtocolAdapterMockSupport method newCoapExchange.

static CoapExchange newCoapExchange(final Buffer payload, final Type requestType, final OptionSet options) {
    final Request request = mock(Request.class);
    when(request.getType()).thenReturn(requestType);
    when(request.isConfirmable()).thenReturn(requestType == Type.CON);
    when(request.getOptions()).thenReturn(options);
    final Exchange exchange = new Exchange(request, Origin.REMOTE, mock(Executor.class));
    final CoapExchange coapExchange = mock(CoapExchange.class);
    when(coapExchange.advanced()).thenReturn(exchange);
    Optional.ofNullable(payload).ifPresent(b -> when(coapExchange.getRequestPayload()).thenReturn(b.getBytes()));
    when(coapExchange.getRequestOptions()).thenReturn(options);
    when(coapExchange.getQueryParameter(anyString())).thenAnswer(invocation -> {
        final String key = invocation.getArgument(0);
        return options.getUriQuery().stream().map(param -> param.split("=", 2)).filter(keyValue -> key.equals(keyValue[0])).findFirst().map(keyValue -> keyValue.length < 2 ? Boolean.TRUE.toString() : keyValue[1]).orElse(null);
    });
    return coapExchange;
}
Also used : CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Exchange(org.eclipse.californium.core.network.Exchange) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) BeforeEach(org.junit.jupiter.api.BeforeEach) Executor(java.util.concurrent.Executor) Exchange(org.eclipse.californium.core.network.Exchange) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) Future(io.vertx.core.Future) Origin(org.eclipse.californium.core.network.Exchange.Origin) Request(org.eclipse.californium.core.coap.Request) Buffer(io.vertx.core.buffer.Buffer) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Optional(java.util.Optional) Span(io.opentracing.Span) OptionSet(org.eclipse.californium.core.coap.OptionSet) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) ProtocolAdapterMockSupport(org.eclipse.hono.adapter.test.ProtocolAdapterMockSupport) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Type(org.eclipse.californium.core.coap.CoAP.Type) Executor(java.util.concurrent.Executor) Request(org.eclipse.californium.core.coap.Request) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange)

Aggregations

Future (io.vertx.core.Future)2 Buffer (io.vertx.core.buffer.Buffer)2 Optional (java.util.Optional)2 Executor (java.util.concurrent.Executor)2 Type (org.eclipse.californium.core.coap.CoAP.Type)2 OptionSet (org.eclipse.californium.core.coap.OptionSet)2 Request (org.eclipse.californium.core.coap.Request)2 Exchange (org.eclipse.californium.core.network.Exchange)2 Origin (org.eclipse.californium.core.network.Exchange.Origin)2 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)2 VertxMockSupport (org.eclipse.hono.test.VertxMockSupport)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.when (org.mockito.Mockito.when)2 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 HashMap (java.util.HashMap)1