Search in sources :

Example 6 with PluginMetaData

use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.

the class BaseRuleService method validateRuleAndPluginState.

private void validateRuleAndPluginState(RuleMetaData rule) {
    if (org.springframework.util.StringUtils.isEmpty(rule.getPluginToken())) {
        return;
    }
    PluginMetaData pluginMd = pluginService.findPluginByApiToken(rule.getPluginToken());
    if (pluginMd == null) {
        throw new IncorrectParameterException("Rule points to non-existent plugin!");
    }
    if (!pluginMd.getTenantId().equals(systemTenantId) && !pluginMd.getTenantId().equals(rule.getTenantId())) {
        throw new IncorrectParameterException("Rule access plugin that belongs to different tenant!");
    }
    if (rule.getState() == ComponentLifecycleState.ACTIVE && pluginMd.getState() != ComponentLifecycleState.ACTIVE) {
        throw new IncorrectParameterException("Can't save active rule that points to inactive plugin!");
    }
    ComponentDescriptor pluginDescriptor = componentDescriptorService.findByClazz(pluginMd.getClazz());
    String actionClazz = getIfValid(ComponentType.ACTION.name(), rule.getAction(), "clazz", JsonNode::isTextual, JsonNode::asText);
    if (!Arrays.asList(pluginDescriptor.getActions().split(",")).contains(actionClazz)) {
        throw new IncorrectParameterException("Rule's action is not supported by plugin with token " + rule.getPluginToken() + "!");
    }
}
Also used : IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 7 with PluginMetaData

use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.

the class BasePluginControllerTest method testActivatePlugin.

@Test
public void testActivatePlugin() throws Exception {
    PluginMetaData plugin = new PluginMetaData();
    plugin.setName("My plugin");
    plugin.setApiToken("myplugin");
    plugin.setConfiguration(mapper.readTree("{}"));
    plugin.setClazz(TelemetryStoragePlugin.class.getName());
    PluginMetaData savedPlugin = doPost("/api/plugin", plugin, PluginMetaData.class);
    doPost("/api/plugin/" + savedPlugin.getId().getId().toString() + "/activate").andExpect(status().isOk());
}
Also used : PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) TelemetryStoragePlugin(org.thingsboard.server.extensions.core.plugin.telemetry.TelemetryStoragePlugin) Test(org.junit.Test)

Example 8 with PluginMetaData

use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.

the class BasePluginControllerTest method testFindPluginById.

@Test
public void testFindPluginById() throws Exception {
    PluginMetaData plugin = new PluginMetaData();
    plugin.setName("My plugin");
    plugin.setApiToken("myplugin");
    plugin.setConfiguration(mapper.readTree("{}"));
    plugin.setClazz(TelemetryStoragePlugin.class.getName());
    PluginMetaData savedPlugin = doPost("/api/plugin", plugin, PluginMetaData.class);
    PluginMetaData foundPlugin = doGet("/api/plugin/" + savedPlugin.getId().getId().toString(), PluginMetaData.class);
    Assert.assertNotNull(foundPlugin);
    Assert.assertEquals(savedPlugin, foundPlugin);
}
Also used : PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) TelemetryStoragePlugin(org.thingsboard.server.extensions.core.plugin.telemetry.TelemetryStoragePlugin) Test(org.junit.Test)

Example 9 with PluginMetaData

use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.

the class BaseRuleControllerTest method beforeTest.

@Before
public void beforeTest() throws Exception {
    loginSysAdmin();
    sysPlugin = new PluginMetaData();
    sysPlugin.setName("Sys plugin");
    sysPlugin.setApiToken("sysplugin");
    sysPlugin.setConfiguration(mapper.readTree("{}"));
    sysPlugin.setClazz(TelemetryStoragePlugin.class.getName());
    sysPlugin = doPost("/api/plugin", sysPlugin, PluginMetaData.class);
    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);
    tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(savedTenant.getId());
    tenantAdmin.setEmail("tenant2@thingsboard.org");
    tenantAdmin.setFirstName("Joe");
    tenantAdmin.setLastName("Downs");
    tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
    tenantPlugin = new PluginMetaData();
    tenantPlugin.setName("My plugin");
    tenantPlugin.setApiToken("myplugin");
    tenantPlugin.setConfiguration(mapper.readTree("{}"));
    tenantPlugin.setClazz(TelemetryStoragePlugin.class.getName());
    tenantPlugin = doPost("/api/plugin", tenantPlugin, PluginMetaData.class);
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) TelemetryStoragePlugin(org.thingsboard.server.extensions.core.plugin.telemetry.TelemetryStoragePlugin) Before(org.junit.Before)

Example 10 with PluginMetaData

use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.

the class DefaultActorServiceTest method before.

