Search in sources :

Example 1 with TbResourceInfo

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());
}
Also used : TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TbResource(org.thingsboard.server.common.data.TbResource) Test(org.junit.Test)

Example 2 with TbResourceInfo

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());
    }
}
Also used : TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TbResource(org.thingsboard.server.common.data.TbResource) Test(org.junit.Test)

Example 3 with TbResourceInfo

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);
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge) HttpValidationCallback(org.thingsboard.server.controller.HttpValidationCallback) Customer(org.thingsboard.server.common.data.Customer) Autowired(org.springframework.beans.factory.annotation.Autowired) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) ToErrorResponseEntity(org.thingsboard.server.service.telemetry.exception.ToErrorResponseEntity) PreDestroy(javax.annotation.PreDestroy) TenantService(org.thingsboard.server.dao.tenant.TenantService) Rpc(org.thingsboard.server.common.data.rpc.Rpc) ResourceService(org.thingsboard.server.dao.resource.ResourceService) RpcService(org.thingsboard.server.dao.rpc.RpcService) ApiUsageStateService(org.thingsboard.server.dao.usagerecord.ApiUsageStateService) EntityViewService(org.thingsboard.server.dao.entityview.EntityViewService) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) EdgeId(org.thingsboard.server.common.data.id.EdgeId) AssetService(org.thingsboard.server.dao.asset.AssetService) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Function(com.google.common.base.Function) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) EdgeService(org.thingsboard.server.dao.edge.EdgeService) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) ApiUsageStateId(org.thingsboard.server.common.data.id.ApiUsageStateId) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) Executors(java.util.concurrent.Executors) EntityView(org.thingsboard.server.common.data.EntityView) PostConstruct(javax.annotation.PostConstruct) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Operation(org.thingsboard.server.service.security.permission.Operation) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) OtaPackageService(org.thingsboard.server.dao.ota.OtaPackageService) UserService(org.thingsboard.server.dao.user.UserService) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Device(org.thingsboard.server.common.data.Device) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AssetId(org.thingsboard.server.common.data.id.AssetId) Tenant(org.thingsboard.server.common.data.Tenant) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) User(org.thingsboard.server.common.data.User) EntityIdFactory(org.thingsboard.server.common.data.id.EntityIdFactory) DeviceService(org.thingsboard.server.dao.device.DeviceService) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) EntityId(org.thingsboard.server.common.data.id.EntityId) BiConsumer(java.util.function.BiConsumer) CustomerService(org.thingsboard.server.dao.customer.CustomerService) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) RpcId(org.thingsboard.server.common.data.id.RpcId) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) TbResourceId(org.thingsboard.server.common.data.id.TbResourceId) FutureCallback(com.google.common.util.concurrent.FutureCallback) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) DeviceProfileService(org.thingsboard.server.dao.device.DeviceProfileService) HttpStatus(org.springframework.http.HttpStatus) Futures(com.google.common.util.concurrent.Futures) Component(org.springframework.stereotype.Component) UserId(org.thingsboard.server.common.data.id.UserId) TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Resource(org.thingsboard.server.service.security.permission.Resource) ResponseEntity(org.springframework.http.ResponseEntity) Asset(org.thingsboard.server.common.data.asset.Asset) AccessControlService(org.thingsboard.server.service.security.permission.AccessControlService) TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) TbResourceId(org.thingsboard.server.common.data.id.TbResourceId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException)

Example 4 with TbResourceInfo

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());
}
Also used : TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TbResource(org.thingsboard.server.common.data.TbResource) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test) DaoSqlTest(org.thingsboard.server.dao.service.DaoSqlTest)

Example 5 with TbResourceInfo

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());
}
Also used : TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TbResource(org.thingsboard.server.common.data.TbResource) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test) DaoSqlTest(org.thingsboard.server.dao.service.DaoSqlTest)

Aggregations

TbResourceInfo (org.thingsboard.server.common.data.TbResourceInfo)8 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 TbResource (org.thingsboard.server.common.data.TbResource)5 PageLink (org.thingsboard.server.common.data.page.PageLink)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 Tenant (org.thingsboard.server.common.data.Tenant)3 TenantId (org.thingsboard.server.common.data.id.TenantId)3 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)2 TbResourceId (org.thingsboard.server.common.data.id.TbResourceId)2 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)2 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)2 DaoSqlTest (org.thingsboard.server.dao.service.DaoSqlTest)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Function (com.google.common.base.Function)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1