From 1c706bf40441f12bbf6805e5e06520ee768f1192 Mon Sep 17 00:00:00 2001 From: Viet Nguyen Date: Thu, 23 Nov 2023 15:53:16 +0700 Subject: [PATCH] fix: getScheduledNotifications() should take in DataSetNotificationTrigger (#15760) * fix: getScheduledNotifications() should take in DataSetNotificationTrigger (#15752) * code styles * code style --- .../DataSetNotificationTemplateService.java | 3 +- .../DataSetNotificationTemplateStore.java | 3 +- .../DefaultDataSetNotificationService.java | 3 +- ...ultDataSetNotificationTemplateService.java | 4 +- ...rnateDataSetNotificationTemplateStore.java | 4 +- .../DataSetNotificationTemplateStoreTest.java | 97 +++++++++++++++++++ .../org/hisp/dhis/DhisConvenienceTest.java | 23 +++-- 7 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateStoreTest.java diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateService.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateService.java index 963c07af7909..7c115460649b 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateService.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateService.java @@ -29,7 +29,6 @@ import java.util.List; import org.hisp.dhis.dataset.DataSet; -import org.hisp.dhis.program.notification.NotificationTrigger; /** Created by zubair@dhis2.org on 20.07.17. */ public interface DataSetNotificationTemplateService { @@ -39,7 +38,7 @@ public interface DataSetNotificationTemplateService { List getCompleteNotifications(DataSet dataSet); - List getScheduledNotifications(NotificationTrigger trigger); + List getScheduledNotifications(DataSetNotificationTrigger trigger); List getAll(); diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateStore.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateStore.java index fce43cd27122..e0667bbe0290 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateStore.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateStore.java @@ -30,7 +30,6 @@ import java.util.List; import org.hisp.dhis.common.IdentifiableObjectStore; import org.hisp.dhis.dataset.DataSet; -import org.hisp.dhis.program.notification.NotificationTrigger; /** Created by zubair@dhis2.org on 13.07.17. */ public interface DataSetNotificationTemplateStore @@ -38,5 +37,5 @@ public interface DataSetNotificationTemplateStore List getNotificationsByTriggerType( DataSet dataSet, DataSetNotificationTrigger trigger); - List getScheduledNotifications(NotificationTrigger trigger); + List getScheduledNotifications(DataSetNotificationTrigger trigger); } diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/DefaultDataSetNotificationService.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/DefaultDataSetNotificationService.java index f852dbc0306a..b3d207be141c 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/DefaultDataSetNotificationService.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/DefaultDataSetNotificationService.java @@ -60,7 +60,6 @@ import org.hisp.dhis.program.message.ProgramMessage; import org.hisp.dhis.program.message.ProgramMessageRecipients; import org.hisp.dhis.program.message.ProgramMessageService; -import org.hisp.dhis.program.notification.NotificationTrigger; import org.hisp.dhis.user.User; import org.hisp.dhis.user.UserGroup; import org.hisp.dhis.util.DateUtils; @@ -188,7 +187,7 @@ public void sendScheduledDataSetNotificationsForDay(Date day) { List batches = new ArrayList<>(); List scheduledTemplates = - dsntService.getScheduledNotifications(NotificationTrigger.SCHEDULED_DAYS_DUE_DATE); + dsntService.getScheduledNotifications(DataSetNotificationTrigger.SCHEDULED_DAYS); if (scheduledTemplates == null || scheduledTemplates.isEmpty()) { return; diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/DefaultDataSetNotificationTemplateService.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/DefaultDataSetNotificationTemplateService.java index 52f538b44a25..7ddc021c1f0b 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/DefaultDataSetNotificationTemplateService.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/DefaultDataSetNotificationTemplateService.java @@ -31,7 +31,6 @@ import java.util.List; import org.hisp.dhis.dataset.DataSet; -import org.hisp.dhis.program.notification.NotificationTrigger; import org.springframework.stereotype.Service; /** Created by zubair@dhis2.org on 20.07.17. */ @@ -63,7 +62,8 @@ public List getCompleteNotifications(DataSet dataSe } @Override - public List getScheduledNotifications(NotificationTrigger trigger) { + public List getScheduledNotifications( + DataSetNotificationTrigger trigger) { return store.getScheduledNotifications(trigger); } diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/HibernateDataSetNotificationTemplateStore.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/HibernateDataSetNotificationTemplateStore.java index 204c2cdc02f0..36059632c539 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/HibernateDataSetNotificationTemplateStore.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/notifications/HibernateDataSetNotificationTemplateStore.java @@ -32,7 +32,6 @@ import org.hibernate.SessionFactory; import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore; import org.hisp.dhis.dataset.DataSet; -import org.hisp.dhis.program.notification.NotificationTrigger; import org.hisp.dhis.security.acl.AclService; import org.hisp.dhis.user.CurrentUserService; import org.springframework.context.ApplicationEventPublisher; @@ -73,7 +72,8 @@ public List getNotificationsByTriggerType( } @Override - public List getScheduledNotifications(NotificationTrigger trigger) { + public List getScheduledNotifications( + DataSetNotificationTrigger trigger) { CriteriaBuilder builder = getCriteriaBuilder(); return getList( diff --git a/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateStoreTest.java b/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateStoreTest.java new file mode 100644 index 000000000000..a13033fe1a22 --- /dev/null +++ b/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/notifications/DataSetNotificationTemplateStoreTest.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2004-2023, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.dataset.notifications; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Set; +import org.hisp.dhis.TransactionalIntegrationTest; +import org.hisp.dhis.dataset.DataSet; +import org.hisp.dhis.dataset.DataSetStore; +import org.hisp.dhis.notification.SendStrategy; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +class DataSetNotificationTemplateStoreTest extends TransactionalIntegrationTest { + + @Autowired private DataSetNotificationTemplateStore store; + @Autowired private DataSetStore dataSetStore; + + @Test + void testGetNotificationsByTriggerTypeAndDataSet() { + DataSet dataSet = createDataSet('A'); + dataSetStore.save(dataSet); + + DataSetNotificationTemplate template = + createDataSetNotificationTemplate( + "A", + DataSetNotificationRecipient.USER_GROUP, + DataSetNotificationTrigger.DATA_SET_COMPLETION, + 1, + SendStrategy.SINGLE_NOTIFICATION); + template.setDataSets(Set.of(dataSet)); + store.save(template); + + assertEquals( + 1, + store + .getNotificationsByTriggerType(dataSet, DataSetNotificationTrigger.DATA_SET_COMPLETION) + .size()); + } + + @Test + void testGetScheduledNotificationsValid() { + DataSetNotificationTemplate template = + createDataSetNotificationTemplate( + "A", + DataSetNotificationRecipient.USER_GROUP, + DataSetNotificationTrigger.DATA_SET_COMPLETION, + 1, + SendStrategy.SINGLE_NOTIFICATION); + store.save(template); + + Assertions.assertEquals( + 1, store.getScheduledNotifications(DataSetNotificationTrigger.DATA_SET_COMPLETION).size()); + } + + @Test + void testGetScheduledNotificationsNotExist() { + DataSetNotificationTemplate template = + createDataSetNotificationTemplate( + "A", + DataSetNotificationRecipient.USER_GROUP, + DataSetNotificationTrigger.DATA_SET_COMPLETION, + 1, + SendStrategy.SINGLE_NOTIFICATION); + store.save(template); + + Assertions.assertEquals( + 0, store.getScheduledNotifications(DataSetNotificationTrigger.SCHEDULED_DAYS).size()); + } +} diff --git a/dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java b/dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java index a9c8bd8ac2db..d155e014f74b 100644 --- a/dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java +++ b/dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java @@ -2123,16 +2123,19 @@ public static DataSetNotificationTemplate createDataSetNotificationTemplate( DataSetNotificationTrigger dataSetNotificationTrigger, Integer relativeScheduledDays, SendStrategy sendStrategy) { - return new DataSetNotificationTemplate( - Sets.newHashSet(), - Sets.newHashSet(), - "Message", - notificationRecipient, - dataSetNotificationTrigger, - "Subject", - null, - relativeScheduledDays, - sendStrategy); + DataSetNotificationTemplate dst = + new DataSetNotificationTemplate( + Set.of(), + Set.of(), + "Message", + notificationRecipient, + dataSetNotificationTrigger, + "Subject", + null, + relativeScheduledDays, + sendStrategy); + dst.setName(name); + return dst; } public static ValidationNotificationTemplate createValidationNotificationTemplate(String name) {