Support deletion of archived services in SDC 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 static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.Mockito.when;
29
30 import fj.data.Either;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
37 import org.openecomp.sdc.be.components.distribution.engine.DistributionEngine;
38 import org.openecomp.sdc.be.components.distribution.engine.INotificationData;
39 import org.openecomp.sdc.be.components.distribution.engine.NotificationDataImpl;
40 import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic;
41 import org.openecomp.sdc.be.components.impl.ActivationRequestInformation;
42 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
43 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
44 import org.openecomp.sdc.be.components.path.ForwardingPathValidator;
45 import org.openecomp.sdc.be.components.utils.ComponentBusinessLogicMock;
46 import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation;
47 import org.openecomp.sdc.be.dao.api.ActionStatus;
48 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
49 import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo;
50 import org.openecomp.sdc.be.impl.ComponentsUtils;
51 import org.openecomp.sdc.be.model.ComponentParametersView;
52 import org.openecomp.sdc.be.model.DistributionStatusEnum;
53 import org.openecomp.sdc.be.model.Service;
54 import org.openecomp.sdc.be.model.User;
55 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
56 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
57 import org.openecomp.sdc.common.impl.ExternalConfiguration;
58 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
59 import org.openecomp.sdc.exception.ResponseFormat;
60
61 class ServiceDistributionBLTest extends ComponentBusinessLogicMock {
62
63     private final ServiceDistributionValidation serviceDistributionValidation = Mockito.mock(ServiceDistributionValidation.class);
64     private final DistributionEngine distributionEngine = Mockito.mock(DistributionEngine.class);
65     private final HealthCheckBusinessLogic healthCheckBusinessLogic = Mockito.mock(HealthCheckBusinessLogic.class);
66     private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
67     private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
68     private final ForwardingPathValidator forwardingPathValidator = Mockito.mock(ForwardingPathValidator.class);
69     private final UiComponentDataConverter uiComponentDataConverter = Mockito.mock(UiComponentDataConverter.class);
70     private final ModelOperation modelOperation = Mockito.mock(ModelOperation.class);
71
72     @InjectMocks
73     private final ServiceBusinessLogic bl = new ServiceBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
74         groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation,
75         artifactsBusinessLogic, distributionEngine, componentInstanceBusinessLogic,
76         serviceDistributionValidation, forwardingPathValidator, uiComponentDataConverter,
77         artifactToscaOperation, componentContactIdValidator,
78         componentNameValidator, componentTagsValidator, componentValidator, componentIconValidator,
79         componentProjectCodeValidator, componentDescriptionValidator, modelOperation);
80
81     private Service serviceToActivate;
82     private ActivationRequestInformation activationRequestInformation;
83     private String WORKLOAD_CONTEXT = "vnfContext";
84     private String TENANT = "tenant";
85     private String DID = "distributionId";
86     private User modifier;
87
88     @BeforeEach
89     public void setup() {
90
91         ExternalConfiguration.setAppName("catalog-be");
92         MockitoAnnotations.openMocks(this);
93         final ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
94         bl.setComponentsUtils(componentsUtils);
95         serviceToActivate = new Service();
96         serviceToActivate.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED);
97         activationRequestInformation = new ActivationRequestInformation(serviceToActivate, WORKLOAD_CONTEXT, TENANT);
98         when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class)))
99             .thenReturn(Either.left(serviceToActivate));
100         when(distributionEngine.buildServiceForDistribution(any(Service.class), anyString(), anyString()))
101             .thenReturn(new NotificationDataImpl());
102         modifier = new User();
103         modifier.setUserId("uid");
104         modifier.setFirstName("user");
105     }
106
107     @Test
108     void testActivateServiceOnTenantValidationFails() {
109         int VALIDATION_FAIL_STATUS = 666;
110         when(serviceDistributionValidation.validateActivateServiceRequest
111             (anyString(), anyString(), any(User.class), any(ServiceDistributionReqInfo.class)))
112             .thenReturn(Either.right(new ResponseFormat(VALIDATION_FAIL_STATUS)));
113         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
114         assertTrue(stringResponseFormatEither.isRight());
115         assertEquals((int) stringResponseFormatEither.right().value().getStatus(), VALIDATION_FAIL_STATUS);
116     }
117
118     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
119     @Test()
120     void testDistributionAuthenticationFails() {
121         mockAllMethodsUntilDENotification();
122         when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
123             .thenReturn(ActionStatus.AUTHENTICATION_ERROR);
124         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
125         assertTrue(stringResponseFormatEither.isRight());
126         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
127         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
128     }
129
130     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
131     @Test()
132     void testDistributionUnknownHostFails() {
133         mockAllMethodsUntilDENotification();
134         when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
135             .thenReturn(ActionStatus.UNKNOWN_HOST);
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     void testDistributionConnectionErrorFails() {
145         mockAllMethodsUntilDENotification();
146         when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
147             .thenReturn(ActionStatus.CONNNECTION_ERROR);
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     void testDistributionObjectNotFoundFails() {
157         mockAllMethodsUntilDENotification();
158         when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
159             .thenReturn(ActionStatus.OBJECT_NOT_FOUND);
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     @Test()
167     void testDistributionGeneralFails() {
168         mockAllMethodsUntilDENotification();
169         when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
170             .thenReturn(ActionStatus.GENERAL_ERROR);
171         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
172         assertTrue(stringResponseFormatEither.isRight());
173         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
174         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
175     }
176
177     @Test
178     void testDistributionOk() {
179         mockAllMethodsUntilDENotification();
180         ThreadLocalsHolder.setUuid(DID);
181         when(distributionEngine
182             .notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), anyString(), any(User.class)))
183             .thenReturn(ActionStatus.OK);
184         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
185         assertTrue(stringResponseFormatEither.isLeft());
186         assertEquals(stringResponseFormatEither.left().value(), DID);
187     }
188
189     private void mockAllMethodsUntilDENotification() {
190         when(serviceDistributionValidation.validateActivateServiceRequest
191             (anyString(), anyString(), any(User.class), any(ServiceDistributionReqInfo.class)))
192             .thenReturn(Either.left(activationRequestInformation));
193         when(healthCheckBusinessLogic.isDistributionEngineUp()).thenReturn(true);
194     }
195
196     private Either<String, ResponseFormat> callActivateServiceOnTenantWIthDefaults() {
197         return bl.activateServiceOnTenantEnvironment("serviceId", "envId", modifier, new ServiceDistributionReqInfo("workload"));
198     }
199 }