summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImplTest.java
blob: 866dec0d6db23deff6560acdc6fde5868888455d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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();
    }


}