use of org.thingsboard.server.common.data.TbResourceInfo in project thingsboard by thingsboard.
the class BaseTbResourceControllerTest method testFindSystemTbResources.
@Test
public void testFindSystemTbResources() throws Exception {
loginSysAdmin();
List<TbResourceInfo> resources = new ArrayList<>();
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");
resources.add(new TbResourceInfo(save(resource)));
}
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(resources, idComparator);
Collections.sort(loadedResources, idComparator);
Assert.assertEquals(resources, loadedResources);
for (TbResourceInfo resource : resources) {
doDelete("/api/resource/" + resource.getId().getId().toString()).andExpect(status().isOk());
}
pageLink = new PageLink(27);
loadedResources.clear();
do {
pageData = doGetTypedWithPageLink("/api/resource?", new TypeReference<PageData<TbResourceInfo>>() {
}, pageLink);
loadedResources.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Assert.assertTrue(loadedResources.isEmpty());
}
use of org.thingsboard.server.common.data.TbResourceInfo 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.TbResourceInfo in project thingsboard by thingsboard.
the class AccessValidator method validateResource.
private void validateResource(SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) {
ListenableFuture<TbResourceInfo> resourceFuture = resourceService.findResourceInfoByIdAsync(currentUser.getTenantId(), new TbResourceId(entityId.getId()));
Futures.addCallback(resourceFuture, getCallback(callback, resource -> {
if (resource == null) {
return ValidationResult.entityNotFound("Resource with requested id wasn't found!");
} else {
try {
accessControlService.checkPermission(currentUser, Resource.TB_RESOURCE, operation, entityId, resource);
} catch (ThingsboardException e) {
return ValidationResult.accessDenied(e.getMessage());
}
return ValidationResult.ok(resource);
}
}), executor);
}
use of org.thingsboard.server.common.data.TbResourceInfo in project thingsboard by thingsboard.
the class BaseTbResourceServiceTest method testFindTenantResourcesByTenantId.
@Test
public void testFindTenantResourcesByTenantId() throws Exception {
loginSysAdmin();
Tenant tenant = new Tenant();
tenant.setTitle("Test tenant");
tenant = doPost("/api/tenant", tenant, Tenant.class);
TenantId tenantId = tenant.getId();
List<TbResourceInfo> resources = new ArrayList<>();
for (int i = 0; i < 165; i++) {
TbResource resource = new TbResource();
resource.setTenantId(tenantId);
resource.setTitle("Resource" + i);
resource.setResourceType(ResourceType.JKS);
resource.setFileName(i + DEFAULT_FILE_NAME);
resource.setData("Test Data");
resources.add(new TbResourceInfo(resourceService.saveResource(resource)));
}
List<TbResourceInfo> loadedResources = new ArrayList<>();
PageLink pageLink = new PageLink(16);
PageData<TbResourceInfo> pageData;
do {
pageData = resourceService.findTenantResourcesByTenantId(tenantId, pageLink);
loadedResources.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Collections.sort(resources, idComparator);
Collections.sort(loadedResources, idComparator);
Assert.assertEquals(resources, loadedResources);
resourceService.deleteResourcesByTenantId(tenantId);
pageLink = new PageLink(31);
pageData = resourceService.findTenantResourcesByTenantId(tenantId, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertTrue(pageData.getData().isEmpty());
doDelete("/api/tenant/" + tenantId.getId().toString()).andExpect(status().isOk());
}
use of org.thingsboard.server.common.data.TbResourceInfo in project thingsboard by thingsboard.
the class BaseTbResourceServiceTest method testFindAllTenantResourcesByTenantId.
@Test
public void testFindAllTenantResourcesByTenantId() throws Exception {
loginSysAdmin();
Tenant tenant = new Tenant();
tenant.setTitle("Test tenant");
tenant = doPost("/api/tenant", tenant, Tenant.class);
TenantId tenantId = tenant.getId();
List<TbResourceInfo> resources = new ArrayList<>();
for (int i = 0; i < 100; i++) {
TbResource resource = new TbResource();
resource.setTenantId(TenantId.SYS_TENANT_ID);
resource.setTitle("System Resource" + i);
resource.setResourceType(ResourceType.JKS);
resource.setFileName(i + DEFAULT_FILE_NAME);
resource.setData("Test Data");
TbResourceInfo tbResourceInfo = new TbResourceInfo(resourceService.saveResource(resource));
if (i >= 50) {
resources.add(tbResourceInfo);
}
}
for (int i = 0; i < 50; i++) {
TbResource resource = new TbResource();
resource.setTenantId(tenantId);
resource.setTitle("Tenant Resource" + i);
resource.setResourceType(ResourceType.JKS);
resource.setFileName(i + DEFAULT_FILE_NAME);
resource.setData("Test Data");
resources.add(new TbResourceInfo(resourceService.saveResource(resource)));
}
List<TbResourceInfo> loadedResources = new ArrayList<>();
PageLink pageLink = new PageLink(10);
PageData<TbResourceInfo> pageData;
do {
pageData = resourceService.findAllTenantResourcesByTenantId(tenantId, pageLink);
loadedResources.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Collections.sort(resources, idComparator);
Collections.sort(loadedResources, idComparator);
Assert.assertEquals(resources, loadedResources);
resourceService.deleteResourcesByTenantId(tenantId);
pageLink = new PageLink(100);
pageData = resourceService.findAllTenantResourcesByTenantId(tenantId, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(pageData.getData().size(), 100);
resourceService.deleteResourcesByTenantId(TenantId.SYS_TENANT_ID);
pageLink = new PageLink(100);
pageData = resourceService.findAllTenantResourcesByTenantId(TenantId.SYS_TENANT_ID, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertTrue(pageData.getData().isEmpty());
doDelete("/api/tenant/" + tenantId.getId().toString()).andExpect(status().isOk());
}
Aggregations