Pause Upon VF Module Failure
[vid.git] / vid-automation / src / main / java / org / onap / vid / api / AsyncInstantiationBase.java
1 package org.onap.vid.api;
2
3 import static java.lang.Boolean.FALSE;
4 import static java.lang.Boolean.TRUE;
5 import static java.util.Collections.emptyList;
6 import static java.util.stream.Collectors.joining;
7 import static java.util.stream.Collectors.toMap;
8 import static org.hamcrest.CoreMatchers.hasItem;
9 import static org.hamcrest.MatcherAssert.assertThat;
10 import static org.hamcrest.Matchers.containsInAnyOrder;
11 import static org.hamcrest.Matchers.hasSize;
12 import static org.onap.simulator.presetGenerator.presets.mso.PresetMSOServiceInstanceGen2WithNames.Keys;
13 import static org.testng.Assert.assertNotNull;
14 import static org.testng.AssertJUnit.assertEquals;
15 import static org.testng.AssertJUnit.assertTrue;
16 import static vid.automation.test.utils.ExtendedHamcrestMatcher.hasItemsFromCollection;
17
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.UUID;
24 import java.util.concurrent.atomic.AtomicReference;
25 import java.util.function.Predicate;
26 import java.util.stream.Collectors;
27 import java.util.stream.IntStream;
28 import java.util.stream.Stream;
29 import org.apache.commons.lang3.StringUtils;
30 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
31 import org.apache.commons.lang3.builder.ToStringStyle;
32 import org.hamcrest.BaseMatcher;
33 import org.hamcrest.CoreMatchers;
34 import org.hamcrest.Description;
35 import org.hamcrest.MatcherAssert;
36 import org.onap.simulator.presetGenerator.presets.BasePresets.BaseMSOPreset;
37 import org.onap.simulator.presetGenerator.presets.BasePresets.BasePreset;
38 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetCloudOwnersByCloudRegionId;
39 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
40 import org.onap.simulator.presetGenerator.presets.ecompportal_att.PresetGetSessionSlotCheckIntervalGet;
41 import org.onap.simulator.presetGenerator.presets.mso.PresetMSOBaseCreateInstancePost;
42 import org.onap.simulator.presetGenerator.presets.mso.PresetMSOBaseDelete;
43 import org.onap.simulator.presetGenerator.presets.mso.PresetMSOCreateServiceInstanceGen2WithNames;
44 import org.onap.simulator.presetGenerator.presets.mso.PresetMSOOrchestrationRequestGet;
45 import org.onap.vid.model.asyncInstantiation.JobAuditStatus;
46 import org.onap.vid.model.asyncInstantiation.ServiceInfo;
47 import org.onap.vid.model.mso.MsoResponseWrapper2;
48 import org.springframework.core.ParameterizedTypeReference;
49 import org.springframework.http.HttpMethod;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.web.client.RestTemplate;
53 import org.testng.Assert;
54 import org.testng.annotations.AfterMethod;
55 import org.testng.annotations.BeforeClass;
56 import org.testng.annotations.DataProvider;
57 import vid.automation.test.infra.Features;
58 import vid.automation.test.infra.Wait;
59 import vid.automation.test.model.JobStatus;
60 import vid.automation.test.model.ServiceAction;
61 import vid.automation.test.services.AsyncJobsService;
62 import vid.automation.test.services.SimulatorApi;
63
64 public class AsyncInstantiationBase extends BaseMsoApiTest {
65
66     public static final String CREATE_BULK_OF_ALACARTE_REQUEST_WITH_VNF = "asyncInstantiation/vidRequestCreateALaCarteWithVnf.json";
67     protected static final String CREATE_BULK_OF_MACRO_REQUEST = "asyncInstantiation/vidRequestCreateBulkOfMacro.json";
68
69     protected static final String MSO_BASE_ERROR =
70             "Received error from SDN-C: java.lang.IllegalArgumentException: All keys must be specified for class org."+
71             "opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.vf.module.assignments.vf."+
72             "module.assignments.vms.VmKey. Missing key is getVmType. Supplied key is VmKey [].";
73     protected static final String MSO_ERROR = MSO_BASE_ERROR + StringUtils.repeat(" and a lot of sentences for long message", 60);
74
75     @BeforeClass
76     protected void muteAndDropNameCounter() {
77         AsyncJobsService asyncJobsService = new AsyncJobsService();
78         asyncJobsService.muteAllAsyncJobs();
79         asyncJobsService.dropAllFromNameCounter();
80     }
81
82     @AfterMethod
83     protected void muteAllAsyncJobs() {
84         AsyncJobsService asyncJobsService = new AsyncJobsService();
85         asyncJobsService.muteAllAsyncJobs();
86     }
87
88     @DataProvider
89     public static Object[][] trueAndFalse() {
90             return new Object[][]{{TRUE},{FALSE}};
91     }
92
93     protected String getCreateBulkUri() {
94         return uri.toASCIIString() + "/asyncInstantiation/bulk";
95     }
96
97     protected String getHideServiceUri(String jobId) {
98         return uri.toASCIIString() + "/asyncInstantiation/hide/"+jobId;
99     }
100
101     protected String getServiceInfoUrl() {
102         return uri.toASCIIString() + "/asyncInstantiation";
103     }
104
105     protected String getTemplateInfoUrl(String serviceModelId) {
106         return uri.toASCIIString() + "/instantiationTemplates?serviceModelId=" + serviceModelId;
107     }
108
109     protected String getJobAuditUrl() {
110         return uri.toASCIIString() + "/asyncInstantiation/auditStatus/{JOB_ID}?source={SOURCE}";
111     }
112
113     protected String getMsoJobAuditUrl() {
114         return uri.toASCIIString() + "/asyncInstantiation/auditStatus/{JOB_ID}/mso";
115     }
116
117     protected String getDeleteServiceUrl(String uuid) {
118         return uri.toASCIIString() + "/asyncInstantiation/job/" + uuid;
119     }
120
121     protected String getInstanceAuditInfoUrl() {
122         return uri.toASCIIString() + "/asyncInstantiation/auditStatus/{TYPE}/{INSTANCE_ID}/mso";
123     }
124
125     protected String getRetryJobUrl() {
126         return uri.toASCIIString() + "/asyncInstantiation/retry/{JOB_ID}";
127     }
128     protected String getTopologyForRetryUrl() {
129         return uri.toASCIIString() + "/asyncInstantiation/bulkForRetry/{JOB_ID}";
130     }
131
132
133     protected String getRetryJobWithChangedDataUrl() {
134         return uri.toASCIIString() + "/asyncInstantiation/retryJobWithChangedData/{JOB_ID}";
135     }
136
137     protected boolean getExpectedRetryEnabled(JobStatus jobStatus) {
138         return Features.FLAG_1902_RETRY_JOB.isActive() && (jobStatus==JobStatus.FAILED || jobStatus==JobStatus.COMPLETED_WITH_ERRORS
139                 || jobStatus==JobStatus.FAILED_AND_PAUSED);
140     }
141
142     public List<BasePreset> getPresets(List<PresetMSOBaseDelete> presetOnDeleteList, List<PresetMSOBaseCreateInstancePost> presetOnCreateList, List<PresetMSOOrchestrationRequestGet> presetInProgressList) {
143
144         final ImmutableList.Builder<BasePreset> basePresetBuilder = new ImmutableList.Builder<>();
145         basePresetBuilder
146                 .add(new PresetGetSessionSlotCheckIntervalGet())
147                 .add(new PresetAAIGetSubscribersGet())
148                 .addAll(presetOnDeleteList)
149                 .addAll(presetOnCreateList)
150                 .addAll(presetInProgressList);
151         return basePresetBuilder.build();
152     }
153
154     public List<BasePreset> getDeletePresets(List<PresetMSOBaseDelete> presetOnDeleteList, List<PresetMSOOrchestrationRequestGet> presetInProgressList) {
155         return getPresets(presetOnDeleteList, emptyList(), presetInProgressList);
156     }
157
158     public List<BasePreset> getPresets(List<PresetMSOBaseCreateInstancePost> presetOnCreateList, List<PresetMSOOrchestrationRequestGet> presetInProgressList) {
159         return getPresets(emptyList(), presetOnCreateList, presetInProgressList);
160     }
161
162     public void assertServiceInfoSpecific1(String jobId, JobStatus jobStatus, String serviceInstanceName, String userName) {
163         assertServiceInfoSpecific1(jobId, jobStatus, serviceInstanceName, userName, null, ServiceAction.INSTANTIATE);
164     }
165
166     public void assertServiceInfoSpecific1(String jobId, JobStatus jobStatus, String serviceInstanceName, String userName, String instanceId, ServiceAction action) {
167         assertExpectedStatusAndServiceInfo(jobStatus, jobId, new ServiceInfo(
168                 userName, jobStatus, false,
169                 "038d99af-0427-42c2-9d15-971b99b9b489", "Lucine Sarika", "zasaki",
170                 "de738e5f-3704-4a14-b98f-3bf86ac0c0a0", "voloyakane-senamo",
171                 "c85f0e80-0636-44a4-8cb2-4ec00d056e79", "Hedvika Wendelin",
172                 "a93f8383-707e-43fa-8191-a6e69a1aab17", null,
173                 "TYLER SILVIA", "SILVIA ROBBINS",
174                 instanceId, serviceInstanceName,
175                 "e3c34d88-a216-4f1d-a782-9af9f9588705", "gayawabawe", "5.1",
176                 jobId, null, action, false)
177         );
178     }
179
180     public void assertServiceInfoSpecific1(String jobId, JobStatus jobStatus, String serviceInstanceName) {
181         assertServiceInfoSpecific1(jobId, jobStatus, serviceInstanceName, "us16807000");
182     }
183
184     protected void assertAuditStatuses(String jobId, List<JobAuditStatus> expectedVidStatuses, List<JobAuditStatus> expectedMsoStatuses) {
185         assertAuditStatuses(jobId, expectedVidStatuses, expectedMsoStatuses, 15);
186     }
187
188     protected void assertAuditStatuses(String jobId, List<JobAuditStatus> expectedVidStatuses, List<JobAuditStatus> expectedMsoStatuses, long timeoutInSeconds) {
189         assertAndRetryIfNeeded(() -> {
190             final List<JobAuditStatus> auditVidStatuses = getAuditStatuses(jobId, JobAuditStatus.SourceStatus.VID.name());
191             assertThat(auditVidStatuses, hasItemsFromCollection(expectedVidStatuses));
192             if (expectedMsoStatuses!=null) {
193                 final List<JobAuditStatus> auditMsoStatuses = getAuditStatuses(jobId, JobAuditStatus.SourceStatus.MSO.name());
194                 assertThat(auditMsoStatuses, containsInAnyOrder(expectedMsoStatuses.toArray()));
195             }
196         }, timeoutInSeconds);
197     }
198
199     protected void assertAndRetryIfNeeded(Runnable asserter, long timeoutInSeconds) {
200         TestUtils.assertAndRetryIfNeeded(timeoutInSeconds, asserter);
201     }
202
203     protected ImmutableList<JobAuditStatus> vidAuditStatusesCompletedWithErrors(String jobId) {
204         return ImmutableList.of(
205                 vidAuditStatus(jobId, "PENDING", false),
206                 vidAuditStatus(jobId, "IN_PROGRESS", false),
207                 vidAuditStatus(jobId, "COMPLETED_WITH_ERROR", true)
208         );
209     }
210
211     protected ImmutableList<JobAuditStatus> vidAuditStatusesFailed(String jobId) {
212         return ImmutableList.of(
213                 vidAuditStatus(jobId, "PENDING", false),
214                 vidAuditStatus(jobId, "IN_PROGRESS", false),
215                 vidAuditStatus(jobId, "FAILED", true)
216         );
217     }
218     protected ImmutableList<JobAuditStatus> vidAuditStatusesFailedAndPaused(String jobId) {
219         return ImmutableList.of(
220                 vidAuditStatus(jobId, "PENDING", false),
221                 vidAuditStatus(jobId, "IN_PROGRESS", false),
222                 vidAuditStatus(jobId, "FAILED_AND_PAUSED", true)
223         );
224     }
225     protected JobAuditStatus vidAuditStatus(String jobId, String jobStatus, boolean isFinal) {
226         return new JobAuditStatus(UUID.fromString(jobId), jobStatus, JobAuditStatus.SourceStatus.VID, null, null, isFinal);
227     }
228
229     public static class JobIdAndStatusMatcher extends BaseMatcher<ServiceInfo> {
230         protected String expectedJobId;
231
232         public JobIdAndStatusMatcher(String expectedJobId) {
233             this.expectedJobId = expectedJobId;
234         }
235
236         @Override
237         public boolean matches(Object item) {
238             if (!(item instanceof ServiceInfo)) {
239                 return false;
240             }
241             ServiceInfo serviceInfo = (ServiceInfo) item;
242             return expectedJobId.equals(serviceInfo.jobId);
243         }
244
245         @Override
246         public void describeTo(Description description) {
247             description.appendText("failed to find job with uuid ")
248                     .appendValue(expectedJobId);
249         }
250     }
251
252
253
254     protected Map<Keys,String> generateNames() {
255         return Stream.of(Keys.values()).collect(
256                 Collectors.toMap(x->x, x -> UUID.randomUUID().toString().replace("-","")));
257     }
258
259     protected ImmutableList<BasePreset> addPresetsForCreateBulkOfCreateInstances(int bulkSize, Map<Keys, String> names){
260         ImmutableList<BasePreset> msoBulkPresets = generateMsoCreateBulkPresets(bulkSize, names);
261         ImmutableList<BasePreset> presets = new ImmutableList.Builder<BasePreset>()
262                 .add(new PresetGetSessionSlotCheckIntervalGet())
263                 .add(new PresetAAIGetSubscribersGet())
264                 .add(PresetAAIGetCloudOwnersByCloudRegionId.PRESET_MTN3_TO_ATT_SABABA)
265                 .addAll(msoBulkPresets)
266                 .add(new PresetMSOOrchestrationRequestGet())
267                 .build();
268         return presets;
269
270     }
271
272     protected ImmutableList<BasePreset> generateMsoCreateBulkPresets(int bulkSize, Map<Keys, String> names) {
273         return IntStream.rangeClosed(0, bulkSize-1).
274                 mapToObj(i-> new PresetMSOCreateServiceInstanceGen2WithNames(names, i))
275                 .collect(ImmutableList.toImmutableList());
276     }
277
278     protected ResponseEntity<List<JobAuditStatus>> auditStatusCall(String url) {
279         return restTemplate.exchange(
280                 url,
281                 org.springframework.http.HttpMethod.GET,
282                 null,
283                 new ParameterizedTypeReference<List<JobAuditStatus>>() {});
284     }
285
286     @DataProvider
287     public static Object[][] auditSources() {
288         return new Object[][]{{JobAuditStatus.SourceStatus.VID},{JobAuditStatus.SourceStatus.MSO}};
289     }
290
291
292
293     protected List<String> createBulkAndWaitForBeCompleted(int bulkSize){
294         Map<Keys, String> names = generateNames();
295         ImmutableList<BasePreset> presets = addPresetsForCreateBulkOfCreateInstances(bulkSize, names);
296         final List<String> jobIds = createBulkOfMacroInstances(presets, false, bulkSize, names);
297         Assert.assertEquals(jobIds.size(),bulkSize);
298
299         waitForJobsToSuccessfullyCompleted(bulkSize, jobIds);
300         return jobIds;
301     }
302
303     public void waitForJobsToSuccessfullyCompleted(int bulkSize, List<String> jobIds) {
304         assertTrue(String.format("Not all services with ids: %s are in state completed after 30 sec",
305                 jobIds.stream().collect(joining(","))),
306
307                 Wait.waitFor(y-> serviceListCall().getBody().stream()
308                         .filter(si -> jobIds.contains(si.jobId))
309                         .filter(si -> si.jobStatus== JobStatus.COMPLETED)
310                         .count() == bulkSize,
311                 null, 30, 1 ));
312     }
313
314     protected List<JobAuditStatus> getJobMsoAuditStatusForAlaCarte(String jobUUID, String requestId, String serviceInstanceId){
315         String url = getMsoJobAuditUrl().replace("{JOB_ID}",jobUUID);
316
317         if(!StringUtils.isEmpty(requestId)) {
318             url = url + "?requestId=" + requestId;
319             if(!StringUtils.isEmpty(serviceInstanceId)) {
320                 url = url + "&serviceInstanceId=" + serviceInstanceId;
321             }
322         }
323         return callAuditStatus(url);
324     }
325
326     protected List<JobAuditStatus> getAuditStatuses(String jobUUID, String source){
327         String url = getJobAuditUrl().replace("{JOB_ID}",jobUUID).replace("{SOURCE}", source);
328         return callAuditStatus(url);
329     }
330
331     protected List<JobAuditStatus> getAuditStatusesForInstance(String type, String instanceId){
332         String url = getInstanceAuditInfoUrl().replace("{TYPE}",type).replace("{INSTANCE_ID}", instanceId);
333         return callAuditStatus(url);
334     }
335
336     private List<JobAuditStatus> callAuditStatus(String url) {
337         ResponseEntity<List<JobAuditStatus>> statusesResponse = auditStatusCall(url);
338         assertThat(statusesResponse.getStatusCode(), CoreMatchers.equalTo(HttpStatus.OK));
339         return statusesResponse.getBody();
340     }
341
342     protected Map<String, JobStatus> addBulkAllPendingButOneInProgress(){
343         return addBulkAllPendingButOneInProgress(3);
344     }
345
346     protected Map<String, JobStatus> addBulkAllPendingButOneInProgress(int bulkSize){
347         Map<Keys, String> names = generateNames();
348         ImmutableList<BasePreset> msoBulkPresets = generateMsoCreateBulkPresets(bulkSize, names);
349         ImmutableList<BasePreset> presets = new ImmutableList.Builder<BasePreset>()
350                 .add(new PresetGetSessionSlotCheckIntervalGet())
351                 .add(new PresetAAIGetSubscribersGet())
352                 .add(PresetAAIGetCloudOwnersByCloudRegionId.PRESET_MTN3_TO_ATT_SABABA)
353                 .addAll(msoBulkPresets)
354                 .add(new PresetMSOOrchestrationRequestGet("IN_PROGRESS"))
355                 .build();
356         final List<String> jobIds = createBulkOfMacroInstances(presets, false, bulkSize, names);
357
358         // wait for single IN_PROGRESS, so statuses will stop from changing
359         Wait.waitFor(foo -> serviceListCall().getBody().stream()
360                         .filter(si -> jobIds.contains(si.jobId))
361                         .anyMatch(si -> si.jobStatus.equals(JobStatus.IN_PROGRESS)),
362                 null, 20, 1);
363
364         final Map<String, JobStatus> statusMapBefore = serviceListCall().getBody().stream()
365                 .filter(si -> jobIds.contains(si.jobId))
366                 .collect(toMap(si -> si.jobId, si -> si.jobStatus));
367
368         assertThat(jobIds, hasSize(bulkSize));
369
370
371         return statusMapBefore;
372     }
373
374     protected String deleteOneJobHavingTheStatus(Map<String, JobStatus> jobIdToStatus, JobStatus jobStatus) {
375         final String jobToDelete = jobIdToStatus.entrySet().stream()
376                 .filter(entry -> entry.getValue().equals(jobStatus))
377                 .map(Map.Entry::getKey)
378                 .findFirst().orElseThrow(() -> new AssertionError("no job in " + jobStatus + " state: " + jobIdToStatus));
379
380
381         restTemplate.delete(getDeleteServiceUrl(jobToDelete));
382
383         return jobToDelete;
384     }
385
386
387     protected MsoResponseWrapper2 hideService(String jobId) {
388         MsoResponseWrapper2 responseWrapper2 = callMsoForResponseWrapper(org.springframework.http.HttpMethod.POST, getHideServiceUri(jobId), "");
389         return responseWrapper2;
390     }
391
392     protected List<String> createBulkOfInstancesAndAssert(ImmutableList<BasePreset> presets, boolean isPause, int bulkSize, JobStatus finalState, Map<Keys, String> names){
393         List<String> jobIds = createBulkOfMacroInstances(presets, isPause, bulkSize, names);
394         Assert.assertEquals(jobIds.size(), bulkSize);
395         for(String jobId: jobIds) {
396             assertExpectedStatusAndServiceInfo(isPause, finalState, names, jobId);
397         }
398
399         return jobIds;
400     }
401
402     protected void assertExpectedStatusAndServiceInfo(boolean isPause, JobStatus finalState, Map<Keys, String> names, String jobId) {
403         assertExpectedStatusAndServiceInfo(finalState, jobId, new ServiceInfo("us16807000", JobStatus.IN_PROGRESS, isPause, "someID",
404                 "someName", "myProject", "NFT1", "NFTJSSSS-NFT1", "greatTenant", "greatTenant", "hvf3", null,
405                 "mySubType", "a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb", null, names.get(Keys.SERVICE_NAME),
406                 "5c9e863f-2716-467b-8799-4a67f378dcaa", "AIM_TRANSPORT_00004", "1.0", jobId, null, ServiceAction.INSTANTIATE, false));
407     }
408
409     protected void assertExpectedStatusAndServiceInfo(JobStatus finalState, String jobId, ServiceInfo expectedServiceInfo) {
410         assertExpectedStatusAndServiceInfo(finalState, jobId, PATIENCE_LEVEL.FAIL_FAST, expectedServiceInfo);
411     }
412
413     enum PATIENCE_LEVEL { FAIL_FAST, FAIL_SLOW, FAIL_VERY_SLOW }
414
415     protected void assertExpectedStatusAndServiceInfo(JobStatus finalState, String jobId, PATIENCE_LEVEL patienceLevel, ServiceInfo expectedServiceInfo) {
416         JobInfoChecker<Integer> jobInfoChecker = new JobInfoChecker<>(
417                 restTemplate, ImmutableSet.of(JobStatus.PENDING, JobStatus.IN_PROGRESS, finalState), jobId, expectedServiceInfo);
418         boolean result = jobInfoChecker.test(null);
419         assertTrue("service info of jobId: " + jobId + " was in status: " + jobInfoChecker.lastStatus, result);
420
421         jobInfoChecker.setExpectedJobStatus(ImmutableSet.of(finalState));
422         if (ImmutableList.of(JobStatus.COMPLETED, JobStatus.PAUSE).contains(finalState) && expectedServiceInfo.serviceInstanceId==null) {
423             expectedServiceInfo.serviceInstanceId = BaseMSOPreset.DEFAULT_INSTANCE_ID;
424         }
425         result = Wait.waitFor(jobInfoChecker, null, 30, waitIntervalBy(patienceLevel));
426         assertTrue("service info of jobId: " + jobId + " was in status: " + jobInfoChecker.lastStatus, result);
427     }
428
429     private int waitIntervalBy(PATIENCE_LEVEL patienceLevel) {
430         switch (patienceLevel) {
431             case FAIL_SLOW:
432                 return 2;
433             case FAIL_VERY_SLOW:
434                 return 3;
435             default:
436                 return 1;
437         }
438     }
439
440     protected List<String> createBulkOfMacroInstances(ImmutableList<BasePreset> presets, boolean isPause, int bulkSize, Map<Keys, String> names) {
441         SimulatorApi.registerExpectationFromPresets(presets, SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
442         return createBulkOfInstances(isPause, bulkSize, names, CREATE_BULK_OF_MACRO_REQUEST);
443     }
444
445     public List<String> createBulkOfInstances(boolean isPause, int bulkSize, Map<Keys, String> names, String requestDetailsFileName){
446
447         String requestBody = TestUtils.convertRequest(objectMapper, requestDetailsFileName);
448         requestBody = requestBody.replace("\"IS_PAUSE_VALUE\"", String.valueOf(isPause)).replace("\"BULK_SIZE\"", String.valueOf(bulkSize));
449         for (Map.Entry<Keys, String> e : names.entrySet()) {
450             requestBody = requestBody.replace(e.getKey().name(), e.getValue());
451         }
452         MsoResponseWrapper2 responseWrapper2 = callMsoForResponseWrapper(org.springframework.http.HttpMethod.POST, getCreateBulkUri(), requestBody);
453         assertNotNull(responseWrapper2);
454         return (List<String>)responseWrapper2.getEntity();
455     }
456
457     protected List<String> retryJob(String jobId) {
458         ResponseEntity<String> retryBulkPayload = getRetryBulk(jobId);
459         return retryJobWithChangedData(jobId, retryBulkPayload.getBody());
460     }
461
462     protected List<String> retryJobWithChangedData(String jobId, String requestBody) {
463         String retryUri = getRetryJobWithChangedDataUrl();
464         retryUri = retryUri.replace("{JOB_ID}", jobId);
465         MsoResponseWrapper2 responseWrapper2 = callMsoForResponseWrapper(HttpMethod.POST, retryUri, requestBody);
466         assertNotNull(responseWrapper2);
467         return (List<String>)responseWrapper2.getEntity();
468     }
469
470     protected ResponseEntity<String> getRetryBulk(String jobId) {
471         String retryUri = getTopologyForRetryUrl();
472         retryUri = retryUri.replace("{JOB_ID}", jobId);
473         return restTemplateErrorAgnostic.getForEntity(retryUri, String.class);
474     }
475
476     protected Object getResourceAuditInfo(String trackById) {
477         return restTemplate.getForObject(buildUri("/asyncInstantiation/auditStatusForRetry/{trackById}"), Object.class, trackById);
478     }
479
480     public class JobInfoChecker<Integer> implements Predicate<Integer> {
481
482         protected final RestTemplate restTemplate;
483         protected Set<JobStatus> expectedJobStatus;
484         protected ServiceInfo expectedServiceInfo;
485         protected final String jobId;
486         protected JobStatus lastStatus;
487
488         public JobInfoChecker(RestTemplate restTemplate, Set<JobStatus> expectedJobStatus, String jobId, ServiceInfo expectedServiceInfo) {
489             this.restTemplate = restTemplate;
490             this.expectedJobStatus = expectedJobStatus;
491             this.jobId = jobId;
492             this.expectedServiceInfo = expectedServiceInfo;
493         }
494
495         public void setExpectedJobStatus(Set<JobStatus> expectedJobStatus) {
496             this.expectedJobStatus = expectedJobStatus;
497         }
498
499         @Override
500         public boolean test(Integer integer) {
501             ResponseEntity<List<ServiceInfo>> serviceListResponse = serviceListCall();
502             assertThat(serviceListResponse.getStatusCode(), CoreMatchers.equalTo(HttpStatus.OK));
503             assertThat(serviceListResponse.getBody(), hasItem(new JobIdAndStatusMatcher(jobId)));
504             ServiceInfo serviceInfoFromDB = serviceListResponse.getBody().stream()
505                     .filter(serviceInfo -> serviceInfo.jobId.equals(jobId))
506                     .findFirst().orElse(null);
507             Assert.assertNotNull(serviceInfoFromDB);
508             Assert.assertEquals(serviceInfoDataReflected(serviceInfoFromDB), serviceInfoDataReflected(expectedServiceInfo));
509             assertTrue("actual service instance doesn't contain template service name:" + expectedServiceInfo.serviceInstanceName,
510                     serviceInfoFromDB.serviceInstanceName.contains(expectedServiceInfo.serviceInstanceName));
511
512             if (expectedServiceInfo.serviceInstanceId != null && ImmutableList.of(JobStatus.COMPLETED, JobStatus.PAUSE, JobStatus.COMPLETED_WITH_ERRORS).contains(serviceInfoFromDB.jobStatus)) {
513                 MatcherAssert.assertThat("service instance id is wrong", serviceInfoFromDB.serviceInstanceId, CoreMatchers.is(expectedServiceInfo.serviceInstanceId));
514             }
515             if (expectedJobStatus.size()==1) {
516                 assertEquals("job status is wrong", getExpectedRetryEnabled((JobStatus)(expectedJobStatus.toArray()[0])), serviceInfoFromDB.isRetryEnabled);
517             }
518             lastStatus = serviceInfoFromDB.jobStatus;
519             return expectedJobStatus.contains(serviceInfoFromDB.jobStatus);
520         }
521     }
522
523     protected ResponseEntity<List<ServiceInfo>> serviceListCall() {
524         return restTemplate.exchange(
525                 getServiceInfoUrl(),
526                 org.springframework.http.HttpMethod.GET,
527                 null,
528                 new ParameterizedTypeReference<List<ServiceInfo>>() {});
529     }
530
531     //serialize fields except of fields we cannot know ahead of time
532     protected static String serviceInfoDataReflected(ServiceInfo service1) {
533         return new ReflectionToStringBuilder(service1, ToStringStyle.SHORT_PREFIX_STYLE)
534                 .setExcludeFieldNames("jobStatus", "templateId", "statusModifiedDate", "createdBulkDate", "serviceInstanceId", "serviceInstanceName", "isRetryEnabled")
535                 .toString();
536     }
537
538     protected Map<Keys, String> addBulkPendingWithCustomList(List<BasePreset> customPresets){
539         Map<Keys, String> names = generateNames();
540         final int bulkSize = 2 + customPresets.size();
541
542         List<BasePreset> msoBulkPresets = generateMsoCreateBulkPresets(bulkSize, names);
543         ImmutableList<BasePreset> presets = new ImmutableList.Builder<BasePreset>()
544                 .add(new PresetGetSessionSlotCheckIntervalGet())
545                 .add(new PresetAAIGetSubscribersGet())
546                 .add(PresetAAIGetCloudOwnersByCloudRegionId.PRESET_MTN3_TO_ATT_SABABA, PresetAAIGetCloudOwnersByCloudRegionId.PRESET_MDT1_TO_ATT_NC)
547                 .addAll(msoBulkPresets)
548                 .addAll(customPresets)
549                 .build();
550
551         List<String> jobIds = createBulkOfMacroInstances(presets, false, bulkSize, names);
552         Assert.assertEquals(jobIds.size(),bulkSize);
553
554         return names;
555     }
556
557     protected void verifyAuditStatuses(String jobId, List<String> statuses, JobAuditStatus.SourceStatus source) {
558         int statusesSize = statuses.size();
559         AtomicReference<List<JobAuditStatus>> actualAudits = new AtomicReference<>();
560         if (source.equals(JobAuditStatus.SourceStatus.VID)) {
561             actualAudits.set(getAuditStatuses(jobId, JobAuditStatus.SourceStatus.VID.name()));
562             org.junit.Assert.assertEquals("Received number of VID statuses is not as expected", statusesSize, actualAudits.get().size());
563         } else {
564             boolean isStatusedSizeAsExpected = Wait.waitFor(x-> {
565                 actualAudits.set(getAuditStatuses(jobId, JobAuditStatus.SourceStatus.MSO.name()));
566                 return actualAudits.get().size() == statusesSize;
567             },null,5,1);
568             org.junit.Assert.assertTrue("Received number of MSO statuses is not as expected. Expected: " + statusesSize + ". Received: " + actualAudits.get().size(), isStatusedSizeAsExpected);
569         }
570         IntStream.range(0, statusesSize).forEach(i-> org.junit.Assert.assertEquals(source + " status #" + i + " is not as expected", statuses.get(i), actualAudits.get().get(i).getJobStatus()));
571     }
572
573     protected void verifyInstanceAuditStatuses(List<JobAuditStatus> expectedStatuses, List<JobAuditStatus> actualStatuses) {
574         final int expectedSize = expectedStatuses.size();
575         assertTrue("Expected statuses size is "+ expectedSize +", actual size is "+actualStatuses.size(), new Integer(expectedSize).equals(actualStatuses.size()));
576         IntStream.range(0, expectedSize).forEach(i-> {
577
578             final JobAuditStatus expectedStatus = expectedStatuses.get(i);
579             final JobAuditStatus actualStatus = actualStatuses.get(i);
580             org.junit.Assert.assertEquals("MSO status #" + i + " is not as expected", expectedStatus.getJobStatus(), actualStatus.getJobStatus());
581             org.junit.Assert.assertEquals("MSO requestId #" + i + " is not as expected", expectedStatus.getRequestId(), actualStatus.getRequestId());
582             org.junit.Assert.assertEquals("MSO additionalInfo #" + i + " is not as expected", expectedStatus.getAdditionalInfo(), actualStatus.getAdditionalInfo());
583             org.junit.Assert.assertEquals("MSO jobID #" + i + " is not as expected", expectedStatus.getJobId(), actualStatus.getJobId());
584             org.junit.Assert.assertEquals("MSO instanceName #" + i + " is not as expected", expectedStatus.getInstanceName(), actualStatus.getInstanceName());
585             org.junit.Assert.assertEquals("MSO instanceType  #" + i + " is not as expected", expectedStatus.getInstanceType(), actualStatus.getInstanceType());
586         });
587     }
588     protected static JobStatus getErrorStatus() {
589             return Features.FLAG_2008_PAUSE_VFMODULE_INSTANTIATION_FAILURE.isActive() ?
590                             JobStatus.FAILED_AND_PAUSED : JobStatus.COMPLETED_WITH_ERRORS;
591    }
592 }