Browse Source

Merge 38d59e0fce into c349342ac1

pull/15174/merge
Jan Christoph Bernack 6 days ago
committed by GitHub
parent
commit
68143cf3c0
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 11
      application/src/test/java/org/thingsboard/server/transport/coap/AbstractCoapIntegrationTest.java
  2. 52
      application/src/test/java/org/thingsboard/server/transport/coap/client/AbstractCoapClientPiggybackedIntegrationTest.java
  3. 32
      application/src/test/java/org/thingsboard/server/transport/coap/client/CoapClientIntegrationTest.java
  4. 40
      application/src/test/java/org/thingsboard/server/transport/coap/client/CoapClientNoPiggybackIntegrationTest.java
  5. 40
      application/src/test/java/org/thingsboard/server/transport/coap/client/CoapClientPiggybackedIntegrationTest.java
  6. 1
      common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/callback/AbstractSyncSessionCallback.java
  7. 5
      common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/callback/CoapResponseCallback.java
  8. 8
      common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/callback/CoapResponseCodeCallback.java

11
application/src/test/java/org/thingsboard/server/transport/coap/AbstractCoapIntegrationTest.java

@ -42,6 +42,7 @@ import org.thingsboard.server.common.data.device.profile.JsonTransportPayloadCon
import org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration;
import org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration;
import org.thingsboard.server.common.data.security.DeviceCredentials;
import org.thingsboard.server.common.msg.session.FeatureType;
import org.thingsboard.server.transport.AbstractTransportIntegrationTest;
import org.thingsboard.server.utils.PortFinder;
@ -176,4 +177,14 @@ public abstract class AbstractCoapIntegrationTest extends AbstractTransportInteg
device.setType(type);
return doPost("/api/device", device, Device.class);
}
protected CoapTestClient createClientForFeatureWithConfirmableParameter(FeatureType featureType, boolean confirmable) {
CoapTestClient coapTestClient = new CoapTestClient(accessToken, featureType);
if (confirmable) {
coapTestClient.useCONs();
} else {
coapTestClient.useNONs();
}
return coapTestClient;
}
}

52
application/src/test/java/org/thingsboard/server/transport/coap/client/AbstractCoapClientPiggybackedIntegrationTest.java

