Search in sources :

Example 11 with TbResource

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();
}
Also used : TbResource(org.thingsboard.server.common.data.TbResource) Test(org.junit.Test)

Example 12 with TbResource

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());
    }
}
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 13 with TbResource

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());
}
Also used : TbResource(org.thingsboard.server.common.data.TbResource) Test(org.junit.Test)

Example 14 with TbResource

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);
}
Also used : TbResource(org.thingsboard.server.common.data.TbResource)

Example 15 with TbResource

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());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) ResourceType(org.thingsboard.server.common.data.ResourceType) ByteString(com.google.protobuf.ByteString) UUID(java.util.UUID) TbResource(org.thingsboard.server.common.data.TbResource)

Aggregations

TbResource (org.thingsboard.server.common.data.TbResource)29 Test (org.junit.Test)19 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)11 DaoSqlTest (org.thingsboard.server.dao.service.DaoSqlTest)11 ArrayList (java.util.ArrayList)5 TbResourceInfo (org.thingsboard.server.common.data.TbResourceInfo)5 PageLink (org.thingsboard.server.common.data.page.PageLink)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)4 ApiOperation (io.swagger.annotations.ApiOperation)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 TbResourceId (org.thingsboard.server.common.data.id.TbResourceId)3 TenantId (org.thingsboard.server.common.data.id.TenantId)3 UUID (java.util.UUID)2 Tenant (org.thingsboard.server.common.data.Tenant)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteString (com.google.protobuf.ByteString)1 MessagingException (javax.mail.MessagingException)1