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());
}
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());
}
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);
}
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);
}
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")));
}
Aggregations