Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Minor updates and fix tests

Rich Turner589b47a8e322
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.java
index 9153a2e89..aa38f96fc 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusNetwork.java
@@ -26,10 +26,7 @@ import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.value.Value;
import java.util.*;
-import java.util.concurrent.Future;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.*;
import java.util.function.Consumer;
import static org.openremote.agent.protocol.velbus.AbstractVelbusProtocol.LOG;
@@ -76,15 +73,17 @@ public class VelbusNetwork {
return this.executorService;
}
- public synchronized void sendPackets(VelbusPacket... packets) {
+ public void sendPackets(VelbusPacket... packets) {
if (getConnectionStatus() != ConnectionStatus.CONNECTED) {
return;
}
- messageQueue.addAll(Arrays.asList(packets));
+ synchronized (messageQueue) {
+ messageQueue.addAll(Arrays.asList(packets));
- if (queueProcessingTask == null) {
- startSendingPackets();
+ if (queueProcessingTask == null) {
+ startSendingPackets();
+ }
}
}
@@ -276,12 +275,16 @@ public class VelbusNetwork {
);
}
- protected synchronized void doSendPacket() {
+ protected void doSendPacket() {
if (getConnectionStatus() != ConnectionStatus.CONNECTED) {
return;
}
- VelbusPacket packet = messageQueue.poll();
+ VelbusPacket packet;
+
+ synchronized (messageQueue) {
+ packet = messageQueue.poll();
+ }
if (packet == null) {
queueProcessingTask.cancel(false);
diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
index eb58ef0fd..b0fa48e98 100644
--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java
+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
@@ -1089,30 +1089,32 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
return container.getServices(Protocol.class).stream().map((protocol) -> {
ProtocolDescriptor pd = protocol.getProtocolDescriptor();
- // Translate meta item descriptors into asset descriptors
- AttributeDescriptor[] attributeDescriptors = pd.getProtocolConfigurationMetaItems().stream()
- .map((descriptor) -> {
- AttributeValueDescriptor valueDescriptor = EnumUtil.enumFromString(AttributeValueType.class, descriptor.getValueType().name()).orElse(AttributeValueType.OBJECT);
-
- return new AttributeDescriptorImpl(
- descriptor.getUrn(),
- valueDescriptor,
- descriptor.getInitialValue()
- );
- }).toArray(AttributeDescriptor[]::new);
-
- return new AgentDescriptorImpl(
- pd.getDisplayName(),
- pd.getName(),
- "cogs",
- null,
- attributeDescriptors)
- .setInstanceDiscovery(pd.isConfigurationDiscovery())
- .setInstanceImport(pd.isConfigurationImport())
- .setAssetDiscovery(pd.isDeviceDiscovery())
- .setAssetImport(pd.isDeviceImport());
-
- }).toArray(AgentDescriptor[]::new);
+ if (pd != null) {
+ // Translate meta item descriptors into asset descriptors
+ AttributeDescriptor[] attributeDescriptors = pd.getProtocolConfigurationMetaItems() == null ? new AttributeDescriptor[0] : pd.getProtocolConfigurationMetaItems().stream()
+ .map((descriptor) -> {
+ AttributeValueDescriptor valueDescriptor = EnumUtil.enumFromString(AttributeValueType.class, descriptor.getValueType().name()).orElse(AttributeValueType.OBJECT);
+
+ return new AttributeDescriptorImpl(
+ descriptor.getUrn(),
+ valueDescriptor,
+ descriptor.getInitialValue()
+ );
+ }).toArray(AttributeDescriptor[]::new);
+
+ return new AgentDescriptorImpl(
+ pd.getDisplayName(),
+ pd.getName(),
+ "cogs",
+ null,
+ attributeDescriptors)
+ .setInstanceDiscovery(pd.isConfigurationDiscovery())
+ .setInstanceImport(pd.isConfigurationImport())
+ .setAssetDiscovery(pd.isDeviceDiscovery())
+ .setAssetImport(pd.isDeviceImport());
+ }
+ return null;
+ }).filter(Objects::nonNull).toArray(AgentDescriptor[]::new);
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
index 592824c01..c127cba30 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
@@ -286,12 +286,6 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
gatewayService = container.getService(GatewayService.class);
EventSubscriptionAuthorizer assetEventAuthorizer = AssetStorageService.assetInfoAuthorizer(identityService, this);
- META_ITEM_RESTRICTED_READ_SQL_FRAGMENT =
- " ('" + Arrays.stream(AssetModelUtil.getMetaItemDescriptors()).filter(i -> i.getAccess().restrictedRead).map(MetaItemDescriptor::getUrn).collect(joining("','")) + "')";
-
- META_ITEM_PUBLIC_READ_SQL_FRAGMENT =
- " ('" + Arrays.stream(AssetModelUtil.getMetaItemDescriptors()).filter(i -> i.getAccess().publicRead).map(MetaItemDescriptor::getUrn).collect(joining("','")) + "')";
-
clientEventService.addSubscriptionAuthorizer((auth, subscription) ->
(subscription.isEventType(AssetTreeModifiedEvent.class))
&& identityService.getIdentityProvider().canSubscribeWith(
@@ -336,6 +330,11 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
@Override
public void start(Container container) throws Exception {
+ META_ITEM_RESTRICTED_READ_SQL_FRAGMENT =
+ " ('" + Arrays.stream(AssetModelUtil.getMetaItemDescriptors()).filter(i -> i.getAccess().restrictedRead).map(MetaItemDescriptor::getUrn).collect(joining("','")) + "')";
+
+ META_ITEM_PUBLIC_READ_SQL_FRAGMENT =
+ " ('" + Arrays.stream(AssetModelUtil.getMetaItemDescriptors()).filter(i -> i.getAccess().publicRead).map(MetaItemDescriptor::getUrn).collect(joining("','")) + "')";
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
index a7928d0ce..d060b0ab6 100644
--- a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
+++ b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
@@ -184,6 +184,7 @@ public class EmailNotificationHandler implements NotificationHandler {
.collect(Collectors.toList()));
break;
case USER:
+ case CUSTOM:
// Nothing to do here
mappedTargets.add(new Notification.Target(targetType, targetId));
break;
@@ -326,15 +327,12 @@ public class EmailNotificationHandler implements NotificationHandler {
});
}
- if ((emailNotificationMessage.getTo() == null || emailNotificationMessage.getTo().isEmpty())
- && (emailNotificationMessage.getCc() == null || emailNotificationMessage.getCc().isEmpty())
- && (emailNotificationMessage.getBcc() == null || emailNotificationMessage.getBcc().isEmpty())) {
- LOG.warning("No recipient found for " + targetType.name().toLowerCase() + ": " + targetId);
+ if (emailBuilder.getRecipients().isEmpty()) {
return NotificationSendResult.failure("No recipients set for " + targetType.name().toLowerCase() + ": " + targetId);
}
// Set from based on source if not already set
- if (emailNotificationMessage.getFrom() == null) {
+ if (emailBuilder.getFromRecipient() == null) {
emailBuilder.from(defaultFrom);
}
diff --git a/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql b/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql
index 5c408dfc9..b52941998 100644
--- a/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql
+++ b/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql
@@ -99,7 +99,7 @@ create table NOTIFICATION (
NAME varchar(255),
TYPE varchar(50) not null,
TARGET varchar(50) not null,
- TARGET_ID varchar(43) not null,
+ TARGET_ID varchar(255) not null,
SOURCE varchar(50) not null,
SOURCE_ID varchar(43),
MESSAGE jsonb,
diff --git a/model/src/main/java/org/openremote/model/notification/SentNotification.java b/model/src/main/java/org/openremote/model/notification/SentNotification.java
index eb11f89b3..5b9387606 100644
--- a/model/src/main/java/org/openremote/model/notification/SentNotification.java
+++ b/model/src/main/java/org/openremote/model/notification/SentNotification.java
@@ -42,11 +42,11 @@ public class SentNotification {
protected String name;
@NotNull
- @Column(name = "TYPE", nullable = false)
+ @Column(name = "TYPE", nullable = false, length = 50)
protected String type;
@NotNull
- @Column(name = "TARGET")
+ @Column(name = "TARGET", length = 50)
@Enumerated(EnumType.STRING)
protected Notification.TargetType target;
@@ -55,11 +55,11 @@ public class SentNotification {
protected String targetId;
@NotNull()
- @Column(name = "SOURCE")
+ @Column(name = "SOURCE", length = 50)
@Enumerated(EnumType.STRING)
protected Notification.Source source;
- @Column(name = "SOURCE_ID")
+ @Column(name = "SOURCE_ID", length = 43)
protected String sourceId;
@Column(name = "MESSAGE", columnDefinition = "jsonb")
diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
index ec0a70a2d..a88d0279b 100644
--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
@@ -526,11 +526,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
def "Check email notification functionality"() {
- def notificationIds = []
- def notificationTargetTypes = []
- def notificationTargetIds = []
- def notificationMessages = []
- def toAddresses = []
+ List<Email> sentEmails = []
given: "the container environment is started with the mock handler"
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
@@ -543,23 +539,12 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
and: "a mock email notification handler"
EmailNotificationHandler mockEmailNotificationHandler = Spy(emailNotificationHandler)
mockEmailNotificationHandler.isValid() >> true
- mockEmailNotificationHandler.sendMessage(_ as Long, _ as Notification.Source, _ as String, _ as Notification.Target, _ as AbstractNotificationMessage) >> {
- id, source, sourceId, target, message ->
- notificationIds << id
- notificationTargetTypes << target.type
- notificationTargetIds << target.id
- notificationMessages << message
- callRealMethod()
- }
- // Assume sent to FCM
+ // Log email and assume sent to SMTP Server
mockEmailNotificationHandler.sendMessage(_ as Email) >> {
- email ->
- if (email instanceof List) {
- def e = ((List)email).get(0) as Email
- e.recipients.forEach {toAddresses.add(it.address)}
- return NotificationSendResult.success()
- }
+ Email email ->
+ sentEmails << email
+ return NotificationSendResult.success()
}
notificationService.notificationHandlerMap.put(emailNotificationHandler.getTypeName(), mockEmailNotificationHandler)
@@ -572,14 +557,12 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
then: "the email should have been sent to all tenant users"
conditions.eventually {
- assert notificationMessages.size() == 4
- assert ((EmailNotificationMessage) notificationMessages.get(0)).getText() == "Hello world!"
- assert ((EmailNotificationMessage) notificationMessages.get(0)).getSubject() == "Test"
- assert ((EmailNotificationMessage) notificationMessages.get(1)).getText() == "Hello world!"
- assert ((EmailNotificationMessage) notificationMessages.get(1)).getSubject() == "Test"
- assert toAddresses.any { it == "[email protected]" }
- assert toAddresses.any { it == "[email protected]" }
- assert toAddresses.any { it == "[email protected]" }
+ assert sentEmails.size() == 4
+ assert sentEmails.every {it.getPlainText() == "Hello world!"}
+ assert sentEmails.every {it.getSubject() == "Test"}
+ assert sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}
+ assert sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}
+ assert sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}
}
when: "an email attribute is added to an asset"
@@ -588,14 +571,31 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
kitchen = assetStorageService.merge(kitchen)
and: "an email notification is sent to a parent asset"
+ ((EmailNotificationMessage)notification.message).subject = "Test 2"
notification.setTargets([new Notification.Target(Notification.TargetType.ASSET, managerTestSetup.apartment1Id)])
notificationService.sendNotification(notification)
then: "the child asset with the email attribute should have been sent an email"
conditions.eventually {
- assert notificationMessages.size() == 5
- assert toAddresses.size() == 5
- assert toAddresses.any { it == "[email protected]" }
+ assert sentEmails.size() == 5
+ assert sentEmails.get(4).getSubject() == "Test 2"
+ assert sentEmails.get(4).getRecipients().get(0).getAddress() == "[email protected]"
+ }
+
+ when: "an email is sent to a custom target"
+ ((EmailNotificationMessage)notification.message).subject = "Test Custom"
+ notification.setTargets([new Notification.Target(Notification.TargetType.CUSTOM, "[email protected];to:[email protected];cc:[email protected];bcc:[email protected]")])
+ notificationService.sendNotification(notification)
+
+ then: "the email should have been sent to all custom recipients"
+ conditions.eventually {
+ assert sentEmails.size() == 6
+ assert sentEmails.get(5).getSubject() == "Test Custom"
+ assert sentEmails.get(5).getRecipients().size() == 4
+ assert sentEmails.get(5).getRecipients().any{ it.type == javax.mail.Message.RecipientType.TO && it.address == "[email protected]"}
+ assert sentEmails.get(5).getRecipients().any{ it.type == javax.mail.Message.RecipientType.TO && it.address == "[email protected]"}
+ assert sentEmails.get(5).getRecipients().any{ it.type == javax.mail.Message.RecipientType.CC && it.address == "[email protected]"}
+ assert sentEmails.get(5).getRecipients().any{ it.type == javax.mail.Message.RecipientType.BCC && it.address == "[email protected]"}
}
cleanup: "the mock is removed"