re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / AutomatedUpgradeEndpointTest.java
1 package org.openecomp.sdc.be.servlets;
2
3 import com.fasterxml.jackson.databind.DeserializationFeature;
4 import fj.data.Either;
5 import org.glassfish.jersey.client.ClientConfig;
6 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
7 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
8 import org.glassfish.jersey.server.ResourceConfig;
9 import org.junit.Before;
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
13 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
14 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
15 import org.openecomp.sdc.be.components.upgrade.ServiceInfo;
16 import org.openecomp.sdc.be.components.upgrade.UpgradeBusinessLogic;
17 import org.openecomp.sdc.be.components.upgrade.UpgradeRequest;
18 import org.openecomp.sdc.be.components.upgrade.UpgradeStatus;
19 import org.openecomp.sdc.be.components.validation.UserValidations;
20 import org.openecomp.sdc.be.config.ConfigurationManager;
21 import org.openecomp.sdc.be.dao.api.ActionStatus;
22 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
23 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
24 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
25 import org.openecomp.sdc.be.impl.ComponentsUtils;
26 import org.openecomp.sdc.be.model.*;
27 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
28 import org.openecomp.sdc.be.model.jsontitan.operations.UpgradeOperation;
29 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
30 import org.openecomp.sdc.common.api.ConfigurationSource;
31 import org.openecomp.sdc.common.api.Constants;
32 import org.openecomp.sdc.common.impl.ExternalConfiguration;
33 import org.openecomp.sdc.common.impl.FSConfigurationSource;
34 import org.openecomp.sdc.exception.ResponseFormat;
35 import org.openecomp.sdc.exception.ServiceException;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.Import;
38 import org.springframework.http.HttpStatus;
39
40 import javax.ws.rs.client.Entity;
41 import javax.ws.rs.client.Invocation;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
44 import java.util.ArrayList;
45 import java.util.List;
46
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.mockito.ArgumentMatchers.any;
49 import static org.mockito.ArgumentMatchers.anyBoolean;
50 import static org.mockito.ArgumentMatchers.anyString;
51 import static org.mockito.ArgumentMatchers.eq;
52 import static org.mockito.Mockito.*;
53
54 public class AutomatedUpgradeEndpointTest extends JerseySpringBaseTest {
55     static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
56     static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
57
58     private static final String RESOURCE_ID_PREV = "prevVF";
59     private static final String RESOURCE_ID_NEW = "newVF";
60     private static final String SERVICE_ID_PREV = "prevSer";
61     private static final String SERVICE_ID_NEW = "newSer";
62     private static final String SERVICE_ID_PROXY = "serProxy";
63     private static final String SERVICE_ID_PROXY_PREV = "serProxyContainerPrev";
64     private static final String SERVICE_ID_PROXY_NEW = "serProxyContainerNew";
65     private static final String INVARIANT_ID = "invariantUUID";
66     private static final String USER_ID = "userId";
67
68     private static LifecycleBusinessLogic lifecycleBusinessLogic;
69     private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
70     private static UserValidations userValidations;
71     private static ToscaOperationFacade toscaOperationFacade;
72     private static ComponentsUtils componentsUtils;
73     private static UpgradeOperation upgradeOperation;
74     private static TitanDao titanDao;
75
76     private static User user;
77     private static Resource vfPrev;
78     private static Resource vfNew;
79     private static Service servicePrev;
80     private static Service serviceNew;
81     private static Service serviceNewCheckIn;
82     private static ComponentInstance istanceNew;
83
84     private static Service serviceProxy;
85     private static Service serviceProxyContainerPrev;
86     private static Service serviceProxyContainerNew;
87
88     @org.springframework.context.annotation.Configuration
89     @Import(BaseTestConfig.class)
90     static class AutomatedUpgradeTestConfig {
91
92         @Bean
93         AutomatedUpgradeEndpoint automatedUpgradeEndpoint() {
94             return new AutomatedUpgradeEndpoint(upgradeBusinessLogic());
95         }
96
97         @Bean
98         UpgradeBusinessLogic upgradeBusinessLogic() {
99             return new UpgradeBusinessLogic(lifecycleBusinessLogic, componentInstanceBusinessLogic, userValidations, toscaOperationFacade, componentsUtils, upgradeOperation, titanDao);
100         }
101     }
102
103     @BeforeClass
104     public static void initClass() {
105         lifecycleBusinessLogic = mock(LifecycleBusinessLogic.class);
106         componentInstanceBusinessLogic = mock(ComponentInstanceBusinessLogic.class);
107         userValidations = mock(UserValidations.class);
108         toscaOperationFacade = mock(ToscaOperationFacade.class);
109         componentsUtils = mock(ComponentsUtils.class);
110         upgradeOperation = mock(UpgradeOperation.class);
111         titanDao = mock(TitanDao.class);
112         user = mock(User.class);
113     }
114
115     @Before
116     public void init() {
117         prepareComponents();
118         when(userValidations.validateUserExists(eq(USER_ID), anyString(), anyBoolean())).thenReturn(user);
119         when(toscaOperationFacade.getToscaFullElement(eq(RESOURCE_ID_PREV))).thenReturn(Either.left(vfPrev));
120         when(toscaOperationFacade.getToscaFullElement(eq(RESOURCE_ID_NEW))).thenReturn(Either.left(vfNew));
121         when(toscaOperationFacade.getToscaFullElement(eq(SERVICE_ID_PREV))).thenReturn(Either.left(servicePrev));
122         when(toscaOperationFacade.getToscaFullElement(eq(SERVICE_ID_NEW))).thenReturn(Either.left(serviceNew));
123
124         Either<Service, ResponseFormat> fromLifeCycle = Either.left(serviceNew);
125         doReturn(fromLifeCycle).when(lifecycleBusinessLogic).changeComponentState(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID_PREV), any(User.class), eq(LifeCycleTransitionEnum.CHECKOUT), any(LifecycleChangeInfoWithAction.class), eq(false),
126                 eq(true));
127
128         when(toscaOperationFacade.getToscaElement(eq(RESOURCE_ID_PREV), any(ComponentParametersView.class))).thenReturn(Either.left(vfPrev));
129         when(componentInstanceBusinessLogic.changeInstanceVersion(any(Service.class), any(ComponentInstance.class), any(ComponentInstance.class), any(User.class), eq(ComponentTypeEnum.SERVICE))).thenReturn(Either.left(istanceNew));
130
131         doReturn(Either.left(serviceNewCheckIn)).when(lifecycleBusinessLogic).changeComponentState(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID_NEW), any(User.class), eq(LifeCycleTransitionEnum.CHECKIN), any(LifecycleChangeInfoWithAction.class),
132                 eq(false), eq(true));
133
134     }
135
136     @Override
137     protected void configureClient(ClientConfig config) {
138         final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
139         config.register(jacksonJsonProvider);
140     }
141
142     @Override
143     protected ResourceConfig configure() {
144         return super.configure(AutomatedUpgradeTestConfig.class).register(AutomatedUpgradeEndpoint.class);
145     }
146
147     @Test
148     public void upgradeVfInService_success() {
149         List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
150         UpgradeRequest request = new UpgradeRequest(SERVICE_ID_PREV);
151         inputsToUpdate.add(request);
152
153         Invocation.Builder builder = buildAutomatedUpgradeCall(RESOURCE_ID_NEW);
154         UpgradeStatus status = builder.post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), UpgradeStatus.class);
155
156         assertThat(status.getStatus()).isEqualTo(ActionStatus.OK);
157         List<ServiceInfo> expected = new ArrayList<>();
158         ServiceInfo serviceInfo = new ServiceInfo(serviceNewCheckIn.getUniqueId(), ActionStatus.OK);
159         serviceInfo.setName(serviceNewCheckIn.getName());
160         serviceInfo.setVersion(serviceNewCheckIn.getVersion());
161         expected.add(serviceInfo);
162         assertThat(status.getComponentToUpgradeStatus()).hasSameSizeAs(expected);
163         assertThat(status.getComponentToUpgradeStatus()).hasSameElementsAs(expected);
164
165     }
166
167     @Test
168     public void upgradeVfInService_IdNotExist() {
169         List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
170         UpgradeRequest request = new UpgradeRequest(SERVICE_ID_PREV);
171         inputsToUpdate.add(request);
172         String wrongId = "1234";
173         when(toscaOperationFacade.getToscaFullElement(eq(wrongId))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
174         when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
175         String[] variables = { wrongId };
176         ServiceException serviceException = new ServiceException("SVC4063", "Error: Requested '%1' resource was not found.", variables);
177         ResponseFormat expected = new ResponseFormat(HttpStatus.NOT_FOUND.value());
178         expected.setServiceException(serviceException);
179         when(componentsUtils.getResponseFormatByResource(eq(ActionStatus.RESOURCE_NOT_FOUND), eq(wrongId))).thenReturn(expected);
180
181         Response response = buildAutomatedUpgradeCall(wrongId).post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), Response.class);
182         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
183
184         ResponseFormat actual = response.readEntity(ResponseFormat.class);
185         assertThat(actual.getMessageId()).isEqualTo(expected.getMessageId());
186         assertThat(actual.getFormattedMessage()).isEqualTo(expected.getFormattedMessage());
187
188     }
189
190     @Test
191     public void upgradeVfInService_NotHihgestCertified() {
192         List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
193         UpgradeRequest request = new UpgradeRequest(RESOURCE_ID_PREV);
194         inputsToUpdate.add(request);
195
196         when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
197         String[] variables = { vfPrev.getName() };
198         ServiceException serviceException = new ServiceException("SVC4699", "Error: Component %1 is not highest certified", variables);
199         ResponseFormat expected = new ResponseFormat(HttpStatus.BAD_REQUEST.value());
200         expected.setServiceException(serviceException);
201         when(componentsUtils.getResponseFormat(eq(ActionStatus.COMPONENT_IS_NOT_HIHGEST_CERTIFIED), eq(vfPrev.getName()))).thenReturn(expected);
202
203         Response response = buildAutomatedUpgradeCall(RESOURCE_ID_PREV).post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), Response.class);
204         assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
205
206         ResponseFormat actual = response.readEntity(ResponseFormat.class);
207         assertThat(actual.getMessageId()).isEqualTo(expected.getMessageId());
208         assertThat(actual.getFormattedMessage()).isEqualTo(expected.getFormattedMessage());
209     }
210
211 //    @Test
212 //    public void upgradeProxyInService_successful() {
213 //        List<UpgradeRequest> inputsToUpdate = new ArrayList<>();
214 //        UpgradeRequest request = new UpgradeRequest(SERVICE_ID_PROXY_PREV);
215 //        inputsToUpdate.add(request);
216 //
217 //        when(toscaOperationFacade.getToscaElement(eq(SERVICE_ID_PROXY))).thenReturn(Either.left(serviceProxy));
218 //        
219 //        UpgradeStatus status = buildAutomatedUpgradeCall(SERVICE_ID_PROXY).post(Entity.entity(inputsToUpdate, MediaType.APPLICATION_JSON), UpgradeStatus.class);
220 //        assertThat(status.getStatus()).isEqualTo(ActionStatus.OK);
221 //    }
222
223     private Invocation.Builder buildAutomatedUpgradeCall(String id) {
224         return target("/v1/catalog/resources/{id}/automatedupgrade").resolveTemplate("id", id).request(MediaType.APPLICATION_JSON).header(Constants.USER_ID_HEADER, USER_ID);
225     }
226
227     private void prepareComponents() {
228         createVF();
229
230         createService();
231     }
232
233     private void createService() {
234         servicePrev = createService("service1", SERVICE_ID_PREV, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
235         
236         ComponentInstance ci = new ComponentInstance();
237         ci.setComponentUid(RESOURCE_ID_PREV);
238         ci.setName("inst 1");
239         List<ComponentInstance> resourceInstances = new ArrayList<>();
240         resourceInstances.add(ci);
241         servicePrev.setComponentInstances(resourceInstances);
242
243         serviceNew = createService("service1", SERVICE_ID_NEW, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
244         serviceNew.setComponentInstances(resourceInstances);
245
246         serviceNewCheckIn = createService("service1", SERVICE_ID_NEW, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
247         
248         serviceNewCheckIn.setComponentInstances(resourceInstances);
249
250         istanceNew = new ComponentInstance();
251         istanceNew.setComponentUid(RESOURCE_ID_NEW);
252         istanceNew.setName("inst 1");
253
254         serviceProxy = createService("serviceProxy", SERVICE_ID_PROXY, LifecycleStateEnum.CERTIFIED);
255         serviceProxyContainerPrev = createService("serviceProxyContainer", SERVICE_ID_PROXY_PREV, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
256         serviceProxyContainerNew = createService("serviceProxyContainer", SERVICE_ID_PROXY_NEW, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
257        
258         
259
260     }
261
262     private Service createService(String name, String id, LifecycleStateEnum state){
263         Service service = new Service();
264         service.setName(name);
265         service.setUniqueId(id);
266         service.setLifecycleState(state);
267         service.setHighestVersion(true);
268         return service;
269     }
270     private void createVF() {
271         vfPrev = new Resource();
272         vfPrev.setName("vf1");
273         vfPrev.setUniqueId(RESOURCE_ID_PREV);
274         vfPrev.setLifecycleState(LifecycleStateEnum.CERTIFIED);
275         vfPrev.setHighestVersion(false);
276         vfPrev.setResourceType(ResourceTypeEnum.VF);
277         vfPrev.setVersion("1.0");
278         vfPrev.setInvariantUUID(INVARIANT_ID);
279         vfPrev.setComponentType(ComponentTypeEnum.RESOURCE);
280
281         vfNew = new Resource();
282         vfNew.setName("vf1");
283         vfNew.setUniqueId(RESOURCE_ID_PREV);
284         vfNew.setLifecycleState(LifecycleStateEnum.CERTIFIED);
285         vfNew.setHighestVersion(true);
286         vfNew.setResourceType(ResourceTypeEnum.VF);
287         vfNew.setVersion("2.0");
288         vfNew.setInvariantUUID(INVARIANT_ID);
289         vfNew.setComponentType(ComponentTypeEnum.RESOURCE);
290
291     }
292 }