7b111ba52fb57ec532a5c11436755a077698e452
[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.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.openecomp.sdc.be.components.validation.UserValidations;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.info.DistributionStatusListResponse;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
38 import org.openecomp.sdc.be.resources.data.auditing.DistributionStatusEvent;
39 import org.openecomp.sdc.exception.ResponseFormat;
40
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.Mockito.when;
49
50 public class DistributionMonitoringBusinessLogicTest extends BaseBusinessLogicMock {
51
52     private String uId;
53     private User user;
54     private String ditributionId;
55     private String serviceId;
56
57     private DistributionMonitoringBusinessLogic businessLogic;
58
59     @Mock
60     private AuditCassandraDao cassandraDao;
61
62     @Mock
63     private UserValidations userValidations;
64
65     @Mock
66     private ComponentsUtils componentsUtils;
67
68     @Before
69     public void setUp() {
70         MockitoAnnotations.openMocks(this);
71         businessLogic = new DistributionMonitoringBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
72             groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation,
73             cassandraDao, artifactToscaOperation);
74         businessLogic.setUserValidations(userValidations);
75         businessLogic.setComponentsUtils(componentsUtils);
76
77         user = new User();
78         uId = "userId";
79         ditributionId = "did";
80         serviceId = "serviceId";
81
82         when(userValidations.validateUserExists(Mockito.eq(uId)))
83                 .thenReturn(user);
84     }
85
86     @Test
87     public void testGetListOfDistribution_givenInvalidDistributionId_thenReturnsError() {
88         when(cassandraDao.getListOfDistributionStatuses(ditributionId))
89                 .thenReturn(Either.right(ActionStatus.DISTRIBUTION_NOT_FOUND));
90
91         assertTrue(businessLogic.getListOfDistributionStatus(ditributionId, uId).isRight());
92     }
93
94     @Test
95     public void testGetListOfDistribution_givenValidDistributionId_thenReturnsSuccessful() {
96
97         List<DistributionStatusEvent> distributionEvents = new ArrayList<>();
98         DistributionStatusEvent event = new DistributionStatusEvent();
99         distributionEvents.add(event);
100
101         when(cassandraDao.getListOfDistributionStatuses(ditributionId))
102                 .thenReturn(Either.left(distributionEvents));
103
104         Either<DistributionStatusListResponse, ResponseFormat> result = businessLogic.getListOfDistributionStatus(ditributionId, uId);
105
106         assertTrue(result.isLeft());
107         assertEquals(1, result.left().value().getDistributionStatusList().size());
108     }
109
110     @Test
111     public void testGetDistributionServiceStatus_givenInvalidServiceIdId_thenReturnsError() {
112
113         when(cassandraDao.getServiceDistributionStatusesList(serviceId))
114                 .thenReturn(Either.right(ActionStatus.DISTRIBUTION_NOT_FOUND));
115         assertTrue(businessLogic.getListOfDistributionServiceStatus(serviceId, uId).isRight());
116     }
117
118     @Test
119     public void testGetDistributionServiceStatus_givenValidServiceId_thenReturnsSuccessful() {
120
121         List<AuditingGenericEvent> serviceEvents = new ArrayList<>();
122         AuditingGenericEvent event1 = new AuditingGenericEvent();
123         AuditingGenericEvent event2 = new AuditingGenericEvent();
124
125         Map<String, Object> event1Fields = new HashMap<>();
126         Map<String, Object> event2Fields = new HashMap<>();
127
128         event1Fields.put("DID", "event1");
129         event1Fields.put("ACTION", "DRequest");
130         event1Fields.put("STATUS", "200");
131         event1Fields.put("MODIFIER", uId);
132
133         event2Fields.put("DID", "event2");
134         event2Fields.put("ACTION", "DNotify");
135         event2Fields.put("STATUS", "200");
136         event2Fields.put("MODIFIER", uId);
137
138         event1.setFields(event1Fields);
139         event2.setFields(event2Fields);
140
141         serviceEvents.add(event1);
142         serviceEvents.add(event2);
143
144         when(cassandraDao.getServiceDistributionStatusesList(serviceId))
145                 .thenReturn(Either.left(serviceEvents));
146
147         assertTrue(businessLogic.getListOfDistributionServiceStatus(serviceId, uId).isLeft());
148     }
149 }