Search in sources :

Example 21 with TbResource

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

the class BaseTbResourceServiceTest method testSaveTbResource.

@Test
public void testSaveTbResource() throws Exception {
    TbResource resource = new TbResource();
    resource.setTenantId(tenantId);
    resource.setResourceType(ResourceType.JKS);
    resource.setTitle("My first resource");
    resource.setFileName(DEFAULT_FILE_NAME);
    resource.setData("Test Data");
    TbResource savedResource = resourceService.saveResource(resource);
    Assert.assertNotNull(savedResource);
    Assert.assertNotNull(savedResource.getId());
    Assert.assertTrue(savedResource.getCreatedTime() > 0);
    Assert.assertEquals(resource.getTenantId(), savedResource.getTenantId());
    Assert.assertEquals(resource.getTitle(), savedResource.getTitle());
    Assert.assertEquals(resource.getResourceKey(), savedResource.getResourceKey());
    Assert.assertEquals(resource.getData(), savedResource.getData());
    savedResource.setTitle("My new resource");
    resourceService.saveResource(savedResource);
    TbResource foundResource = resourceService.findResourceById(tenantId, savedResource.getId());
    Assert.assertEquals(foundResource.getTitle(), savedResource.getTitle());
    resourceService.deleteResource(tenantId, savedResource.getId());
}
Also used : 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 22 with TbResource

use of org.thingsboard.server.common.data.TbResource 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)

Example 23 with TbResource

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

the class BaseTbResourceServiceTest method testDeleteResource.

@Test
public void testDeleteResource() throws Exception {
    TbResource resource = new TbResource();
    resource.setResourceType(ResourceType.JKS);
    resource.setTitle("My resource");
    resource.setFileName(DEFAULT_FILE_NAME);
    resource.setData("Test Data");
    TbResource savedResource = resourceService.saveResource(resource);
    TbResource foundResource = resourceService.findResourceById(tenantId, savedResource.getId());
    Assert.assertNotNull(foundResource);
    resourceService.deleteResource(tenantId, savedResource.getId());
    foundResource = resourceService.findResourceById(tenantId, savedResource.getId());
    Assert.assertNull(foundResource);
}
Also used : 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 24 with TbResource

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

the class BaseTbResourceServiceTest method testSaveTbResourceWithInvalidTenant.

@Test(expected = DataValidationException.class)
public void testSaveTbResourceWithInvalidTenant() throws Exception {
    TbResource resource = new TbResource();
    resource.setTenantId(TenantId.fromUUID(Uuids.timeBased()));
    resource.setResourceType(ResourceType.JKS);
    resource.setTitle("My resource");
    resource.setFileName(DEFAULT_FILE_NAME);
    resource.setData("Test Data");
    resourceService.saveResource(resource);
}
Also used : 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 25 with TbResource

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

the class BaseTbResourceControllerTest method saveResourceInfoWithViolationOfLengthValidation.

@Test
public void saveResourceInfoWithViolationOfLengthValidation() throws Exception {
    TbResource resource = new TbResource();
    resource.setResourceType(ResourceType.JKS);
    resource.setTitle(RandomStringUtils.randomAlphabetic(300));
    resource.setFileName(DEFAULT_FILE_NAME);
    resource.setData("Test Data");
    doPost("/api/resource", resource).andExpect(statusReason(containsString("length of title must be equal or less than 255")));
}
Also used : TbResource(org.thingsboard.server.common.data.TbResource) Test(org.junit.Test)

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