2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import com.fasterxml.jackson.databind.DeserializationFeature;
31 import fj.data.Either;
32 import java.util.ArrayList;
33 import java.util.List;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.client.Invocation;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38 import org.glassfish.jersey.client.ClientConfig;
39 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
40 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
41 import org.glassfish.jersey.server.ResourceConfig;
42 import org.junit.jupiter.api.AfterEach;
43 import org.junit.jupiter.api.BeforeAll;
44 import org.junit.jupiter.api.BeforeEach;
45 import org.junit.jupiter.api.Test;
46 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
47 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
48 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
49 import org.openecomp.sdc.be.components.upgrade.ServiceInfo;
50 import org.openecomp.sdc.be.components.upgrade.UpgradeBusinessLogic;
51 import org.openecomp.sdc.be.components.upgrade.UpgradeRequest;
52 import org.openecomp.sdc.be.components.upgrade.UpgradeStatus;
53 import org.openecomp.sdc.be.components.validation.UserValidations;
54 import org.openecomp.sdc.be.config.ConfigurationManager;
55 import org.openecomp.sdc.be.dao.api.ActionStatus;
56 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
57 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
58 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
59 import org.openecomp.sdc.be.impl.ComponentsUtils;
60 import org.openecomp.sdc.be.model.ComponentInstance;
61 import org.openecomp.sdc.be.model.ComponentParametersView;
62 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
63 import org.openecomp.sdc.be.model.LifecycleStateEnum;
64 import org.openecomp.sdc.be.model.Resource;
65 import org.openecomp.sdc.be.model.Service;
66 import org.openecomp.sdc.be.model.User;
67 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
68 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.UpgradeOperation;
69 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
70 import org.openecomp.sdc.be.user.UserBusinessLogic;
71 import org.openecomp.sdc.common.api.ConfigurationSource;
72 import org.openecomp.sdc.common.api.Constants;
73 import org.openecomp.sdc.common.impl.ExternalConfiguration;
74 import org.openecomp.sdc.common.impl.FSConfigurationSource;
75 import org.openecomp.sdc.exception.ResponseFormat;
76 import org.openecomp.sdc.exception.ServiceException;
77 import org.springframework.context.annotation.Bean;
78 import org.springframework.context.annotation.Import;
79 import org.springframework.http.HttpStatus;
81 class AutomatedUpgradeEndpointTest extends JerseySpringBaseTest {
83 static ConfigurationSource configurationSource = new FSConfigurationSource(
84 ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
85 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
87 private static final String RESOURCE_ID_PREV = "prevVF";
88 private static final String RESOURCE_ID_NEW = "newVF";
89 private static final String SERVICE_ID_PREV = "prevSer";
90 private static final String SERVICE_ID_NEW = "newSer";
91 private static final String SERVICE_ID_PROXY = "serProxy";
92 private static final String SERVICE_ID_PROXY_PREV = "serProxyContainerPrev";
93 private static final String SERVICE_ID_PROXY_NEW = "serProxyContainerNew";
94 private static final String INVARIANT_ID = "invariantUUID";
95 private static final String USER_ID = "userId";
97 private static LifecycleBusinessLogic lifecycleBusinessLogic;
98 private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
99 private static UserValidations userValidations;
100 private static ToscaOperationFacade toscaOperationFacade;
101 private static ComponentsUtils componentsUtils;
102 private static UpgradeOperation upgradeOperation;
103 private static JanusGraphDao janusGraphDao;
105 private static User user;
106 private static Resource vfPrev;
107 private static Resource vfNew;
108 private static Service servicePrev;
109 private static Service serviceNew;
110 private static Service serviceNewCheckIn;
111 private static ComponentInstance istanceNew;
113 private static Service serviceProxy;
114 private static Service serviceProxyContainerPrev;
115 private static Service serviceProxyContainerNew;
117 @org.springframework.context.annotation.Configuration
118 @Import(BaseTestConfig.class)
119 static class AutomatedUpgradeTestConfig {
122 AutomatedUpgradeEndpoint automatedUpgradeEndpoint() {
124 UserBusinessLogic userBusinessLogic = mock(UserBusinessLogic.class);
125 ComponentsUtils componentsUtils = mock(ComponentsUtils.class);
127 return new AutomatedUpgradeEndpoint(userBusinessLogic, componentsUtils,
128 upgradeBusinessLogic());
132 UpgradeBusinessLogic upgradeBusinessLogic() {
133 return new UpgradeBusinessLogic(lifecycleBusinessLogic, componentInstanceBusinessLogic, userValidations,
134 toscaOperationFacade, componentsUtils, upgradeOperation,
140 public static void initClass() {
141 lifecycleBusinessLogic = mock(LifecycleBusinessLogic.class);
142 componentInstanceBusinessLogic = mock(ComponentInstanceBusinessLogic.class);
143 userValidations = mock(UserValidations.class);
144 toscaOperationFacade = mock(ToscaOperationFacade.class);
145 componentsUtils = mock(ComponentsUtils.class);
146 upgradeOperation = mock(UpgradeOperation.class);
147 janusGraphDao = mock(JanusGraphDao.class);
148 user = mock(User.class);
152 public void init() throws Exception {
155 when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
156 when(toscaOperationFacade.getToscaFullElement(eq(RESOURCE_ID_PREV))).thenReturn(Either.left(vfPrev));
157 when(toscaOperationFacade.getToscaFullElement(eq(RESOURCE_ID_NEW))).thenReturn(Either.left(vfNew));
158 when(toscaOperationFacade.getToscaFullElement(eq(SERVICE_ID_PREV))).thenReturn(Either.left(servicePrev));
159 when(toscaOperationFacade.getToscaFullElement(eq(SERVICE_ID_NEW))).thenReturn(Either.left(serviceNew));
161 Either<Service, ResponseFormat> fromLifeCycle = Either.left(serviceNew);
162 doReturn(fromLifeCycle).when(lifecycleBusinessLogic)
163 .changeComponentState(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID_PREV), any(User.class),
164 eq(LifeCycleTransitionEnum.CHECKOUT), any(LifecycleChangeInfoWithAction.class), eq(false),
167 when(toscaOperationFacade.getToscaElement(eq(RESOURCE_ID_PREV), any(ComponentParametersView.class)))
168 .thenReturn(Either.left(vfPrev));
169 when(componentInstanceBusinessLogic
170 .changeInstanceVersion(any(Service.class), any(ComponentInstance.class), any(ComponentInstance.class),
171 any(User.class), eq(ComponentTypeEnum.SERVICE))).thenReturn(istanceNew);
173 doReturn(Either.left(serviceNewCheckIn)).when(lifecycleBusinessLogic)
174 .changeComponentState(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID_NEW), any(User.class),
175 eq(LifeCycleTransitionEnum.CHECKIN), any(LifecycleChangeInfoWithAction.class),
176 eq(false), eq(true));
181 void after() throws Exception {
186 protected void configureClient(ClientConfig config) {
187 final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider()
188 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
189 config.register(jacksonJsonProvider);
193 protected ResourceConfig configure() {
194 return super.configure(AutomatedUpgradeTestConfig.class).register(AutomatedUpgradeEndpoint.class);
198 void upgradeVfInService_success() {
199 List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
200 UpgradeRequest request = new UpgradeRequest(SERVICE_ID_PREV);
201 inputsToUpdate.add(request);
203 Invocation.Builder builder = buildAutomatedUpgradeCall(RESOURCE_ID_NEW);
204 UpgradeStatus status = builder
205 .post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), UpgradeStatus.class);
207 assertThat(status.getStatus()).isEqualTo(ActionStatus.OK);
208 List<ServiceInfo> expected = new ArrayList<>();
209 ServiceInfo serviceInfo = new ServiceInfo(serviceNewCheckIn.getUniqueId(), ActionStatus.OK);
210 serviceInfo.setName(serviceNewCheckIn.getName());
211 serviceInfo.setVersion(serviceNewCheckIn.getVersion());
212 expected.add(serviceInfo);
213 assertThat(status.getComponentToUpgradeStatus()).hasSameSizeAs(expected);
214 assertThat(status.getComponentToUpgradeStatus()).hasSameElementsAs(expected);
219 void upgradeVfInService_IdNotExist() {
220 List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
221 UpgradeRequest request = new UpgradeRequest(SERVICE_ID_PREV);
222 inputsToUpdate.add(request);
223 String wrongId = "1234";
224 when(toscaOperationFacade.getToscaFullElement(eq(wrongId)))
225 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
226 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND)))
227 .thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
228 String[] variables = {wrongId};
229 ServiceException serviceException = new ServiceException("SVC4063",
230 "Error: Requested '%1' resource was not found.", variables);
231 ResponseFormat expected = new ResponseFormat(HttpStatus.NOT_FOUND.value());
232 expected.setServiceException(serviceException);
233 when(componentsUtils.getResponseFormatByResource(eq(ActionStatus.RESOURCE_NOT_FOUND), eq(wrongId)))
234 .thenReturn(expected);
236 Response response = buildAutomatedUpgradeCall(wrongId)
237 .post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), Response.class);
238 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
240 ResponseFormat actual = response.readEntity(ResponseFormat.class);
241 assertThat(actual.getMessageId()).isEqualTo(expected.getMessageId());
242 assertThat(actual.getFormattedMessage()).isEqualTo(expected.getFormattedMessage());
247 void upgradeVfInService_NotHihgestCertified() {
248 List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
249 UpgradeRequest request = new UpgradeRequest(RESOURCE_ID_PREV);
250 inputsToUpdate.add(request);
252 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND)))
253 .thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
254 String[] variables = {vfPrev.getName()};
255 ServiceException serviceException = new ServiceException("SVC4699",
256 "Error: Component %1 is not highest certified", variables);
257 ResponseFormat expected = new ResponseFormat(HttpStatus.BAD_REQUEST.value());
258 expected.setServiceException(serviceException);
260 .getResponseFormat(eq(ActionStatus.COMPONENT_IS_NOT_HIHGEST_CERTIFIED), eq(vfPrev.getName())))
261 .thenReturn(expected);
263 Response response = buildAutomatedUpgradeCall(RESOURCE_ID_PREV)
264 .post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), Response.class);
265 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
267 ResponseFormat actual = response.readEntity(ResponseFormat.class);
268 assertThat(actual.getMessageId()).isEqualTo(expected.getMessageId());
269 assertThat(actual.getFormattedMessage()).isEqualTo(expected.getFormattedMessage());
273 // public void upgradeProxyInService_successful() {
274 // List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
275 // UpgradeRequest request = new UpgradeRequest(SERVICE_ID_PROXY_PREV);
276 // inputsToUpdate.add(request);
278 // when(toscaOperationFacade.getToscaElement(eq(SERVICE_ID_PROXY))).thenReturn(Either.left(serviceProxy));
280 // UpgradeStatus status = buildAutomatedUpgradeCall(SERVICE_ID_PROXY).post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), UpgradeStatus.class);
281 // assertThat(status.getStatus()).isEqualTo(ActionStatus.OK);
284 private Invocation.Builder buildAutomatedUpgradeCall(String id) {
285 return target("/v1/catalog/resources/{id}/automatedupgrade").resolveTemplate("id", id)
286 .request(MediaType.APPLICATION_JSON).header(Constants.USER_ID_HEADER, USER_ID);
289 private void prepareComponents() {
295 private void createService() {
296 servicePrev = createService("service1", SERVICE_ID_PREV, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
298 ComponentInstance ci = new ComponentInstance();
299 ci.setComponentUid(RESOURCE_ID_PREV);
300 ci.setName("inst 1");
301 List<ComponentInstance> resourceInstances = new ArrayList<>();
302 resourceInstances.add(ci);
303 servicePrev.setComponentInstances(resourceInstances);
305 serviceNew = createService("service1", SERVICE_ID_NEW, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
306 serviceNew.setComponentInstances(resourceInstances);
308 serviceNewCheckIn = createService("service1", SERVICE_ID_NEW, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
310 serviceNewCheckIn.setComponentInstances(resourceInstances);
312 istanceNew = new ComponentInstance();
313 istanceNew.setComponentUid(RESOURCE_ID_NEW);
314 istanceNew.setName("inst 1");
316 serviceProxy = createService("serviceProxy", SERVICE_ID_PROXY, LifecycleStateEnum.CERTIFIED);
317 serviceProxyContainerPrev = createService("serviceProxyContainer", SERVICE_ID_PROXY_PREV,
318 LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
319 serviceProxyContainerNew = createService("serviceProxyContainer", SERVICE_ID_PROXY_NEW,
320 LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
325 private Service createService(String name, String id, LifecycleStateEnum state) {
326 Service service = new Service();
327 service.setName(name);
328 service.setUniqueId(id);
329 service.setLifecycleState(state);
330 service.setHighestVersion(true);
334 private void createVF() {
335 vfPrev = new Resource();
336 vfPrev.setName("vf1");
337 vfPrev.setUniqueId(RESOURCE_ID_PREV);
338 vfPrev.setLifecycleState(LifecycleStateEnum.CERTIFIED);
339 vfPrev.setHighestVersion(false);
340 vfPrev.setResourceType(ResourceTypeEnum.VF);
341 vfPrev.setVersion("1.0");
342 vfPrev.setInvariantUUID(INVARIANT_ID);
343 vfPrev.setComponentType(ComponentTypeEnum.RESOURCE);
345 vfNew = new Resource();
346 vfNew.setName("vf1");
347 vfNew.setUniqueId(RESOURCE_ID_PREV);
348 vfNew.setLifecycleState(LifecycleStateEnum.CERTIFIED);
349 vfNew.setHighestVersion(true);
350 vfNew.setResourceType(ResourceTypeEnum.VF);
351 vfNew.setVersion("2.0");
352 vfNew.setInvariantUUID(INVARIANT_ID);
353 vfNew.setComponentType(ComponentTypeEnum.RESOURCE);