Skip to content

Commit a98a153

Browse files
committed
Refactor CoapResponse with builder pattern
resp
1 parent a247c83 commit a98a153

49 files changed

Lines changed: 386 additions & 197 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

coap-cli/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies {
1717
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.0")
1818
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.0")
1919
testImplementation("org.awaitility:awaitility:4.2.0")
20+
testImplementation(testFixtures(project(":coap-core")))
2021
}
2122

2223
tasks {

coap-cli/src/main/java/com/mbed/coap/cli/DeviceEmulator.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package com.mbed.coap.cli;
1818

19-
import static java.util.concurrent.CompletableFuture.completedFuture;
2019
import com.mbed.coap.client.RegistrationManager;
2120
import com.mbed.coap.packet.CoapRequest;
2221
import com.mbed.coap.packet.CoapResponse;
@@ -77,20 +76,20 @@ public Integer call() throws Exception {
7776
}
7877

7978
protected Service<CoapRequest, CoapResponse> createRouting() {
80-
Service<CoapRequest, CoapResponse> timeResource = __ -> completedFuture(CoapResponse.ok(Instant.now().toString()));
79+
Service<CoapRequest, CoapResponse> timeResource = __ -> CoapResponse.ok(Instant.now().toString()).toFuture();
8180

8281
scheduledExecutor.scheduleAtFixedRate(() ->
8382
obsManager.sendObservation("/time", timeResource),
8483
30, 30, TimeUnit.SECONDS
8584
);
8685

8786
return RouterService.builder()
88-
.get("/3/0/1", __ -> completedFuture(CoapResponse.ok("Acme")))
89-
.get("/3/0/2", __ -> completedFuture(CoapResponse.ok("Emulator")))
90-
.get("/3/0/3", __ -> completedFuture(CoapResponse.ok("0.0.1")))
87+
.get("/3/0/1", __ -> CoapResponse.ok("Acme").toFuture())
88+
.get("/3/0/2", __ -> CoapResponse.ok("Emulator").toFuture())
89+
.get("/3/0/3", __ -> CoapResponse.ok("0.0.1").toFuture())
9190
.get("/delayed-10s", __ -> {
9291
CompletableFuture<CoapResponse> promise = new CompletableFuture<>();
93-
scheduledExecutor.schedule(() -> promise.complete(CoapResponse.ok("OK")), 10, TimeUnit.SECONDS);
92+
scheduledExecutor.schedule(() -> promise.complete(CoapResponse.ok("OK").build()), 10, TimeUnit.SECONDS);
9493
return promise;
9594
})
9695
.get("/time", obsManager.then(timeResource))

coap-cli/src/test/java/com/mbed/coap/cli/CoapCliTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616
package com.mbed.coap.cli;
1717

1818
import static com.mbed.coap.packet.BlockSize.S_16;
19+
import static com.mbed.coap.packet.CoapRequest.get;
20+
import static com.mbed.coap.packet.CoapResponse.coapResponse;
21+
import static com.mbed.coap.packet.CoapOptionsBuilder.options;
1922
import static com.mbed.coap.packet.CoapResponse.ok;
2023
import static com.mbed.coap.transport.udp.DatagramSocketTransport.udp;
2124
import static java.lang.String.format;
22-
import static java.util.concurrent.CompletableFuture.completedFuture;
2325
import static org.awaitility.Awaitility.await;
2426
import static org.junit.jupiter.api.Assertions.assertEquals;
2527
import static org.junit.jupiter.api.Assertions.assertFalse;
2628
import static org.junit.jupiter.api.Assertions.assertTrue;
2729
import com.mbed.coap.packet.CoapRequest;
28-
import com.mbed.coap.packet.CoapResponse;
2930
import com.mbed.coap.packet.Code;
3031
import com.mbed.coap.packet.MediaTypes;
3132
import com.mbed.coap.packet.Opaque;
@@ -54,10 +55,10 @@ void beforeAll() throws IOException {
5455
.route(RouterService.builder()
5556
.post("/rd", req -> {
5657
String epName = req.options().getUriQueryMap().get("ep");
57-
return completedFuture(CoapResponse.of(Code.C201_CREATED, Opaque.EMPTY, o -> o.locationPath("/rd/" + epName)));
58+
return coapResponse(Code.C201_CREATED).locationPath("/rd/" + epName).toFuture();
5859
})
59-
.get("/test", __ -> completedFuture(ok("Dziala!")))
60-
.post("/test", req -> completedFuture(ok("Received " + req.getPayload().size())))
60+
.get("/test", __ -> ok("Dziala!").toFuture())
61+
.post("/test", req -> ok("Received " + req.getPayload().size()).toFuture())
6162

6263
)
6364
.build()

coap-core/src/jmh/java/microbenchmark/ServerBenchmark.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package microbenchmark;
1717

18-
import static java.util.concurrent.CompletableFuture.completedFuture;
1918
import ch.qos.logback.classic.Level;
2019
import ch.qos.logback.classic.Logger;
2120
import com.mbed.coap.exception.CoapException;
@@ -66,10 +65,9 @@ public void setup() throws CoapException, IOException {
6665
server = CoapServer.builder()
6766
.transport(mockTransport)
6867
.route(RouterService.builder()
69-
.get("/path1/sub2/sub3", __ -> completedFuture(
70-
CoapResponse.ok("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890")
71-
.maxAge(requestCounter++)
72-
))
68+
.get("/path1/sub2/sub3", __ ->
69+
CoapResponse.ok("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890").maxAge((int) requestCounter++).toFuture()
70+
)
7371
)
7472
.build();
7573
server.start();

coap-core/src/main/java/com/mbed/coap/exception/CoapCodeException.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Copyright (C) 2022 java-coap contributors (https://github.com/open-coap/java-coap)
1+
/*
2+
* Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap)
33
* Copyright (C) 2011-2018 ARM Limited. All rights reserved.
44
* SPDX-License-Identifier: Apache-2.0
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +16,7 @@
1616
*/
1717
package com.mbed.coap.exception;
1818

19+
import static com.mbed.coap.packet.CoapResponse.coapResponse;
1920
import com.mbed.coap.packet.CoapResponse;
2021
import com.mbed.coap.packet.Code;
2122

@@ -46,7 +47,7 @@ public Code getCode() {
4647
return code;
4748
}
4849

49-
public CoapResponse toResponse() {
50-
return CoapResponse.of(code, getMessage());
50+
public CoapResponse.Builder toResponse() {
51+
return coapResponse(code).payload(getMessage());
5152
}
5253
}

coap-core/src/main/java/com/mbed/coap/exception/CoapRequestEntityTooLarge.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Copyright (C) 2022 java-coap contributors (https://github.com/open-coap/java-coap)
1+
/*
2+
* Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap)
33
* Copyright (C) 2011-2018 ARM Limited. All rights reserved.
44
* SPDX-License-Identifier: Apache-2.0
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -38,14 +38,15 @@ public CoapRequestEntityTooLarge(BlockOption blockOptionHint, String message) {
3838
}
3939

4040
@Override
41-
public CoapResponse toResponse() {
42-
CoapResponse resp = super.toResponse();
43-
if (maxSize > 0) {
44-
resp.options().setSize1(maxSize);
45-
}
46-
if (blockOptionHint != null) {
47-
resp.options().setBlock1Req(blockOptionHint);
48-
}
49-
return resp;
41+
public CoapResponse.Builder toResponse() {
42+
return super.toResponse()
43+
.options(o -> {
44+
if (maxSize > 0) {
45+
o.size1(maxSize);
46+
}
47+
if (blockOptionHint != null) {
48+
o.block1Req(blockOptionHint);
49+
}
50+
});
5051
}
5152
}

coap-core/src/main/java/com/mbed/coap/packet/CoapOptionsBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static CoapOptionsBuilder from(HeaderOptions options) {
3030
return new CoapOptionsBuilder(options.duplicate());
3131
}
3232

33-
CoapOptionsBuilder(HeaderOptions options) {
33+
private CoapOptionsBuilder(HeaderOptions options) {
3434
this.options = options;
3535
}
3636

0 commit comments

Comments
 (0)