Search in sources :

Example 6 with WidgetTypeDetails

use of org.thingsboard.server.common.data.widget.WidgetTypeDetails in project thingsboard by thingsboard.

the class BaseWidgetTypeServiceTest method testSaveWidgetTypeWithEmptyName.

@Test(expected = DataValidationException.class)
public void testSaveWidgetTypeWithEmptyName() throws IOException {
    WidgetsBundle widgetsBundle = new WidgetsBundle();
    widgetsBundle.setTenantId(tenantId);
    widgetsBundle.setTitle("Widgets bundle");
    WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
    WidgetTypeDetails widgetType = new WidgetTypeDetails();
    widgetType.setTenantId(tenantId);
    widgetType.setBundleAlias(savedWidgetsBundle.getAlias());
    widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class));
    try {
        widgetTypeService.saveWidgetType(widgetType);
    } finally {
        widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) WidgetsBundle(org.thingsboard.server.common.data.widget.WidgetsBundle) WidgetTypeDetails(org.thingsboard.server.common.data.widget.WidgetTypeDetails) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 7 with WidgetTypeDetails

use of org.thingsboard.server.common.data.widget.WidgetTypeDetails in project thingsboard by thingsboard.

the class BaseWidgetTypeServiceTest method testSaveWidgetTypeWithInvalidBundleAlias.

@Test(expected = DataValidationException.class)
public void testSaveWidgetTypeWithInvalidBundleAlias() throws IOException {
    WidgetTypeDetails widgetType = new WidgetTypeDetails();
    widgetType.setTenantId(tenantId);
    widgetType.setBundleAlias("some_alias");
    widgetType.setName("Widget Type");
    widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class));
    widgetTypeService.saveWidgetType(widgetType);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) WidgetTypeDetails(org.thingsboard.server.common.data.widget.WidgetTypeDetails) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 8 with WidgetTypeDetails

use of org.thingsboard.server.common.data.widget.WidgetTypeDetails in project thingsboard by thingsboard.

the class BaseWidgetTypeServiceTest method testSaveWidgetTypeWithEmptyDescriptor.

@Test(expected = DataValidationException.class)
public void testSaveWidgetTypeWithEmptyDescriptor() throws IOException {
    WidgetsBundle widgetsBundle = new WidgetsBundle();
    widgetsBundle.setTenantId(tenantId);
    widgetsBundle.setTitle("Widgets bundle");
    WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
    WidgetTypeDetails widgetType = new WidgetTypeDetails();
    widgetType.setTenantId(tenantId);
    widgetType.setName("Widget Type");
    widgetType.setBundleAlias(savedWidgetsBundle.getAlias());
    widgetType.setDescriptor(new ObjectMapper().readValue("{}", JsonNode.class));
    try {
        widgetTypeService.saveWidgetType(widgetType);
    } finally {
        widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) WidgetsBundle(org.thingsboard.server.common.data.widget.WidgetsBundle) WidgetTypeDetails(org.thingsboard.server.common.data.widget.WidgetTypeDetails) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 9 with WidgetTypeDetails

use of org.thingsboard.server.common.data.widget.WidgetTypeDetails in project thingsboard by thingsboard.

the class BaseWidgetTypeServiceTest method testDeleteWidgetType.

@Test
public void testDeleteWidgetType() throws IOException {
    WidgetsBundle widgetsBundle = new WidgetsBundle();
    widgetsBundle.setTenantId(tenantId);
    widgetsBundle.setTitle("Widgets bundle");
    WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
    WidgetTypeDetails widgetType = new WidgetTypeDetails();
    widgetType.setTenantId(tenantId);
    widgetType.setBundleAlias(savedWidgetsBundle.getAlias());
    widgetType.setName("Widget Type");
    widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class));
    WidgetType savedWidgetType = widgetTypeService.saveWidgetType(widgetType);
    WidgetType foundWidgetType = widgetTypeService.findWidgetTypeById(tenantId, savedWidgetType.getId());
    Assert.assertNotNull(foundWidgetType);
    widgetTypeService.deleteWidgetType(tenantId, savedWidgetType.getId());
    foundWidgetType = widgetTypeService.findWidgetTypeById(tenantId, savedWidgetType.getId());
    Assert.assertNull(foundWidgetType);
    widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) WidgetType(org.thingsboard.server.common.data.widget.WidgetType) WidgetsBundle(org.thingsboard.server.common.data.widget.WidgetsBundle) WidgetTypeDetails(org.thingsboard.server.common.data.widget.WidgetTypeDetails) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 10 with WidgetTypeDetails

use of org.thingsboard.server.common.data.widget.WidgetTypeDetails in project thingsboard by thingsboard.

the class InstallScripts method loadSystemWidgets.

public void loadSystemWidgets() throws Exception {
    Path widgetBundlesDir = Paths.get(getDataDir(), JSON_DIR, SYSTEM_DIR, WIDGET_BUNDLES_DIR);
    try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(widgetBundlesDir, path -> path.toString().endsWith(JSON_EXT))) {
        dirStream.forEach(path -> {
            try {
                JsonNode widgetsBundleDescriptorJson = objectMapper.readTree(path.toFile());
                JsonNode widgetsBundleJson = widgetsBundleDescriptorJson.get("widgetsBundle");
                WidgetsBundle widgetsBundle = objectMapper.treeToValue(widgetsBundleJson, WidgetsBundle.class);
                WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
                JsonNode widgetTypesArrayJson = widgetsBundleDescriptorJson.get("widgetTypes");
                widgetTypesArrayJson.forEach(widgetTypeJson -> {
                    try {
                        WidgetTypeDetails widgetTypeDetails = objectMapper.treeToValue(widgetTypeJson, WidgetTypeDetails.class);
                        widgetTypeDetails.setBundleAlias(savedWidgetsBundle.getAlias());
                        widgetTypeService.saveWidgetType(widgetTypeDetails);
                    } catch (Exception e) {
                        log.error("Unable to load widget type from json: [{}]", path.toString());
                        throw new RuntimeException("Unable to load widget type from json", e);
                    }
                });
            } catch (Exception e) {
                log.error("Unable to load widgets bundle from json: [{}]", path.toString());
                throw new RuntimeException("Unable to load widgets bundle from json", e);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) JsonNode(com.fasterxml.jackson.databind.JsonNode) WidgetsBundle(org.thingsboard.server.common.data.widget.WidgetsBundle) WidgetTypeDetails(org.thingsboard.server.common.data.widget.WidgetTypeDetails) IOException(java.io.IOException)

Aggregations

WidgetTypeDetails (org.thingsboard.server.common.data.widget.WidgetTypeDetails)30 JsonNode (com.fasterxml.jackson.databind.JsonNode)26 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)25 Test (org.junit.Test)25 WidgetsBundle (org.thingsboard.server.common.data.widget.WidgetsBundle)12 WidgetType (org.thingsboard.server.common.data.widget.WidgetType)6 ArrayList (java.util.ArrayList)2 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 MessagingException (javax.mail.MessagingException)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 WidgetTypeId (org.thingsboard.server.common.data.id.WidgetTypeId)1 BaseWidgetType (org.thingsboard.server.common.data.widget.BaseWidgetType)1 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)1 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)1