Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-notification-lib / openecomp-sdc-notification-core / src / test / java / org / openecomp / sdc / notification / services / impl / PropagationServiceImplTest.java
1 package org.openecomp.sdc.notification.services.impl;
2
3 import org.junit.Assert;
4 import org.junit.Before;
5 import org.junit.Rule;
6 import org.junit.Test;
7 import org.junit.rules.ExpectedException;
8 import org.mockito.ArgumentCaptor;
9 import org.mockito.Captor;
10 import org.mockito.InjectMocks;
11 import org.mockito.Mock;
12 import org.mockito.MockitoAnnotations;
13 import org.mockito.Spy;
14 import org.openecomp.sdc.destinationprovider.DestinationProvider;
15 import org.openecomp.sdc.notification.dao.NotificationsDao;
16 import org.openecomp.sdc.notification.dtos.Event;
17
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
22
23 import static org.mockito.Matchers.anyList;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.verify;
27
28 /**
29  * @author avrahamg
30  * @since July 13, 2017
31  */
32 public class PropagationServiceImplTest {
33     @Mock
34     private NotificationsDao notificationsDaoMock;
35     @Mock
36     private Event eventMock;
37     @Mock
38     private DestinationProvider destinationProviderMock;
39     @Captor
40     private ArgumentCaptor<List> createBatchCaptor;
41
42     @Rule
43     public ExpectedException thrown= ExpectedException.none();
44
45     @InjectMocks
46     @Spy
47     private PropagationServiceImpl propagationService;
48     private List<String> subscribersList = Arrays.asList("A1, A2, A3");;
49
50     @Before
51     public void setUp() throws Exception {
52         MockitoAnnotations.initMocks(this);
53         initEventMock();
54     }
55
56     @Test
57     public void shouldCallToNotificationsDaoWithCreateBatchWithNotificationEntitiesAsNumberOfSubscribers()
58         throws Exception {
59         doReturn(subscribersList).when(destinationProviderMock).getSubscribers();
60         propagationService.notify(eventMock, destinationProviderMock);
61         verify(notificationsDaoMock).createBatch(createBatchCaptor.capture());
62         Assert.assertEquals(createBatchCaptor.getValue().size(), subscribersList.size());
63     }
64
65     @Test
66     public void shouldNotCallNotificationDaoIfSubscriberIsNull() throws Exception {
67         doReturn(Collections.EMPTY_LIST).when(destinationProviderMock).getSubscribers();
68         verify(notificationsDaoMock,never()).createBatch(anyList());
69     }
70
71     @Test
72     public void shouldThrowExceptionIfEventTypeIsNull() throws Exception {
73         doReturn(null).when(eventMock).getEventType();
74         callToNotify();
75     }
76
77     @Test
78     public void shouldThrowExceptionIfOriginatorIdIsNull() throws Exception {
79         doReturn(null).when(eventMock).getOriginatorId();
80         callToNotify();
81     }
82
83     private void callToNotify() {
84         thrown.expect(NullPointerException.class);
85         propagationService.notify(eventMock, destinationProviderMock);
86     }
87
88     private void initEventMock() {
89         doReturn("eventType").when(eventMock).getEventType();
90         doReturn("originator").when(eventMock).getOriginatorId();
91         doReturn("entity").when(eventMock).getEntityId();
92         doReturn(new HashMap<>()).when(eventMock).getAttributes();
93     }
94
95
96 }