Search in sources :

Example 16 with TbResource

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);
    }
}
Also used : TbResource(org.thingsboard.server.common.data.TbResource) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 17 with TbResource

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();
}
Also used : TypeReference(com.fasterxml.jackson.core.type.TypeReference) TbResource(org.thingsboard.server.common.data.TbResource)

Example 18 with TbResource

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

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

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

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