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