Skip to content

Commit e8a35d3

Browse files
committed
Refactor CoapRequest with builder pattern
1 parent a98a153 commit e8a35d3

30 files changed

+390
-277
lines changed

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

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

1919
import static com.mbed.coap.cli.TransportOptions.addressFromUri;
20+
import static com.mbed.coap.packet.CoapRequest.request;
2021
import com.mbed.coap.CoapConstants;
2122
import com.mbed.coap.client.CoapClient;
2223
import com.mbed.coap.packet.BlockSize;
@@ -82,15 +83,16 @@ public Integer call() throws Exception {
8283
Thread.sleep(200);
8384

8485
String uriPath = uri.getPath().isEmpty() ? CoapConstants.WELL_KNOWN_CORE : uri.getPath();
85-
request = CoapRequest.of(null, method, uriPath)
86+
request = request(method, uriPath)
8687
.query(uri.getQuery() == null ? "" : uri.getQuery())
8788
.blockSize(blockSize)
8889
.payload(hexPayload ? Opaque.decodeHex(payload) : Opaque.of(payload))
90+
.contentFormat(contentFormat)
91+
.accept(accept)
8992
.options(o -> o
90-
.contentFormat(contentFormat)
9193
.proxyUri(proxyUri)
92-
.accept(accept)
93-
);
94+
)
95+
.build();
9496
CoapResponse resp = cli.sendSync(request);
9597

9698
if (resp.getPayload().nonEmpty()) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import static com.mbed.coap.packet.BlockSize.S_16;
1919
import static com.mbed.coap.packet.CoapRequest.get;
2020
import static com.mbed.coap.packet.CoapResponse.coapResponse;
21-
import static com.mbed.coap.packet.CoapOptionsBuilder.options;
2221
import static com.mbed.coap.packet.CoapResponse.ok;
2322
import static com.mbed.coap.transport.udp.DatagramSocketTransport.udp;
23+
import static com.mbed.coap.utils.Assertions.assertEquals;
2424
import static java.lang.String.format;
2525
import static org.awaitility.Awaitility.await;
2626
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -93,7 +93,7 @@ void simplestRequest() {
9393
// then
9494
assertEquals(0, exitCode);
9595
assertEquals("\nDziala!\n", sw.toString().replace("\r", ""));
96-
assertEquals(CoapRequest.get("/test"), sendCommand.request);
96+
assertEquals(get("/test"), sendCommand.request);
9797
}
9898

9999
@Test
@@ -109,10 +109,10 @@ void requestWithAllCoapParameters() {
109109
assertEquals(0, exitCode);
110110
assertEquals("\nReceived 29\n", sw.toString().replace("\r", ""));
111111

112-
CoapRequest expected = CoapRequest.post("/test")
112+
CoapRequest.Builder expected = CoapRequest.post("/test")
113113
.options(it -> it
114114
.query("par1", "val1")
115-
.block1Req(1, S_16, false)
115+
.block1Req(0, S_16, true)
116116
.proxyUri("http://another-uri")
117117
.requestTag(sendCommand.request.options().getRequestTag()) // it's random
118118
)
@@ -129,7 +129,7 @@ public void requestWithHexPayload() {
129129
assertEquals(0, exitCode);
130130
assertEquals("\nReceived 27\n", sw.toString().replace("\r", ""));
131131

132-
CoapRequest expected = CoapRequest.post("/test")
132+
CoapRequest.Builder expected = CoapRequest.post("/test")
133133
.payload(Opaque.decodeHex("a16e626573745f6775697461726973746a476172795f4d6f6f7265"), MediaTypes.CT_APPLICATION_CBOR)
134134
.accept(MediaTypes.CT_APPLICATION_JSON);
135135
assertEquals(expected, sendCommand.request);

coap-core/src/main/java/com/mbed/coap/client/CoapClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ public static CoapClient create(InetSocketAddress target, CoapServer server, Fun
6060
}
6161

6262
public CompletableFuture<CoapResponse> send(CoapRequest request) {
63-
return clientService.apply(request.address(destination));
63+
return clientService.apply(request.withAddress(destination));
64+
}
65+
66+
public CompletableFuture<CoapResponse> send(CoapRequest.Builder request) {
67+
return send(request.build());
6468
}
6569

6670
public CoapResponse sendSync(CoapRequest request) throws CoapException {
6771
return await(send(request));
6872
}
6973

74+
public CoapResponse sendSync(CoapRequest.Builder request) throws CoapException {
75+
return sendSync(request.build());
76+
}
77+
7078
private static CoapResponse await(CompletableFuture<CoapResponse> future) throws CoapException {
7179
try {
7280
return future.get(5, TimeUnit.MINUTES);

0 commit comments

Comments
 (0)