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.Before;
32 import org.junit.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;
61 * Created by chaya on 10/26/2017.
63 public class ServiceDistributionBLTest extends ComponentBusinessLogicMock {
65 private final ServiceDistributionValidation serviceDistributionValidation = Mockito.mock(ServiceDistributionValidation.class);
66 private final DistributionEngine distributionEngine = Mockito.mock(DistributionEngine.class);
67 private final HealthCheckBusinessLogic healthCheckBusinessLogic = Mockito.mock(HealthCheckBusinessLogic.class);
68 private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
69 private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
70 private final ForwardingPathValidator forwardingPathValidator = Mockito.mock(ForwardingPathValidator.class);
71 private final UiComponentDataConverter uiComponentDataConverter = Mockito.mock(UiComponentDataConverter.class);
75 ServiceBusinessLogic bl = new ServiceBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
76 groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation,
77 artifactsBusinessLogic, distributionEngine, componentInstanceBusinessLogic,
78 serviceDistributionValidation, forwardingPathValidator, uiComponentDataConverter,
79 artifactToscaOperation, componentContactIdValidator,
80 componentNameValidator, componentTagsValidator, componentValidator, componentIconValidator,
81 componentProjectCodeValidator, componentDescriptionValidator);
83 ComponentsUtils componentsUtils;
85 private Service serviceToActivate;
86 private ActivationRequestInformation activationRequestInformation;
87 private String WORKLOAD_CONTEXT = "vnfContext";
88 private String TENANT = "tenant";
89 private String DID = "distributionId";
90 private User modifier;
93 public ServiceDistributionBLTest() {
99 ExternalConfiguration.setAppName("catalog-be");
100 MockitoAnnotations.initMocks(this);
101 componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
102 bl.setComponentsUtils(componentsUtils);
103 serviceToActivate = new Service();
104 serviceToActivate.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED);
105 activationRequestInformation = new ActivationRequestInformation(serviceToActivate, WORKLOAD_CONTEXT, TENANT);
106 when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class)))
107 .thenReturn(Either.left(serviceToActivate));
108 when(distributionEngine.buildServiceForDistribution(any(Service.class), anyString(), anyString()))
109 .thenReturn(new NotificationDataImpl());
110 modifier = new User();
111 modifier.setUserId("uid");
112 modifier.setFirstName("user");
116 public void testActivateServiceOnTenantValidationFails() {
117 int VALIDATION_FAIL_STATUS = 666;
118 when(serviceDistributionValidation.validateActivateServiceRequest
119 (anyString(), anyString(),any(User.class), any(ServiceDistributionReqInfo.class)))
120 .thenReturn(Either.right(new ResponseFormat(VALIDATION_FAIL_STATUS)));
121 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
122 assertTrue(stringResponseFormatEither.isRight());
123 assertEquals((int) stringResponseFormatEither.right().value().getStatus(), VALIDATION_FAIL_STATUS);
126 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
128 public void testDistributionAuthenticationFails() {
129 mockAllMethodsUntilDENotification();
130 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(),any(User.class)))
131 .thenReturn(ActionStatus.AUTHENTICATION_ERROR);
132 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
133 assertTrue(stringResponseFormatEither.isRight());
134 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
135 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
138 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
140 public void testDistributionUnknownHostFails() {
141 mockAllMethodsUntilDENotification();
142 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
143 .thenReturn(ActionStatus.UNKNOWN_HOST);
144 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
145 assertTrue(stringResponseFormatEither.isRight());
146 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
147 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
150 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
152 public void testDistributionConnectionErrorFails() {
153 mockAllMethodsUntilDENotification();
154 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
155 .thenReturn(ActionStatus.CONNNECTION_ERROR);
156 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
157 assertTrue(stringResponseFormatEither.isRight());
158 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
159 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
162 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
164 public void testDistributionObjectNotFoundFails() {
165 mockAllMethodsUntilDENotification();
166 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
167 .thenReturn(ActionStatus.OBJECT_NOT_FOUND);
168 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
169 assertTrue(stringResponseFormatEither.isRight());
170 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
171 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
175 public void testDistributionGeneralFails() {
176 mockAllMethodsUntilDENotification();
177 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
178 .thenReturn(ActionStatus.GENERAL_ERROR);
179 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
180 assertTrue(stringResponseFormatEither.isRight());
181 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
182 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
186 public void testDistributionOk() {
187 mockAllMethodsUntilDENotification();
188 ThreadLocalsHolder.setUuid(DID);
189 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), anyString(), any(User.class)))
190 .thenReturn(ActionStatus.OK);
191 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
192 assertTrue(stringResponseFormatEither.isLeft());
193 assertEquals(stringResponseFormatEither.left().value(), DID);
196 private void mockAllMethodsUntilDENotification() {
197 when(serviceDistributionValidation.validateActivateServiceRequest
198 (anyString(), anyString(),any(User.class), any(ServiceDistributionReqInfo.class)))
199 .thenReturn(Either.left(activationRequestInformation));
200 when(healthCheckBusinessLogic.isDistributionEngineUp()).thenReturn(true);
203 private Either<String, ResponseFormat> callActivateServiceOnTenantWIthDefaults() {
204 return bl.activateServiceOnTenantEnvironment("serviceId", "envId", modifier, new ServiceDistributionReqInfo("workload"));