Skip to content

Commit

Permalink
#73 Add context modifier to CoapRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Nov 20, 2023
1 parent fa79e86 commit ffb8f57
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 6 additions & 0 deletions coap-core/src/main/java/com/mbed/coap/packet/CoapRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.time.Duration;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;

public final class CoapRequest {
private final Method method;
Expand Down Expand Up @@ -187,6 +188,11 @@ public CoapRequest withAddress(InetSocketAddress newPeerAddress) {
return new CoapRequest(method, token, options, payload, newPeerAddress, transContext);
}

public CoapRequest withContext(Function<TransportContext, TransportContext> contextFunc) {
TransportContext newTransContext = contextFunc.apply(transContext);
return new CoapRequest(method, token, options, payload, peerAddress, newTransContext);
}

public static class Builder {
private final Method method;
private Opaque token = Opaque.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import static com.mbed.coap.packet.MediaTypes.CT_APPLICATION_JSON;
import static com.mbed.coap.packet.Opaque.EMPTY;
import static com.mbed.coap.packet.Opaque.decodeHex;
import static com.mbed.coap.transport.TransportContext.RESPONSE_TIMEOUT;
import static java.time.Duration.ofSeconds;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -41,6 +43,7 @@
import org.junit.jupiter.api.Test;

class CoapRequestTest {
private static final TransportContext.Key<String> DUMMY_KEY = new TransportContext.Key<>(null);

@Test
void shouldCreatePing() {
Expand Down Expand Up @@ -87,6 +90,17 @@ void testToString() {
assertEquals("CoapRequest[PING]", CoapRequest.ping(LOCAL_5683, TransportContext.EMPTY).toString());
}

@Test
void shouldModifyTransportContext() {
CoapRequest request = CoapRequest.delete("/test").token(1023).build();

// when
CoapRequest request2 = request.withContext(ctx -> ctx.with(DUMMY_KEY, "test"));

// then
assertEquals("test", request2.getTransContext(DUMMY_KEY));
}

@Test
public void equalsAndHashTest() {
EqualsVerifier.forClass(CoapRequest.class).suppress(Warning.NONFINAL_FIELDS)
Expand Down Expand Up @@ -115,9 +129,17 @@ public void buildWithAllPossibleFields() {
.size1(342)
.observe()
.payload("perse", MediaTypes.CT_TEXT_PLAIN)
.context(RESPONSE_TIMEOUT, ofSeconds(12))
.context(DUMMY_KEY, "test")
.from(LOCAL_5683);

CoapRequest expected = new CoapRequest(Method.GET, Opaque.ofBytes(0xB1, 0x97), new HeaderOptions(), Opaque.of("perse"), LOCAL_5683, TransportContext.EMPTY);
CoapRequest expected = new CoapRequest(
Method.GET,
Opaque.ofBytes(0xB1, 0x97),
new HeaderOptions(), Opaque.of("perse"),
LOCAL_5683,
TransportContext.of(RESPONSE_TIMEOUT, ofSeconds(12)).with(DUMMY_KEY, "test")
);
expected.options().setUriPath("/0/1/2");
expected.options().setAccept(CT_APPLICATION_JSON);
expected.options().setObserve(0);
Expand Down

0 comments on commit ffb8f57

Please sign in to comment.