Improve testing stability
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / validation / ServiceDistributionValidationTest.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.validation;
23
24 import fj.data.Either;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.openecomp.sdc.be.components.distribution.engine.IDistributionEngine;
31 import org.openecomp.sdc.be.components.impl.ActivationRequestInformation;
32 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
33 import org.openecomp.sdc.be.config.ConfigurationManager;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
38 import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.model.LifecycleStateEnum;
41 import org.openecomp.sdc.be.model.Service;
42 import org.openecomp.sdc.be.model.User;
43 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
46 import org.openecomp.sdc.common.api.ConfigurationSource;
47 import org.openecomp.sdc.common.impl.ExternalConfiguration;
48 import org.openecomp.sdc.common.impl.FSConfigurationSource;
49 import org.openecomp.sdc.exception.ResponseFormat;
50
51 import static org.junit.Assert.assertEquals;
52 import static org.mockito.ArgumentMatchers.anyString;
53 import static org.mockito.ArgumentMatchers.eq;
54 import static org.mockito.Mockito.verifyZeroInteractions;
55 import static org.mockito.Mockito.when;
56
57 public class ServiceDistributionValidationTest {
58
59     ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
60     ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
61     private static final String USER_ID = "userId";
62     private static final String SERVICE_ID = "serviceId";
63     private static final String ENV_ID = "envId";
64
65     @InjectMocks
66     private ServiceDistributionValidation testInstance;
67
68     @Mock
69     private UserValidations userValidations;
70
71     @Mock
72     private ToscaOperationFacade toscaOperationFacade;
73
74     @Mock
75     private OperationalEnvironmentDao operationalEnvironmentDao;
76
77     @Mock
78     private IDistributionEngine distributionEngine;
79
80     @Mock
81     private ComponentsUtils componentsUtils;
82
83     private User user;
84     private Service service;
85     private OperationalEnvironmentEntry operationalEnvironmentEntry;
86     private ResponseFormat errResponse;
87
88     @Before
89     public void setUp() throws Exception {
90         MockitoAnnotations.openMocks(this);
91         user = new User();
92         user.setUserId(USER_ID);
93         errResponse = new ResponseFormat();
94         service = new Service();
95         service.setUniqueId(SERVICE_ID);
96         service.setLifecycleState(LifecycleStateEnum.CERTIFIED);
97         operationalEnvironmentEntry = new OperationalEnvironmentEntry();
98         operationalEnvironmentEntry.setStatus(EnvironmentStatusEnum.COMPLETED);
99     }
100
101     @Test
102     public void validateActivateServiceRequest_userNotExist() {
103         when(userValidations.validateUserExists(eq(USER_ID))).thenThrow(new ByResponseFormatComponentException(errResponse));
104         try {
105             testInstance.validateActivateServiceRequest(SERVICE_ID, ENV_ID, user, new ServiceDistributionReqInfo("distributionData"));
106         } catch(ByResponseFormatComponentException e){
107             assertEquals(errResponse, e.getResponseFormat());
108         }
109         verifyZeroInteractions(toscaOperationFacade, operationalEnvironmentDao, componentsUtils);
110     }
111
112     @Test
113     public void validateActivateServiceRequest_ServiceNotExist() {
114         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
115         when(toscaOperationFacade.getLatestServiceByUuid(eq(SERVICE_ID))).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
116         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.GENERAL_ERROR);
117         when(componentsUtils.getResponseFormat(ActionStatus.API_RESOURCE_NOT_FOUND, ApiResourceEnum.SERVICE_ID.getValue())).thenReturn(errResponse);
118         Either<ActivationRequestInformation, ResponseFormat> activateServiceReq = testInstance.validateActivateServiceRequest(SERVICE_ID, ENV_ID, user, new ServiceDistributionReqInfo("distributionData"));
119         assertEquals(errResponse, activateServiceReq.right().value());
120         verifyZeroInteractions(operationalEnvironmentDao);
121     }
122
123     @Test
124     public void validateActivateServiceRequest_ServiceLifeCycleStateNotReadyForDistribution() {
125         service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
126         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
127         when(toscaOperationFacade.getLatestServiceByUuid(eq(SERVICE_ID))).thenReturn(Either.left(service));
128         when(componentsUtils.getResponseFormat(eq(ActionStatus.INVALID_SERVICE_STATE))).thenReturn(errResponse);
129         Either<ActivationRequestInformation, ResponseFormat> activateServiceReq = testInstance.validateActivateServiceRequest(SERVICE_ID, ENV_ID, user, new ServiceDistributionReqInfo("distributionData"));
130         assertEquals(errResponse, activateServiceReq.right().value());
131         verifyZeroInteractions(operationalEnvironmentDao);
132     }
133
134     @Test
135     public void validateActivateServiceRequest_operationalEnvNotExist() throws Exception {
136         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
137         when(toscaOperationFacade.getLatestServiceByUuid(eq(SERVICE_ID))).thenReturn(Either.left(service));
138         when(distributionEngine.getEnvironmentById(ENV_ID)).thenReturn(null);
139         when(componentsUtils.getResponseFormat(eq(ActionStatus.API_RESOURCE_NOT_FOUND), anyString())).thenReturn(errResponse);
140         Either<ActivationRequestInformation, ResponseFormat> activateServiceReq = testInstance.validateActivateServiceRequest(SERVICE_ID, ENV_ID, user, new ServiceDistributionReqInfo("distributionData"));
141         assertEquals(errResponse, activateServiceReq.right().value());
142     }
143
144     @Test
145     public void validateActivateServiceRequest_operationalEnvStatusNotComplete() {
146         operationalEnvironmentEntry.setStatus(EnvironmentStatusEnum.IN_PROGRESS);
147         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
148         when(toscaOperationFacade.getLatestServiceByUuid(eq(SERVICE_ID))).thenReturn(Either.left(service));
149         when(distributionEngine.getEnvironmentById(ENV_ID)).thenReturn(operationalEnvironmentEntry);
150         when(componentsUtils.getResponseFormat(eq(ActionStatus.API_RESOURCE_NOT_FOUND), anyString())).thenReturn(errResponse);
151         Either<ActivationRequestInformation, ResponseFormat> activateServiceReq = testInstance.validateActivateServiceRequest(SERVICE_ID, ENV_ID, user, new ServiceDistributionReqInfo("distributionData"));
152         assertEquals(errResponse, activateServiceReq.right().value());
153     }
154
155     @Test
156     public void validateActivateServiceRequest_couldNotParseDistributionData() {
157         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
158         when(toscaOperationFacade.getLatestServiceByUuid(eq(SERVICE_ID))).thenReturn(Either.left(service));
159         when(distributionEngine.getEnvironmentById(ENV_ID)).thenReturn(operationalEnvironmentEntry);
160         when(componentsUtils.getResponseFormat(eq(ActionStatus.MISSING_BODY))).thenReturn(errResponse);
161         Either<ActivationRequestInformation, ResponseFormat> activateServiceReq = testInstance.validateActivateServiceRequest(SERVICE_ID, ENV_ID, user, new ServiceDistributionReqInfo());
162         assertEquals(errResponse, activateServiceReq.right().value());
163     }
164
165     @Test
166     public void validateActivateServiceRequest_distributionDataHasNoWorkloadContext() {
167         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
168         when(toscaOperationFacade.getLatestServiceByUuid(eq(SERVICE_ID))).thenReturn(Either.left(service));
169         when(distributionEngine.getEnvironmentById(ENV_ID)).thenReturn(operationalEnvironmentEntry);
170         when(componentsUtils.getResponseFormat(eq(ActionStatus.MISSING_BODY))).thenReturn(errResponse);
171         Either<ActivationRequestInformation, ResponseFormat> activateServiceReq = testInstance.validateActivateServiceRequest(SERVICE_ID, ENV_ID, user, new ServiceDistributionReqInfo(""));
172         assertEquals(errResponse, activateServiceReq.right().value());
173     }
174
175     @Test
176     public void validateActivateServiceRequest_requestValid() {
177         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
178         when(toscaOperationFacade.getLatestServiceByUuid(eq(SERVICE_ID))).thenReturn(Either.left(service));
179         when(distributionEngine.getEnvironmentById(ENV_ID)).thenReturn(operationalEnvironmentEntry);
180         Either<ActivationRequestInformation, ResponseFormat> activateServiceReq = testInstance.validateActivateServiceRequest(SERVICE_ID, ENV_ID, user, new ServiceDistributionReqInfo("context"));
181         assertEquals(service, activateServiceReq.left().value().getServiceToActivate());
182         assertEquals("context", activateServiceReq.left().value().getWorkloadContext());
183     }
184 }