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 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.config.ConfigurationManager;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
45 import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo;
46 import org.openecomp.sdc.be.impl.ComponentsUtils;
47 import org.openecomp.sdc.be.model.ComponentParametersView;
48 import org.openecomp.sdc.be.model.DistributionStatusEnum;
49 import org.openecomp.sdc.be.model.Service;
50 import org.openecomp.sdc.be.model.User;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeFilterOperation;
52 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
53 import org.openecomp.sdc.common.api.ConfigurationSource;
54 import org.openecomp.sdc.common.impl.ExternalConfiguration;
55 import org.openecomp.sdc.common.impl.FSConfigurationSource;
56 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
57 import org.openecomp.sdc.exception.ResponseFormat;
59 import static org.junit.Assert.assertEquals;
60 import static org.junit.Assert.assertTrue;
61 import static org.mockito.ArgumentMatchers.any;
62 import static org.mockito.ArgumentMatchers.anyString;
63 import static org.mockito.Mockito.when;
66 * Created by chaya on 10/26/2017.
68 public class ServiceDistributionBLTest extends ComponentBusinessLogicMock {
70 private final ServiceDistributionValidation serviceDistributionValidation = Mockito.mock(ServiceDistributionValidation.class);
71 private final DistributionEngine distributionEngine = Mockito.mock(DistributionEngine.class);
72 private final HealthCheckBusinessLogic healthCheckBusinessLogic = Mockito.mock(HealthCheckBusinessLogic.class);
73 private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
74 private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
75 private final ForwardingPathValidator forwardingPathValidator = Mockito.mock(ForwardingPathValidator.class);
76 private final UiComponentDataConverter uiComponentDataConverter = Mockito.mock(UiComponentDataConverter.class);
77 private final NodeFilterOperation serviceFilterOperation = Mockito.mock(NodeFilterOperation.class);
78 private final NodeFilterValidator serviceFilterValidator = Mockito.mock(NodeFilterValidator.class);
82 ServiceBusinessLogic bl = new ServiceBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
83 groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation,
84 artifactsBusinessLogic, distributionEngine, componentInstanceBusinessLogic,
85 serviceDistributionValidation, forwardingPathValidator, uiComponentDataConverter,
86 serviceFilterOperation, serviceFilterValidator, artifactToscaOperation, componentContactIdValidator,
87 componentNameValidator, componentTagsValidator, componentValidator, componentIconValidator,
88 componentProjectCodeValidator, componentDescriptionValidator);
90 ComponentsUtils componentsUtils;
92 private Service serviceToActivate;
93 private ActivationRequestInformation activationRequestInformation;
94 private String WORKLOAD_CONTEXT = "vnfContext";
95 private String TENANT = "tenant";
96 private String DID = "distributionId";
97 private User modifier;
100 public ServiceDistributionBLTest() {
104 public void setup() {
106 ExternalConfiguration.setAppName("catalog-be");
107 MockitoAnnotations.initMocks(this);
108 // init Configuration
109 String appConfigDir = "src/test/resources/config/catalog-be";
110 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
111 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
112 componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
113 bl.setComponentsUtils(componentsUtils);
114 serviceToActivate = new Service();
115 serviceToActivate.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED);
116 activationRequestInformation = new ActivationRequestInformation(serviceToActivate, WORKLOAD_CONTEXT, TENANT);
117 when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class)))
118 .thenReturn(Either.left(serviceToActivate));
119 when(distributionEngine.buildServiceForDistribution(any(Service.class), anyString(), anyString()))
120 .thenReturn(new NotificationDataImpl());
121 modifier = new User();
122 modifier.setUserId("uid");
123 modifier.setFirstName("user");
127 public void testActivateServiceOnTenantValidationFails() {
128 int VALIDATION_FAIL_STATUS = 666;
129 when(serviceDistributionValidation.validateActivateServiceRequest
130 (anyString(), anyString(),any(User.class), any(ServiceDistributionReqInfo.class)))
131 .thenReturn(Either.right(new ResponseFormat(VALIDATION_FAIL_STATUS)));
132 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
133 assertTrue(stringResponseFormatEither.isRight());
134 assertEquals((int) stringResponseFormatEither.right().value().getStatus(), VALIDATION_FAIL_STATUS);
137 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
139 public void testDistributionAuthenticationFails() {
140 mockAllMethodsUntilDENotification();
141 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(),any(User.class)))
142 .thenReturn(ActionStatus.AUTHENTICATION_ERROR);
143 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
144 assertTrue(stringResponseFormatEither.isRight());
145 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
146 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
149 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
151 public void testDistributionUnknownHostFails() {
152 mockAllMethodsUntilDENotification();
153 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
154 .thenReturn(ActionStatus.UNKNOWN_HOST);
155 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
156 assertTrue(stringResponseFormatEither.isRight());
157 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
158 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
161 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
163 public void testDistributionConnectionErrorFails() {
164 mockAllMethodsUntilDENotification();
165 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
166 .thenReturn(ActionStatus.CONNNECTION_ERROR);
167 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
168 assertTrue(stringResponseFormatEither.isRight());
169 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
170 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
173 //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
175 public void testDistributionObjectNotFoundFails() {
176 mockAllMethodsUntilDENotification();
177 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
178 .thenReturn(ActionStatus.OBJECT_NOT_FOUND);
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 testDistributionGeneralFails() {
187 mockAllMethodsUntilDENotification();
188 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
189 .thenReturn(ActionStatus.GENERAL_ERROR);
190 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
191 assertTrue(stringResponseFormatEither.isRight());
192 assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
193 assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
197 public void testDistributionOk() {
198 mockAllMethodsUntilDENotification();
199 ThreadLocalsHolder.setUuid(DID);
200 when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), anyString(), any(User.class)))
201 .thenReturn(ActionStatus.OK);
202 Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
203 assertTrue(stringResponseFormatEither.isLeft());
204 assertEquals(stringResponseFormatEither.left().value(), DID);
207 private void mockAllMethodsUntilDENotification() {
208 when(serviceDistributionValidation.validateActivateServiceRequest
209 (anyString(), anyString(),any(User.class), any(ServiceDistributionReqInfo.class)))
210 .thenReturn(Either.left(activationRequestInformation));
211 when(healthCheckBusinessLogic.isDistributionEngineUp()).thenReturn(true);
214 private Either<String, ResponseFormat> callActivateServiceOnTenantWIthDefaults() {
215 return bl.activateServiceOnTenantEnvironment("serviceId", "envId", modifier, new ServiceDistributionReqInfo("workload"));