getAttributes black-box-test

This commit is contained in:
ShvaykaD 2018-12-12 20:02:39 +02:00
parent 928f8f84b6
commit ab58776f04
5 changed files with 107 additions and 7 deletions

View File

@ -16,7 +16,6 @@
package org.thingsboard.server.actors.device; package org.thingsboard.server.actors.device;
import akka.actor.ActorContext; import akka.actor.ActorContext;
import akka.event.LoggingAdapter;
import com.datastax.driver.core.utils.UUIDs; import com.datastax.driver.core.utils.UUIDs;
import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
@ -42,7 +41,6 @@ import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody;
import org.thingsboard.server.common.msg.TbMsg; import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgDataType; import org.thingsboard.server.common.msg.TbMsgDataType;
import org.thingsboard.server.common.msg.TbMsgMetaData; import org.thingsboard.server.common.msg.TbMsgMetaData;
import org.thingsboard.server.common.msg.cluster.ClusterEventMsg;
import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest; import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest;
import org.thingsboard.server.common.msg.session.SessionMsgType; import org.thingsboard.server.common.msg.session.SessionMsgType;
import org.thingsboard.server.common.msg.timeout.DeviceActorClientSideRpcTimeoutMsg; import org.thingsboard.server.common.msg.timeout.DeviceActorClientSideRpcTimeoutMsg;
@ -295,7 +293,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
return systemContext.getAttributesService().findAll(tenantId, deviceId, scope); return systemContext.getAttributesService().findAll(tenantId, deviceId, scope);
} }
} else { } else {
return Futures.immediateFuture(Collections.emptyList()); return systemContext.getAttributesService().findAll(tenantId, deviceId, scope);
} }
} }

View File

