1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
package org.openecomp.sdc.asdctool.migration.tasks.mig1710;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
import org.openecomp.sdc.asdctool.migration.tasks.handlers.XlsOutputHandler;
import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
import org.openecomp.sdc.be.config.Configuration;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.IUserAdminOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.common.api.ConfigurationSource;
import org.openecomp.sdc.exception.ResponseFormat;
import com.google.common.collect.Lists;
import fj.data.Either;
@RunWith(MockitoJUnitRunner.class)
public class UpgradeMigration1710Test {
private final static String USER = "jh0003";
private final static String CONF_LEVEL = "5.0";
private final User user = new User();
@InjectMocks
private UpgradeMigration1710 migration = new UpgradeMigration1710();
@Mock
private IUserAdminOperation userAdminOperation;
@Mock
private ToscaOperationFacade toscaOperationFacade;
@Mock
private LifecycleBusinessLogic lifecycleBusinessLogic;
@Mock
private TitanDao titanDao;
@Mock
private ComponentsUtils componentUtils;
@Mock
private ConfigurationSource configurationSource;
@Mock
private XlsOutputHandler outputHandler;
private static ConfigurationManager configurationManager;
private static List<String> resources = Stream.of("org.openecomp.resource.cp.extCP").collect(Collectors.toList());
private static Map<String, List<String>> resourcesForUpgrade;
@BeforeClass
public static void setUpClass() {
resourcesForUpgrade = new HashMap<>();
resourcesForUpgrade.put(CONF_LEVEL, resources);
}
@Before
public void setUp() {
user.setUserId(USER);
configurationManager = new ConfigurationManager(configurationSource);
configurationManager.setConfiguration(new Configuration());
configurationManager.getConfiguration().setSkipUpgradeVSPs(true);
configurationManager.getConfiguration().setSkipUpgradeFailedVfs(true);
configurationManager.getConfiguration().setAutoHealingOwner(USER);
}
@Test
public void nodeTypesUpgradeFailed() {
resolveUserAndDefineUpgradeLevel();
when(titanDao.getByCriteria(any(), any(), any(), any()))
.thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus());
}
@Test
public void nodeTypesUpgradePassedAndVFsUpgradeFailedWhenSkipFailedVFsIsNotSupported() {
final boolean failOnVfUpgrade = true;
final boolean upgradeServices = false;
final boolean exceptionOnVfUpgrade = false;
final boolean upgradeVFC = false;
configurationManager.getConfiguration().setSkipUpgradeFailedVfs(false);
resolveUserAndDefineUpgradeLevel();
upgradeRules(failOnVfUpgrade, exceptionOnVfUpgrade, upgradeServices, upgradeVFC);
assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus());
verify(titanDao, times(1)).commit();
verify(titanDao, times(2)).rollback();
}
@Test
public void upgradeAllVFsUpgradeFailedOnExceptionWhenSkipFailedVFsIsNotSupported() {
final boolean failOnVfUpgrade = false;
final boolean upgradeServices = false;
final boolean exceptionOnVfUpgrade = true;
final boolean upgradeVFC = false;
configurationManager.getConfiguration().setSkipUpgradeFailedVfs(false);
resolveUserAndDefineUpgradeLevel();
upgradeRules(failOnVfUpgrade, exceptionOnVfUpgrade, upgradeServices, upgradeVFC);
assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus());
verify(titanDao, times(1)).commit();
verify(titanDao, times(2)).rollback();
}
@Test
public void upgradeAllIfVFsUpgradeFailedOnExceptionWhenSkipFailedVFsIsSupported() {
final boolean failOnVfUpgrade = false;
final boolean upgradeServices = true;
final boolean exceptionOnFvUpgrade = true;
final boolean upgradeVFC = false;
configurationManager.getConfiguration().setSkipUpgradeFailedVfs(true);
resolveUserAndDefineUpgradeLevel();
upgradeRules(failOnVfUpgrade, exceptionOnFvUpgrade, upgradeServices, upgradeVFC);
assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus());
verify(titanDao, times(2)).commit();
verify(titanDao, times(3)).rollback();
}
@Test
public void upgradeAll() {
final boolean failOnVfUpgrade = false;
final boolean upgradeServices = true;
final boolean exceptionOnFvUpgrade = false;
final boolean upgradeVFC = false;
resolveUserAndDefineUpgradeLevel();
upgradeRules(failOnVfUpgrade, exceptionOnFvUpgrade, upgradeServices, upgradeVFC);
assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus());
verify(titanDao, times(2)).commit();
verify(titanDao, times(3)).rollback();
}
@Test
public void upgradeAllWhenVspUpgradeIsRequired() {
final boolean failOnVfUpgrade = false;
final boolean upgradeServices = true;
final boolean exceptionOnFvUpgrade = false;
final boolean upgradeVFC = true;
resolveUserAndDefineUpgradeLevel();
upgradeRules(failOnVfUpgrade, exceptionOnFvUpgrade, upgradeServices, upgradeVFC);
configurationManager.getConfiguration().setSkipUpgradeVSPs(false);
// migration.setComponentsUtils(componentUtils);
assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus());
}
@Test
public void migrationFailedWhenUserNotResolved() {
when(userAdminOperation.getUserData(anyString(), eq(false))).thenReturn(Either.right(ActionStatus.MISSING_INFORMATION));
when(titanDao.rollback()).thenReturn(TitanOperationStatus.OK);
assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus());
}
private void resolveUserAndDefineUpgradeLevel() {
when(userAdminOperation.getUserData(anyString(), eq(false))).thenReturn(Either.left(user));
configurationManager.getConfiguration().setToscaConformanceLevel(CONF_LEVEL);
configurationManager.getConfiguration().setResourcesForUpgrade(resourcesForUpgrade);
}
private void upgradeRules(boolean failedVfUpgrade, boolean exceptionOnVfUpgrade, boolean upgradeService, boolean upgradeVFCs) {
GraphVertex component = new GraphVertex();
component.setJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE, LifecycleStateEnum.CERTIFIED.name());
component.setJsonMetadataField(JsonPresentationFields.UNIQUE_ID, "12345");
List<GraphVertex> components = Lists.newArrayList();
components.add(component);
Resource resource = new Resource();
Either<Component, StorageOperationStatus> foundResource = Either.left(resource);
when(titanDao.getByCriteria(any(), any(), any(), any()))
.thenReturn(Either.left(components));
when(titanDao.getParentVertecies(any(GraphVertex.class), any(), any()))
//1th node to upgrade
.thenReturn(Either.left(components))
//parent of the 1th node - stop recursion
.thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
if (failedVfUpgrade) {
Either<Component, StorageOperationStatus> getToscaForVF = Either.right(StorageOperationStatus.NOT_FOUND);
when(toscaOperationFacade.getToscaElement(anyString())).thenReturn(foundResource)
.thenReturn(foundResource)
.thenReturn(getToscaForVF);
}
else {
if (exceptionOnVfUpgrade) {
when(toscaOperationFacade.getToscaElement(anyString())).thenReturn(foundResource)
.thenReturn(foundResource)
.thenThrow(new RuntimeException());
}
else {
when(toscaOperationFacade.getToscaElement(anyString())).thenReturn(foundResource);
//happy flow
if (upgradeService) {
Either<Resource, StorageOperationStatus> service = Either.left(resource);
if (upgradeVFCs) {
when(componentUtils.convertFromStorageResponse(any(), any())).thenCallRealMethod();
when(componentUtils.getResponseFormat(any(ActionStatus.class),any())).thenCallRealMethod();
when(toscaOperationFacade.getLatestCertifiedByToscaResourceName(any(), any(), any()))
.thenReturn(service)
.thenReturn(Either.right(StorageOperationStatus.NOT_FOUND))
.thenReturn(service)
.thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
}
else {
when(toscaOperationFacade.getLatestCertifiedByToscaResourceName(any(), any(), any()))
.thenReturn(service);
}
}
}
}
List<ComponentInstance> instances = Lists.newArrayList();
instances.add(createComponentInstance());
resource.setComponentInstances(instances);
Either<Resource, ResponseFormat> fromLifeCycle = Either.left(resource);
doReturn(fromLifeCycle).when(lifecycleBusinessLogic)
.changeComponentState(any(), any(), any(), any(), any(),eq(true), eq(false));
}
private ComponentInstance createComponentInstance() {
ComponentInstance instance = new ComponentInstance();
instance.setIcon("");
instance.setUniqueId("");
instance.setName("");
instance.setComponentUid("");
instance.setCreationTime(1L);
instance.setModificationTime(2L);
instance.setDescription("");
instance.setPosX("");
instance.setPosY("");
instance.setPropertyValueCounter(1);
instance.setNormalizedName("");
instance.setOriginType(OriginTypeEnum.CVFC);
instance.setCustomizationUUID("");
instance.setComponentName("");
instance.setComponentVersion("");
instance.setToscaComponentName("");
instance.setInvariantName("");
instance.setSourceModelInvariant("");
instance.setSourceModelName("");
instance.setSourceModelUuid("");
instance.setSourceModelUid("");
instance.setIsProxy(false);
return instance;
}
}
|