Improve testing stability
[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 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.notification.services.impl;
22
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.mockito.*;
29 import org.openecomp.sdc.destinationprovider.DestinationProvider;
30 import org.openecomp.sdc.notification.dao.NotificationsDao;
31 import org.openecomp.sdc.notification.dtos.Event;
32
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.List;
37
38 import static org.mockito.Matchers.anyList;
39 import static org.mockito.Mockito.*;
40
41 /**
42  * @author avrahamg
43  * @since July 13, 2017
44  */
45 public class PropagationServiceImplTest {
46     @Mock
47     private NotificationsDao notificationsDaoMock;
48     @Mock
49     private Event eventMock;
50     @Mock
51     private DestinationProvider destinationProviderMock;
52     @Captor
53     private ArgumentCaptor<List> createBatchCaptor;
54
55     @Rule
56     public ExpectedException thrown= ExpectedException.none();
57
58     @InjectMocks
59     @Spy
60     private PropagationServiceImpl propagationService;
61     private List<String> subscribersList = Arrays.asList("A1, A2, A3");;
62
63     @Before
64     public void setUp() throws Exception {
65         MockitoAnnotations.openMocks(this);
66         initEventMock();
67     }
68
69     @Test
70     public void shouldCallToNotificationsDaoWithCreateBatchWithNotificationEntitiesAsNumberOfSubscribers()
71         throws Exception {
72         doReturn(subscribersList).when(destinationProviderMock).getSubscribers();
73         propagationService.notify(eventMock, destinationProviderMock);
74         verify(notificationsDaoMock).createBatch(createBatchCaptor.capture());
75         Assert.assertEquals(createBatchCaptor.getValue().size(), subscribersList.size());
76     }
77
78     @Test
79     public void shouldNotCallNotificationDaoIfSubscriberIsNull() throws Exception {
80         doReturn(Collections.EMPTY_LIST).when(destinationProviderMock).getSubscribers();
81         verify(notificationsDaoMock,never()).createBatch(anyList());
82     }
83
84     @Test
85     public void shouldThrowExceptionIfEventTypeIsNull() throws Exception {
86         doReturn(null).when(eventMock).getEventType();
87         callToNotify();
88     }
89
90     @Test
91     public void shouldThrowExceptionIfOriginatorIdIsNull() throws Exception {
92         doReturn(null).when(eventMock).getOriginatorId();
93         callToNotify();
94     }
95
96     private void callToNotify() {
97         thrown.expect(NullPointerException.class);
98         propagationService.notify(eventMock, destinationProviderMock);
99     }
100
101     private void initEventMock() {
102         doReturn("eventType").when(eventMock).getEventType();
103         doReturn("originator").when(eventMock).getOriginatorId();
104         doReturn("entity").when(eventMock).getEntityId();
105         doReturn(new HashMap<>()).when(eventMock).getAttributes();
106     }
107
108
109 }