Skip to content

armgit / core

Public Imported from GitHub
0

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

Added modification date to console device token, exposed tokens on resource API

Christian Bauer44a95a637da0
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/AdminModule.java b/manager/client/src/main/java/org/openremote/manager/client/admin/AdminModule.java
index 689ebec0e..4268c0b37 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/AdminModule.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/AdminModule.java
@@ -28,6 +28,7 @@ import org.openremote.manager.client.admin.navigation.AdminNavigationPresenter;
import org.openremote.manager.client.admin.syslog.*;
import org.openremote.manager.client.admin.tenant.*;
import org.openremote.manager.client.admin.users.*;
+import org.openremote.manager.shared.notification.NotificationResource;
import org.openremote.manager.shared.security.UserResource;
import org.openremote.manager.shared.syslog.SyslogResource;
@@ -66,4 +67,10 @@ public class AdminModule extends AbstractGinModule {
return $wnd.UserResource;
}-*/;
+ @Provides
+ @Singleton
+ public native NotificationResource getNotificationResource() /*-{
+ return $wnd.NotificationResource;
+ }-*/;
+
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/DeviceNotificationTokenMapper.java b/manager/client/src/main/java/org/openremote/manager/client/admin/DeviceNotificationTokenMapper.java
new file mode 100644
index 000000000..0bab6be5e
--- /dev/null
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/DeviceNotificationTokenMapper.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.client.admin;
+
+import com.github.nmorel.gwtjackson.client.ObjectMapper;
+import com.github.nmorel.gwtjackson.client.annotation.JsonMixIns;
+import org.openremote.manager.client.interop.jackson.DefaultJsonMixin;
+import org.openremote.manager.shared.http.EntityReader;
+import org.openremote.manager.shared.notification.DeviceNotificationToken;
+
+import java.util.List;
+
+@JsonMixIns({@JsonMixIns.JsonMixIn(target = DeviceNotificationToken.class, mixIn = DefaultJsonMixin.class)})
+public interface DeviceNotificationTokenMapper
+ extends ObjectMapper<List<DeviceNotificationToken>>,
+ EntityReader<List<DeviceNotificationToken>> {
+}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java
index fc13fdb27..824d5f496 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java
@@ -25,6 +25,7 @@ import org.openremote.manager.client.admin.*;
import org.openremote.manager.client.admin.navigation.AdminNavigation;
import org.openremote.manager.client.event.ShowSuccessEvent;
import org.openremote.manager.client.mvp.AppActivity;
+import org.openremote.manager.shared.notification.NotificationResource;
import org.openremote.manager.shared.security.Credential;
import org.openremote.manager.shared.security.Role;
import org.openremote.manager.shared.security.User;
@@ -39,6 +40,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
+import java.util.logging.Logger;
import static org.openremote.manager.client.http.RequestExceptionHandler.handleRequestException;
@@ -46,11 +48,15 @@ public class AdminUserActivity
extends AbstractAdminActivity<AdminUserPlace, AdminUser>
implements AdminUser.Presenter {
+ private static final Logger LOG = Logger.getLogger(AdminUserActivity.class.getName());
+
final protected Environment environment;
final protected UserResource userResource;
final protected UserMapper userMapper;
final protected CredentialMapper credentialMapper;
final protected RoleArrayMapper roleArrayMapper;
+ final protected NotificationResource notificationResource;
+ final protected DeviceNotificationTokenMapper deviceNotificationTokenMapper;
final protected Consumer<ConstraintViolation[]> validationErrorHandler = violations -> {
for (ConstraintViolation violation : violations) {
@@ -92,13 +98,17 @@ public class AdminUserActivity
UserResource userResource,
UserMapper userMapper,
CredentialMapper credentialMapper,
- RoleArrayMapper roleArrayMapper) {
+ RoleArrayMapper roleArrayMapper,
+ NotificationResource notificationResource,
+ DeviceNotificationTokenMapper deviceNotificationTokenMapper) {
super(adminView, adminNavigationPresenter, view);
this.environment = environment;
this.userResource = userResource;
this.userMapper = userMapper;
this.credentialMapper = credentialMapper;
this.roleArrayMapper = roleArrayMapper;
+ this.notificationResource = notificationResource;
+ this.deviceNotificationTokenMapper = deviceNotificationTokenMapper;
}
@Override
@@ -297,7 +307,7 @@ public class AdminUserActivity
user -> {
this.user = user;
this.realm = user.getRealm();
- loadRoles(() -> {
+ loadRoles(() -> loadDeviceRegistrations(() -> {
writeToView();
adminContent.setFormBusy(false);
adminContent.enableCreate(false);
@@ -305,7 +315,7 @@ public class AdminUserActivity
adminContent.enableDelete(true);
adminContent.enableResetPassword(true);
adminContent.setUsernameEditEnabled(false);
- });
+ }));
},
ex -> handleRequestException(ex, environment)
);
@@ -328,6 +338,19 @@ public class AdminUserActivity
);
}
+ protected void loadDeviceRegistrations(Runnable onComplete) {
+ environment.getRequestService().execute(
+ deviceNotificationTokenMapper,
+ requestParams -> notificationResource.getDeviceTokens(requestParams, userId),
+ 200,
+ deviceNotificationTokens -> {
+ // TODO Display device tokens
+ onComplete.run();
+ },
+ ex -> handleRequestException(ex, environment)
+ );
+ }
+
protected void writeToView() {
adminContent.setUsername(user.getUsername());
adminContent.setFirstName(user.getFirstName());
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.java b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.java
index ce30f6797..7a79bf634 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.java
@@ -27,6 +27,7 @@ import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;
import com.google.inject.Provider;
+import org.openremote.manager.client.Environment;
import org.openremote.manager.client.app.dialog.Confirmation;
import org.openremote.manager.client.widget.FormCheckBox;
import org.openremote.manager.client.widget.FormGroup;
@@ -90,6 +91,9 @@ public class AdminUserImpl extends FormViewImpl implements AdminUser {
@UiField
FlowPanel rolesPanel;
+ @UiField
+ FlowPanel registeredDevicesContainer;
+
@UiField
PushButton updateButton;
@@ -107,10 +111,13 @@ public class AdminUserImpl extends FormViewImpl implements AdminUser {
protected Presenter presenter;
@Inject
- public AdminUserImpl(Provider<Confirmation> confirmationDialogProvider) {
+ public AdminUserImpl(Environment environment,
+ Provider<Confirmation> confirmationDialogProvider) {
super(confirmationDialogProvider);
UI ui = GWT.create(UI.class);
initWidget(ui.createAndBindUi(this));
+
+ clearRegisteredDevices(false);
}
@Override
@@ -124,6 +131,7 @@ public class AdminUserImpl extends FormViewImpl implements AdminUser {
enabledCheckBox.setValue(false);
resetPasswordInput.setValue(null);
resetPasswordControlInput.setValue(null);
+ clearRegisteredDevices(false);
}
}
@@ -288,11 +296,13 @@ public class AdminUserImpl extends FormViewImpl implements AdminUser {
@Override
public void enableCreate(boolean enable) {
createButton.setVisible(enable);
+ clearRegisteredDevices(!enable);
}
@Override
public void enableUpdate(boolean enable) {
updateButton.setVisible(enable);
+ clearRegisteredDevices(enable);
}
@Override
@@ -323,4 +333,13 @@ public class AdminUserImpl extends FormViewImpl implements AdminUser {
if (presenter != null)
presenter.cancel();
}
+
+ protected void clearRegisteredDevices(boolean addEmptyMessage) {
+ registeredDevicesContainer.clear();
+ if (addEmptyMessage) {
+ Label emptyLabel = new Label(managerMessages.noRegisteredDevices());
+ emptyLabel.addStyleName(widgetStyle.FormListEmptyMessage());
+ registeredDevicesContainer.add(emptyLabel);
+ }
+ }
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.ui.xml b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.ui.xml
index fdbfb6356..2f837aadb 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.ui.xml
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.ui.xml
@@ -21,7 +21,7 @@
<w:Form ui:field="form" addStyleNames="flex">
<g:HTMLPanel ui:field="mainContent"
- styleName="flex layout vertical {widgetStyle.MainContent} scrollable">
+ styleName="flex-none layout vertical {widgetStyle.MainContent}">
<g:FlowPanel
styleName="layout horizontal {style.formMessages} {widgetStyle.FormMessages} success"
@@ -145,6 +145,9 @@
</g:HTMLPanel>
+ <g:FlowPanel ui:field="registeredDevicesContainer"
+ styleName="flex layout vertical {widgetStyle.MainContent} {widgetStyle.FormList}"/>
+
<g:FlowPanel styleName="flex-none layout vertical {widgetStyle.MainContent}">
<w:FormGroup>
<w:field>
diff --git a/manager/client/src/main/java/org/openremote/manager/client/i18n/ManagerMessages.java b/manager/client/src/main/java/org/openremote/manager/client/i18n/ManagerMessages.java
index 3e41ce723..5572568f7 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/i18n/ManagerMessages.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/i18n/ManagerMessages.java
@@ -448,4 +448,6 @@ public interface ManagerMessages extends Messages {
String waitingForStatus();
String noAttributesLinkedToSimulator();
+
+ String noRegisteredDevices();
}
diff --git a/manager/client/src/main/resources/org/openremote/manager/client/i18n/ManagerMessages.properties b/manager/client/src/main/resources/org/openremote/manager/client/i18n/ManagerMessages.properties
index 0e670ff02..39e2ae154 100644
--- a/manager/client/src/main/resources/org/openremote/manager/client/i18n/ManagerMessages.properties
+++ b/manager/client/src/main/resources/org/openremote/manager/client/i18n/ManagerMessages.properties
@@ -371,4 +371,5 @@ addConverterValue=Add value
deleteConverterValue=Delete value
linkAssetUsers=Link asset users
waitingForStatus=Waiting for connection status...
-noAttributesLinkedToSimulator=No attributes are linked to this simulator protocol configuration.
\ No newline at end of file
+noAttributesLinkedToSimulator=No attributes are linked to this simulator protocol configuration.
+noRegisteredDevices=The user never logged in on any console device.
\ No newline at end of file
diff --git a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java
index 05919997f..92852e1a2 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java
@@ -21,17 +21,22 @@ package org.openremote.manager.server.notification;
import org.openremote.container.web.WebResource;
import org.openremote.manager.shared.http.RequestParams;
+import org.openremote.manager.shared.notification.DeviceNotificationToken;
import org.openremote.model.notification.AlertNotification;
import org.openremote.manager.shared.notification.NotificationResource;
import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
import java.util.List;
+import java.util.logging.Logger;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
public class NotificationResourceImpl extends WebResource implements NotificationResource {
+ private static final Logger LOG = Logger.getLogger(NotificationResourceImpl.class.getName());
+
final protected NotificationService notificationService;
public NotificationResourceImpl(NotificationService notificationService) {
@@ -46,6 +51,15 @@ public class NotificationResourceImpl extends WebResource implements Notificatio
notificationService.storeDeviceToken(deviceId, getUserId(), token, deviceType);
}
+ @Override
+ public List<DeviceNotificationToken> getDeviceTokens(RequestParams requestParams, String userId) {
+ if (!isSuperUser()) {
+ LOG.fine("Forbidden access for user '" + getUsername() + "', can't get device tokens for user: " + userId);
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+ return notificationService.findAllTokenForUser(userId);
+ }
+
@Override
public void storeAlertNotification(AlertNotification alertNotification) {
if (alertNotification == null) {
diff --git a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java
index d94c78365..3c1fe153e 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java
@@ -25,7 +25,9 @@ import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.persistence.PersistenceService;
+import org.openremote.container.timer.TimerService;
import org.openremote.container.web.WebService;
+import org.openremote.manager.shared.notification.DeviceNotificationToken;
import org.openremote.model.notification.AlertNotification;
import org.openremote.model.notification.DeliveryStatus;
import org.openremote.model.user.UserQuery;
@@ -35,7 +37,9 @@ import javax.persistence.Query;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;
+import java.util.Date;
import java.util.List;
+import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -47,18 +51,20 @@ public class NotificationService implements ContainerService {
public static final String NOTIFICATION_FIREBASE_URL = "NOTIFICATION_FIREBASE_URL";
public static final String NOTIFICATION_FIREBASE_URL_DEFAULT = "https://fcm.googleapis.com/fcm/send";
+ protected TimerService timerService;
protected PersistenceService persistenceService;
protected ResteasyWebTarget firebaseTarget;
private String fcmKey;
@Override
public void init(Container container) throws Exception {
+ this.timerService = container.getService(TimerService.class);
+ this.persistenceService = container.getService(PersistenceService.class);
fcmKey = container.getConfig().get(NOTIFICATION_FIREBASE_API_KEY);
if (fcmKey == null) {
LOG.info(NOTIFICATION_FIREBASE_API_KEY + " not defined, can not send notifications to user devices");
}
- this.persistenceService = container.getService(PersistenceService.class);
container.getService(WebService.class).getApiSingletons().add(
new NotificationResourceImpl(this)
@@ -80,21 +86,24 @@ public class NotificationService implements ContainerService {
persistenceService.doTransaction(entityManager -> {
DeviceNotificationToken.Id id = new DeviceNotificationToken.Id(deviceId, userId);
DeviceNotificationToken deviceToken = new DeviceNotificationToken(id, token, deviceType);
+ deviceToken.setUpdatedOn(new Date(timerService.getCurrentTimeMillis()));
entityManager.merge(deviceToken);
});
}
- public String findDeviceToken(String deviceId, String userId) {
+ public Optional<DeviceNotificationToken> findDeviceToken(String deviceId, String userId) {
return persistenceService.doReturningTransaction(entityManager -> {
DeviceNotificationToken.Id id = new DeviceNotificationToken.Id(deviceId, userId);
DeviceNotificationToken deviceToken = entityManager.find(DeviceNotificationToken.class, id);
- return deviceToken != null ? deviceToken.getToken() : null;
+ return Optional.ofNullable(deviceToken);
});
}
public List<DeviceNotificationToken> findAllTokenForUser(String userId) {
return persistenceService.doReturningTransaction(entityManager -> {
- Query query = entityManager.createQuery("SELECT dnt FROM DeviceNotificationToken dnt WHERE dnt.id.userId =:userId");
+ Query query = entityManager.createQuery(
+ "SELECT dnt FROM DeviceNotificationToken dnt WHERE dnt.id.userId =:userId order by dnt.updatedOn desc"
+ );
query.setParameter("userId", userId);
return query.getResultList();
});
diff --git a/manager/server/src/main/resources/META-INF/persistence.xml b/manager/server/src/main/resources/META-INF/persistence.xml
index ef1a06bac..33dc9c760 100644
--- a/manager/server/src/main/resources/META-INF/persistence.xml
+++ b/manager/server/src/main/resources/META-INF/persistence.xml
@@ -10,7 +10,7 @@
<class>org.openremote.container.persistence</class>
<class>org.openremote.manager.server.asset.ServerAsset</class>
- <class>org.openremote.manager.server.notification.DeviceNotificationToken</class>
+ <class>org.openremote.manager.shared.notification.DeviceNotificationToken</class>
<class>org.openremote.model.notification.AlertNotification</class>
<class>org.openremote.model.notification.AlertAction</class>
<class>org.openremote.manager.server.security.UserConfiguration</class>
diff --git a/manager/server/src/main/java/org/openremote/manager/server/notification/DeviceNotificationToken.java b/manager/shared/src/main/java/org/openremote/manager/shared/notification/DeviceNotificationToken.java
similarity index 86%
rename from manager/server/src/main/java/org/openremote/manager/server/notification/DeviceNotificationToken.java
rename to manager/shared/src/main/java/org/openremote/manager/shared/notification/DeviceNotificationToken.java
index 6c1c5f934..a33fcb249 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/notification/DeviceNotificationToken.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/notification/DeviceNotificationToken.java
@@ -17,13 +17,11 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-package org.openremote.manager.server.notification;
+package org.openremote.manager.shared.notification;
-import javax.persistence.Column;
-import javax.persistence.EmbeddedId;
-import javax.persistence.Entity;
-import javax.persistence.Table;
+import javax.persistence.*;
import java.io.Serializable;
+import java.util.Date;
@Entity
@Table(name = "DEVICE_NOTIFICATION_TOKEN")
@@ -88,9 +86,13 @@ public class DeviceNotificationToken {
@Column(name = "TOKEN", nullable = false, length = 4096)
protected String token;
- @Column(name = "DEVICE_TYPE", nullable = true)
+ @Column(name = "DEVICE_TYPE")
protected String deviceType;
+ @Temporal(TemporalType.TIMESTAMP)
+ @Column(name = "UPDATED_ON", updatable = false, nullable = false, columnDefinition = "TIMESTAMP WITH TIME ZONE")
+ protected Date updatedOn;
+
public DeviceNotificationToken() {
}
@@ -124,12 +126,21 @@ public class DeviceNotificationToken {
this.deviceType = deviceType;
}
+ public Date getUpdatedOn() {
+ return updatedOn;
+ }
+
+ public void setUpdatedOn(Date updatedOn) {
+ this.updatedOn = updatedOn;
+ }
+
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"id=" + id +
", token='" + token + '\'' +
", deviceType='" + deviceType + '\'' +
+ ", updatedOn=" + updatedOn +
'}';
}
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java b/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java
index f15f220d9..256008563 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java
@@ -49,6 +49,16 @@ public interface NotificationResource {
@FormParam("token") String token,
@FormParam("device_type") String deviceType);
+ /**
+ * Only the superuser can call this operation.
+ */
+ @GET
+ @Path("token/{userId}")
+ @Produces(APPLICATION_JSON)
+ @RolesAllowed({"read:admin"})
+ List<DeviceNotificationToken> getDeviceTokens(@BeanParam RequestParams requestParams,
+ @PathParam("userId") String userId);
+
@POST
@Path("alert")
@SuccessStatusCode(204)
diff --git a/manager/test/src/test/groovy/org/openremote/test/admin/AdminUsersActivityTest.groovy b/manager/test/src/test/groovy/org/openremote/test/admin/AdminUsersActivityTest.groovy
index 3e1ac1d88..13ba56c80 100644
--- a/manager/test/src/test/groovy/org/openremote/test/admin/AdminUsersActivityTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/admin/AdminUsersActivityTest.groovy
@@ -21,6 +21,8 @@ import org.openremote.manager.client.style.WidgetStyle
import org.openremote.manager.server.setup.AbstractKeycloakSetup
import org.openremote.manager.server.setup.SetupService
import org.openremote.manager.shared.http.EntityReader
+import org.openremote.manager.shared.notification.DeviceNotificationToken
+import org.openremote.manager.shared.notification.NotificationResource
import org.openremote.manager.shared.security.*
import org.openremote.manager.shared.validation.ConstraintViolationReport
import org.openremote.model.event.Event
@@ -140,6 +142,9 @@ class AdminUsersActivityTest extends Specification implements ManagerContainerTr
def userResource = Stub(UserResource) {
_(*_) >> { callResourceProxy(container.JSON, clientTarget, getDelegate()) }
}
+ def notificationResource = Stub(NotificationResource) {
+ _(*_) >> { callResourceProxy(container.JSON, clientTarget, getDelegate()) }
+ }
def adminUsersView = Mock(AdminUsers) {
setTenants(_, _) >> { Tenant[] tenants, String selectedRealm ->
@@ -161,6 +166,7 @@ class AdminUsersActivityTest extends Specification implements ManagerContainerTr
def userMapper = new ClientObjectMapper(container.JSON, User.class) as UserMapper
def credentialMapper = new ClientObjectMapper(container.JSON, Credential.class) as CredentialMapper
def roleArrayMapper = new ClientObjectMapper(container.JSON, Role[].class) as RoleArrayMapper
+ def notificationTokenMapper = new ClientObjectMapper(container.JSON, List.class) as DeviceNotificationTokenMapper
AdminUserActivity adminUserActivity
and: "An activity management configuration"
@@ -193,7 +199,7 @@ class AdminUsersActivityTest extends Specification implements ManagerContainerTr
},
{
adminUserActivity = new AdminUserActivity(
- environment, adminView, adminNavigationPresenter, adminUserView, userResource, userMapper, credentialMapper, roleArrayMapper
+ environment, adminView, adminNavigationPresenter, adminUserView, userResource, userMapper, credentialMapper, roleArrayMapper, notificationResource, notificationTokenMapper
)
return adminUserActivity
},
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentNotifyAlarmTriggerTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentNotifyAlarmTriggerTest.groovy
index 31d9c8cb0..32a182172 100644
--- a/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentNotifyAlarmTriggerTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentNotifyAlarmTriggerTest.groovy
@@ -82,7 +82,7 @@ class ApartmentNotifyAlarmTriggerTest extends Specification implements ManagerCo
notificationResource.storeDeviceToken(null, "device123", "token123", "ANDROID")
then: "the tokens should be in the database"
- notificationService.findDeviceToken("device123", keycloakDemoSetup.testuser3Id) == "token123"
+ notificationService.findDeviceToken("device123", keycloakDemoSetup.testuser3Id).get().token == "token123"
when: "the alarm is enabled"
setPseudoClocksToRealTime(container, apartment1Engine)
diff --git a/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy b/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy
index ae940fe8e..e98d11047 100644
--- a/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy
@@ -1,5 +1,6 @@
package org.openremote.test.user
+import org.openremote.container.timer.TimerService
import org.openremote.manager.server.notification.NotificationService
import org.openremote.manager.server.setup.SetupService
import org.openremote.manager.server.setup.builtin.KeycloakDemoSetup
@@ -12,7 +13,12 @@ import org.openremote.model.user.UserQuery
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
+import static org.openremote.container.util.MapAccess.getString
+import static org.openremote.manager.server.setup.AbstractKeycloakSetup.SETUP_KEYCLOAK_ADMIN_PASSWORD
+import static org.openremote.manager.server.setup.AbstractKeycloakSetup.SETUP_KEYCLOAK_ADMIN_PASSWORD_DEFAULT
import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID
+import static org.openremote.model.Constants.MASTER_REALM
+import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER
class NotificationServiceTest extends Specification implements ManagerContainerTrait {
@@ -35,6 +41,15 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
"testuser3"
).token
+ and: "an authenticated superuser"
+ def adminAccessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), SETUP_KEYCLOAK_ADMIN_PASSWORD, SETUP_KEYCLOAK_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
def notificationAlert = new AlertNotification(
title: "The Title",
appUrl: '#test',
@@ -48,7 +63,10 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
notificationAlert.addAction(alertAction)
and: "the notification resource"
+ // For user operations
def notificationResource = getClientTarget(serverUri(serverPort), realm, accessToken).proxy(NotificationResource.class)
+ // For superuser operations
+ def adminNotificationResource = getClientTarget(serverUri(serverPort), MASTER_REALM, adminAccessToken).proxy(NotificationResource.class)
expect: "there should be no user registered to receive notifications"
notificationService.findAllUsersWithToken().isEmpty()
@@ -58,8 +76,15 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
notificationResource.storeDeviceToken(null, "device456", "token456", "ANDROID")
then: "the tokens should be in the database"
- notificationService.findDeviceToken("device123", keycloakDemoSetup.testuser3Id) == "token123"
- notificationService.findDeviceToken("device456", keycloakDemoSetup.testuser3Id) == "token456"
+ def tokens = adminNotificationResource.getDeviceTokens(null, keycloakDemoSetup.testuser3Id)
+ tokens[0].id.deviceId == "device456"
+ tokens[0].deviceType == "ANDROID"
+ tokens[0].token == "token456"
+ tokens[0].updatedOn.time <= container.getService(TimerService.class).currentTimeMillis
+ tokens[1].id.deviceId == "device123"
+ tokens[1].deviceType == "ANDROID"
+ tokens[1].token == "token123"
+ tokens[1].updatedOn.time <= container.getService(TimerService.class).currentTimeMillis
and: "test user must be registered to receive notifications"
notificationService.findAllUsersWithToken().size() == 1
@@ -86,7 +111,8 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
notificationResource.storeDeviceToken(null, "device456", "token789", "ANDROID")
then: "the updated token should be in the database"
- notificationService.findDeviceToken("device456", keycloakDemoSetup.testuser3Id) == "token789"
+ def thirdToken = notificationService.findDeviceToken("device456", keycloakDemoSetup.testuser3Id)
+ thirdToken.get().token == "token789"
notificationService.findAllTokenForUser(keycloakDemoSetup.testuser3Id).size() == 2
when: "the Alert Notification is stored"