use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.
the class TbResourceController method saveResource.
@ApiOperation(value = "Create Or Update Resource (saveResource)", notes = "Create or update the Resource. When creating the Resource, platform generates Resource id as " + UUID_WIKI_LINK + "The newly created Resource id will be present in the response. " + "Specify existing Resource id to update the Resource. " + "Referencing non-existing Resource Id will cause 'Not Found' error. " + "\n\nResource combination of the title with the key is unique in the scope of tenant. " + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH, produces = "application/json", consumes = "application/json")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/resource", method = RequestMethod.POST)
@ResponseBody
public TbResource saveResource(@ApiParam(value = "A JSON value representing the Resource.") @RequestBody TbResource resource) throws ThingsboardException {
boolean created = resource.getId() == null;
try {
resource.setTenantId(getTenantId());
checkEntity(resource.getId(), resource, Resource.TB_RESOURCE);
TbResource savedResource = checkNotNull(resourceService.saveResource(resource));
tbClusterService.onResourceChange(savedResource, null);
logEntityAction(savedResource.getId(), savedResource, null, created ? ActionType.ADDED : ActionType.UPDATED, null);
return savedResource;
} catch (Exception e) {
logEntityAction(emptyId(EntityType.TB_RESOURCE), resource, null, created ? ActionType.ADDED : ActionType.UPDATED, e);
throw handleException(e);
}
}
use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.
the class AbstractLwM2MIntegrationTest method init.
private void init() throws Exception {
executor = Executors.newScheduledThreadPool(10, ThingsBoardThreadFactory.forName("test-lwm2m-scheduled"));
loginTenantAdmin();
for (String resourceName : this.resources) {
TbResource lwModel = new TbResource();
lwModel.setResourceType(ResourceType.LWM2M_MODEL);
lwModel.setTitle(resourceName);
lwModel.setFileName(resourceName);
lwModel.setTenantId(tenantId);
byte[] bytes = IOUtils.toByteArray(AbstractLwM2MIntegrationTest.class.getClassLoader().getResourceAsStream("lwm2m/" + resourceName));
lwModel.setData(Base64.getEncoder().encodeToString(bytes));
lwModel = doPostWithTypedResponse("/api/resource", lwModel, new TypeReference<>() {
});
Assert.assertNotNull(lwModel);
}
wsClient = buildAndConnectWebSocketClient();
}
use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.
the class BaseTbResourceServiceTest method testSaveLwm2mTbResource.
@Test
public void testSaveLwm2mTbResource() throws Exception {
TbResource resource = new TbResource();
resource.setTenantId(tenantId);
resource.setResourceType(ResourceType.LWM2M_MODEL);
resource.setFileName("test_model.xml");
resource.setData(Base64.getEncoder().encodeToString(LWM2M_TEST_MODEL.getBytes()));
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("My first resource id=0 v1.0", savedResource.getTitle());
Assert.assertEquals("0_1.0", savedResource.getResourceKey());
Assert.assertEquals(resource.getData(), savedResource.getData());
resourceService.deleteResource(tenantId, savedResource.getId());
}
use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.
the class BaseTbResourceServiceTest method testFindResourceByTenantIdAndResourceTypeAndResourceKey.
@Test
public void testFindResourceByTenantIdAndResourceTypeAndResourceKey() throws Exception {
TbResource resource = new TbResource();
resource.setResourceType(ResourceType.JKS);
resource.setTenantId(tenantId);
resource.setTitle("My resource");
resource.setFileName(DEFAULT_FILE_NAME);
resource.setData("Test Data");
TbResource savedResource = resourceService.saveResource(resource);
TbResource foundResource = resourceService.getResource(tenantId, savedResource.getResourceType(), savedResource.getResourceKey());
Assert.assertNotNull(foundResource);
Assert.assertEquals(savedResource, foundResource);
resourceService.deleteResource(tenantId, savedResource.getId());
}
use of org.thingsboard.server.common.data.TbResource 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());
}
Aggregations