Upgrade SDC from Titan to Janus Graph
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / migration / tasks / mig1710 / UpgradeMigration1710Test.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.asdctool.migration.tasks.mig1710;
23
24 import com.google.common.collect.Lists;
25 import fj.data.Either;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
35 import org.openecomp.sdc.asdctool.migration.tasks.handlers.XlsOutputHandler;
36 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
39 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
40 import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic;
41 import org.openecomp.sdc.be.config.Configuration;
42 import org.openecomp.sdc.be.config.ConfigurationManager;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
45 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
46 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
47 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
48 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
49 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
50 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
51 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
52 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
53 import org.openecomp.sdc.be.impl.ComponentsUtils;
54 import org.openecomp.sdc.be.model.*;
55 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
56 import org.openecomp.sdc.be.model.operations.api.IUserAdminOperation;
57 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
58 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
59 import org.openecomp.sdc.common.api.ConfigurationSource;
60 import org.openecomp.sdc.common.http.client.api.HttpRequestHandler;
61 import org.openecomp.sdc.exception.ResponseFormat;
62
63 import java.util.ArrayList;
64 import java.util.HashMap;
65 import java.util.List;
66 import java.util.Map;
67 import java.util.stream.Collectors;
68 import java.util.stream.Stream;
69
70 import static org.junit.Assert.assertEquals;
71 import static org.mockito.ArgumentMatchers.any;
72 import static org.mockito.ArgumentMatchers.anyString;
73 import static org.mockito.ArgumentMatchers.eq;
74 import static org.mockito.Mockito.*;
75
76 @RunWith(MockitoJUnitRunner.class)
77 public class UpgradeMigration1710Test {
78
79     private static final String USER = "jh0003";
80     private static final String CONF_LEVEL = "5.0";
81     private static final String COMPONENT_UNIQUE_ID = "12345";
82     private static final String OLD_VERSION = "1.0";
83     private static final String UPDATED_VERSION = "2.0";
84     private static final String CSAR_UUID = "1234578";
85     private static HttpRequestHandler originHandler;
86
87     private final User user = new User();
88
89     @InjectMocks
90     private UpgradeMigration1710 migration = new UpgradeMigration1710();
91     @Mock
92     private IUserAdminOperation userAdminOperation;
93     @Mock
94     private ToscaOperationFacade toscaOperationFacade;
95     @Mock
96     private LifecycleBusinessLogic lifecycleBusinessLogic;
97     @Mock
98     private JanusGraphDao janusGraphDao;
99     @Mock
100     private ComponentsUtils componentUtils;
101     @Mock
102     private CsarOperation csarOperation;
103     @Mock
104     private ConfigurationSource configurationSource;
105     //don't remove - it is intended to avoid the xls file generating
106     @Mock
107     private XlsOutputHandler outputHandler;
108     @Mock
109     private ResourceBusinessLogic resourceBusinessLogic;
110     @Mock
111     private ServiceBusinessLogic serviceBusinessLogic;
112     @Mock
113     private ResponseFormat responseFormat;
114     @Mock
115     private ComponentsCleanBusinessLogic componentsCleanBusinessLogic;
116
117     private static ConfigurationManager configurationManager;
118     private static List<String> resources = Stream.of("org.openecomp.resource.cp.extCP").collect(Collectors.toList());
119     private static Map<String, List<String>> resourcesForUpgrade;
120
121     private Resource resource;
122     private Service service;
123     private List<String> vfList = new ArrayList<>();
124
125     @BeforeClass
126     public static void setUpClass() {
127         resourcesForUpgrade = new HashMap<>();
128         resourcesForUpgrade.put(CONF_LEVEL, resources);
129         originHandler = HttpRequestHandler.get();
130     }
131
132     @AfterClass
133     public static void tearDownClass() {
134         //put the origin handler back
135         HttpRequestHandler.setTestInstance(originHandler);
136     }
137
138     @Before
139     public void setUp() {
140         user.setUserId(USER);
141         configurationManager = new ConfigurationManager(configurationSource);
142         configurationManager.setConfiguration(new Configuration());
143         configurationManager.getConfiguration().setSkipUpgradeVSPs(true);
144         configurationManager.getConfiguration().setSkipUpgradeFailedVfs(true);
145         configurationManager.getConfiguration().setAutoHealingOwner(USER);
146         configurationManager.getConfiguration().setSupportAllottedResourcesAndProxy(true);
147         configurationManager.getConfiguration().setDeleteLockTimeoutInSeconds(10);
148         configurationManager.getConfiguration().setMaxDeleteComponents(5);
149         configurationManager.getConfiguration().setEnableAutoHealing(true);
150         configurationManager.getConfiguration().setToscaConformanceLevel("5.0");
151         HashMap<String, List<String>> resourcesForUpgrade = new HashMap();
152         resourcesForUpgrade.put("5.0", Lists.newArrayList("port"));
153         configurationManager.getConfiguration().setResourcesForUpgrade(resourcesForUpgrade);
154
155         migration.init();
156         migration.setNodeTypesSupportOnly(false);
157         when(componentsCleanBusinessLogic.lockDeleteOperation()).thenReturn(StorageOperationStatus.OK);
158
159         resource = new Resource();
160         resource.setCsarUUID(CSAR_UUID);
161         resource.setVersion(OLD_VERSION);
162         resource.setUniqueId(COMPONENT_UNIQUE_ID);
163
164         service = new Service();
165         service.setVersion(OLD_VERSION);
166         service.setUniqueId(COMPONENT_UNIQUE_ID);
167
168         vfList.add(COMPONENT_UNIQUE_ID);
169
170         when(responseFormat.getFormattedMessage())
171                 .thenReturn("");
172         when(componentUtils.getResponseFormat(any(ActionStatus.class), any()))
173                 .thenReturn(responseFormat);
174         when(componentUtils.convertFromStorageResponse(any(), any())).thenCallRealMethod();
175         mockChangeComponentState();
176     }
177
178     @Test
179     public void nodeTypesUpgradeFailed() {
180         migration.setNodeTypesSupportOnly(true);
181         resolveUserAndDefineUpgradeLevel();
182         when(janusGraphDao.getByCriteria(any(), any(), any(), any()))
183                 .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
184         assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus());
185     }
186
187     @Test
188     public void migrationDisabled() {
189         configurationManager.getConfiguration().setEnableAutoHealing(false);
190         migration.init();
191         assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus());
192         verify(janusGraphDao, times(0)).commit();
193         verify(janusGraphDao, times(0)).rollback();
194     }
195
196     @Test
197     public void migrationFailedIfDeleteNodeLockFailed() {
198         when(componentsCleanBusinessLogic.lockDeleteOperation())
199                 .thenReturn(StorageOperationStatus.BAD_REQUEST);
200         assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus());
201     }
202
203     @Test
204     public void migrationFailedIfDeleteNodeLockRetryFailed() {
205         when(componentsCleanBusinessLogic.lockDeleteOperation())
206                 .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT)
207                 .thenReturn(StorageOperationStatus.BAD_REQUEST);
208         assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus());
209     }
210
211     @Test
212     public void nodeTypesOnlyUpgradePassed() {
213         migration.setNodeTypesSupportOnly(true);
214         upgradeAllScenario(false);
215         assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus());
216         verify(janusGraphDao, times(2)).commit();
217         verify(janusGraphDao, times(0)).rollback();
218     }
219
220     @Test
221     public void nodeTypesUpgradePassedAndVFsUpgradeFailedWhenSkipFailedVFsIsNotSupported() {
222         final boolean failOnVfUpgrade = true;
223         final boolean upgradeServices = false;
224         final boolean exceptionOnVfUpgrade = false;
225         final boolean upgradeVFC = false;
226         final boolean isFailed = true;
227         configurationManager.getConfiguration().setSkipUpgradeFailedVfs(false);
228         migration.init();
229         migration.setNodeTypesSupportOnly(false);
230         resolveUserAndDefineUpgradeLevel();
231         upgradeRules(failOnVfUpgrade, exceptionOnVfUpgrade, upgradeServices, upgradeVFC, isFailed);
232         assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus());
233         verify(janusGraphDao, times(1)).commit();
234         verify(janusGraphDao, times(2)).rollback();
235     }
236
237
238     @Test
239     public void upgradeAllVFsUpgradeFailedOnExceptionWhenSkipFailedVFsIsNotSupported() {
240         final boolean failOnVfUpgrade = false;
241         final boolean upgradeServices = false;
242         final boolean exceptionOnVfUpgrade = true;
243         final boolean upgradeVFC = false;
244         final boolean isFailed = true;
245         configurationManager.getConfiguration().setSkipUpgradeFailedVfs(false);
246         resolveUserAndDefineUpgradeLevel();
247         upgradeRules(failOnVfUpgrade, exceptionOnVfUpgrade, upgradeServices, upgradeVFC, isFailed);
248         migration.init();
249         assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus());
250         verify(janusGraphDao, times(2)).commit();
251         verify(janusGraphDao, times(0)).rollback();
252     }
253
254     @Test
255     public void upgradeAllIfVFsUpgradeFailedOnExceptionWhenSkipFailedVFsIsSupported() {
256         final boolean failOnVfUpgrade = false;
257         final boolean upgradeServices = true;
258         final boolean exceptionOnFvUpgrade = true;
259         final boolean upgradeVFC = false;
260         final boolean isFailed = false;
261         configurationManager.getConfiguration().setSkipUpgradeFailedVfs(true);
262         resolveUserAndDefineUpgradeLevel();
263         upgradeRules(failOnVfUpgrade, exceptionOnFvUpgrade, upgradeServices, upgradeVFC, isFailed);
264         assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus());
265         verify(janusGraphDao, times(3)).commit();
266         verify(janusGraphDao, times(1)).rollback();
267     }
268
269
270     @Test
271     public void upgradeAll() {
272         upgradeAllScenario(true);
273         assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus());
274         verify(janusGraphDao, times(4)).commit();
275         verify(janusGraphDao, times(0)).rollback();
276     }
277
278     @Test
279     public void upgradeAllWhenDeleteLockRetrySucceeded() {
280         when(componentsCleanBusinessLogic.lockDeleteOperation())
281                 .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT)
282                 .thenReturn(StorageOperationStatus.OK);
283         upgradeAllScenario(true);
284         assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus());
285         verify(janusGraphDao, times(4)).commit();
286         verify(janusGraphDao, times(0)).rollback();
287     }
288
289     @Test
290     public void upgradeAllWhenVspUpgradeIsRequired() {
291         final boolean failOnVfUpgrade = false;
292         final boolean upgradeServices = true;
293         final boolean exceptionOnFvUpgrade = false;
294         final boolean upgradeVFC = true;
295         final boolean isFailed = true;
296         resolveUserAndDefineUpgradeLevel();
297         upgradeRules(failOnVfUpgrade, exceptionOnFvUpgrade, upgradeServices, upgradeVFC, isFailed);
298         configurationManager.getConfiguration().setSkipUpgradeVSPs(false);
299         assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus());
300     }
301
302     @Test
303     public void migrationFailedWhenUserNotResolved() {
304         when(userAdminOperation.getUserData(anyString(), eq(false))).thenReturn(Either.right(ActionStatus.MISSING_INFORMATION));
305         when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
306         assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus());
307     }
308
309     @Test
310     public void verifyThatCheckedOutResourcesMarkedAsDeletedIfUpgradeFailed() {
311         mockCheckoutFlow();
312         when(resourceBusinessLogic.validateAndUpdateResourceFromCsar(any(Resource.class), any(), any(), any(),
313                 any()))
314                 .thenThrow(new ByResponseFormatComponentException(responseFormat));
315         when(resourceBusinessLogic.deleteResource(anyString(), any()))
316                 .thenReturn(responseFormat);
317         mockChangeComponentState();
318         migration.upgradeVFs(vfList, false);
319         verify(resourceBusinessLogic).deleteResource(anyString(), any());
320     }
321
322     @Test
323     public void verifyThatCheckedOutAllottedResourcesMarkedAsDeletedIfUpgradeFailed() {
324         mockCheckoutFlow();
325         when(resourceBusinessLogic.validateAndUpdateResourceFromCsar(any(Resource.class), any(), any(), any(),
326                 any()))
327                 .thenThrow(new ByResponseFormatComponentException(responseFormat));
328         when(resourceBusinessLogic.deleteResource(anyString(), any()))
329                 .thenReturn(responseFormat);
330         mockChangeComponentState();
331         migration.upgradeVFs(vfList, true);
332         verify(resourceBusinessLogic).deleteResource(anyString(), any());
333     }
334
335     @Test
336     public void verifyThatCheckedOutResourceIsNotMarkedAsDeletedIfUpgradeSucceeded() {
337         mockCheckoutFlow();
338         resource.setVersion(UPDATED_VERSION);
339         when(resourceBusinessLogic.validateAndUpdateResourceFromCsar(any(Resource.class), any(), any(), any(),
340                 any()))
341                 .thenReturn(resource);
342         mockChangeComponentState();
343         migration.upgradeVFs(vfList, true);
344         verify(resourceBusinessLogic, times(0)).deleteResource(anyString(), any());
345     }
346
347     @Test
348     public void verifyThatCheckedOutServicesMarkedAsDeletedIfUpgradeFailed() {
349         List<String> servicesForUpgrade = new ArrayList<>();
350         servicesForUpgrade.add(COMPONENT_UNIQUE_ID);
351
352         Either<Resource, StorageOperationStatus> foundServices = Either.left(resource);
353         mockCheckoutFlow();
354         when(toscaOperationFacade.getToscaElement(any(), any(ComponentParametersView.class)))
355                 .thenReturn(Either.left(service));
356         when(toscaOperationFacade.getLatestCertifiedByToscaResourceName(any(), any(), any()))
357                 .thenReturn(foundServices);
358         migration.upgradeServices(servicesForUpgrade, component -> true, "services");
359         verify(serviceBusinessLogic, times(0)).deleteService(anyString(), any());
360     }
361
362     @Test
363     public void verifyThatCheckedOutServicesIsNotMarkedAsDeletedIfUpgradeSucceeded() {
364         List<String> servicesForUpgrade = new ArrayList<>();
365         servicesForUpgrade.add(COMPONENT_UNIQUE_ID);
366
367         mockCheckoutFlow();
368         when(toscaOperationFacade.getLatestCertifiedByToscaResourceName(anyString(), any(VertexTypeEnum.class), any(JsonParseFlagEnum.class)))
369                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
370         when(serviceBusinessLogic.deleteService(anyString(), any()))
371                 .thenReturn(responseFormat);
372         migration.upgradeServices(servicesForUpgrade, component -> true, "services");
373         verify(serviceBusinessLogic).deleteService(anyString(), any());
374     }
375
376
377     @Test
378     public void unlockDeleteOperationIsPerformedIfItWasLocked() {
379         migration.isLockDeleteOperationSucceeded();
380         migration.unlockDeleteOperation();
381         verify(componentsCleanBusinessLogic).unlockDeleteOperation();
382     }
383
384     @Test
385     public void unlockDeleteOperationIsNotPerformedIfItWasNotLocked() {
386         when(componentsCleanBusinessLogic.lockDeleteOperation()).thenReturn(StorageOperationStatus.GENERAL_ERROR);
387         migration.isLockDeleteOperationSucceeded();
388         migration.unlockDeleteOperation();
389         verify(componentsCleanBusinessLogic, times(0)).unlockDeleteOperation();
390     }
391
392     @Test
393     public void deleteLockSucceededAfterRetry() {
394         when(componentsCleanBusinessLogic.lockDeleteOperation())
395                 .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT)
396                 .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT)
397                 .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT)
398                 .thenReturn(StorageOperationStatus.OK);
399         migration.isLockDeleteOperationSucceeded();
400         migration.unlockDeleteOperation();
401         verify(componentsCleanBusinessLogic).unlockDeleteOperation();
402     }
403
404     @Test
405     public void deleteLockFailedAfterRetry() {
406         when(componentsCleanBusinessLogic.lockDeleteOperation())
407                 .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT);
408         migration.isLockDeleteOperationSucceeded();
409         migration.unlockDeleteOperation();
410         verify(componentsCleanBusinessLogic, times(0)).unlockDeleteOperation();
411     }
412
413     @Test
414     public void deleteMarkedResourcesWhenLimitIsReached() {
415         ArrayList<NodeTypeEnum> componentsToClean = new ArrayList<>();
416         componentsToClean.add(NodeTypeEnum.Resource);
417         migration.setUser(user);
418         migration.setMarkedAsDeletedResourcesCnt(5);
419         migration.deleteResourcesIfLimitIsReached();
420         verify(componentsCleanBusinessLogic).cleanComponents(componentsToClean, true);
421     }
422
423     @Test
424     public void deleteMarkedResourcesNotCalledWhenLimitIsNotReached() {
425         ArrayList<NodeTypeEnum> componentsToClean = new ArrayList<>();
426         componentsToClean.add(NodeTypeEnum.Resource);
427         migration.setUser(user);
428         migration.setMarkedAsDeletedResourcesCnt(3);
429         migration.deleteResourcesIfLimitIsReached();
430         verify(componentsCleanBusinessLogic, times(0)).cleanComponents(componentsToClean, true);
431     }
432
433     @Test
434     public void deleteMarkedServicesWhenLimitIsReached() {
435         ArrayList<NodeTypeEnum> componentsToClean = new ArrayList<>();
436         componentsToClean.add(NodeTypeEnum.Service);
437         migration.setUser(user);
438         migration.setMarkedAsDeletedServicesCnt(5);
439         migration.deleteServicesIfLimitIsReached();
440         verify(componentsCleanBusinessLogic).cleanComponents(componentsToClean, true);
441     }
442
443     @Test
444     public void deleteMarkedServicesNotCalledWhenLimitIsNotReached() {
445         ArrayList<NodeTypeEnum> componentsToClean = new ArrayList<>();
446         componentsToClean.add(NodeTypeEnum.Service);
447         migration.setUser(user);
448         migration.setMarkedAsDeletedServicesCnt(2);
449         migration.deleteServicesIfLimitIsReached();
450         verify(componentsCleanBusinessLogic, times(0)).cleanComponents(componentsToClean, true);
451     }
452
453     @Test
454     public void getVfUpgradeStatusWhenUpgradeFailedAndItIsInstance() {
455         assertEquals(UpgradeMigration1710.UpgradeStatus.NOT_UPGRADED, migration.getVfUpgradeStatus(false, true));
456     }
457
458     @Test
459     public void getVfUpgradeStatusWhenUpgradeFailedAndItIsNotInstance() {
460         assertEquals(UpgradeMigration1710.UpgradeStatus.NOT_UPGRADED, migration.getVfUpgradeStatus(false, false));
461     }
462
463     @Test
464     public void getVfUpgradeStatusWhenUpgradeSucceededAndItIsInstance() {
465         assertEquals(UpgradeMigration1710.UpgradeStatus.UPGRADED_AS_INSTANCE, migration.getVfUpgradeStatus(true, true));
466     }
467
468     @Test
469     public void getVfUpgradeStatusWhenUpgradeSucceededAndItIsNotInstance() {
470         assertEquals(UpgradeMigration1710.UpgradeStatus.UPGRADED, migration.getVfUpgradeStatus(true, false));
471     }
472
473     private void resolveUserAndDefineUpgradeLevel() {
474         when(userAdminOperation.getUserData(anyString(), eq(false))).thenReturn(Either.left(user));
475         configurationManager.getConfiguration().setToscaConformanceLevel(CONF_LEVEL);
476         configurationManager.getConfiguration().setResourcesForUpgrade(resourcesForUpgrade);
477     }
478
479     private void upgradeAllScenario(boolean upgradeServices) {
480         final boolean failOnVfUpgrade = false;
481         final boolean exceptionOnFvUpgrade = false;
482         final boolean upgradeVFC = false;
483         final boolean isFailed = false;
484         final boolean isProxy = true;
485
486         resolveUserAndDefineUpgradeLevel();
487         mockCheckoutFlow();
488         when(resourceBusinessLogic.validateAndUpdateResourceFromCsar(any(Resource.class), any(), any(), any(),
489                 any()))
490                 .thenReturn(resource);
491         upgradeRules(failOnVfUpgrade, exceptionOnFvUpgrade, upgradeServices, upgradeVFC, isFailed, isProxy);
492     }
493
494     private void upgradeRules(boolean failedVfUpgrade, boolean exceptionOnVfUpgrade, boolean upgradeService,
495                               boolean upgradeVFCs, boolean isFailed) {
496         upgradeRules(failedVfUpgrade, exceptionOnVfUpgrade, upgradeService, upgradeVFCs, isFailed, false);
497     }
498
499     private void upgradeRules(boolean failedVfUpgrade, boolean exceptionOnVfUpgrade, boolean upgradeService,
500                               boolean upgradeVFCs, boolean isFailed, boolean isProxy) {
501
502         mockNodeTypesUpgrade();
503         Either<Component, StorageOperationStatus> foundResource = Either.left(resource);
504
505         if (failedVfUpgrade) {
506             getToscaElementMockForVfUpgradeFailedScenario(foundResource);
507         } else {
508             if (exceptionOnVfUpgrade) {
509                 getToscaElementMockForExceptionOnUpgradeScenario(foundResource, upgradeService);
510             } else {
511                 when(toscaOperationFacade.getToscaElement(anyString()))
512                         .thenReturn(foundResource);
513             }
514         }
515         //happy flow
516         if (upgradeService) {
517             mockForUpgradeServiceScenario(foundResource, upgradeVFCs, isFailed);
518         }
519     }
520
521     private void mockNodeTypesUpgrade() {
522         GraphVertex component = createComponent();
523         List<GraphVertex> components = Lists.newArrayList();
524         components.add(component);
525
526         when(janusGraphDao.getByCriteria(any(), any(), any(), any()))
527                 .thenReturn(Either.left(components));
528         when(janusGraphDao.getParentVertecies(any(GraphVertex.class), any(EdgeLabelEnum.class), any(JsonParseFlagEnum.class)))
529                 //1th node to upgrade
530                 .thenReturn(Either.left(components))
531                 //parent of the 1th node - stop recursion
532                 .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
533     }
534
535     private GraphVertex createComponent() {
536         GraphVertex component = new GraphVertex();
537         component.setJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE,LifecycleStateEnum.CERTIFIED.name());
538         component.setJsonMetadataField(JsonPresentationFields.UNIQUE_ID,COMPONENT_UNIQUE_ID);
539         component.setJsonMetadataField(JsonPresentationFields.CI_COMPONENT_VERSION,UPDATED_VERSION);
540         return component;
541     }
542
543     private void mockChangeComponentState() {
544         List<ComponentInstance> instances = Lists.newArrayList();
545         instances.add(createComponentInstance());
546
547         Resource checkedOutResource = new Resource();
548         checkedOutResource.setUniqueId("123400");
549         checkedOutResource.setComponentInstances(instances);
550         Either<Resource, ResponseFormat> fromLifeCycle = Either.left(checkedOutResource);
551         doReturn(fromLifeCycle).when(lifecycleBusinessLogic)
552                 .changeComponentState(any(), any(), any(), any(), any(),eq(true), eq(false));
553     }
554
555     private void getToscaElementMockForVfUpgradeFailedScenario(Either<Component, StorageOperationStatus> foundResource) {
556         when(toscaOperationFacade.getToscaElement(anyString()))
557                 .thenReturn(foundResource)
558                 .thenReturn(foundResource)
559                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
560     }
561
562     private void mockForUpgradeServiceScenario(Either<Component, StorageOperationStatus> foundResource, boolean upgradeVFC, boolean isFailed) {
563         Either<Resource, StorageOperationStatus> foundService = Either.left(resource);
564         if (upgradeVFC) {
565             when(toscaOperationFacade.getToscaElement(anyString()))
566                     .thenReturn(foundResource)
567                     .thenReturn(foundResource)
568                     .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
569         }
570         else if (!isFailed) {
571             when(toscaOperationFacade.getToscaElement(any(), any(ComponentParametersView.class)))
572                     .thenReturn(Either.left(resource));
573             when(toscaOperationFacade.getLatestCertifiedByToscaResourceName(any(), any(), any()))
574                     .thenReturn(foundService);
575         }
576     }
577
578     private void getToscaElementMockForExceptionOnUpgradeScenario(Either<Component, StorageOperationStatus> foundResource, boolean upgradeService) {
579         if (upgradeService) {
580             service.setVersion(UPDATED_VERSION);
581             Either<Component, StorageOperationStatus> foundService = Either.left(service);
582             when(toscaOperationFacade.getToscaElement(anyString()))
583                     .thenReturn(foundResource)
584                     .thenReturn(foundResource)
585                     .thenThrow(new RuntimeException())
586                     .thenReturn(foundService);
587         }
588         else {
589             when(toscaOperationFacade.getToscaElement(anyString()))
590                     .thenReturn(foundResource)
591                     .thenReturn(foundResource)
592                     .thenThrow(new RuntimeException());
593         }
594     }
595
596     private void mockCheckoutFlow() {
597         GraphVertex component = new GraphVertex();
598         component.setJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE, LifecycleStateEnum.CERTIFIED.name());
599         component.setJsonMetadataField(JsonPresentationFields.UNIQUE_ID, COMPONENT_UNIQUE_ID);
600         List<GraphVertex> components = Lists.newArrayList();
601         components.add(component);
602
603         when(toscaOperationFacade.getToscaElement(anyString())).thenReturn(Either.left(resource));
604         when(janusGraphDao.getByCriteria(any(), any(), any(), any()))
605                 .thenReturn(Either.left(components));
606         when(csarOperation.getCsarLatestVersion(anyString(), any()))
607                 .thenReturn(Either.left("2.0"));
608     }
609
610     private ComponentInstance createComponentInstance() {
611         ComponentInstance instance = new ComponentInstance();
612         instance.setIcon("");
613         instance.setUniqueId("");
614         instance.setName("");
615         instance.setComponentUid("");
616         instance.setCreationTime(1L);
617         instance.setModificationTime(2L);
618         instance.setDescription("");
619         instance.setPosX("");
620         instance.setPosY("");
621         instance.setPropertyValueCounter(1);
622         instance.setNormalizedName("");
623         instance.setOriginType(OriginTypeEnum.CVFC);
624         instance.setCustomizationUUID("");
625         instance.setComponentName("");
626         instance.setComponentVersion(OLD_VERSION);
627         instance.setToscaComponentName("");
628         instance.setInvariantName("");
629         instance.setSourceModelInvariant("");
630         instance.setSourceModelName("");
631         instance.setSourceModelUuid("");
632         instance.setSourceModelUid("");
633         instance.setIsProxy(false);
634         return instance;
635     }
636
637
638 }