2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
22 package org.openecomp.sdc.be.components;
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;
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.common.impl.ExternalConfiguration;
57 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
58 import org.openecomp.sdc.exception.ResponseFormat;
60 class ServiceDistributionBLTest extends ComponentBusinessLogicMock {
62 private final ServiceDistributionValidation serviceDistributionValidation = Mockito.mock(ServiceDistributionValidation.class);
63 private final DistributionEngine distributionEngine = Mockito.mock(DistributionEngine.class);
64 private final HealthCheckBusinessLogic healthCheckBusinessLogic = Mockito.mock(HealthCheckBusinessLogic.class);
65 private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
66 private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
67 private final ForwardingPathValidator forwardingPathValidator = Mockito.mock(ForwardingPathValidator.class);
68 private final UiComponentDataConverter uiComponentDataConverter = Mockito.mock(UiComponentDataConverter.class);
71 private final ServiceBusinessLogic bl = new ServiceBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
72 groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation,
73 artifactsBusinessLogic, distributionEngine, componentInstanceBusinessLogic,
74 serviceDistributionValidation, forwardingPathValidator, uiComponentDataConverter,
75 artifactToscaOperation, componentContactIdValidator,
76 componentNameValidator, componentTagsValidator, componentValidator, componentIconValidator,
77 componentProjectCodeValidator, componentDescriptionValidator);
79 private Service serviceToActivate;
80 private ActivationRequestInformation activationRequestInformation;
81 private String WORKLOAD_CONTEXT = "vnfContext";
82 private String TENANT = "tenant";
83 private String DID = "distributionId";
84 private User modifier;
89 ExternalConfiguration.setAppName("catalog-be");
90 MockitoAnnotations.initMocks(this);
91 final ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
92 bl.setComponentsUtils(componentsUtils);
93 serviceToActivate = new Service();
94 serviceToActivate.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED);
95 activationRequestInformation = new ActivationRequestInformation(serviceToActivate, WORKLOAD_CONTEXT, TENANT);
96 when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class)))
97 .thenReturn(Either.left(serviceToActivate));
98 when(distributionEngine.buildServiceForDistribution(any(Service.class), anyString(), anyString()))
99 .thenReturn(new NotificationDataImpl());
100 modifier = new User();
101 modifier.setUserId("uid");
102 modifier.setFirstName("user");
106 void testActivateServiceOnTenantValidationFails() {
107 int VALIDATION_FAIL_STATUS = 666;
108 when(serviceDistributionValidation.validateActivateServiceRequest
109 (anyString(), anyString(), any(User.class), any(ServiceDistributionReqInfo.class)))
110 .thenReturn(Either.right(new ResponseFormat(VALIDATION_FAIL_STATUS)));
111 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
112 assertTrue(stringResponseFormatEither.isRight());
113 assertEquals((int) stringResponseFormatEither.right().value().getStatus(), VALIDATION_FAIL_STATUS);
116 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
118 void testDistributionAuthenticationFails() {
119 mockAllMethodsUntilDENotification();
120 when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
121 .thenReturn(ActionStatus.AUTHENTICATION_ERROR);
122 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
123 assertTrue(stringResponseFormatEither.isRight());
124 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
125 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
128 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
130 void testDistributionUnknownHostFails() {
131 mockAllMethodsUntilDENotification();
132 when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
133 .thenReturn(ActionStatus.UNKNOWN_HOST);
134 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
135 assertTrue(stringResponseFormatEither.isRight());
136 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
137 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
140 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
142 void testDistributionConnectionErrorFails() {
143 mockAllMethodsUntilDENotification();
144 when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
145 .thenReturn(ActionStatus.CONNNECTION_ERROR);
146 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
147 assertTrue(stringResponseFormatEither.isRight());
148 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
149 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
152 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
154 void testDistributionObjectNotFoundFails() {
155 mockAllMethodsUntilDENotification();
156 when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
157 .thenReturn(ActionStatus.OBJECT_NOT_FOUND);
158 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
159 assertTrue(stringResponseFormatEither.isRight());
160 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
161 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
165 void testDistributionGeneralFails() {
166 mockAllMethodsUntilDENotification();
167 when(distributionEngine.notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
168 .thenReturn(ActionStatus.GENERAL_ERROR);
169 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
170 assertTrue(stringResponseFormatEither.isRight());
171 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
172 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
176 void testDistributionOk() {
177 mockAllMethodsUntilDENotification();
178 ThreadLocalsHolder.setUuid(DID);
179 when(distributionEngine
180 .notifyService(anyString(), any(Service.class), any(INotificationData.class), anyString(), anyString(), any(User.class)))
181 .thenReturn(ActionStatus.OK);
182 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
183 assertTrue(stringResponseFormatEither.isLeft());
184 assertEquals(stringResponseFormatEither.left().value(), DID);
187 private void mockAllMethodsUntilDENotification() {
188 when(serviceDistributionValidation.validateActivateServiceRequest
189 (anyString(), anyString(), any(User.class), any(ServiceDistributionReqInfo.class)))
190 .thenReturn(Either.left(activationRequestInformation));
191 when(healthCheckBusinessLogic.isDistributionEngineUp()).thenReturn(true);
194 private Either<String, ResponseFormat> callActivateServiceOnTenantWIthDefaults() {
195 return bl.activateServiceOnTenantEnvironment("serviceId", "envId", modifier, new ServiceDistributionReqInfo("workload"));