@ -0,0 +1,52 @@
/**
* Copyright © 2016-2026 The Thingsboard Authors
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.transport.coap.client;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.CoAP;
import org.junit.After;
import org.junit.Before;
import org.thingsboard.server.common.msg.session.FeatureType;
import org.thingsboard.server.transport.coap.AbstractCoapIntegrationTest;
import org.thingsboard.server.transport.coap.CoapTestConfigProperties;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@Slf4j
public abstract class AbstractCoapClientPiggybackedIntegrationTest extends AbstractCoapIntegrationTest {
@Before
public void beforeTest() throws Exception {
CoapTestConfigProperties configProperties = CoapTestConfigProperties.builder()
.deviceName("CoAP Message Flow Piggyback Device")
.build();
processBeforeTest(configProperties);
}
@After
public void afterTest() throws Exception {
processAfterTest();
}
protected void testCase(boolean confirmable, CoAP.Type expectedResponseType) throws Exception {
client = createClientForFeatureWithConfirmableParameter(FeatureType.ATTRIBUTES, confirmable);
CoapResponse response = client.postMethod(PAYLOAD_VALUES_STR);
assertNotNull(response);
assertEquals(expectedResponseType, response.advanced().getType());
}
}

32
application/src/test/java/org/thingsboard/server/transport/coap/client/CoapClientIntegrationTest.java

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@ -100,7 +100,7 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
client = createClientForFeatureWithConfirmableParameter(FeatureType.ATTRIBUTES, confirmable);
CoapResponse coapResponse = client.postMethod(PAYLOAD_VALUES_STR.getBytes());
assertEquals(CoAP.ResponseCode.CREATED, coapResponse.getCode());
assertEquals("CoAP response type is wrong!", client.getType(), coapResponse.advanced().getType());
assertValidResponseType(client.getType(), coapResponse.advanced().getType());
DeviceId deviceId = savedDevice.getId();
List<String> actualKeys = getActualKeysList(deviceId);
@ -184,7 +184,7 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
String featureTokenUrl = CoapTestClient.getFeatureTokenUrl(accessToken, FeatureType.ATTRIBUTES) + "?clientKeys=" + keysParam + "&sharedKeys=" + keysParam;
client.setURI(featureTokenUrl);
CoapResponse response = client.getMethod();
assertEquals("CoAP response type is wrong!", client.getType(), response.advanced().getType());
assertValidResponseType(client.getType(), response.advanced().getType());
}
@SuppressWarnings({"unchecked", "rawtypes"})
@ -260,7 +260,7 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
payloadBytes = response.getPayload();
responseCode = response.getCode();
observe = response.getOptions().getObserve();
wasSuccessful = client.getType().equals(response.advanced().getType());
wasSuccessful = isValidResponseType(client.getType(), response.advanced().getType());
if (observe != null) {
if (observe > 0) {
processOnLoadResponse(response, client);
@ -291,17 +291,21 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
}, DEVICE_RESPONSE, MediaTypeRegistry.APPLICATION_JSON);
}
private CoapTestClient createClientForFeatureWithConfirmableParameter(FeatureType featureType, boolean confirmable) {
CoapTestClient coapTestClient = new CoapTestClient(accessToken, featureType);
if (confirmable) {
coapTestClient.useCONs();
} else {
coapTestClient.useNONs();
}
return coapTestClient;
}
private List<EntityKey> getEntityKeys(EntityKeyType scope) {
return CoapClientIntegrationTest.EXPECTED_KEYS.stream().map(key -> new EntityKey(scope, key)).collect(Collectors.toList());
}
private static void assertValidResponseType(CoAP.Type requestType, CoAP.Type responseType) {
assertTrue("Unexpected CoAP response type " + responseType + " for request type " + requestType,
isValidResponseType(requestType, responseType));
}
private static boolean isValidResponseType(CoAP.Type requestType, CoAP.Type responseType) {
return switch (requestType) {
case CON ->
CoAP.Type.ACK.equals(responseType) || CoAP.Type.CON.equals(responseType) || CoAP.Type.NON.equals(responseType);
case NON -> CoAP.Type.NON.equals(responseType) || CoAP.Type.CON.equals(responseType);
default -> false;
};
}
}

40
application/src/test/java/org/thingsboard/server/transport/coap/client/CoapClientNoPiggybackIntegrationTest.java

@ -0,0 +1,40 @@
/**
* Copyright © 2016-2026 The Thingsboard Authors
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.transport.coap.client;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.californium.core.coap.CoAP;
import org.junit.Test;
import org.springframework.test.context.TestPropertySource;
import org.thingsboard.server.dao.service.DaoSqlTest;
@Slf4j
@DaoSqlTest
@TestPropertySource(properties = {
"transport.coap.piggyback_timeout=0"
})
public class CoapClientNoPiggybackIntegrationTest extends AbstractCoapClientPiggybackedIntegrationTest {
@Test
public void testConfirmable() throws Exception {
// response should be sent via seperate CON transaction (not piggybacked)
testCase(true, CoAP.Type.CON);
}
@Test
public void testNonConfirmable() throws Exception {
testCase(false, CoAP.Type.NON);
}
}

40
application/src/test/java/org/thingsboard/server/transport/coap/client/CoapClientPiggybackedIntegrationTest.java

@ -0,0 +1,40 @@
/**
* Copyright © 2016-2026 The Thingsboard Authors
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.transport.coap.client;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.californium.core.coap.CoAP;
import org.junit.Test;
import org.springframework.test.context.TestPropertySource;
import org.thingsboard.server.dao.service.DaoSqlTest;
@Slf4j
@DaoSqlTest
@TestPropertySource(properties = {
"transport.coap.piggyback_timeout=2000"
})
public class CoapClientPiggybackedIntegrationTest extends AbstractCoapClientPiggybackedIntegrationTest {
@Test
public void testConfirmable() throws Exception {
// response should be included in the ACK packet (piggybacked)
testCase(true, CoAP.Type.ACK);
}
@Test
public void testNonConfirmable() throws Exception {
testCase(false, CoAP.Type.NON);
}
}

1
common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/callback/AbstractSyncSessionCallback.java

@ -87,7 +87,6 @@ public abstract class AbstractSyncSessionCallback implements SessionMsgListener
protected void respond(Response response) {
response.getOptions().setContentFormat(TbCoapContentFormatUtil.getContentFormat(exchange.getRequestOptions().getContentFormat(), state.getContentFormat()));
response.setConfirmable(exchange.advanced().getRequest().isConfirmable());
exchange.respond(response);
}

5
common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/callback/CoapResponseCallback.java

@ -36,7 +36,6 @@ public class CoapResponseCallback implements TransportServiceCallback<Void> {
*/
@Override
public void onSuccess(Void msg) {
this.onSuccessResponse.setConfirmable(isConRequest());
exchange.respond(this.onSuccessResponse);
}
@ -47,8 +46,4 @@ public class CoapResponseCallback implements TransportServiceCallback<Void> {
public void onError(Throwable e) {
exchange.respond(onFailureResponse);
}
protected boolean isConRequest() {
return exchange.advanced().getRequest().isConfirmable();
}
}

8
common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/callback/CoapResponseCodeCallback.java

@ -34,17 +34,11 @@ public class CoapResponseCodeCallback implements TransportServiceCallback<Void>
@Override
public void onSuccess(Void msg) {
Response response = new Response(onSuccessResponse);
response.setConfirmable(isConRequest());
exchange.respond(response);
exchange.respond(new Response(onSuccessResponse));
}
@Override
public void onError(Throwable e) {
exchange.respond(onFailureResponse);
}
protected boolean isConRequest() {
return exchange.advanced().getRequest().isConfirmable();
}
}

Loading…
Cancel
Save