Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / ServiceDistributionBLTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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  */
20
21 package org.openecomp.sdc.be.components;
22
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
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.ServiceBusinessLogic;
37 import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation;
38 import org.openecomp.sdc.be.config.ConfigurationManager;
39 import org.openecomp.sdc.be.dao.api.ActionStatus;
40 import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo;
41 import org.openecomp.sdc.be.impl.ComponentsUtils;
42 import org.openecomp.sdc.be.model.ComponentParametersView;
43 import org.openecomp.sdc.be.model.DistributionStatusEnum;
44 import org.openecomp.sdc.be.model.Service;
45 import org.openecomp.sdc.be.model.User;
46 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
47 import org.openecomp.sdc.common.api.ConfigurationSource;
48 import org.openecomp.sdc.common.impl.ExternalConfiguration;
49 import org.openecomp.sdc.common.impl.FSConfigurationSource;
50 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
51 import org.openecomp.sdc.exception.ResponseFormat;
52
53 import static org.junit.Assert.assertEquals;
54 import static org.junit.Assert.assertTrue;
55 import static org.mockito.ArgumentMatchers.any;
56 import static org.mockito.ArgumentMatchers.anyString;
57 import static org.mockito.Mockito.when;
58
59 /**
60  * Created by chaya on 10/26/2017.
61  */
62 public class ServiceDistributionBLTest {
63
64     @InjectMocks
65     ServiceBusinessLogic bl = new ServiceBusinessLogic();
66
67     @Mock
68     ServiceDistributionValidation serviceDistributionValidation;
69
70     @Mock
71     HealthCheckBusinessLogic healthCheckBusinessLogic;
72
73     @Mock
74     ToscaOperationFacade toscaOperationFacade;
75
76     ComponentsUtils componentsUtils;
77
78     @Mock
79     DistributionEngine distributionEngine;
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
89     public ServiceDistributionBLTest() {
90     }
91
92     @Before
93     public void setup() {
94
95         ExternalConfiguration.setAppName("catalog-be");
96         MockitoAnnotations.initMocks(this);
97         // init Configuration
98         String appConfigDir = "src/test/resources/config/catalog-be";
99         ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
100         ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
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");
113     }
114
115     @Test
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);
124     }
125
126     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
127     @Test
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());
136     }
137
138     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
139     @Test
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());
148     }
149
150     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
151     @Test
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());
160     }
161
162     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
163     @Test
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());
172     }
173
174     @Test
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());
183     }
184
185     @Test
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);
194     }
195
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);
201     }
202
203     private Either<String, ResponseFormat> callActivateServiceOnTenantWIthDefaults() {
204         return bl.activateServiceOnTenantEnvironment("serviceId", "envId", modifier, new ServiceDistributionReqInfo("workload"));
205     }
206 }