Added JUnit Test for coverage
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / DistributionMonitoringBusinessLogicTest.java
1 /*
2  * -
3  *  * ============LICENSE_START=======================================================
4  *  *  Copyright (C) 2019  Nordix Foundation.
5  *  * ================================================================================
6  *  * Licensed under the Apache License, Version 2.0 (the "License");
7  *  * you may not use this file except in compliance with the License.
8  *  * You may obtain a copy of the License at
9  *  *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  *  *
12  *  * Unless required by applicable law or agreed to in writing, software
13  *  * distributed under the License is distributed on an "AS IS" BASIS,
14  *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  * See the License for the specific language governing permissions and
16  *  * limitations under the License.
17  *  *
18  *  * SPDX-License-Identifier: Apache-2.0
19  *  * ============LICENSE_END=========================================================
20  *
21  */
22
23 package org.openecomp.sdc.be.components.impl;
24
25 import fj.data.Either;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.MockitoAnnotations;
32 import org.openecomp.sdc.be.components.validation.UserValidations;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.be.info.DistributionStatusListResponse;
37 import org.openecomp.sdc.be.model.User;
38 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
39 import org.openecomp.sdc.be.resources.data.auditing.DistributionStatusEvent;
40 import org.openecomp.sdc.exception.ResponseFormat;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertTrue;
48 import static org.mockito.ArgumentMatchers.anyBoolean;
49 import static org.mockito.ArgumentMatchers.eq;
50 import static org.mockito.Mockito.when;
51
52 public class DistributionMonitoringBusinessLogicTest {
53
54     private String uId;
55     private User user;
56     private String ditributionId;
57     private String serviceId;
58
59     @InjectMocks
60     private DistributionMonitoringBusinessLogic businessLogic;
61
62     @Mock
63     private AuditCassandraDao cassandraDao;
64
65     @Mock
66     private UserValidations userValidations;
67
68     @Mock
69     private ComponentsUtils componentsUtils;
70
71     @Before
72     public void setUp() {
73         businessLogic = new DistributionMonitoringBusinessLogic();
74         MockitoAnnotations.initMocks(this);
75
76         user = new User();
77         uId = "userId";
78         ditributionId = "did";
79         serviceId = "serviceId";
80
81         when(userValidations.validateUserExists(Mockito.eq(uId), eq(ditributionId), anyBoolean()))
82                 .thenReturn(user);
83     }
84
85     @Test
86     public void testGetListOfDistribution_givenInvalidDistributionId_thenReturnsError() {
87         when(cassandraDao.getListOfDistributionStatuses(ditributionId))
88                 .thenReturn(Either.right(ActionStatus.DISTRIBUTION_NOT_FOUND));
89
90         assertTrue(businessLogic.getListOfDistributionStatus(ditributionId, uId).isRight());
91     }
92
93     @Test
94     public void testGetListOfDistribution_givenValidDistributionId_thenReturnsSuccessful() {
95
96         List<DistributionStatusEvent> distributionEvents = new ArrayList<>();
97         DistributionStatusEvent event = new DistributionStatusEvent();
98         distributionEvents.add(event);
99
100         when(cassandraDao.getListOfDistributionStatuses(ditributionId))
101                 .thenReturn(Either.left(distributionEvents));
102
103         Either<DistributionStatusListResponse, ResponseFormat> result = businessLogic.getListOfDistributionStatus(ditributionId, uId);
104
105         assertTrue(result.isLeft());
106         assertEquals(1, result.left().value().getDistributionStatusList().size());
107     }
108
109     @Test
110     public void testGetDistributionServiceStatus_givenInvalidServiceIdId_thenReturnsError() {
111
112         when(cassandraDao.getServiceDistributionStatusesList(serviceId))
113                 .thenReturn(Either.right(ActionStatus.DISTRIBUTION_NOT_FOUND));
114         assertTrue(businessLogic.getListOfDistributionServiceStatus(serviceId, uId).isRight());
115     }
116
117     @Test
118     public void testGetDistributionServiceStatus_givenValidServiceId_thenReturnsSuccessful() {
119
120         List<AuditingGenericEvent> serviceEvents = new ArrayList<>();
121         AuditingGenericEvent event1 = new AuditingGenericEvent();
122         AuditingGenericEvent event2 = new AuditingGenericEvent();
123
124         Map<String, Object> event1Fields = new HashMap<>();
125         Map<String, Object> event2Fields = new HashMap<>();
126
127         event1Fields.put("DID", "event1");
128         event1Fields.put("ACTION", "DRequest");
129         event1Fields.put("STATUS", "200");
130         event1Fields.put("MODIFIER", uId);
131
132         event2Fields.put("DID", "event2");
133         event2Fields.put("ACTION", "DNotify");
134         event2Fields.put("STATUS", "200");
135         event2Fields.put("MODIFIER", uId);
136
137         event1.setFields(event1Fields);
138         event2.setFields(event2Fields);
139
140         serviceEvents.add(event1);
141         serviceEvents.add(event2);
142
143         when(cassandraDao.getServiceDistributionStatusesList(serviceId))
144                 .thenReturn(Either.left(serviceEvents));
145
146         assertTrue(businessLogic.getListOfDistributionServiceStatus(serviceId, uId).isLeft());
147     }
148 }