diff options
author | talig <talig@amdocs.com> | 2017-12-20 14:30:43 +0200 |
---|---|---|
committer | Vitaly Emporopulo <Vitaliy.Emporopulo@amdocs.com> | 2017-12-21 11:12:33 +0000 |
commit | 8e9c0653dd6c6862123c9609ae34e1206d86456e (patch) | |
tree | 5eeef00ec0677133baa439ca8d7ffd7aca4804b6 /openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core | |
parent | 785ebcc95de3e064e843bec04ba7a209d854fc7c (diff) |
Add collaboration feature
Issue-ID: SDC-767
Change-Id: I14fb4c1f54086ed03a56a7ff7fab9ecd40381795
Signed-off-by: talig <talig@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core')
22 files changed, 1254 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/pom.xml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/pom.xml new file mode 100644 index 0000000000..4fe33ea1c3 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/pom.xml @@ -0,0 +1,42 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <name>openecomp-sdc-notification-core</name> + <artifactId>openecomp-sdc-notification-core</artifactId> + + + <parent> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-lib</artifactId> + <version>1.2.0-SNAPSHOT</version> + <relativePath>../..</relativePath> + </parent> + <dependencies> + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-notification-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <scope>test</scope> + <version>1.10.19</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + <version>${junit.version}</version> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-module-junit4</artifactId> + <version>1.6.5</version> + <scope>test</scope> + </dependency> + </dependencies> + +</project> diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestination.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestination.java new file mode 100644 index 0000000000..431f9c18d6 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestination.java @@ -0,0 +1,44 @@ +package org.openecomp.sdc.destinationprovider.impl; + +import org.apache.commons.lang3.ArrayUtils; +import org.openecomp.sdc.destinationprovider.DestinationProvider; +import org.openecomp.sdc.notification.services.SubscriptionService; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * @author avrahamg + * @since July 09, 2017 + */ +public class MulticastDestination implements DestinationProvider { + + private String entityId; + private SubscriptionService subscriptionService; + private String[] excludedSubscribers; + + public MulticastDestination(String entityId, SubscriptionService subscriptionService, + String... excludedSubscribers) { + this.entityId = entityId; + this.excludedSubscribers = excludedSubscribers; + this.subscriptionService = subscriptionService; + } + + public List<String> getSubscribers() { + ArrayList<String> subscribers = new ArrayList<>(subscriptionService.getSubscribers(entityId)); + if (ArrayUtils.isNotEmpty(excludedSubscribers)) { + subscribers.removeAll(Arrays.asList(excludedSubscribers)); + } + return Collections.unmodifiableList(subscribers); + } + + public String getEntityId() { + return entityId; + } + + public void setEntityId(String entityId) { + this.entityId = entityId; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/UnicastDestination.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/UnicastDestination.java new file mode 100644 index 0000000000..693c816d01 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/UnicastDestination.java @@ -0,0 +1,23 @@ +package org.openecomp.sdc.destinationprovider.impl; + +import org.openecomp.sdc.destinationprovider.DestinationProvider; + +import java.util.Collections; +import java.util.List; + +/** + * @author avrahamg + * @since July 09, 2017 + */ +public class UnicastDestination implements DestinationProvider { + + private String originatorId; + + public UnicastDestination(String originatorId) { + this.originatorId = originatorId; + } + + public List<String> getSubscribers() { + return Collections.unmodifiableList(Collections.singletonList(originatorId)); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/LastNotificationDaoCassandraImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/LastNotificationDaoCassandraImpl.java new file mode 100644 index 0000000000..0bfd6a314d --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/LastNotificationDaoCassandraImpl.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao.impl; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.mapping.Mapper; +import com.datastax.driver.mapping.Result; +import com.datastax.driver.mapping.annotations.Accessor; +import com.datastax.driver.mapping.annotations.Query; +import org.openecomp.core.dao.impl.CassandraBaseDao; +import org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.sdc.notification.dao.LastNotificationDao; +import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity; + +import java.util.Collection; +import java.util.UUID; + + +public class LastNotificationDaoCassandraImpl extends CassandraBaseDao<LastSeenNotificationEntity> implements LastNotificationDao { + + private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); + private static final Mapper<LastSeenNotificationEntity> mapper = + noSqlDb.getMappingManager().mapper(LastSeenNotificationEntity.class); + private static final LastNotificationAccessor accessor = + noSqlDb.getMappingManager().createAccessor(LastNotificationAccessor.class); + + @Override + protected Mapper<LastSeenNotificationEntity> getMapper() { + return mapper; + } + + @Override + protected Object[] getKeys(LastSeenNotificationEntity entity) { + return new Object[]{entity.getOwnerId()}; + } + + @Override + public Collection<LastSeenNotificationEntity> list(LastSeenNotificationEntity entity) { + return accessor.list(entity.getOwnerId()).all(); + } + + @Override + public UUID getOwnerLastEventId(String ownerId) { + ResultSet ownerLastEventId = accessor.getOwnerLastEventId(ownerId); + Row one = ownerLastEventId.one(); + return one != null ? one.getUUID("event_id") : null; + } + + @Override + public void persistOwnerLastEventId(String ownerId, UUID eventId) { + accessor.updateOwnerLastEventId(eventId, ownerId); + } + + @Accessor + interface LastNotificationAccessor { + + @Query("select * from last_notification where owner_id=?") + Result<LastSeenNotificationEntity> list(String ownerId); + + @Query("select event_id from last_notification where owner_id=?") + ResultSet getOwnerLastEventId(String ownerId); + + @Query("update last_notification set event_id=? where owner_id=?") + ResultSet updateOwnerLastEventId(UUID eventId, String ownerId); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/NotificationsDaoCassandraImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/NotificationsDaoCassandraImpl.java new file mode 100644 index 0000000000..151c2c81d9 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/NotificationsDaoCassandraImpl.java @@ -0,0 +1,287 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao.impl; + +import com.datastax.driver.core.BatchStatement; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Statement; +import com.datastax.driver.core.utils.UUIDs; +import com.datastax.driver.mapping.Mapper; +import com.datastax.driver.mapping.Result; +import com.datastax.driver.mapping.annotations.Accessor; +import com.datastax.driver.mapping.annotations.Query; +import org.apache.commons.collections.CollectionUtils; +import org.openecomp.core.dao.impl.CassandraBaseDao; +import org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.dtos.NotificationsStatus; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.stream.Collectors; + +import static org.openecomp.core.nosqldb.impl.cassandra.CassandraSessionFactory.getSession; + +//import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity; +//import java.util.Optional; + +public class NotificationsDaoCassandraImpl extends CassandraBaseDao<NotificationEntity> + implements NotificationsDao { + + private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); + private static final Mapper<NotificationEntity> mapper = + noSqlDb.getMappingManager().mapper(NotificationEntity.class); + private static final NotificationsAccessor accessor = + noSqlDb.getMappingManager().createAccessor(NotificationsAccessor.class); + + @Override + protected Mapper<NotificationEntity> getMapper() { + return mapper; + } + + @Override + protected Object[] getKeys(NotificationEntity entity) { + return new Object[]{entity.getOwnerId(), entity.getEventId()}; + } + + @Override + public List<NotificationEntity> list(NotificationEntity entity) { + return accessor.list(entity.getOwnerId()).all(); + } + + @Override + public List<NotificationEntity> getNotificationsByOwnerId(String ownerId, int limit) { + return accessor.getNotifications(ownerId, limit).all(); + } + + @Override + public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId) { + return getNewNotificationsByOwnerId(ownerId, eventId, + DEFAULT_LIMIT_OF_RESULTS_FOR_OWNER_NOTIFICATIONS); + } + + @Override + public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId, int limit) { + if (Objects.isNull(eventId)) { + return getNotificationsByOwnerId(ownerId, limit); + } + return accessor.getNewNotifications(ownerId, eventId, limit).all(); + } + + @Override + public void markNotificationAsRead(String ownerId, Collection<UUID> eventIds) { + eventIds.forEach(eventId -> accessor.markAsRead(ownerId, eventId)); + } + + @Override + public NotificationsStatus getNotificationsStatus(String ownerId, UUID lastScannedEventId, int numOfRecordsToReturn) { + NotificationsStatusImpl notificationsStatus = new NotificationsStatusImpl(); + List<NotificationEntity> entities = accessor.getNotifications(ownerId, numOfRecordsToReturn).all(); + if (CollectionUtils.isNotEmpty(entities)) { + long lastSeen = UUIDs.unixTimestamp(lastScannedEventId); + populateNewNotifications(notificationsStatus, entities, lastSeen); + UUID firstScannedEventId = entities.get(0).getEventId(); + notificationsStatus.setLastScanned(firstScannedEventId); + notificationsStatus.setNumOfNotSeenNotifications(accessor.getNewNotificationsCount(ownerId, lastScannedEventId, firstScannedEventId).one().getLong(0)); + } + return notificationsStatus; + } + + private void populateNewNotifications(NotificationsStatusImpl notificationsStatus, List<NotificationEntity> entities, long lastSeen) { + for (NotificationEntity entity : entities) { + UUID eventId = entity.getEventId(); + notificationsStatus.addNotification(entity); + if (UUIDs.unixTimestamp(eventId) > lastSeen) { + notificationsStatus.addNewNotificationUUID(eventId); + } + } + } + + @Override + public NotificationsStatus getNotificationsStatus(String ownerId, UUID lastSeenNotification, int numOfRecordsToReturn, UUID prevLastScannedEventId) { + NotificationsStatusImpl notificationsStatus = new NotificationsStatusImpl(); + List<NotificationEntity> entities = accessor.getPrevNotifications(ownerId, prevLastScannedEventId, numOfRecordsToReturn).all(); + if (CollectionUtils.isNotEmpty(entities)) { + long lastSeen = UUIDs.unixTimestamp(lastSeenNotification); + populateNewNotifications(notificationsStatus, entities, lastSeen); + } + return notificationsStatus; + } + +/* + @Override + public NotificationsStatus getNotificationsStatus(String ownerId, + LastSeenNotificationEntity lastSeenNotification, + int numOfRecordsToReturn) { + + List<NotificationEntity> notificationEntities = + fetchNewNotifications(lastSeenNotification, numOfRecordsToReturn); + NotificationsStatusImpl notificationsStatus = new NotificationsStatusImpl(); + if (CollectionUtils.isEmpty(notificationEntities)) { + return notificationsStatus; + } + + notificationEntities.forEach(notification -> { + if (isNewNotification(lastSeenNotification, notification)) { + notificationsStatus.addNewNotificationUUID(notification.getEventId()); + } + notificationsStatus.addNotification(notification); + }); + + Optional<NotificationEntity> latestNotification = notificationEntities.stream().findFirst(); + latestNotification.ifPresent(e -> notificationsStatus.setLastScanned(e.getEventId())); + return notificationsStatus; + } + + private List<NotificationEntity> fetchNewNotifications( + LastSeenNotificationEntity lastSeenNotification, int numOfRecordsToReturn) { + String ownerId = lastSeenNotification.getOwnerId(); + UUID lastEventId = lastSeenNotification.getLastEventId(); + List<NotificationEntity> newNotificationsByOwnerId = + getNewNotificationsByOwnerId(ownerId, lastEventId); + newNotificationsByOwnerId = fetchMoreIfNeeded(ownerId, newNotificationsByOwnerId, + numOfRecordsToReturn, lastEventId); + return newNotificationsByOwnerId; + } + + private boolean isNewNotification(LastSeenNotificationEntity lastSeenNotification, + NotificationEntity notification) { + return Objects.isNull(lastSeenNotification.getLastEventId()) || + UUIDs.unixTimestamp(notification.getEventId()) > + UUIDs.unixTimestamp(lastSeenNotification.getLastEventId()); + } +*/ + + @Override + public void createBatch(List<NotificationEntity> notificationEntities) { + BatchStatement batch = new BatchStatement(); + List<Statement> statements = notificationEntities.stream() + .map(mapper::saveQuery) + .collect(Collectors.toList()); + batch.addAll(statements); + getSession().execute(batch); + } + + @Accessor + interface NotificationsAccessor { + + @Query("select * from notifications where owner_id=?") + Result<NotificationEntity> list(String ownerId); + + @Query("select * from notifications where owner_id=? limit ?") + Result<NotificationEntity> getNotifications(String ownerId, int limit); + + @Query("select * from notifications where owner_id=? and event_id > ? limit ?") + Result<NotificationEntity> getNewNotifications(String ownerId, UUID lastScannedEventId, int limit); + + @Query("select * from notifications where owner_id=? and event_id < ? limit ?") + Result<NotificationEntity> getPrevNotifications(String ownerId, UUID prevLastScannedEventId, int limit); + + @Query("select count(*) from notifications where owner_id=? and event_id > ? and event_id <= ?") + ResultSet getNewNotificationsCount(String ownerId, UUID lastScannedEventId, UUID firstScannedEventId); + + @Query("update notifications set read=true where owner_id=? and event_id=?") + ResultSet markAsRead(String ownerId, UUID eventId); + } + + private class NotificationsStatusImpl implements NotificationsStatus { + + private List<NotificationEntity> notifications = new ArrayList<>(); + private List<UUID> newEntries = new ArrayList<>(); + private UUID lastScanned; + private UUID endOfPage; + private long numOfNotSeenNotifications = 0; + + void addNotification(NotificationEntity notification) { + notifications.add(notification); + endOfPage = notification.getEventId(); + } + + void addNewNotificationUUID(UUID notificationUuid) { + newEntries.add(notificationUuid); + } + + @Override + public List<NotificationEntity> getNotifications() { + return Collections.unmodifiableList(notifications); + } + + @Override + public List<UUID> getNewEntries() { + return Collections.unmodifiableList(newEntries); + } + + @Override + public UUID getLastScanned() { + return lastScanned; + } + + void setLastScanned(UUID lastScanned) { + this.lastScanned = lastScanned; + } + + @Override + public UUID getEndOfPage() { + return endOfPage; + } + + @Override + public long getNumOfNotSeenNotifications() { + return numOfNotSeenNotifications; + } + + void setNumOfNotSeenNotifications(long numOfNotSeenNotifications) { + this.numOfNotSeenNotifications = numOfNotSeenNotifications; + } + } + +/* + private List<NotificationEntity> fetchMoreIfNeeded(String ownerId, + List<NotificationEntity> notificationEntities, + int numOfRecordsToReturn, UUID lastEventId) { + + if (numOfRecordsToReturn <= notificationEntities.size() || Objects.isNull(lastEventId)) { + return notificationEntities; + } + + int multiplier = 2; + while (numOfRecordsToReturn > notificationEntities.size()) { + + int bring = notificationEntities.size() + + (numOfRecordsToReturn - notificationEntities.size()) * multiplier; + notificationEntities = getNotificationsByOwnerId(ownerId, bring); + + if (notificationEntities.size() < bring) { + return notificationEntities; + } + multiplier++; + } + return notificationEntities; + } +*/ + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/SubscribersDaoCassandraImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/SubscribersDaoCassandraImpl.java new file mode 100644 index 0000000000..8e8cf9b181 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/SubscribersDaoCassandraImpl.java @@ -0,0 +1,104 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao.impl; + +import com.datastax.driver.mapping.Mapper; +import com.datastax.driver.mapping.Result; +import com.datastax.driver.mapping.annotations.Accessor; +import com.datastax.driver.mapping.annotations.Query; +import com.google.common.collect.Sets; +import org.openecomp.core.dao.impl.CassandraBaseDao; +import org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.sdc.notification.dao.SubscribersDao; +import org.openecomp.sdc.notification.dao.types.SubscribersEntity; + +import java.util.Collection; +import java.util.Collections; +import java.util.Objects; +import java.util.Set; + +import static java.util.Objects.isNull; + +public class SubscribersDaoCassandraImpl extends CassandraBaseDao<SubscribersEntity> implements + SubscribersDao { + + private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); + private static final Mapper<SubscribersEntity> mapper = + noSqlDb.getMappingManager().mapper(SubscribersEntity.class); + private static final SubscribersAccessor accessor = + noSqlDb.getMappingManager().createAccessor(SubscribersAccessor.class); + + + @Override + protected Object[] getKeys(SubscribersEntity entity) { + return new Object[]{entity.getEntityId()}; + } + + @Override + protected Mapper<SubscribersEntity> getMapper() { + return mapper; + } + + @Override + public void subscribe(String ownerId, String entityId) { + Objects.requireNonNull(ownerId); + Objects.requireNonNull(entityId); + accessor.subscribe(Sets.newHashSet(ownerId), entityId); + } + + @Override + @Deprecated + public Collection<SubscribersEntity> list(SubscribersEntity entity) { + throw new UnsupportedOperationException(); + } + + @Override + public void unsubscribe(String ownerId, String entityId) { + Objects.requireNonNull(ownerId); + Objects.requireNonNull(entityId); + accessor.unsubscribe(Sets.newHashSet(ownerId), entityId); + } + + @Override + public Set<String> getSubscribers(String entityId) { + Objects.requireNonNull(entityId); + SubscribersEntity subscribersEntity = accessor.getSubscribers(entityId).one(); + if (isNull(subscribersEntity)) { + return Collections.emptySet(); + } + return subscribersEntity.getSubscribers(); + } + + @Accessor + interface SubscribersAccessor { + + @Query("select * from notification_subscribers where entity_id=?") + Result<SubscribersEntity> getSubscribers(String entityId); + + @Query("update notification_subscribers set subscribers=subscribers+? WHERE entity_id=?") + void subscribe(Set<String> ownerId, String entityId); + + @Query("update notification_subscribers set subscribers=subscribers-? WHERE entity_id=?") + void unsubscribe(Set<String> ownerId, String entityId); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/LastNotificationDaoFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/LastNotificationDaoFactoryImpl.java new file mode 100644 index 0000000000..7ac9e00967 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/LastNotificationDaoFactoryImpl.java @@ -0,0 +1,19 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.dao.LastNotificationDao; +import org.openecomp.sdc.notification.dao.impl.LastNotificationDaoCassandraImpl; +import org.openecomp.sdc.notification.factories.LastNotificationDaoFactory; + +/** + * @author itzikpa + * @since June 23, 2017 + */ + +public class LastNotificationDaoFactoryImpl extends LastNotificationDaoFactory { + private static final LastNotificationDao INSTANCE = new LastNotificationDaoCassandraImpl(); + + @Override + public LastNotificationDao createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationPropagationManagerFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationPropagationManagerFactoryImpl.java new file mode 100644 index 0000000000..7b9e51e8c9 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationPropagationManagerFactoryImpl.java @@ -0,0 +1,19 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory; +import org.openecomp.sdc.notification.factories.PropagationServiceFactory; +import org.openecomp.sdc.notification.factories.SubscriptionServiceFactory; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; +import org.openecomp.sdc.notification.services.impl.NotificationPropagationManagerImpl; + +public class NotificationPropagationManagerFactoryImpl extends + NotificationPropagationManagerFactory { + private static final NotificationPropagationManager INSTANCE = new NotificationPropagationManagerImpl( + PropagationServiceFactory.getInstance().createInterface(), SubscriptionServiceFactory + .getInstance().createInterface()); + + @Override + public NotificationPropagationManager createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsDaoFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsDaoFactoryImpl.java new file mode 100644 index 0000000000..edb020637b --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsDaoFactoryImpl.java @@ -0,0 +1,18 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dao.impl.NotificationsDaoCassandraImpl; +import org.openecomp.sdc.notification.factories.NotificationsDaoFactory; + +/** + * @author Avrahamg + * @since June 20, 2017 + */ +public class NotificationsDaoFactoryImpl extends NotificationsDaoFactory { + private static final NotificationsDao INSTANCE = new NotificationsDaoCassandraImpl(); + + @Override + public NotificationsDao createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsServiceFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsServiceFactoryImpl.java new file mode 100644 index 0000000000..ac265e3c8c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsServiceFactoryImpl.java @@ -0,0 +1,22 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.factories.LastNotificationDaoFactory; +import org.openecomp.sdc.notification.factories.NotificationsDaoFactory; +import org.openecomp.sdc.notification.factories.NotificationsServiceFactory; +import org.openecomp.sdc.notification.services.NotificationsService; +import org.openecomp.sdc.notification.services.impl.NotificationsServiceImpl; + +/** + * @author Avrahamg + * @since June 20, 2017 + */ +public class NotificationsServiceFactoryImpl extends NotificationsServiceFactory { + private static final NotificationsService INSTANCE = new NotificationsServiceImpl( + LastNotificationDaoFactory.getInstance().createInterface(), NotificationsDaoFactory + .getInstance().createInterface()); + + @Override + public NotificationsService createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/PropagationServiceFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/PropagationServiceFactoryImpl.java new file mode 100644 index 0000000000..e5987fe266 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/PropagationServiceFactoryImpl.java @@ -0,0 +1,16 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.factories.NotificationsDaoFactory; +import org.openecomp.sdc.notification.factories.PropagationServiceFactory; +import org.openecomp.sdc.notification.services.PropagationService; +import org.openecomp.sdc.notification.services.impl.PropagationServiceImpl; + +public class PropagationServiceFactoryImpl extends PropagationServiceFactory { + private static final PropagationService INSTANCE = new PropagationServiceImpl( + NotificationsDaoFactory.getInstance().createInterface()); + + @Override + public PropagationService createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscribersDaoFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscribersDaoFactoryImpl.java new file mode 100644 index 0000000000..fb87b944f9 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscribersDaoFactoryImpl.java @@ -0,0 +1,15 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.dao.SubscribersDao; +import org.openecomp.sdc.notification.dao.impl.SubscribersDaoCassandraImpl; +import org.openecomp.sdc.notification.factories.SubscribersDaoFactory; + + +public class SubscribersDaoFactoryImpl extends SubscribersDaoFactory { + private static final SubscribersDao INSTANCE = new SubscribersDaoCassandraImpl(); + + @Override + public SubscribersDao createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscriptionServiceFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscriptionServiceFactoryImpl.java new file mode 100644 index 0000000000..604cff0b5e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscriptionServiceFactoryImpl.java @@ -0,0 +1,17 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.factories.SubscribersDaoFactory; +import org.openecomp.sdc.notification.factories.SubscriptionServiceFactory; +import org.openecomp.sdc.notification.services.SubscriptionService; +import org.openecomp.sdc.notification.services.impl.SubscriptionServiceImpl; + + +public class SubscriptionServiceFactoryImpl extends SubscriptionServiceFactory { + private static final SubscriptionService INSTANCE = new SubscriptionServiceImpl + (SubscribersDaoFactory.getInstance().createInterface()); + + @Override + public SubscriptionService createInterface() { + return INSTANCE; + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImpl.java new file mode 100644 index 0000000000..d381268b3f --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImpl.java @@ -0,0 +1,35 @@ +package org.openecomp.sdc.notification.services.impl; + +import org.openecomp.sdc.destinationprovider.impl.MulticastDestination; +import org.openecomp.sdc.destinationprovider.impl.UnicastDestination; +import org.openecomp.sdc.notification.dtos.Event; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; +import org.openecomp.sdc.notification.services.PropagationService; +import org.openecomp.sdc.notification.services.SubscriptionService; + +/** + * @author avrahamg + * @since July 10, 2017 + */ +public class NotificationPropagationManagerImpl implements NotificationPropagationManager { + + private PropagationService propagationService; + private SubscriptionService subscriptionService; + + public NotificationPropagationManagerImpl(PropagationService propagationService, + SubscriptionService subscriptionService) { + this.propagationService = propagationService; + this.subscriptionService = subscriptionService; + } + + @Override + public void notifySubscribers(Event event, String ... excludedSubscribers) { + propagationService.notify(event, new MulticastDestination(event.getEntityId(), + subscriptionService, excludedSubscribers)); + } + + @Override + public void directNotification(Event event, String destinationId) { + propagationService.notify(event, new UnicastDestination(destinationId)); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImpl.java new file mode 100644 index 0000000000..8a7ab8bdae --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImpl.java @@ -0,0 +1,92 @@ +package org.openecomp.sdc.notification.services.impl; + +import org.openecomp.sdc.notification.dao.LastNotificationDao; +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.dtos.NotificationsStatus; +import org.openecomp.sdc.notification.exceptons.NotificationNotExistException; +import org.openecomp.sdc.notification.services.NotificationsService; + +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +/** + * @author Avrahamg + * @since June 26, 2017 + */ +public class NotificationsServiceImpl implements NotificationsService { + + private LastNotificationDao lastNotificationDao; + private NotificationsDao notificationsDao; + + public NotificationsServiceImpl(LastNotificationDao lastNotificationDao, + NotificationsDao notificationsDao) { + this.lastNotificationDao = lastNotificationDao; + this.notificationsDao = notificationsDao; + } + + @Override + public LastSeenNotificationEntity getLastNotification(String ownerId) { + return new LastSeenNotificationEntity(ownerId, + lastNotificationDao.getOwnerLastEventId(ownerId)); + } + + @Override + public void updateLastSeenNotification(String ownerId, UUID eventId) + { + lastNotificationDao.persistOwnerLastEventId(ownerId, eventId); + } + + @Override + public NotificationsStatus getNotificationsStatus(String ownerId, UUID lastDelivered, int numOfRecordsToReturn, UUID endOfPage) { + if (Objects.isNull(lastDelivered)) { + LastSeenNotificationEntity entity = getLastNotification(ownerId); + if (Objects.nonNull(entity)) { + lastDelivered = entity.getLastEventId(); + } + if (Objects.isNull(lastDelivered)) { + lastDelivered = UUID.fromString("00000000-0000-1000-8080-808080808080"); // Lowest time UUID value + } + } + if (Objects.isNull(endOfPage)) { + // First page + return notificationsDao.getNotificationsStatus(ownerId, lastDelivered, numOfRecordsToReturn); + } + else { + // Next page + return notificationsDao.getNotificationsStatus(ownerId, lastDelivered, numOfRecordsToReturn, endOfPage); + } + } + + @Override + public void markAsRead(String ownerId, String notificationId) throws + NotificationNotExistException { + NotificationEntity notificationEntity = + notificationsDao.get(new NotificationEntity(ownerId, UUID.fromString(notificationId))); + if (Objects.isNull(notificationEntity)) { + throw new NotificationNotExistException( + "Notification '" + notificationId + "' is not related to ownerId" + + " '" + ownerId + "'"); + } + notificationEntity.setRead(true); + notificationsDao.update(notificationEntity); + } + + @Override + public List<NotificationEntity> getNotificationsByOwnerId(String ownerId, int limit) { + return notificationsDao.getNotificationsByOwnerId(ownerId, limit); + } + + @Override + public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId) { + return notificationsDao.getNewNotificationsByOwnerId(ownerId, eventId); + } + + @Override + public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId, int limit) { + return notificationsDao.getNewNotificationsByOwnerId(ownerId, eventId, limit); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImpl.java new file mode 100644 index 0000000000..99d35b3726 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImpl.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.services.impl; + +import com.datastax.driver.core.utils.UUIDs; +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.sdc.destinationprovider.DestinationProvider; +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.dtos.Event; +import org.openecomp.sdc.notification.services.PropagationService; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; + +import static java.util.Objects.requireNonNull; + +public class PropagationServiceImpl implements PropagationService { + + private NotificationsDao notificationsDao; + + public PropagationServiceImpl(NotificationsDao notificationsDao) { + this.notificationsDao = notificationsDao; + } + + + @Override + public void notify(Event event, DestinationProvider destinationProvider) { + requireNonNull(event.getEventType()); + requireNonNull(event.getOriginatorId()); + List<String> subscribers = destinationProvider.getSubscribers(); + if (CollectionUtils.isEmpty(subscribers)) { + return; + } + List<NotificationEntity> notificationEntities = subscribers.stream().map( + subscriber -> { + UUID eventId = UUIDs.timeBased(); + return createNotificationEntity(event.getEventType(), subscriber, + event.getOriginatorId(), event.getAttributes(), eventId); + }).collect(Collectors.toList()); + if(CollectionUtils.isNotEmpty(notificationEntities)) { + notificationsDao.createBatch(notificationEntities); + } + } + + private NotificationEntity createNotificationEntity(String eventType, String subscriber, + String originatorId, + Map<String, Object> attributes, + UUID eventId) { + NotificationEntity notificationEntity = + new NotificationEntity(subscriber, eventId, eventType, originatorId); + if (attributes != null && !attributes.isEmpty()) { + notificationEntity.setEventAttributes(JsonUtil.object2Json(attributes)); + } + return notificationEntity; + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/SubscriptionServiceImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/SubscriptionServiceImpl.java new file mode 100644 index 0000000000..2a12463fae --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/SubscriptionServiceImpl.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.services.impl; + +import org.openecomp.sdc.notification.dao.SubscribersDao; +import org.openecomp.sdc.notification.services.SubscriptionService; + +import java.util.Set; + +public class SubscriptionServiceImpl implements SubscriptionService { + + private SubscribersDao subscribersDao; + + public SubscriptionServiceImpl(SubscribersDao subscribersDao) { + this.subscribersDao = subscribersDao; + } + + @Override + public void subscribe(String ownerId, String entityId) { + subscribersDao.subscribe(ownerId, entityId); + } + + @Override + public void unsubscribe(String ownerId, String entityId) { + subscribersDao.unsubscribe(ownerId, entityId); + } + + @Override + public Set<String> getSubscribers(String entityId) { + return subscribersDao.getSubscribers(entityId); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/resources/factoryConfiguration.json b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/resources/factoryConfiguration.json new file mode 100644 index 0000000000..3f1bdf3656 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/resources/factoryConfiguration.json @@ -0,0 +1,9 @@ +{ + "org.openecomp.sdc.notification.factories.LastNotificationDaoFactory": "org.openecomp.sdc.notification.factories.impl.LastNotificationDaoFactoryImpl", + "org.openecomp.sdc.notification.factories.NotificationsDaoFactory": "org.openecomp.sdc.notification.factories.impl.NotificationsDaoFactoryImpl", + "org.openecomp.sdc.notification.factories.NotificationsServiceFactory": "org.openecomp.sdc.notification.factories.impl.NotificationsServiceFactoryImpl", + "org.openecomp.sdc.notification.factories.SubscribersDaoFactory": "org.openecomp.sdc.notification.factories.impl.SubscribersDaoFactoryImpl", + "org.openecomp.sdc.notification.factories.PropagationServiceFactory": "org.openecomp.sdc.notification.factories.impl.PropagationServiceFactoryImpl", + "org.openecomp.sdc.notification.factories.SubscriptionServiceFactory": "org.openecomp.sdc.notification.factories.impl.SubscriptionServiceFactoryImpl", + "org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory": "org.openecomp.sdc.notification.factories.impl.NotificationPropagationManagerFactoryImpl" +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestinationTest.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestinationTest.java new file mode 100644 index 0000000000..a0dd00632e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestinationTest.java @@ -0,0 +1,67 @@ +package org.openecomp.sdc.destinationprovider.impl; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.notification.services.SubscriptionService; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; + +/** + * @author avrahamg + * @since July 13, 2017 + */ +public class MulticastDestinationTest { + @Mock + private SubscriptionService subscriptionServiceMock; + + private final String excludedSubscriber = "excluded"; + private Set<String> subscribers = new HashSet<>(Arrays.asList("a", "b", excludedSubscriber)); + private MulticastDestination multicastDestination; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void shouldReturnAllSubscribersIfNoExcludedProvided() throws Exception { + doReturn(subscribers).when(subscriptionServiceMock).getSubscribers(any()); + multicastDestination = new MulticastDestination("aa", subscriptionServiceMock); + assertEquals(subscribers.size(), multicastDestination.getSubscribers().size()); + List<String> actualSubscribers = multicastDestination.getSubscribers(); + assertTrue(actualSubscribers.containsAll(subscribers)); + } + + @Test + public void shouldReturnAllSubscribersExceptExcluded() throws Exception { + doReturn(subscribers).when(subscriptionServiceMock).getSubscribers(any()); + multicastDestination = + new MulticastDestination("aa", subscriptionServiceMock, excludedSubscriber); + List<String> actualSubscribers = multicastDestination.getSubscribers(); + assertNotEquals(this.subscribers.size(), actualSubscribers.size()); + assertFalse(actualSubscribers.containsAll(subscribers)); + assertFalse(actualSubscribers.contains(excludedSubscriber)); + } + + @Test(expected = UnsupportedOperationException.class) + public void shouldThrowUnsupportedOperationExceptionWhenTryingToChangeSubscribersList() throws + Exception { + doReturn(subscribers).when(subscriptionServiceMock).getSubscribers(any()); + multicastDestination = + new MulticastDestination("aa", subscriptionServiceMock, excludedSubscriber); + List<String> actualSubscribers = multicastDestination.getSubscribers(); + actualSubscribers.add("sss"); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImplTest.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImplTest.java new file mode 100644 index 0000000000..0eb2b6235c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImplTest.java @@ -0,0 +1,64 @@ +package org.openecomp.sdc.notification.services.impl; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.destinationprovider.DestinationProvider; +import org.openecomp.sdc.destinationprovider.impl.MulticastDestination; +import org.openecomp.sdc.destinationprovider.impl.UnicastDestination; +import org.openecomp.sdc.notification.dtos.Event; +import org.openecomp.sdc.notification.services.PropagationService; +import org.openecomp.sdc.notification.services.SubscriptionService; + +import static org.mockito.Mockito.verify; + + +/** + * @author avrahamg + * @since July 13, 2017 + */ +public class NotificationPropagationManagerImplTest { + @Mock + private PropagationService propagationServiceMock; + @Mock + private SubscriptionService subscriptionServiceMock; + @Mock + private Event eventMock; + @Captor + private ArgumentCaptor<DestinationProvider> destinationProviderCaptor; + + @Spy + @InjectMocks + private NotificationPropagationManagerImpl notificationPropagationManager; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void shouldCallPropagationServiceNotifyWithMulticastDestinationWhenNotifySubscribers() + throws Exception { + notificationPropagationManager.notifySubscribers(eventMock); + verify(propagationServiceMock).notify(Matchers.eq(eventMock), destinationProviderCaptor + .capture()); + Assert.assertTrue(destinationProviderCaptor.getValue() instanceof MulticastDestination); + + } + + @Test + public void shouldCallPropagationServiceNotifyWithUnicastDestinationWhenDirectNotification() + throws Exception { + notificationPropagationManager.directNotification(eventMock, "aaa"); + verify(propagationServiceMock).notify(Matchers.eq(eventMock), destinationProviderCaptor + .capture()); + Assert.assertTrue(destinationProviderCaptor.getValue() instanceof UnicastDestination); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImplTest.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImplTest.java new file mode 100644 index 0000000000..a20d0cce45 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImplTest.java @@ -0,0 +1,50 @@ +package org.openecomp.sdc.notification.services.impl; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.notification.dao.LastNotificationDao; +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.exceptons.NotificationNotExistException; + +import java.util.UUID; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +/** + * @author avrahamg + * @since July 13, 2017 + */ +public class NotificationsServiceImplTest { + @Mock + private LastNotificationDao lastNotificationDao; + @Mock + private NotificationsDao notificationsDao; + @Spy + @InjectMocks + private NotificationsServiceImpl notificationsService; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + public void shouldCallNotificationsDaoIfNotificationEntityExist() throws Exception { + doReturn(new NotificationEntity()).when(notificationsDao).get(any()); + notificationsService.markAsRead("ownerId", UUID.randomUUID().toString()); + verify(notificationsDao, times(1)).update(any()); + } + + @Test(expected = NotificationNotExistException.class) + public void shouldThrowExceptionIfOwnerIdAndNotificationIdDontRelate() throws Exception { + doReturn(null).when(notificationsDao).get(any()); + notificationsService.markAsRead("ownerId", UUID.randomUUID().toString()); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImplTest.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImplTest.java new file mode 100644 index 0000000000..866dec0d6d --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImplTest.java @@ -0,0 +1,96 @@ +package org.openecomp.sdc.notification.services.impl; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.destinationprovider.DestinationProvider; +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dtos.Event; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +import static org.mockito.Matchers.anyList; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +/** + * @author avrahamg + * @since July 13, 2017 + */ +public class PropagationServiceImplTest { + @Mock + private NotificationsDao notificationsDaoMock; + @Mock + private Event eventMock; + @Mock + private DestinationProvider destinationProviderMock; + @Captor + private ArgumentCaptor<List> createBatchCaptor; + + @Rule + public ExpectedException thrown= ExpectedException.none(); + + @InjectMocks + @Spy + private PropagationServiceImpl propagationService; + private List<String> subscribersList = Arrays.asList("A1, A2, A3");; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + initEventMock(); + } + + @Test + public void shouldCallToNotificationsDaoWithCreateBatchWithNotificationEntitiesAsNumberOfSubscribers() + throws Exception { + doReturn(subscribersList).when(destinationProviderMock).getSubscribers(); + propagationService.notify(eventMock, destinationProviderMock); + verify(notificationsDaoMock).createBatch(createBatchCaptor.capture()); + Assert.assertEquals(createBatchCaptor.getValue().size(), subscribersList.size()); + } + + @Test + public void shouldNotCallNotificationDaoIfSubscriberIsNull() throws Exception { + doReturn(Collections.EMPTY_LIST).when(destinationProviderMock).getSubscribers(); + verify(notificationsDaoMock,never()).createBatch(anyList()); + } + + @Test + public void shouldThrowExceptionIfEventTypeIsNull() throws Exception { + doReturn(null).when(eventMock).getEventType(); + callToNotify(); + } + + @Test + public void shouldThrowExceptionIfOriginatorIdIsNull() throws Exception { + doReturn(null).when(eventMock).getOriginatorId(); + callToNotify(); + } + + private void callToNotify() { + thrown.expect(NullPointerException.class); + propagationService.notify(eventMock, destinationProviderMock); + } + + private void initEventMock() { + doReturn("eventType").when(eventMock).getEventType(); + doReturn("originator").when(eventMock).getOriginatorId(); + doReturn("entity").when(eventMock).getEntityId(); + doReturn(new HashMap<>()).when(eventMock).getAttributes(); + } + + +}
\ No newline at end of file |