Skip to content
This repository has been archived by the owner on Jul 29, 2021. It is now read-only.

Commit

Permalink
feat(notifications): add user notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasGeraud committed Feb 13, 2018
1 parent 10b03ba commit 6dab365
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -32,7 +32,7 @@
<name>Gravitee.io APIM - Repository - Test</name>

<properties>
<gravitee-repository.version>1.13.0</gravitee-repository.version>
<gravitee-repository.version>1.14.0-SNAPSHOT</gravitee-repository.version>
<commons-io.version>1.3.2</commons-io.version>
<jackson-mapper-asl.version>1.9.13</jackson-mapper-asl.version>
<javax.inject.version>1</javax.inject.version>
Expand Down
@@ -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<PortalNotificationConfig> 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<PortalNotificationConfig> 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<PortalNotificationConfig> configs = portalNotificationConfigRepository.findByReferenceAndHook(
"B",
PortalNotificationReferenceType.APPLICATION,
"search");

assertEquals("size", 2, configs.size());
List<String> userIds = configs.stream().map(PortalNotificationConfig::getUserId).collect(Collectors.toList());
assertTrue("userA", userIds.contains("userA"));
assertTrue("userB", userIds.contains("userB"));
}
}
@@ -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<PortalNotification> 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<PortalNotification> notifications = portalNotificationRepository.findByUserId("unknown");

assertTrue(notifications.isEmpty());
}
}
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down
Expand Up @@ -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;
}
}
@@ -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
}
]

0 comments on commit 6dab365

Please sign in to comment.