Fix broken unit tests in catalog-be
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / ServiceDistributionBLTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.be.components;
23
24 import fj.data.Either;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
31 import org.openecomp.sdc.be.components.distribution.engine.DistributionEngine;
32 import org.openecomp.sdc.be.components.distribution.engine.INotificationData;
33 import org.openecomp.sdc.be.components.distribution.engine.NotificationDataImpl;
34 import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic;
35 import org.openecomp.sdc.be.components.impl.ActivationRequestInformation;
36 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
38 import org.openecomp.sdc.be.components.path.ForwardingPathValidator;
39 import org.openecomp.sdc.be.components.utils.ComponentBusinessLogicMock;
40 import org.openecomp.sdc.be.components.validation.NodeFilterValidator;
41 import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation;
42 import org.openecomp.sdc.be.dao.api.ActionStatus;
43 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
44 import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.model.ComponentParametersView;
47 import org.openecomp.sdc.be.model.DistributionStatusEnum;
48 import org.openecomp.sdc.be.model.Service;
49 import org.openecomp.sdc.be.model.User;
50 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeFilterOperation;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
52 import org.openecomp.sdc.common.impl.ExternalConfiguration;
53 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
54 import org.openecomp.sdc.exception.ResponseFormat;
55
56 import static org.junit.Assert.assertEquals;
57 import static org.junit.Assert.assertTrue;
58 import static org.mockito.ArgumentMatchers.any;
59 import static org.mockito.ArgumentMatchers.anyString;
60 import static org.mockito.Mockito.when;
61
62 /**
63  * Created by chaya on 10/26/2017.
64  */
65 public class ServiceDistributionBLTest extends ComponentBusinessLogicMock {
66
67     private final ServiceDistributionValidation serviceDistributionValidation = Mockito.mock(ServiceDistributionValidation.class);
68     private final DistributionEngine distributionEngine = Mockito.mock(DistributionEngine.class);
69     private final HealthCheckBusinessLogic healthCheckBusinessLogic = Mockito.mock(HealthCheckBusinessLogic.class);
70     private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
71     private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
72     private final ForwardingPathValidator forwardingPathValidator = Mockito.mock(ForwardingPathValidator.class);
73     private final UiComponentDataConverter uiComponentDataConverter = Mockito.mock(UiComponentDataConverter.class);
74     private final NodeFilterOperation serviceFilterOperation = Mockito.mock(NodeFilterOperation.class);
75     private final NodeFilterValidator serviceFilterValidator = Mockito.mock(NodeFilterValidator.class);
76
77
78     @InjectMocks
79     ServiceBusinessLogic bl = new ServiceBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
80         groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation,
81         artifactsBusinessLogic, distributionEngine, componentInstanceBusinessLogic,
82         serviceDistributionValidation, forwardingPathValidator, uiComponentDataConverter,
83         serviceFilterOperation, serviceFilterValidator, artifactToscaOperation, componentContactIdValidator,
84         componentNameValidator, componentTagsValidator, componentValidator, componentIconValidator,
85         componentProjectCodeValidator, componentDescriptionValidator);
86
87     ComponentsUtils componentsUtils;
88
89     private Service serviceToActivate;
90     private ActivationRequestInformation activationRequestInformation;
91     private String WORKLOAD_CONTEXT = "vnfContext";
92     private String TENANT = "tenant";
93     private String DID = "distributionId";
94     private User modifier;
95
96
97     public ServiceDistributionBLTest() {
98     }
99
100     @Before
101     public void setup() {
102
103         ExternalConfiguration.setAppName("catalog-be");
104         MockitoAnnotations.initMocks(this);
105         componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
106         bl.setComponentsUtils(componentsUtils);
107         serviceToActivate = new Service();
108         serviceToActivate.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED);
109         activationRequestInformation = new ActivationRequestInformation(serviceToActivate, WORKLOAD_CONTEXT, TENANT);
110         when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class)))
111                 .thenReturn(Either.left(serviceToActivate));
112         when(distributionEngine.buildServiceForDistribution(any(Service.class), anyString(), anyString()))
113                 .thenReturn(new NotificationDataImpl());
114         modifier = new User();
115         modifier.setUserId("uid");
116         modifier.setFirstName("user");
117     }
118
119     @Test
120     public void testActivateServiceOnTenantValidationFails() {
121         int VALIDATION_FAIL_STATUS = 666;
122         when(serviceDistributionValidation.validateActivateServiceRequest
123                 (anyString(), anyString(),any(User.class), any(ServiceDistributionReqInfo.class)))
124                 .thenReturn(Either.right(new ResponseFormat(VALIDATION_FAIL_STATUS)));
125         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
126         assertTrue(stringResponseFormatEither.isRight());
127         assertEquals((int) stringResponseFormatEither.right().value().getStatus(), VALIDATION_FAIL_STATUS);
128     }
129
130     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
131     @Test
132     public void testDistributionAuthenticationFails() {
133         mockAllMethodsUntilDENotification();
134         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(),any(User.class)))
135                 .thenReturn(ActionStatus.AUTHENTICATION_ERROR);
136         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
137         assertTrue(stringResponseFormatEither.isRight());
138         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
139         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
140     }
141
142     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
143     @Test
144     public void testDistributionUnknownHostFails() {
145         mockAllMethodsUntilDENotification();
146         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
147                 .thenReturn(ActionStatus.UNKNOWN_HOST);
148         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
149         assertTrue(stringResponseFormatEither.isRight());
150         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
151         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
152     }
153
154     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
155     @Test
156     public void testDistributionConnectionErrorFails() {
157         mockAllMethodsUntilDENotification();
158         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
159                 .thenReturn(ActionStatus.CONNNECTION_ERROR);
160         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
161         assertTrue(stringResponseFormatEither.isRight());
162         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
163         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
164     }
165
166     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
167     @Test
168     public void testDistributionObjectNotFoundFails() {
169         mockAllMethodsUntilDENotification();
170         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
171                 .thenReturn(ActionStatus.OBJECT_NOT_FOUND);
172         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
173         assertTrue(stringResponseFormatEither.isRight());
174         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
175         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
176     }
177
178     @Test
179     public void testDistributionGeneralFails() {
180         mockAllMethodsUntilDENotification();
181         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
182                 .thenReturn(ActionStatus.GENERAL_ERROR);
183         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
184         assertTrue(stringResponseFormatEither.isRight());
185         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
186         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
187     }
188
189     @Test
190     public void testDistributionOk() {
191         mockAllMethodsUntilDENotification();
192         ThreadLocalsHolder.setUuid(DID);
193         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), anyString(), any(User.class)))
194                 .thenReturn(ActionStatus.OK);
195         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
196         assertTrue(stringResponseFormatEither.isLeft());
197         assertEquals(stringResponseFormatEither.left().value(), DID);
198     }
199
200     private void mockAllMethodsUntilDENotification() {
201         when(serviceDistributionValidation.validateActivateServiceRequest
202                 (anyString(), anyString(),any(User.class), any(ServiceDistributionReqInfo.class)))
203                 .thenReturn(Either.left(activationRequestInformation));
204         when(healthCheckBusinessLogic.isDistributionEngineUp()).thenReturn(true);
205     }
206
207     private Either<String, ResponseFormat> callActivateServiceOnTenantWIthDefaults() {
208         return bl.activateServiceOnTenantEnvironment("serviceId", "envId", modifier, new ServiceDistributionReqInfo("workload"));
209     }
210 }