Search in sources :

Example 16 with WidgetType

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

the class BaseWidgetTypeServiceTest method testSaveWidgetTypeWithInvalidBundleAlias.

@Test(expected = DataValidationException.class)
public void testSaveWidgetTypeWithInvalidBundleAlias() throws IOException {
    WidgetType widgetType = new WidgetType();
    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) WidgetType(org.thingsboard.server.common.data.widget.WidgetType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 17 with WidgetType

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

the class JpaWidgetTypeDaoTest method testFindByTenantIdAndBundleAlias.

@Test
@DatabaseSetup(("classpath:dbunit/widget_type.xml"))
public void testFindByTenantIdAndBundleAlias() {
    UUID tenantId = UUID.fromString("2b7e4c90-2dfe-11e7-94aa-f7f6dbfb4833");
    List<WidgetType> widgetTypes = widgetTypeDao.findWidgetTypesByTenantIdAndBundleAlias(tenantId, "BUNDLE_ALIAS_1");
    assertEquals(3, widgetTypes.size());
}
Also used : UUID(java.util.UUID) WidgetType(org.thingsboard.server.common.data.widget.WidgetType) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test) DatabaseSetup(com.github.springtestdbunit.annotation.DatabaseSetup)

Example 18 with WidgetType

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

the class DefaultSystemDataLoaderService method loadSystemWidgets.

@Override
public void loadSystemWidgets() throws Exception {
    Path widgetBundlesDir = Paths.get(dataDir, 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 {
                        WidgetType widgetType = objectMapper.treeToValue(widgetTypeJson, WidgetType.class);
                        widgetType.setBundleAlias(savedWidgetsBundle.getAlias());
                        widgetTypeService.saveWidgetType(widgetType);
                    } 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) WidgetType(org.thingsboard.server.common.data.widget.WidgetType) WidgetsBundle(org.thingsboard.server.common.data.widget.WidgetsBundle) IOException(java.io.IOException)

Example 19 with WidgetType

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

the class WidgetTypeController method getWidgetType.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/widgetType", params = { "isSystem", "bundleAlias", "alias" }, method = RequestMethod.GET)
@ResponseBody
public WidgetType getWidgetType(@RequestParam boolean isSystem, @RequestParam String bundleAlias, @RequestParam String alias) throws ThingsboardException {
    try {
        TenantId tenantId;
        if (isSystem) {
            tenantId = new TenantId(ModelConstants.NULL_UUID);
        } else {
            tenantId = getCurrentUser().getTenantId();
        }
        WidgetType widgetType = widgetTypeService.findWidgetTypeByTenantIdBundleAliasAndAlias(tenantId, bundleAlias, alias);
        checkWidgetType(widgetType, false);
        return widgetType;
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) WidgetType(org.thingsboard.server.common.data.widget.WidgetType) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 20 with WidgetType

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

the class BaseWidgetTypeServiceTest method testSaveWidgetTypeWithEmptyBundleAlias.

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

Aggregations

WidgetType (org.thingsboard.server.common.data.widget.WidgetType)32 Test (org.junit.Test)26 JsonNode (com.fasterxml.jackson.databind.JsonNode)25 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)24 WidgetsBundle (org.thingsboard.server.common.data.widget.WidgetsBundle)12 TenantId (org.thingsboard.server.common.data.id.TenantId)5 WidgetTypeId (org.thingsboard.server.common.data.id.WidgetTypeId)3 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)2 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 List (java.util.List)1 MessagingException (javax.mail.MessagingException)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)1 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)1