Fix 'Substitution Node not updated during import'-bug
[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, null, null,
80         null, null, null);
81
82     private Service serviceToActivate;
83     private ActivationRequestInformation activationRequestInformation;
84     private String WORKLOAD_CONTEXT = "vnfContext";
85     private String TENANT = "tenant";
86     private String DID = "distributionId";
87     private User modifier;
88
89     @BeforeEach
90     public void setup() {
91
92         ExternalConfiguration.setAppName("catalog-be");
93         MockitoAnnotations.openMocks(this);
94         final ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
95         bl.setComponentsUtils(componentsUtils);
96         serviceToActivate = new Service();
97         serviceToActivate.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED);
98         activationRequestInformation = new ActivationRequestInformation(serviceToActivate, WORKLOAD_CONTEXT, TENANT);
99         when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class)))
100             .thenReturn(Either.left(serviceToActivate));
101         when(distributionEngine.buildServiceForDistribution(any(Service.class), anyString(), anyString()))
102             .thenReturn(new NotificationDataImpl());
103         modifier = new User();
104         modifier.setUserId("uid");
105         modifier.setFirstName("user");
106     }
107
108     @Test
109     void testActivateServiceOnTenantValidationFails() {
110         int VALIDATION_FAIL_STATUS = 666;
111         when(serviceDistributionValidation.validateActivateServiceRequest
112             (anyString(), anyString(), any(User.class), any(ServiceDistributionReqInfo.class)))
113             .thenReturn(Either.right(new ResponseFormat(VALIDATION_FAIL_STATUS)));
114         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
115         assertTrue(stringResponseFormatEither.isRight());
116         assertEquals((int) stringResponseFormatEither.right().value().getStatus(), VALIDATION_FAIL_STATUS);
117     }
118
119     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
120     @Test()
121     void testDistributionAuthenticationFails() {
122         mockAllMethodsUntilDENotification();
123         when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
124             .thenReturn(ActionStatus.AUTHENTICATION_ERROR);
125         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
126         assertTrue(stringResponseFormatEither.isRight());
127         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
128         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
129     }
130
131     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
132     @Test()
133     void testDistributionUnknownHostFails() {
134         mockAllMethodsUntilDENotification();
135         when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
136             .thenReturn(ActionStatus.UNKNOWN_HOST);
137         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
138         assertTrue(stringResponseFormatEither.isRight());
139         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
140         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
141     }
142
143     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
144     @Test()
145     void testDistributionConnectionErrorFails() {
146         mockAllMethodsUntilDENotification();
147         when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
148             .thenReturn(ActionStatus.CONNNECTION_ERROR);
149         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
150         assertTrue(stringResponseFormatEither.isRight());
151         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
152         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
153     }
154
155     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
156     @Test()
157     void testDistributionObjectNotFoundFails() {
158         mockAllMethodsUntilDENotification();
159         when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
160             .thenReturn(ActionStatus.OBJECT_NOT_FOUND);
161         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
162         assertTrue(stringResponseFormatEither.isRight());
163         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
164         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
165     }
166
167     @Test()
168     void testDistributionGeneralFails() {
169         mockAllMethodsUntilDENotification();
170         when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
171             .thenReturn(ActionStatus.GENERAL_ERROR);
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     void testDistributionOk() {
180         mockAllMethodsUntilDENotification();
181         ThreadLocalsHolder.setUuid(DID);
182         when(distributionEngine
183             .notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), anyString(), any(User.class)))
184             .thenReturn(ActionStatus.OK);
185         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
186         assertTrue(stringResponseFormatEither.isLeft());
187         assertEquals(stringResponseFormatEither.left().value(), DID);
188     }
189
190     private void mockAllMethodsUntilDENotification() {
191         when(serviceDistributionValidation.validateActivateServiceRequest
192             (anyString(), anyString(), any(User.class), any(ServiceDistributionReqInfo.class)))
193             .thenReturn(Either.left(activationRequestInformation));
194         when(healthCheckBusinessLogic.isDistributionEngineUp()).thenReturn(true);
195     }
196
197     private Either<String, ResponseFormat> callActivateServiceOnTenantWIthDefaults() {
198         return bl.activateServiceOnTenantEnvironment("serviceId", "envId", modifier, new ServiceDistributionReqInfo("workload"));
199     }
200 }