From 6dab365f0e5a6ec81d7b8801d7e1ba45008fc6cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20G=C3=A9raud?= Date: Mon, 8 Jan 2018 17:35:18 +0100 Subject: [PATCH] feat(notifications): add user notifications fix gravitee-io/issues#929 --- pom.xml | 2 +- ...ortalNotificationConfigRepositoryTest.java | 149 ++++++++++++++++++ .../PortalNotificationRepositoryTest.java | 78 +++++++++ .../config/AbstractRepositoryTest.java | 11 +- .../MockTestRepositoryConfiguration.java | 89 +++++++++++ .../portalNotifications.json | 23 +++ .../portalNotificationConfigs.json | 50 ++++++ 7 files changed, 400 insertions(+), 2 deletions(-) create mode 100644 src/test/java/io/gravitee/repository/PortalNotificationConfigRepositoryTest.java create mode 100644 src/test/java/io/gravitee/repository/PortalNotificationRepositoryTest.java create mode 100644 src/test/resources/data/portalnotification-tests/portalNotifications.json create mode 100644 src/test/resources/data/portalnotificationConfig-tests/portalNotificationConfigs.json diff --git a/pom.xml b/pom.xml index cdaea7fb..0ecadba0 100644 --- a/pom.xml +++ b/pom.xml @@ -32,7 +32,7 @@ Gravitee.io APIM - Repository - Test - 1.13.0 + 1.14.0-SNAPSHOT 1.3.2 1.9.13 1 diff --git a/src/test/java/io/gravitee/repository/PortalNotificationConfigRepositoryTest.java b/src/test/java/io/gravitee/repository/PortalNotificationConfigRepositoryTest.java new file mode 100644 index 00000000..d1ef1f27 --- /dev/null +++ b/src/test/java/io/gravitee/repository/PortalNotificationConfigRepositoryTest.java @@ -0,0 +1,149 @@ +/** + * Copyright (C) 2015 The Gravitee team (http://gravitee.io) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.gravitee.repository; + +import io.gravitee.repository.config.AbstractRepositoryTest; +import io.gravitee.repository.management.model.PortalNotificationConfig; +import io.gravitee.repository.management.model.PortalNotificationReferenceType; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class PortalNotificationConfigRepositoryTest extends AbstractRepositoryTest { + + @Override + protected String getTestCasesPath() { + return "/data/portalnotificationConfig-tests/"; + } + + @Test + public void shouldCreate() throws Exception { + final PortalNotificationConfig cfg = new PortalNotificationConfig(); + cfg.setReferenceType(PortalNotificationReferenceType.API); + cfg.setReferenceId("config-created"); + cfg.setUserId("userid"); + cfg.setHooks(Arrays.asList("A", "B", "C")); + cfg.setUpdatedAt(new Date(1439022010883L)); + cfg.setCreatedAt(new Date(1439022010883L)); + + PortalNotificationConfig notificationCreated = portalNotificationConfigRepository.create(cfg); + + assertEquals(cfg.getReferenceType(), notificationCreated.getReferenceType()); + assertEquals(cfg.getReferenceId(), notificationCreated.getReferenceId()); + assertEquals(cfg.getUserId(), notificationCreated.getUserId()); + assertEquals(cfg.getHooks(), notificationCreated.getHooks()); + assertEquals(cfg.getCreatedAt(), notificationCreated.getCreatedAt()); + assertEquals(cfg.getUpdatedAt(), notificationCreated.getUpdatedAt()); + } + + @Test + public void shouldDelete() throws Exception { + assertTrue(portalNotificationConfigRepository.findById("userid", PortalNotificationReferenceType.API, "config-to-delete").isPresent()); + final PortalNotificationConfig cfg = new PortalNotificationConfig(); + cfg.setReferenceType(PortalNotificationReferenceType.API); + cfg.setReferenceId("config-to-delete"); + cfg.setUserId("userid"); + portalNotificationConfigRepository.delete(cfg); + assertFalse(portalNotificationConfigRepository.findById("userid", PortalNotificationReferenceType.API, "config-to-delete").isPresent()); + } + + @Test + public void shouldUpdate() throws Exception { + final PortalNotificationConfig cfg = new PortalNotificationConfig(); + cfg.setReferenceType(PortalNotificationReferenceType.API); + cfg.setReferenceId("config-to-update"); + cfg.setUserId("userid"); + cfg.setHooks(Arrays.asList("D", "B", "C")); + cfg.setUpdatedAt(new Date(1479022010883L)); + cfg.setCreatedAt(new Date(1469022010883L)); + + PortalNotificationConfig notificationUpdated = portalNotificationConfigRepository.update(cfg); + + assertEquals(cfg.getReferenceType(), notificationUpdated.getReferenceType()); + assertEquals(cfg.getReferenceId(), notificationUpdated.getReferenceId()); + assertEquals(cfg.getUserId(), notificationUpdated.getUserId()); + assertEquals(cfg.getHooks(), notificationUpdated.getHooks()); + assertEquals(cfg.getCreatedAt(), notificationUpdated.getCreatedAt()); + assertEquals(cfg.getUpdatedAt(), notificationUpdated.getUpdatedAt()); + } + + @Test + public void shouldFindById() throws Exception { + final PortalNotificationConfig cfg = new PortalNotificationConfig(); + cfg.setReferenceType(PortalNotificationReferenceType.API); + cfg.setReferenceId("config-to-find"); + cfg.setUserId("userid"); + cfg.setHooks(Arrays.asList("A", "B")); + cfg.setUpdatedAt(new Date(1439022010883L)); + cfg.setCreatedAt(new Date(1439022010883L)); + + Optional optNotificationFound = + portalNotificationConfigRepository.findById("userid", PortalNotificationReferenceType.API, "config-to-find"); + + assertTrue(optNotificationFound.isPresent()); + PortalNotificationConfig notificationFound = optNotificationFound.get(); + assertEquals(cfg.getReferenceType(), notificationFound.getReferenceType()); + assertEquals(cfg.getReferenceId(), notificationFound.getReferenceId()); + assertEquals(cfg.getUserId(), notificationFound.getUserId()); + assertEquals(cfg.getHooks(), notificationFound.getHooks()); + assertEquals(cfg.getCreatedAt(), notificationFound.getCreatedAt()); + assertEquals(cfg.getUpdatedAt(), notificationFound.getUpdatedAt()); + } + + @Test + public void shouldNotFoundById() throws Exception { + Optional optNotificationFound; + //userid + optNotificationFound = portalNotificationConfigRepository.findById( + "userid-unknown", + PortalNotificationReferenceType.API, + "config-to-find"); + assertFalse(optNotificationFound.isPresent()); + //type + optNotificationFound = portalNotificationConfigRepository.findById( + "userid", + PortalNotificationReferenceType.APPLICATION, + "config-to-find"); + assertFalse(optNotificationFound.isPresent()); + //ref + optNotificationFound = portalNotificationConfigRepository.findById( + "userid", + PortalNotificationReferenceType.API, + "config-to-not-find"); + assertFalse(optNotificationFound.isPresent()); + } + + @Test + public void shouldFindByHookAndReference() throws Exception { + List configs = portalNotificationConfigRepository.findByReferenceAndHook( + "B", + PortalNotificationReferenceType.APPLICATION, + "search"); + + assertEquals("size", 2, configs.size()); + List userIds = configs.stream().map(PortalNotificationConfig::getUserId).collect(Collectors.toList()); + assertTrue("userA", userIds.contains("userA")); + assertTrue("userB", userIds.contains("userB")); + } +} diff --git a/src/test/java/io/gravitee/repository/PortalNotificationRepositoryTest.java b/src/test/java/io/gravitee/repository/PortalNotificationRepositoryTest.java new file mode 100644 index 00000000..0cf0ba48 --- /dev/null +++ b/src/test/java/io/gravitee/repository/PortalNotificationRepositoryTest.java @@ -0,0 +1,78 @@ +/** + * Copyright (C) 2015 The Gravitee team (http://gravitee.io) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.gravitee.repository; + +import io.gravitee.repository.config.AbstractRepositoryTest; +import io.gravitee.repository.management.model.PortalNotification; +import org.junit.Test; + +import java.util.Date; +import java.util.List; + +import static org.junit.Assert.*; + +public class PortalNotificationRepositoryTest extends AbstractRepositoryTest { + + @Override + protected String getTestCasesPath() { + return "/data/portalnotification-tests/"; + } + + @Test + public void shouldCreate() throws Exception { + final PortalNotification notification = new PortalNotification(); + notification.setId("notif-create"); + notification.setTitle("notif-title"); + notification.setMessage("notif-message"); + notification.setUserId("notif-userId"); + notification.setCreatedAt(new Date(1439022010883L)); + + PortalNotification notificationCreated = portalNotificationRepository.create(notification); + + assertEquals(notification.getId(), notificationCreated.getId()); + assertEquals(notification.getTitle(), notificationCreated.getTitle()); + assertEquals(notification.getMessage(), notificationCreated.getMessage()); + assertEquals(notification.getUserId(), notificationCreated.getUserId()); + assertEquals(notification.getCreatedAt(), notificationCreated.getCreatedAt()); + } + + @Test + public void shouldDelete() throws Exception { + assertEquals(1, portalNotificationRepository.findByUserId("notif-userId-toDelete").size()); + portalNotificationRepository.delete("notif-toDelete"); + assertTrue(portalNotificationRepository.findByUserId("notif-userId-toDelete").isEmpty()); + } + + @Test + public void shouldFindByUserId() throws Exception { + List notifications = portalNotificationRepository.findByUserId("notif-userId-findByUserId"); + + assertEquals(1, notifications.size()); + PortalNotification notification = notifications.get(0); + assertEquals("notif-findByUserId", notification.getId()); + assertEquals("notif-title-findByUserId", notification.getTitle()); + assertEquals("notif-message-findByUserId", notification.getMessage()); + assertEquals("notif-userId-findByUserId", notification.getUserId()); + assertEquals(new Date(1439022010883L), notification.getCreatedAt()); + } + + @Test + public void shouldNotFindByUsername() throws Exception { + List notifications = portalNotificationRepository.findByUserId("unknown"); + + assertTrue(notifications.isEmpty()); + } +} diff --git a/src/test/java/io/gravitee/repository/config/AbstractRepositoryTest.java b/src/test/java/io/gravitee/repository/config/AbstractRepositoryTest.java index b55b093f..6fadc659 100644 --- a/src/test/java/io/gravitee/repository/config/AbstractRepositoryTest.java +++ b/src/test/java/io/gravitee/repository/config/AbstractRepositoryTest.java @@ -15,7 +15,6 @@ */ package io.gravitee.repository.config; -import io.gravitee.repository.RatingRepositoryTest; import io.gravitee.repository.exceptions.TechnicalException; import io.gravitee.repository.management.api.*; import io.gravitee.repository.management.model.*; @@ -95,6 +94,10 @@ public abstract class AbstractRepositoryTest { protected RatingRepository ratingRepository; @Inject protected RatingAnswerRepository ratingAnswerRepository; + @Inject + protected PortalNotificationRepository portalNotificationRepository; + @Inject + protected PortalNotificationConfigRepository portalNotificationConfigRepository; private ObjectMapper mapper = new ObjectMapper(); @@ -179,6 +182,12 @@ else if (object instanceof Rating) { else if (object instanceof RatingAnswer) { ratingAnswerRepository.create((RatingAnswer) object); } + else if (object instanceof PortalNotification) { + portalNotificationRepository.create((PortalNotification) object); + } + else if (object instanceof PortalNotificationConfig) { + portalNotificationConfigRepository.create((PortalNotificationConfig) object); + } } private Class getClassFromFileName(final String baseName) { diff --git a/src/test/java/io/gravitee/repository/config/MockTestRepositoryConfiguration.java b/src/test/java/io/gravitee/repository/config/MockTestRepositoryConfiguration.java index 2130df4d..c320a8c5 100644 --- a/src/test/java/io/gravitee/repository/config/MockTestRepositoryConfiguration.java +++ b/src/test/java/io/gravitee/repository/config/MockTestRepositoryConfiguration.java @@ -1137,4 +1137,93 @@ public RatingAnswerRepository ratingAnswerRepository() throws Exception { return ratingAnswerRepository; } + + @Bean + public PortalNotificationRepository notificationRepository() throws Exception { + final PortalNotificationRepository notificationRepository = mock(PortalNotificationRepository.class); + + // create + final PortalNotification notificationCreated = new PortalNotification(); + notificationCreated.setId("notif-create"); + notificationCreated.setTitle("notif-title"); + notificationCreated.setMessage("notif-message"); + notificationCreated.setUserId("notif-userId"); + notificationCreated.setCreatedAt(new Date(1439022010883L)); + when(notificationRepository.create(any(PortalNotification.class))).thenReturn(notificationCreated); + + //delete + when(notificationRepository.findByUserId(eq("notif-userId-toDelete"))).thenReturn(singletonList(mock(PortalNotification.class)), emptyList()); + + //findByUserId + final PortalNotification notificationFindByUsername = new PortalNotification(); + notificationFindByUsername.setId("notif-findByUserId"); + notificationFindByUsername.setTitle("notif-title-findByUserId"); + notificationFindByUsername.setMessage("notif-message-findByUserId"); + notificationFindByUsername.setUserId("notif-userId-findByUserId"); + notificationFindByUsername.setCreatedAt(new Date(1439022010883L)); + when(notificationRepository.findByUserId(eq("notif-userId-findByUserId"))).thenReturn(singletonList(notificationFindByUsername)); + when(notificationRepository.findByUserId(eq("unknown"))).thenReturn(emptyList()); + + return notificationRepository; + } + + @Bean + public PortalNotificationConfigRepository portalNotificationConfigRepository() throws Exception { + final PortalNotificationConfigRepository portalNotificationConfigRepository = mock(PortalNotificationConfigRepository.class); + + //create + final PortalNotificationConfig createdCfg = new PortalNotificationConfig(); + createdCfg.setReferenceType(PortalNotificationReferenceType.API); + createdCfg.setReferenceId("config-created"); + createdCfg.setUserId("userid"); + createdCfg.setHooks(Arrays.asList("A", "B", "C")); + createdCfg.setUpdatedAt(new Date(1439022010883L)); + createdCfg.setCreatedAt(new Date(1439022010883L)); + when(portalNotificationConfigRepository.create(any())).thenReturn(createdCfg); + + //update + final PortalNotificationConfig updatedCfg = new PortalNotificationConfig(); + updatedCfg.setReferenceType(PortalNotificationReferenceType.API); + updatedCfg.setReferenceId("config-to-update"); + updatedCfg.setUserId("userid"); + updatedCfg.setHooks(Arrays.asList("D", "B", "C")); + updatedCfg.setUpdatedAt(new Date(1479022010883L)); + updatedCfg.setCreatedAt(new Date(1469022010883L)); + when(portalNotificationConfigRepository.update(any())).thenReturn(updatedCfg); + + //delete + when(portalNotificationConfigRepository.findById("userid", PortalNotificationReferenceType.API, "config-to-delete")). + thenReturn(of(mock(PortalNotificationConfig.class)), empty()); + + //findById + final PortalNotificationConfig foundCfg = new PortalNotificationConfig(); + foundCfg.setReferenceType(PortalNotificationReferenceType.API); + foundCfg.setReferenceId("config-to-find"); + foundCfg.setUserId("userid"); + foundCfg.setHooks(Arrays.asList("A", "B")); + foundCfg.setUpdatedAt(new Date(1439022010883L)); + foundCfg.setCreatedAt(new Date(1439022010883L)); + when(portalNotificationConfigRepository.findById("userid", PortalNotificationReferenceType.API, "config-to-find")). + thenReturn(of(foundCfg)); + + //notFoundById + when(portalNotificationConfigRepository.findById("userid-unknown", PortalNotificationReferenceType.API, "config-to-find")). + thenReturn(empty()); + when(portalNotificationConfigRepository.findById("userid", PortalNotificationReferenceType.APPLICATION, "config-to-find")). + thenReturn(empty()); + when(portalNotificationConfigRepository.findById("userid", PortalNotificationReferenceType.API, "config-to-not-find")). + thenReturn(empty()); + + //findByReferenceAndHook + PortalNotificationConfig n1 = mock(PortalNotificationConfig.class); + when(n1.getUserId()).thenReturn("userA"); + PortalNotificationConfig n2 = mock(PortalNotificationConfig.class); + when(n2.getUserId()).thenReturn("userB"); + when(portalNotificationConfigRepository.findByReferenceAndHook( + "B", + PortalNotificationReferenceType.APPLICATION, + "search")).thenReturn(Arrays.asList(n1, n2)); + + return portalNotificationConfigRepository; + } } diff --git a/src/test/resources/data/portalnotification-tests/portalNotifications.json b/src/test/resources/data/portalnotification-tests/portalNotifications.json new file mode 100644 index 00000000..f0bf6bbf --- /dev/null +++ b/src/test/resources/data/portalnotification-tests/portalNotifications.json @@ -0,0 +1,23 @@ +[ + { + "id": "notif-toUpdate", + "title": "notif-title-toUpdate", + "message": "notif-message-toUpdate", + "userId": "notif-userId-toUpdate", + "createdAt": 1439022010883 + }, + { + "id": "notif-toDelete", + "title": "notif-title-toDelete", + "message": "notif-message-toDelete", + "userId": "notif-userId-toDelete", + "createdAt": 1439022010883 + }, + { + "id": "notif-findByUserId", + "title": "notif-title-findByUserId", + "message": "notif-message-findByUserId", + "userId": "notif-userId-findByUserId", + "createdAt": 1439022010883 + } +] \ No newline at end of file diff --git a/src/test/resources/data/portalnotificationConfig-tests/portalNotificationConfigs.json b/src/test/resources/data/portalnotificationConfig-tests/portalNotificationConfigs.json new file mode 100644 index 00000000..4a2f318c --- /dev/null +++ b/src/test/resources/data/portalnotificationConfig-tests/portalNotificationConfigs.json @@ -0,0 +1,50 @@ +[ + { + "userId": "userid", + "referenceType": "API", + "referenceId": "config-to-delete", + "hooks": ["A", "B"], + "createdAt": 1439022010883, + "updatedAt": 1439022010883 + }, + { + "userId": "userid", + "referenceType": "API", + "referenceId": "config-to-update", + "hooks": ["A", "B"], + "createdAt": 1439022010883, + "updatedAt": 1439022010883 + }, + { + "userId": "userid", + "referenceType": "API", + "referenceId": "config-to-find", + "hooks": ["A", "B"], + "createdAt": 1439022010883, + "updatedAt": 1439022010883 + }, + { + "userId": "userA", + "referenceType": "APPLICATION", + "referenceId": "search", + "hooks": ["A", "B"], + "createdAt": 1439022010883, + "updatedAt": 1439022010883 + }, + { + "userId": "userB", + "referenceType": "APPLICATION", + "referenceId": "search", + "hooks": ["B", "C"], + "createdAt": 1439022010883, + "updatedAt": 1439022010883 + }, + { + "userId": "userC", + "referenceType": "APPLICATION", + "referenceId": "search", + "hooks": ["A", "C"], + "createdAt": 1439022010883, + "updatedAt": 1439022010883 + } +] \ No newline at end of file