Removed unneccesary check, refactoring and added assertion check for validation in callback

This commit is contained in:
imbeacon 2023-09-14 09:40:58 +03:00
parent c82e3d4791
commit c0cde53c52

View File

@ -17,6 +17,7 @@ package org.thingsboard.server.transport.coap.client;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.eclipse.californium.core.CoapHandler; import org.eclipse.californium.core.CoapHandler;
import org.eclipse.californium.core.CoapObserveRelation; import org.eclipse.californium.core.CoapObserveRelation;
@ -101,7 +102,7 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
client = createClientForFeatureWithConfirmableParameter(FeatureType.ATTRIBUTES, confirmable); client = createClientForFeatureWithConfirmableParameter(FeatureType.ATTRIBUTES, confirmable);
CoapResponse coapResponse = client.postMethod(PAYLOAD_VALUES_STR.getBytes()); CoapResponse coapResponse = client.postMethod(PAYLOAD_VALUES_STR.getBytes());
assertEquals(CoAP.ResponseCode.CREATED, coapResponse.getCode()); assertEquals(CoAP.ResponseCode.CREATED, coapResponse.getCode());
validateConfirmableFlag(client, coapResponse); assertEquals("CoAP response type is wrong!", client.getType(), coapResponse.advanced().getType());
DeviceId deviceId = savedDevice.getId(); DeviceId deviceId = savedDevice.getId();
List<String> actualKeys = getActualKeysList(deviceId); List<String> actualKeys = getActualKeysList(deviceId);
@ -185,7 +186,7 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
String featureTokenUrl = CoapTestClient.getFeatureTokenUrl(accessToken, FeatureType.ATTRIBUTES) + "?clientKeys=" + keysParam + "&sharedKeys=" + keysParam; String featureTokenUrl = CoapTestClient.getFeatureTokenUrl(accessToken, FeatureType.ATTRIBUTES) + "?clientKeys=" + keysParam + "&sharedKeys=" + keysParam;
client.setURI(featureTokenUrl); client.setURI(featureTokenUrl);
CoapResponse response = client.getMethod(); CoapResponse response = client.getMethod();
validateConfirmableFlag(client, response); assertEquals("CoAP response type is wrong!", client.getType(), response.advanced().getType());
} }
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
@ -240,13 +241,6 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
assertArrayEquals(EMPTY_PAYLOAD, callback.getPayloadBytes()); assertArrayEquals(EMPTY_PAYLOAD, callback.getPayloadBytes());
} }
private void validateConfirmableFlag(CoapTestClient client, CoapResponse response) {
CoAP.Type responseType = response.advanced().getType();
if (CoAP.Type.CON.equals(responseType) || CoAP.Type.NON.equals(responseType)) {
assertEquals(client.getType(), responseType);
}
}
private void validateTwoWayStateChangedNotification(CoapTestCallback callback, String actualResult) { private void validateTwoWayStateChangedNotification(CoapTestCallback callback, String actualResult) {
assertEquals(DEVICE_RESPONSE, actualResult); assertEquals(DEVICE_RESPONSE, actualResult);
assertNotNull(callback.getPayloadBytes()); assertNotNull(callback.getPayloadBytes());
@ -256,6 +250,9 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
private final CoapTestClient client; private final CoapTestClient client;
@Getter
private boolean wasSuccessful = false;
TestCoapCallbackForRPC(CoapTestClient client) { TestCoapCallbackForRPC(CoapTestClient client) {
this.client = client; this.client = client;
} }
@ -265,7 +262,7 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
payloadBytes = response.getPayload(); payloadBytes = response.getPayload();
responseCode = response.getCode(); responseCode = response.getCode();
observe = response.getOptions().getObserve(); observe = response.getOptions().getObserve();
validateConfirmableFlag(client, response); wasSuccessful = client.getType().equals(response.advanced().getType());
if (observe != null) { if (observe != null) {
if (observe > 0) { if (observe > 0) {
processOnLoadResponse(response, client); processOnLoadResponse(response, client);
@ -280,14 +277,12 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
} }
protected void processOnLoadResponse(CoapResponse response, CoapTestClient client) { protected void processOnLoadResponse(CoapResponse response, CoapTestClient client) {
validateConfirmableFlag(client, response);
JsonNode responseJson = JacksonUtil.fromBytes(response.getPayload()); JsonNode responseJson = JacksonUtil.fromBytes(response.getPayload());
int requestId = responseJson.get("id").asInt(); int requestId = responseJson.get("id").asInt();
client.setURI(CoapTestClient.getFeatureTokenUrl(accessToken, FeatureType.RPC, requestId)); client.setURI(CoapTestClient.getFeatureTokenUrl(accessToken, FeatureType.RPC, requestId));
client.postMethod(new CoapHandler() { client.postMethod(new CoapHandler() {
@Override @Override
public void onLoad(CoapResponse response) { public void onLoad(CoapResponse response) {
validateConfirmableFlag(client, response);
log.warn("RPC {} command response ack: {}", requestId, response.getCode()); log.warn("RPC {} command response ack: {}", requestId, response.getCode());
} }