@Before
public void before() throws Exception {
    actorService = new DefaultActorService();
    actorContext = new ActorSystemContext();
    tenantService = mock(TenantService.class);
    pluginService = mock(PluginService.class);
    ruleService = mock(RuleService.class);
    deviceAuthService = mock(DeviceAuthService.class);
    deviceService = mock(DeviceService.class);
    tsService = mock(TimeseriesService.class);
    rpcService = mock(ClusterRpcService.class);
    discoveryService = mock(DiscoveryService.class);
    routingService = mock(ClusterRoutingService.class);
    attributesService = mock(AttributesService.class);
    componentService = mock(ComponentDiscoveryService.class);
    eventService = mock(EventService.class);
    serverInstance = new ServerInstance(ServerInstanceProtos.ServerInfo.newBuilder().setHost("localhost").setPort(8080).build());
    ReflectionTestUtils.setField(actorService, "actorContext", actorContext);
    ReflectionTestUtils.setField(actorService, "rpcService", rpcService);
    ReflectionTestUtils.setField(actorService, "discoveryService", discoveryService);
    ReflectionTestUtils.setField(actorContext, "syncSessionTimeout", 10000L);
    ReflectionTestUtils.setField(actorContext, "pluginActorTerminationDelay", 10000L);
    ReflectionTestUtils.setField(actorContext, "pluginErrorPersistFrequency", 10000L);
    ReflectionTestUtils.setField(actorContext, "ruleActorTerminationDelay", 10000L);
    ReflectionTestUtils.setField(actorContext, "ruleErrorPersistFrequency", 10000L);
    ReflectionTestUtils.setField(actorContext, "pluginProcessingTimeout", 60000L);
    ReflectionTestUtils.setField(actorContext, "tenantService", tenantService);
    ReflectionTestUtils.setField(actorContext, "pluginService", pluginService);
    ReflectionTestUtils.setField(actorContext, "ruleService", ruleService);
    ReflectionTestUtils.setField(actorContext, "deviceAuthService", deviceAuthService);
    ReflectionTestUtils.setField(actorContext, "deviceService", deviceService);
    ReflectionTestUtils.setField(actorContext, "tsService", tsService);
    ReflectionTestUtils.setField(actorContext, "rpcService", rpcService);
    ReflectionTestUtils.setField(actorContext, "discoveryService", discoveryService);
    ReflectionTestUtils.setField(actorContext, "tsService", tsService);
    ReflectionTestUtils.setField(actorContext, "routingService", routingService);
    ReflectionTestUtils.setField(actorContext, "attributesService", attributesService);
    ReflectionTestUtils.setField(actorContext, "componentService", componentService);
    ReflectionTestUtils.setField(actorContext, "eventService", eventService);
    when(routingService.resolveById((EntityId) any())).thenReturn(Optional.empty());
    when(discoveryService.getCurrentServer()).thenReturn(serverInstance);
    ruleMock = mock(RuleMetaData.class);
    when(ruleMock.getId()).thenReturn(ruleId);
    when(ruleMock.getState()).thenReturn(ComponentLifecycleState.ACTIVE);
    when(ruleMock.getPluginToken()).thenReturn("telemetry");
    TextPageData<RuleMetaData> systemRules = new TextPageData<>(Collections.emptyList(), null, false);
    TextPageData<RuleMetaData> tenantRules = new TextPageData<>(Collections.singletonList(ruleMock), null, false);
    when(ruleService.findSystemRules(any())).thenReturn(systemRules);
    when(ruleService.findTenantRules(any(), any())).thenReturn(tenantRules);
    when(ruleService.findRuleById(ruleId)).thenReturn(ruleMock);
    pluginMock = mock(PluginMetaData.class);
    when(pluginMock.getTenantId()).thenReturn(SYSTEM_TENANT);
    when(pluginMock.getId()).thenReturn(pluginId);
    when(pluginMock.getState()).thenReturn(ComponentLifecycleState.ACTIVE);
    TextPageData<PluginMetaData> systemPlugins = new TextPageData<>(Collections.singletonList(pluginMock), null, false);
    TextPageData<PluginMetaData> tenantPlugins = new TextPageData<>(Collections.emptyList(), null, false);
    when(pluginService.findSystemPlugins(any())).thenReturn(systemPlugins);
    when(pluginService.findTenantPlugins(any(), any())).thenReturn(tenantPlugins);
    when(pluginService.findPluginByApiToken("telemetry")).thenReturn(pluginMock);
    when(pluginService.findPluginById(pluginId)).thenReturn(pluginMock);
    TextPageData<Tenant> tenants = new TextPageData<>(Collections.emptyList(), null, false);
    when(tenantService.findTenants(any())).thenReturn(tenants);
}
Also used : ComponentDiscoveryService(org.thingsboard.server.service.component.ComponentDiscoveryService) PluginService(org.thingsboard.server.dao.plugin.PluginService) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) TextPageData(org.thingsboard.server.common.data.page.TextPageData) DeviceService(org.thingsboard.server.dao.device.DeviceService) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) EventService(org.thingsboard.server.dao.event.EventService) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) ClusterRoutingService(org.thingsboard.server.service.cluster.routing.ClusterRoutingService) TenantService(org.thingsboard.server.dao.tenant.TenantService) Tenant(org.thingsboard.server.common.data.Tenant) DeviceAuthService(org.thingsboard.server.common.transport.auth.DeviceAuthService) ClusterRpcService(org.thingsboard.server.service.cluster.rpc.ClusterRpcService) RuleService(org.thingsboard.server.dao.rule.RuleService) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) DefaultActorService(org.thingsboard.server.actors.service.DefaultActorService) DiscoveryService(org.thingsboard.server.service.cluster.discovery.DiscoveryService) ComponentDiscoveryService(org.thingsboard.server.service.component.ComponentDiscoveryService) ServerInstance(org.thingsboard.server.service.cluster.discovery.ServerInstance) Before(org.junit.Before)

Aggregations

PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)50 Test (org.junit.Test)24 TenantId (org.thingsboard.server.common.data.id.TenantId)17 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)14 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)13 RuleMetaData (org.thingsboard.server.common.data.rule.RuleMetaData)13 PluginId (org.thingsboard.server.common.data.id.PluginId)8 TelemetryStoragePlugin (org.thingsboard.server.extensions.core.plugin.telemetry.TelemetryStoragePlugin)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 ArrayList (java.util.ArrayList)4 TextPageData (org.thingsboard.server.common.data.page.TextPageData)4 List (java.util.List)3 UUID (java.util.UUID)3 Before (org.junit.Before)3 Tenant (org.thingsboard.server.common.data.Tenant)3 ComponentDescriptor (org.thingsboard.server.common.data.plugin.ComponentDescriptor)3 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)2