use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.
the class BaseTbResourceControllerTest method testUpdateTbResourceFromDifferentTenant.
@Test
public void testUpdateTbResourceFromDifferentTenant() throws Exception {
TbResource resource = new TbResource();
resource.setResourceType(ResourceType.JKS);
resource.setTitle("My first resource");
resource.setFileName(DEFAULT_FILE_NAME);
resource.setData("Test Data");
TbResource savedResource = save(resource);
loginDifferentTenant();
doPostWithTypedResponse("/api/resource", savedResource, new TypeReference<>() {
}, status().isForbidden());
deleteDifferentTenant();
}
use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.
the class BaseTbResourceControllerTest method testFindSystemAndTenantTbResources.
@Test
public void testFindSystemAndTenantTbResources() throws Exception {
List<TbResourceInfo> systemResources = new ArrayList<>();
List<TbResourceInfo> expectedResources = new ArrayList<>();
for (int i = 0; i < 73; i++) {
TbResource resource = new TbResource();
resource.setTitle("Resource" + i);
resource.setResourceType(ResourceType.JKS);
resource.setFileName(i + DEFAULT_FILE_NAME);
resource.setData("Test Data");
expectedResources.add(new TbResourceInfo(save(resource)));
}
loginSysAdmin();
for (int i = 0; i < 173; i++) {
TbResource resource = new TbResource();
resource.setTitle("Resource" + i);
resource.setResourceType(ResourceType.JKS);
resource.setFileName(i + DEFAULT_FILE_NAME);
resource.setData("Test Data");
TbResourceInfo savedResource = new TbResourceInfo(save(resource));
systemResources.add(savedResource);
if (i >= 73) {
expectedResources.add(savedResource);
}
}
login(tenantAdmin.getEmail(), "testPassword1");
List<TbResourceInfo> loadedResources = new ArrayList<>();
PageLink pageLink = new PageLink(24);
PageData<TbResourceInfo> pageData;
do {
pageData = doGetTypedWithPageLink("/api/resource?", new TypeReference<PageData<TbResourceInfo>>() {
}, pageLink);
loadedResources.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Collections.sort(expectedResources, idComparator);
Collections.sort(loadedResources, idComparator);
Assert.assertEquals(expectedResources, loadedResources);
loginSysAdmin();
for (TbResourceInfo resource : systemResources) {
doDelete("/api/resource/" + resource.getId().getId().toString()).andExpect(status().isOk());
}
}
use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.
the class BaseTbResourceControllerTest method testDeleteTbResource.
@Test
public void testDeleteTbResource() throws Exception {
TbResource resource = new TbResource();
resource.setResourceType(ResourceType.JKS);
resource.setTitle("My first resource");
resource.setFileName(DEFAULT_FILE_NAME);
resource.setData("Test Data");
TbResource savedResource = save(resource);
doDelete("/api/resource/" + savedResource.getId().getId().toString()).andExpect(status().isOk());
doGet("/api/resource/" + savedResource.getId().getId().toString()).andExpect(status().isNotFound());
}
use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.
the class DefaultTransportResourceCache method get.
@Override
public Optional<TbResource> get(TenantId tenantId, ResourceType resourceType, String resourceKey) {
ResourceCompositeKey compositeKey = new ResourceCompositeKey(tenantId, resourceType, resourceKey);
TbResource resource;
if (keys.contains(compositeKey)) {
resource = resources.get(compositeKey);
if (resource == null) {
resource = resources.get(compositeKey.getSystemKey());
}
} else {
resourceFetchLock.lock();
try {
if (keys.contains(compositeKey)) {
resource = resources.get(compositeKey);
if (resource == null) {
resource = resources.get(compositeKey.getSystemKey());
}
} else {
resource = fetchResource(compositeKey);
keys.add(compositeKey);
}
} finally {
resourceFetchLock.unlock();
}
}
return Optional.ofNullable(resource);
}
use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.
the class DefaultTransportApiService method handle.
private ListenableFuture<TransportApiResponseMsg> handle(GetResourceRequestMsg requestMsg) {
TenantId tenantId = TenantId.fromUUID(new UUID(requestMsg.getTenantIdMSB(), requestMsg.getTenantIdLSB()));
ResourceType resourceType = ResourceType.valueOf(requestMsg.getResourceType());
String resourceKey = requestMsg.getResourceKey();
TransportProtos.GetResourceResponseMsg.Builder builder = TransportProtos.GetResourceResponseMsg.newBuilder();
TbResource resource = resourceService.getResource(tenantId, resourceType, resourceKey);
if (resource == null && !tenantId.equals(TenantId.SYS_TENANT_ID)) {
resource = resourceService.getResource(TenantId.SYS_TENANT_ID, resourceType, resourceKey);
}
if (resource != null) {
builder.setResource(ByteString.copyFrom(dataDecodingEncodingService.encode(resource)));
}
return Futures.immediateFuture(TransportApiResponseMsg.newBuilder().setResourceResponseMsg(builder).build());
}
Aggregations