Merge pull request #10097 from thingsboard/californium_3_11_0_and_2215_MsaCoapTest
coap: msa tests
This commit is contained in:
commit
93d77cd8a0
@ -0,0 +1,81 @@
|
|||||||
|
/**
|
||||||
|
* Copyright © 2016-2024 The Thingsboard Authors
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* 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,
|
||||||
|
* 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.msa;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
import org.eclipse.californium.core.CoapClient;
|
||||||
|
import org.eclipse.californium.core.coap.MediaTypeRegistry;
|
||||||
|
import org.eclipse.californium.core.config.CoapConfig;
|
||||||
|
import org.eclipse.californium.elements.config.Configuration;
|
||||||
|
import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider;
|
||||||
|
import org.eclipse.californium.elements.config.IntegerDefinition;
|
||||||
|
import org.eclipse.californium.elements.config.TcpConfig;
|
||||||
|
import org.thingsboard.common.util.JacksonUtil;
|
||||||
|
import org.thingsboard.server.common.msg.session.FeatureType;
|
||||||
|
|
||||||
|
public abstract class AbstractCoapClientTest extends AbstractContainerTest{
|
||||||
|
|
||||||
|
private static final String COAP_BASE_URL = "coap://localhost:5683/api/v1/";
|
||||||
|
private static final long CLIENT_REQUEST_TIMEOUT = 60000L;
|
||||||
|
|
||||||
|
|
||||||
|
private static final String COAP_CLIENT_TEST = "COAP_CLIENT_TEST.";
|
||||||
|
private static final IntegerDefinition COAP_PORT_DEF = CoapConfig.COAP_PORT;
|
||||||
|
|
||||||
|
private static final ModuleDefinitionsProvider MODULE_DEFINITIONS_PROVIDER = new ModuleDefinitionsProvider() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getModule() {
|
||||||
|
return COAP_CLIENT_TEST;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void applyDefinitions(Configuration config) {
|
||||||
|
TcpConfig.register();
|
||||||
|
config.set(COAP_PORT_DEF, 5683);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
protected CoapClient client;
|
||||||
|
|
||||||
|
protected byte[] createCoapClientAndPublish(String deviceName) throws Exception {
|
||||||
|
String provisionRequestMsg = createTestProvisionMessage(deviceName);
|
||||||
|
Configuration.addDefaultModule(MODULE_DEFINITIONS_PROVIDER);
|
||||||
|
String featureTokenUrl = COAP_BASE_URL + FeatureType.PROVISION.name().toLowerCase();
|
||||||
|
client = new CoapClient(featureTokenUrl);
|
||||||
|
return client.setTimeout(CLIENT_REQUEST_TIMEOUT)
|
||||||
|
.post(provisionRequestMsg.getBytes(), MediaTypeRegistry.APPLICATION_JSON)
|
||||||
|
.getPayload();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void disconnect() {
|
||||||
|
if (client != null) {
|
||||||
|
client.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createTestProvisionMessage(String deviceName) {
|
||||||
|
ObjectNode provisionRequest = JacksonUtil.newObjectNode();
|
||||||
|
provisionRequest.put("provisionDeviceKey", TEST_PROVISION_DEVICE_KEY);
|
||||||
|
provisionRequest.put("provisionDeviceSecret", TEST_PROVISION_DEVICE_SECRET);
|
||||||
|
if (deviceName != null) {
|
||||||
|
provisionRequest.put("deviceName", deviceName);
|
||||||
|
}
|
||||||
|
return provisionRequest.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -1,122 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright © 2016-2024 The Thingsboard Authors
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* 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,
|
|
||||||
* 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.msa;
|
|
||||||
|
|
||||||
import org.eclipse.californium.core.CoapClient;
|
|
||||||
import org.eclipse.californium.core.CoapHandler;
|
|
||||||
import org.eclipse.californium.core.CoapObserveRelation;
|
|
||||||
import org.eclipse.californium.core.CoapResponse;
|
|
||||||
import org.eclipse.californium.core.coap.CoAP;
|
|
||||||
import org.eclipse.californium.core.coap.MediaTypeRegistry;
|
|
||||||
import org.eclipse.californium.core.coap.Request;
|
|
||||||
import org.eclipse.californium.elements.exception.ConnectorException;
|
|
||||||
import org.thingsboard.server.common.msg.session.FeatureType;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class TestCoapClient {
|
|
||||||
|
|
||||||
private static final String COAP_BASE_URL = "coap://localhost:5683/api/v1/";
|
|
||||||
private static final long CLIENT_REQUEST_TIMEOUT = 60000L;
|
|
||||||
|
|
||||||
private final CoapClient client;
|
|
||||||
|
|
||||||
public TestCoapClient(){
|
|
||||||
this.client = createClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
public TestCoapClient(String accessToken, FeatureType featureType) {
|
|
||||||
this.client = createClient(getFeatureTokenUrl(accessToken, featureType));
|
|
||||||
}
|
|
||||||
|
|
||||||
public TestCoapClient(String featureTokenUrl) {
|
|
||||||
this.client = createClient(featureTokenUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connectToCoap(String accessToken) {
|
|
||||||
setURI(accessToken, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connectToCoap(String accessToken, FeatureType featureType) {
|
|
||||||
setURI(accessToken, featureType);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void disconnect() {
|
|
||||||
if (client != null) {
|
|
||||||
client.shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public CoapResponse postMethod(String requestBody) throws ConnectorException, IOException {
|
|
||||||
return this.postMethod(requestBody.getBytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
public CoapResponse postMethod(byte[] requestBodyBytes) throws ConnectorException, IOException {
|
|
||||||
return client.setTimeout(CLIENT_REQUEST_TIMEOUT).post(requestBodyBytes, MediaTypeRegistry.APPLICATION_JSON);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void postMethod(CoapHandler handler, String payload, int format) {
|
|
||||||
client.post(handler, payload, format);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void postMethod(CoapHandler handler, byte[] payload, int format) {
|
|
||||||
client.post(handler, payload, format);
|
|
||||||
}
|
|
||||||
|
|
||||||
public CoapResponse getMethod() throws ConnectorException, IOException {
|
|
||||||
return client.setTimeout(CLIENT_REQUEST_TIMEOUT).get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public CoapObserveRelation getObserveRelation(TestCoapClientCallback callback){
|
|
||||||
Request request = Request.newGet().setObserve();
|
|
||||||
request.setType(CoAP.Type.CON);
|
|
||||||
return client.observe(request, callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setURI(String featureTokenUrl) {
|
|
||||||
if (client == null) {
|
|
||||||
throw new RuntimeException("Failed to connect! CoapClient is not initialized!");
|
|
||||||
}
|
|
||||||
client.setURI(featureTokenUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setURI(String accessToken, FeatureType featureType) {
|
|
||||||
if (featureType == null){
|
|
||||||
featureType = FeatureType.ATTRIBUTES;
|
|
||||||
}
|
|
||||||
setURI(getFeatureTokenUrl(accessToken, featureType));
|
|
||||||
}
|
|
||||||
|
|
||||||
private CoapClient createClient() {
|
|
||||||
return new CoapClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
private CoapClient createClient(String featureTokenUrl) {
|
|
||||||
return new CoapClient(featureTokenUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getFeatureTokenUrl(FeatureType featureType) {
|
|
||||||
return COAP_BASE_URL + featureType.name().toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getFeatureTokenUrl(String token, FeatureType featureType) {
|
|
||||||
return COAP_BASE_URL + token + "/" + featureType.name().toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getFeatureTokenUrl(String token, FeatureType featureType, int requestId) {
|
|
||||||
return COAP_BASE_URL + token + "/" + featureType.name().toLowerCase() + "/" + requestId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,68 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright © 2016-2024 The Thingsboard Authors
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* 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,
|
|
||||||
* 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.msa;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.eclipse.californium.core.CoapHandler;
|
|
||||||
import org.eclipse.californium.core.CoapResponse;
|
|
||||||
import org.eclipse.californium.core.coap.CoAP;
|
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Data
|
|
||||||
public class TestCoapClientCallback implements CoapHandler {
|
|
||||||
|
|
||||||
protected final CountDownLatch latch;
|
|
||||||
protected Integer observe;
|
|
||||||
protected byte[] payloadBytes;
|
|
||||||
protected CoAP.ResponseCode responseCode;
|
|
||||||
|
|
||||||
public TestCoapClientCallback() {
|
|
||||||
this.latch = new CountDownLatch(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TestCoapClientCallback(int subscribeCount) {
|
|
||||||
this.latch = new CountDownLatch(subscribeCount);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getObserve() {
|
|
||||||
return observe;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] getPayloadBytes() {
|
|
||||||
return payloadBytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CoAP.ResponseCode getResponseCode() {
|
|
||||||
return responseCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onLoad(CoapResponse response) {
|
|
||||||
observe = response.getOptions().getObserve();
|
|
||||||
payloadBytes = response.getPayload();
|
|
||||||
responseCode = response.getCode();
|
|
||||||
latch.countDown();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError() {
|
|
||||||
log.warn("Command Response Ack Error, No connect");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -16,7 +16,6 @@
|
|||||||
package org.thingsboard.server.msa.connectivity;
|
package org.thingsboard.server.msa.connectivity;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import org.testng.annotations.AfterMethod;
|
import org.testng.annotations.AfterMethod;
|
||||||
import org.testng.annotations.BeforeMethod;
|
import org.testng.annotations.BeforeMethod;
|
||||||
@ -26,18 +25,14 @@ import org.thingsboard.server.common.data.Device;
|
|||||||
import org.thingsboard.server.common.data.DeviceProfile;
|
import org.thingsboard.server.common.data.DeviceProfile;
|
||||||
import org.thingsboard.server.common.data.DeviceProfileProvisionType;
|
import org.thingsboard.server.common.data.DeviceProfileProvisionType;
|
||||||
import org.thingsboard.server.common.data.security.DeviceCredentials;
|
import org.thingsboard.server.common.data.security.DeviceCredentials;
|
||||||
import org.thingsboard.server.common.msg.session.FeatureType;
|
import org.thingsboard.server.msa.AbstractCoapClientTest;
|
||||||
import org.thingsboard.server.msa.AbstractContainerTest;
|
|
||||||
import org.thingsboard.server.msa.DisableUIListeners;
|
import org.thingsboard.server.msa.DisableUIListeners;
|
||||||
import org.thingsboard.server.msa.TestCoapClient;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.thingsboard.server.msa.prototypes.DevicePrototypes.defaultDevicePrototype;
|
import static org.thingsboard.server.msa.prototypes.DevicePrototypes.defaultDevicePrototype;
|
||||||
|
|
||||||
@DisableUIListeners
|
@DisableUIListeners
|
||||||
public class CoapClientTest extends AbstractContainerTest {
|
public class CoapClientTest extends AbstractCoapClientTest{
|
||||||
private TestCoapClient client;
|
|
||||||
|
|
||||||
private Device device;
|
private Device device;
|
||||||
@BeforeMethod
|
@BeforeMethod
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
@ -48,6 +43,7 @@ public class CoapClientTest extends AbstractContainerTest {
|
|||||||
@AfterMethod
|
@AfterMethod
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
testRestClient.deleteDeviceIfExists(device.getId());
|
testRestClient.deleteDeviceIfExists(device.getId());
|
||||||
|
disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -101,21 +97,5 @@ public class CoapClientTest extends AbstractContainerTest {
|
|||||||
|
|
||||||
assertThat(response.get("status").asText()).isEqualTo("NOT_FOUND");
|
assertThat(response.get("status").asText()).isEqualTo("NOT_FOUND");
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] createCoapClientAndPublish(String deviceName) throws Exception {
|
|
||||||
String provisionRequestMsg = createTestProvisionMessage(deviceName);
|
|
||||||
client = new TestCoapClient(TestCoapClient.getFeatureTokenUrl(FeatureType.PROVISION));
|
|
||||||
return client.postMethod(provisionRequestMsg.getBytes()).getPayload();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String createTestProvisionMessage(String deviceName) {
|
|
||||||
ObjectNode provisionRequest = JacksonUtil.newObjectNode();
|
|
||||||
provisionRequest.put("provisionDeviceKey", TEST_PROVISION_DEVICE_KEY);
|
|
||||||
provisionRequest.put("provisionDeviceSecret", TEST_PROVISION_DEVICE_SECRET);
|
|
||||||
if (deviceName != null) {
|
|
||||||
provisionRequest.put("deviceName", deviceName);
|
|
||||||
}
|
|
||||||
return provisionRequest.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user