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 com.fasterxml.jackson.databind.DeserializationFeature;
24 import fj.data.Either;
25 import org.glassfish.jersey.client.ClientConfig;
26 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
27 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
28 import org.glassfish.jersey.server.ResourceConfig;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
33 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
34 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
35 import org.openecomp.sdc.be.components.upgrade.ServiceInfo;
36 import org.openecomp.sdc.be.components.upgrade.UpgradeBusinessLogic;
37 import org.openecomp.sdc.be.components.upgrade.UpgradeRequest;
38 import org.openecomp.sdc.be.components.upgrade.UpgradeStatus;
39 import org.openecomp.sdc.be.components.validation.UserValidations;
40 import org.openecomp.sdc.be.config.ConfigurationManager;
41 import org.openecomp.sdc.be.dao.api.ActionStatus;
42 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.model.ComponentInstance;
47 import org.openecomp.sdc.be.model.ComponentParametersView;
48 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
49 import org.openecomp.sdc.be.model.LifecycleStateEnum;
50 import org.openecomp.sdc.be.model.Resource;
51 import org.openecomp.sdc.be.model.Service;
52 import org.openecomp.sdc.be.model.User;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
54 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.UpgradeOperation;
55 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
56 import org.openecomp.sdc.be.user.UserBusinessLogic;
57 import org.openecomp.sdc.common.api.ConfigurationSource;
58 import org.openecomp.sdc.common.api.Constants;
59 import org.openecomp.sdc.common.impl.ExternalConfiguration;
60 import org.openecomp.sdc.common.impl.FSConfigurationSource;
61 import org.openecomp.sdc.exception.ResponseFormat;
62 import org.openecomp.sdc.exception.ServiceException;
63 import org.springframework.context.annotation.Bean;
64 import org.springframework.context.annotation.Import;
65 import org.springframework.http.HttpStatus;
67 import javax.ws.rs.client.Entity;
68 import javax.ws.rs.client.Invocation;
69 import javax.ws.rs.core.MediaType;
70 import javax.ws.rs.core.Response;
71 import java.util.ArrayList;
72 import java.util.List;
74 import static org.assertj.core.api.Assertions.assertThat;
75 import static org.mockito.ArgumentMatchers.any;
76 import static org.mockito.ArgumentMatchers.eq;
77 import static org.mockito.Mockito.doReturn;
78 import static org.mockito.Mockito.mock;
79 import static org.mockito.Mockito.when;
81 public class AutomatedUpgradeEndpointTest extends JerseySpringBaseTest {
82 static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
83 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
85 private static final String RESOURCE_ID_PREV = "prevVF";
86 private static final String RESOURCE_ID_NEW = "newVF";
87 private static final String SERVICE_ID_PREV = "prevSer";
88 private static final String SERVICE_ID_NEW = "newSer";
89 private static final String SERVICE_ID_PROXY = "serProxy";
90 private static final String SERVICE_ID_PROXY_PREV = "serProxyContainerPrev";
91 private static final String SERVICE_ID_PROXY_NEW = "serProxyContainerNew";
92 private static final String INVARIANT_ID = "invariantUUID";
93 private static final String USER_ID = "userId";
95 private static LifecycleBusinessLogic lifecycleBusinessLogic;
96 private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
97 private static UserValidations userValidations;
98 private static ToscaOperationFacade toscaOperationFacade;
99 private static ComponentsUtils componentsUtils;
100 private static UpgradeOperation upgradeOperation;
101 private static JanusGraphDao janusGraphDao;
103 private static User user;
104 private static Resource vfPrev;
105 private static Resource vfNew;
106 private static Service servicePrev;
107 private static Service serviceNew;
108 private static Service serviceNewCheckIn;
109 private static ComponentInstance istanceNew;
111 private static Service serviceProxy;
112 private static Service serviceProxyContainerPrev;
113 private static Service serviceProxyContainerNew;
115 @org.springframework.context.annotation.Configuration
116 @Import(BaseTestConfig.class)
117 static class AutomatedUpgradeTestConfig {
120 AutomatedUpgradeEndpoint automatedUpgradeEndpoint() {
122 UserBusinessLogic userBusinessLogic = mock(UserBusinessLogic.class);
123 ComponentsUtils componentsUtils = mock(ComponentsUtils.class);
125 return new AutomatedUpgradeEndpoint(userBusinessLogic, componentsUtils,
126 upgradeBusinessLogic());
130 UpgradeBusinessLogic upgradeBusinessLogic() {
131 return new UpgradeBusinessLogic(lifecycleBusinessLogic, componentInstanceBusinessLogic, userValidations, toscaOperationFacade, componentsUtils, upgradeOperation,
137 public static void initClass() {
138 lifecycleBusinessLogic = mock(LifecycleBusinessLogic.class);
139 componentInstanceBusinessLogic = mock(ComponentInstanceBusinessLogic.class);
140 userValidations = mock(UserValidations.class);
141 toscaOperationFacade = mock(ToscaOperationFacade.class);
142 componentsUtils = mock(ComponentsUtils.class);
143 upgradeOperation = mock(UpgradeOperation.class);
144 janusGraphDao = mock(JanusGraphDao.class);
145 user = mock(User.class);
151 when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
152 when(toscaOperationFacade.getToscaFullElement(eq(RESOURCE_ID_PREV))).thenReturn(Either.left(vfPrev));
153 when(toscaOperationFacade.getToscaFullElement(eq(RESOURCE_ID_NEW))).thenReturn(Either.left(vfNew));
154 when(toscaOperationFacade.getToscaFullElement(eq(SERVICE_ID_PREV))).thenReturn(Either.left(servicePrev));
155 when(toscaOperationFacade.getToscaFullElement(eq(SERVICE_ID_NEW))).thenReturn(Either.left(serviceNew));
157 Either<Service, ResponseFormat> fromLifeCycle = Either.left(serviceNew);
158 doReturn(fromLifeCycle).when(lifecycleBusinessLogic).changeComponentState(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID_PREV), any(User.class), eq(LifeCycleTransitionEnum.CHECKOUT), any(LifecycleChangeInfoWithAction.class), eq(false),
161 when(toscaOperationFacade.getToscaElement(eq(RESOURCE_ID_PREV), any(ComponentParametersView.class))).thenReturn(Either.left(vfPrev));
162 when(componentInstanceBusinessLogic.changeInstanceVersion(any(Service.class), any(ComponentInstance.class), any(ComponentInstance.class), any(User.class), eq(ComponentTypeEnum.SERVICE))).thenReturn(istanceNew);
164 doReturn(Either.left(serviceNewCheckIn)).when(lifecycleBusinessLogic).changeComponentState(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID_NEW), any(User.class), eq(LifeCycleTransitionEnum.CHECKIN), any(LifecycleChangeInfoWithAction.class),
165 eq(false), eq(true));
170 protected void configureClient(ClientConfig config) {
171 final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
172 config.register(jacksonJsonProvider);
176 protected ResourceConfig configure() {
177 return super.configure(AutomatedUpgradeTestConfig.class).register(AutomatedUpgradeEndpoint.class);
181 public void upgradeVfInService_success() {
182 List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
183 UpgradeRequest request = new UpgradeRequest(SERVICE_ID_PREV);
184 inputsToUpdate.add(request);
186 Invocation.Builder builder = buildAutomatedUpgradeCall(RESOURCE_ID_NEW);
187 UpgradeStatus status = builder.post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), UpgradeStatus.class);
189 assertThat(status.getStatus()).isEqualTo(ActionStatus.OK);
190 List<ServiceInfo> expected = new ArrayList<>();
191 ServiceInfo serviceInfo = new ServiceInfo(serviceNewCheckIn.getUniqueId(), ActionStatus.OK);
192 serviceInfo.setName(serviceNewCheckIn.getName());
193 serviceInfo.setVersion(serviceNewCheckIn.getVersion());
194 expected.add(serviceInfo);
195 assertThat(status.getComponentToUpgradeStatus()).hasSameSizeAs(expected);
196 assertThat(status.getComponentToUpgradeStatus()).hasSameElementsAs(expected);
201 public void upgradeVfInService_IdNotExist() {
202 List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
203 UpgradeRequest request = new UpgradeRequest(SERVICE_ID_PREV);
204 inputsToUpdate.add(request);
205 String wrongId = "1234";
206 when(toscaOperationFacade.getToscaFullElement(eq(wrongId))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
207 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
208 String[] variables = { wrongId };
209 ServiceException serviceException = new ServiceException("SVC4063", "Error: Requested '%1' resource was not found.", variables);
210 ResponseFormat expected = new ResponseFormat(HttpStatus.NOT_FOUND.value());
211 expected.setServiceException(serviceException);
212 when(componentsUtils.getResponseFormatByResource(eq(ActionStatus.RESOURCE_NOT_FOUND), eq(wrongId))).thenReturn(expected);
214 Response response = buildAutomatedUpgradeCall(wrongId).post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), Response.class);
215 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
217 ResponseFormat actual = response.readEntity(ResponseFormat.class);
218 assertThat(actual.getMessageId()).isEqualTo(expected.getMessageId());
219 assertThat(actual.getFormattedMessage()).isEqualTo(expected.getFormattedMessage());
224 public void upgradeVfInService_NotHihgestCertified() {
225 List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
226 UpgradeRequest request = new UpgradeRequest(RESOURCE_ID_PREV);
227 inputsToUpdate.add(request);
229 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
230 String[] variables = { vfPrev.getName() };
231 ServiceException serviceException = new ServiceException("SVC4699", "Error: Component %1 is not highest certified", variables);
232 ResponseFormat expected = new ResponseFormat(HttpStatus.BAD_REQUEST.value());
233 expected.setServiceException(serviceException);
234 when(componentsUtils.getResponseFormat(eq(ActionStatus.COMPONENT_IS_NOT_HIHGEST_CERTIFIED), eq(vfPrev.getName()))).thenReturn(expected);
236 Response response = buildAutomatedUpgradeCall(RESOURCE_ID_PREV).post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), Response.class);
237 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
239 ResponseFormat actual = response.readEntity(ResponseFormat.class);
240 assertThat(actual.getMessageId()).isEqualTo(expected.getMessageId());
241 assertThat(actual.getFormattedMessage()).isEqualTo(expected.getFormattedMessage());
245 // public void upgradeProxyInService_successful() {
246 // List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
247 // UpgradeRequest request = new UpgradeRequest(SERVICE_ID_PROXY_PREV);
248 // inputsToUpdate.add(request);
250 // when(toscaOperationFacade.getToscaElement(eq(SERVICE_ID_PROXY))).thenReturn(Either.left(serviceProxy));
252 // UpgradeStatus status = buildAutomatedUpgradeCall(SERVICE_ID_PROXY).post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), UpgradeStatus.class);
253 // assertThat(status.getStatus()).isEqualTo(ActionStatus.OK);
256 private Invocation.Builder buildAutomatedUpgradeCall(String id) {
257 return target("/v1/catalog/resources/{id}/automatedupgrade").resolveTemplate("id", id).request(MediaType.APPLICATION_JSON).header(Constants.USER_ID_HEADER, USER_ID);
260 private void prepareComponents() {
266 private void createService() {
267 servicePrev = createService("service1", SERVICE_ID_PREV, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
269 ComponentInstance ci = new ComponentInstance();
270 ci.setComponentUid(RESOURCE_ID_PREV);
271 ci.setName("inst 1");
272 List<ComponentInstance> resourceInstances = new ArrayList<>();
273 resourceInstances.add(ci);
274 servicePrev.setComponentInstances(resourceInstances);
276 serviceNew = createService("service1", SERVICE_ID_NEW, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
277 serviceNew.setComponentInstances(resourceInstances);
279 serviceNewCheckIn = createService("service1", SERVICE_ID_NEW, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
281 serviceNewCheckIn.setComponentInstances(resourceInstances);
283 istanceNew = new ComponentInstance();
284 istanceNew.setComponentUid(RESOURCE_ID_NEW);
285 istanceNew.setName("inst 1");
287 serviceProxy = createService("serviceProxy", SERVICE_ID_PROXY, LifecycleStateEnum.CERTIFIED);
288 serviceProxyContainerPrev = createService("serviceProxyContainer", SERVICE_ID_PROXY_PREV, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
289 serviceProxyContainerNew = createService("serviceProxyContainer", SERVICE_ID_PROXY_NEW, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
295 private Service createService(String name, String id, LifecycleStateEnum state){
296 Service service = new Service();
297 service.setName(name);
298 service.setUniqueId(id);
299 service.setLifecycleState(state);
300 service.setHighestVersion(true);
303 private void createVF() {
304 vfPrev = new Resource();
305 vfPrev.setName("vf1");
306 vfPrev.setUniqueId(RESOURCE_ID_PREV);
307 vfPrev.setLifecycleState(LifecycleStateEnum.CERTIFIED);
308 vfPrev.setHighestVersion(false);
309 vfPrev.setResourceType(ResourceTypeEnum.VF);
310 vfPrev.setVersion("1.0");
311 vfPrev.setInvariantUUID(INVARIANT_ID);
312 vfPrev.setComponentType(ComponentTypeEnum.RESOURCE);
314 vfNew = new Resource();
315 vfNew.setName("vf1");
316 vfNew.setUniqueId(RESOURCE_ID_PREV);
317 vfNew.setLifecycleState(LifecycleStateEnum.CERTIFIED);
318 vfNew.setHighestVersion(true);
319 vfNew.setResourceType(ResourceTypeEnum.VF);
320 vfNew.setVersion("2.0");
321 vfNew.setInvariantUUID(INVARIANT_ID);
322 vfNew.setComponentType(ComponentTypeEnum.RESOURCE);