Search in sources :

Example 26 with TbResource

use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.

the class BaseTbResourceControllerTest method testFindTenantTbResources.

@Test
public void testFindTenantTbResources() throws Exception {
    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);
}
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 27 with TbResource

use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.

the class BaseTbResourceControllerTest method testSaveTbResource.

@Test
public void testSaveTbResource() 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);
    Assert.assertNotNull(savedResource);
    Assert.assertNotNull(savedResource.getId());
    Assert.assertTrue(savedResource.getCreatedTime() > 0);
    Assert.assertEquals(savedTenant.getId(), savedResource.getTenantId());
    Assert.assertEquals(resource.getTitle(), savedResource.getTitle());
    Assert.assertEquals(DEFAULT_FILE_NAME, savedResource.getFileName());
    Assert.assertEquals(DEFAULT_FILE_NAME, savedResource.getResourceKey());
    Assert.assertEquals(resource.getData(), savedResource.getData());
    savedResource.setTitle("My new resource");
    save(savedResource);
    TbResource foundResource = doGet("/api/resource/" + savedResource.getId().getId().toString(), TbResource.class);
    Assert.assertEquals(foundResource.getTitle(), savedResource.getTitle());
}
Also used : TbResource(org.thingsboard.server.common.data.TbResource) Test(org.junit.Test)

Example 28 with TbResource

use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.

the class BaseTbResourceControllerTest method testFindTbResourceById.

@Test
public void testFindTbResourceById() 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);
    TbResource foundResource = doGet("/api/resource/" + savedResource.getId().getId().toString(), TbResource.class);
    Assert.assertNotNull(foundResource);
    Assert.assertEquals(savedResource, foundResource);
}
Also used : TbResource(org.thingsboard.server.common.data.TbResource) Test(org.junit.Test)

Example 29 with TbResource

use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.

the class DefaultTransportResourceCache method fetchResource.

private TbResource fetchResource(ResourceCompositeKey compositeKey) {
    UUID tenantId = compositeKey.getTenantId().getId();
    TransportProtos.GetResourceRequestMsg.Builder builder = TransportProtos.GetResourceRequestMsg.newBuilder();
    builder.setTenantIdLSB(tenantId.getLeastSignificantBits()).setTenantIdMSB(tenantId.getMostSignificantBits()).setResourceType(compositeKey.resourceType.name()).setResourceKey(compositeKey.resourceKey);
    TransportProtos.GetResourceResponseMsg responseMsg = transportService.getResource(builder.build());
    Optional<TbResource> optionalResource = dataDecodingEncodingService.decode(responseMsg.getResource().toByteArray());
    if (optionalResource.isPresent()) {
        TbResource resource = optionalResource.get();
        resources.put(new ResourceCompositeKey(resource.getTenantId(), resource.getResourceType(), resource.getResourceKey()), resource);
        return resource;
    }
    return null;
}
Also used : TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) 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