Skip to content

Commit

Permalink
Refactor DtlsSessionSuspensionFilter to DtlsSessionSuspensionService (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
akolosov-n authored Aug 22, 2024
1 parent 750bbc2 commit 55f3966
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,20 @@
package org.opencoap.transport.mbedtls;

import static com.mbed.coap.transport.TransportContext.NON_CONFIRMABLE;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.opencoap.transport.mbedtls.DtlsTransportContext.DTLS_SESSION_SUSPENSION_HINT;
import com.mbed.coap.packet.CoapRequest;
import com.mbed.coap.packet.CoapResponse;
import com.mbed.coap.transport.TransportContext;
import com.mbed.coap.utils.Filter;
import com.mbed.coap.utils.Service;
import java.util.concurrent.CompletableFuture;

public class DtlsSessionSuspensionFilter implements Filter.SimpleFilter<CoapRequest, CoapResponse> {

public class DtlsSessionSuspensionService implements Service<CoapRequest, CoapResponse> {
@Override
public CompletableFuture<CoapResponse> apply(CoapRequest request, Service<CoapRequest, CoapResponse> service) {
public CompletableFuture<CoapResponse> apply(CoapRequest request) {
if (!request.getTransContext(NON_CONFIRMABLE)) {
return CoapResponse.badRequest().toFuture();
}

return service
.apply(request)
.thenCompose(resp -> completedFuture(resp.withContext(TransportContext.of(DTLS_SESSION_SUSPENSION_HINT, true))));
return CoapResponse.ok().addContext(TransportContext.of(DTLS_SESSION_SUSPENSION_HINT, true)).toFuture();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.opencoap.transport.mbedtls;

import static com.mbed.coap.packet.CoapResponse.of;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.junit.jupiter.api.Assertions.*;
import com.mbed.coap.packet.CoapRequest;
import com.mbed.coap.packet.CoapResponse;
Expand All @@ -25,8 +24,8 @@
import com.mbed.coap.utils.Service;
import org.junit.jupiter.api.Test;

class DtlsSessionExpirationFilterTest {
private final Service<CoapRequest, CoapResponse> service = (new DtlsSessionSuspensionFilter()).then(coapRequest -> completedFuture(of(Code.C201_CREATED)));
class DtlsSessionSuspensionServiceTest {
private final Service<CoapRequest, CoapResponse> service = new DtlsSessionSuspensionService();

@Test
void shouldReturnBadRequestWhenRequestIsConfirmable() {
Expand All @@ -36,7 +35,7 @@ void shouldReturnBadRequestWhenRequestIsConfirmable() {
@Test
void shouldReturnResponseWithExpirationHint() {
CoapResponse resp = service.apply(CoapRequest.get("/test").context(TransportContext.of(TransportContext.NON_CONFIRMABLE, true)).build()).join();
assertEquals(Code.C201_CREATED, resp.getCode());
assertEquals(Code.C205_CONTENT, resp.getCode());
assertTrue(resp.getTransContext().get(DtlsTransportContext.DTLS_SESSION_SUSPENSION_HINT));
}
}

0 comments on commit 55f3966

Please sign in to comment.