@ -32,7 +32,8 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts; import org.apache.http.ssl.SSLContexts;
import org.junit.*; import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher; import org.junit.rules.TestWatcher;
import org.junit.runner.Description; import org.junit.runner.Description;
@ -43,7 +44,10 @@ import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.msa.mapper.WsTelemetryResponse; import org.thingsboard.server.msa.mapper.WsTelemetryResponse;
import javax.net.ssl.*;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import java.net.URI; import java.net.URI;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.List; import java.util.List;
@ -54,6 +58,7 @@ import java.util.Random;
public abstract class AbstractContainerTest { public abstract class AbstractContainerTest {
protected static final String HTTPS_URL = "https://localhost"; protected static final String HTTPS_URL = "https://localhost";
protected static final String WSS_URL = "wss://localhost"; protected static final String WSS_URL = "wss://localhost";
protected static String TB_TOKEN;
protected static RestClient restClient; protected static RestClient restClient;
protected ObjectMapper mapper = new ObjectMapper(); protected ObjectMapper mapper = new ObjectMapper();

View File

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.server.msa.connectivity; package org.thingsboard.server.msa.connectivity;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -25,6 +26,15 @@ import org.thingsboard.server.msa.AbstractContainerTest;
import org.thingsboard.server.msa.WsClient; import org.thingsboard.server.msa.WsClient;
import org.thingsboard.server.msa.mapper.WsTelemetryResponse; import org.thingsboard.server.msa.mapper.WsTelemetryResponse;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.thingsboard.server.common.data.DataConstants.DEVICE;
import static org.thingsboard.server.common.data.DataConstants.SHARED_SCOPE;
public class HttpClientTest extends AbstractContainerTest { public class HttpClientTest extends AbstractContainerTest {
@Test @Test
@ -52,6 +62,53 @@ public class HttpClientTest extends AbstractContainerTest {
Assert.assertTrue(verify(actualLatestTelemetry, "doubleKey", Double.toString(42.0))); Assert.assertTrue(verify(actualLatestTelemetry, "doubleKey", Double.toString(42.0)));
Assert.assertTrue(verify(actualLatestTelemetry, "longKey", Long.toString(73))); Assert.assertTrue(verify(actualLatestTelemetry, "longKey", Long.toString(73)));
restClient.getRestTemplate().delete(HTTPS_URL + "/api/device/" + device.getId()); restClient.deleteDevice(device.getId());
}
@Test
public void getAttributes() throws Exception {
restClient.login("tenant@thingsboard.org", "tenant");
TB_TOKEN = restClient.getToken();
Device device = createDevice("test");
String accessToken = restClient.getCredentials(device.getId()).getCredentialsId();
assertNotNull(accessToken);
ResponseEntity deviceSharedAttributes = restClient.getRestTemplate()
.postForEntity(HTTPS_URL + "/api/plugins/telemetry/" + DEVICE + "/" + device.getId().toString() + "/attributes/" + SHARED_SCOPE, mapper.readTree(createPayload().toString()),
ResponseEntity.class,
accessToken);
ResponseEntity deviceClientsAttributes = restClient.getRestTemplate()
.postForEntity(HTTPS_URL + "/api/v1/" + accessToken + "/attributes/", mapper.readTree(createPayload().toString()),
ResponseEntity.class,
accessToken);
Assert.assertTrue(deviceSharedAttributes.getStatusCode().is2xxSuccessful());
Assert.assertTrue(deviceClientsAttributes.getStatusCode().is2xxSuccessful());
Optional<JsonNode> allOptional = restClient.getAttributes(accessToken, null, null);
assertTrue(allOptional.isPresent());
JsonNode all = allOptional.get();
assertEquals(all.get("shared"), mapper.readTree(createPayload().toString()));
assertEquals(all.get("client"), mapper.readTree(createPayload().toString()));
Optional<JsonNode> sharedOptional = restClient.getAttributes(accessToken, null, "stringKey");
assertTrue(sharedOptional.isPresent());
JsonNode shared = sharedOptional.get();
assertEquals(shared.get("shared").get("stringKey"), mapper.readTree(createPayload().get("stringKey").toString()));
assertEquals(shared.get("client"), mapper.readTree(createPayload().toString()));
Optional<JsonNode> clientOptional = restClient.getAttributes(accessToken, "longKey,stringKey", null);
assertTrue(clientOptional.isPresent());
JsonNode client = clientOptional.get();
assertEquals(client.get("shared"), mapper.readTree(createPayload().toString()));
assertEquals(client.get("client").get("longKey"), mapper.readTree(createPayload().get("longKey").toString()));
assertEquals(client.get("client").get("stringKey"), mapper.readTree(createPayload().get("stringKey").toString()));
restClient.deleteDevice(device.getId());
} }
} }

View File

@ -23,7 +23,6 @@
<version>2.2.1-SNAPSHOT</version> <version>2.2.1-SNAPSHOT</version>
<artifactId>thingsboard</artifactId> <artifactId>thingsboard</artifactId>
</parent> </parent>
<groupId>org.thingsboard</groupId>
<artifactId>tools</artifactId> <artifactId>tools</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>

View File

@ -107,6 +107,27 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
} }
public Optional<JsonNode> getAttributes(String accessToken, String clientKeys, String sharedKeys) {
Map<String, String> params = new HashMap<>();
params.put("accessToken", accessToken);
params.put("clientKeys", clientKeys);
params.put("sharedKeys", sharedKeys);
try {
ResponseEntity<JsonNode> telemetryEntity = restTemplate.getForEntity(baseURL + "/api/v1/{accessToken}/attributes?clientKeys={clientKeys}&sharedKeys={sharedKeys}", JsonNode.class, params);
return Optional.of(telemetryEntity.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Customer createCustomer(Customer customer) {
return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();
}
public Customer createCustomer(String title) { public Customer createCustomer(String title) {
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTitle(title); customer.setTitle(title);
@ -120,6 +141,14 @@ public class RestClient implements ClientHttpRequestInterceptor {
return restTemplate.postForEntity(baseURL + "/api/device", device, Device.class).getBody(); return restTemplate.postForEntity(baseURL + "/api/device", device, Device.class).getBody();
} }
public Device createDevice(Device device) {
return restTemplate.postForEntity(baseURL + "/api/device", device, Device.class).getBody();
}
public Asset createAsset(Asset asset) {
return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();
}
public Asset createAsset(String name, String type) { public Asset createAsset(String name, String type) {
Asset asset = new Asset(); Asset asset = new Asset();
asset.setName(name); asset.setName(name);
@ -131,6 +160,18 @@ public class RestClient implements ClientHttpRequestInterceptor {
return restTemplate.postForEntity(baseURL + "/api/alarm", alarm, Alarm.class).getBody(); return restTemplate.postForEntity(baseURL + "/api/alarm", alarm, Alarm.class).getBody();
} }
public void deleteCustomer(CustomerId customerId) {
restTemplate.delete(baseURL + "/api/customer/{customerId}", customerId);
}
public void deleteDevice(DeviceId deviceId) {
restTemplate.delete(baseURL + "/api/device/{deviceId}", deviceId);
}
public void deleteAsset(AssetId assetId) {
restTemplate.delete(baseURL + "/api/asset/{assetId}", assetId);
}
public Device assignDevice(CustomerId customerId, DeviceId deviceId) { public Device assignDevice(CustomerId customerId, DeviceId deviceId) {
return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class, return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class,
customerId.toString(), deviceId.toString()).getBody(); customerId.toString(), deviceId.toString()).getBody();