1 package org.onap.vid.api;
3 import com.google.common.collect.ImmutableList;
4 import com.google.common.collect.ImmutableMap;
5 import net.bytebuddy.utility.RandomString;
6 import net.javacrumbs.jsonunit.JsonAssert;
7 import org.apache.commons.lang3.StringUtils;
8 import org.apache.logging.log4j.LogManager;
9 import org.apache.logging.log4j.Logger;
10 import org.hamcrest.collection.IsCollectionWithSize;
11 import org.onap.simulator.presetGenerator.presets.BasePresets.BasePreset;
12 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetCloudOwnersByCloudRegionId;
13 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
14 import org.onap.simulator.presetGenerator.presets.ecompportal_att.PresetGetSessionSlotCheckIntervalGet;
15 import org.onap.simulator.presetGenerator.presets.mso.*;
16 import org.onap.vid.model.asyncInstantiation.JobAuditStatus;
17 import org.onap.vid.model.asyncInstantiation.ServiceInfo;
18 import org.onap.sdc.ci.tests.datatypes.UserCredentials;
19 import org.springframework.http.HttpStatus;
20 import org.springframework.web.client.HttpClientErrorException;
21 import org.springframework.web.client.HttpServerErrorException;
22 import org.testng.Assert;
23 import org.testng.annotations.DataProvider;
24 import org.testng.annotations.Test;
25 import vid.automation.test.infra.FeatureTogglingTest;
26 import vid.automation.test.infra.Wait;
27 import vid.automation.test.model.JobStatus;
28 import vid.automation.test.model.ServiceAction;
29 import vid.automation.test.services.SimulatorApi;
32 import java.util.concurrent.atomic.AtomicReference;
33 import java.util.stream.IntStream;
34 import java.util.stream.Stream;
36 import static java.util.stream.Collectors.*;
37 import static org.hamcrest.CoreMatchers.*;
38 import static org.hamcrest.MatcherAssert.assertThat;
39 import static org.hamcrest.Matchers.hasSize;
40 import static org.onap.simulator.presetGenerator.presets.mso.PresetMSOOrchestrationRequestGet.COMPLETE;
41 import static org.onap.simulator.presetGenerator.presets.mso.PresetMSOServiceInstanceGen2WithNames.Keys;
42 import static org.testng.AssertJUnit.assertEquals;
43 import static org.testng.AssertJUnit.assertTrue;
44 import static vid.automation.test.infra.Features.FLAG_1906_INSTANTIATION_API_USER_VALIDATION;
45 import static vid.automation.test.services.SimulatorApi.registerExpectationFromPresets;
46 import static vid.automation.test.utils.ExtendedHamcrestMatcher.hasItemsFromCollection;
48 public class AsyncInstantiationMacroApiTest extends AsyncInstantiationBase {
49 private static final Logger logger = LogManager.getLogger(AsyncInstantiationMacroApiTest.class);
51 private static final String MSO_BASE_ERROR =
52 "Received error from SDN-C: java.lang.IllegalArgumentException: All keys must be specified for class org."+
53 "opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.vf.module.assignments.vf."+
54 "module.assignments.vms.VmKey. Missing key is getVmType. Supplied key is VmKey [].";
55 private static final String MSO_ERROR = MSO_BASE_ERROR + StringUtils.repeat(" and a lot of sentences for long message", 60);
57 private static final String INSTANCE_GROUP_ID_LABEL = "instanceGroupId";
59 private static final String INSTANCE_GROUP_LABEL = "instanceGroup";
61 private static final String DELETE_MACRO_SERVICE_FILE_NAME = "asyncInstantiation/vidRequestDeleteMacroService.json";
64 public void createBulkOfCreateInstances(){
65 Map<Keys, String> names = generateNames();
66 final int bulkSize = 3;
67 ImmutableList<BasePreset> presets = addPresetsForCreateBulkOfCreateInstances(bulkSize, names);
68 createBulkOfInstancesAndAssert(presets, false, bulkSize, JobStatus.COMPLETED, names);
71 @Test(dataProvider = "auditSources")
72 public void getAuditStatus_nonExistingJobId_returnsEmptyList(JobAuditStatus.SourceStatus source){
73 List<JobAuditStatus> audits = getAuditStatuses(UUID.randomUUID().toString(), source.name());
74 Assert.assertEquals(audits.size(),0);
77 @Test(expectedExceptions = HttpClientErrorException.class)
78 public void getAuditStatus_nonExistingSource_returnsError() {
80 getAuditStatuses(UUID.randomUUID().toString(), new RandomString(8).nextString());
81 } catch (HttpClientErrorException e) {
82 Assert.assertEquals(e.getResponseBodyAsString(),"The parameter source must have a value among : MSO, VID");
83 assertThat(e.getStatusCode(), is(HttpStatus.BAD_REQUEST));
89 public void simulateBulkRequest_getAuditStatus_auditStatusesReturnedAccordingSource() {
90 final int bulkSize = 2;
91 final List<String> jobIds = createBulkAndWaitForBeCompleted(bulkSize);
93 for(String jobId: jobIds) {
94 List<JobAuditStatus> actualVidAudits = getAuditStatuses(jobId, JobAuditStatus.SourceStatus.VID.name());
95 List<JobAuditStatus> expectedVidAudits = Stream.of(JobStatus.PENDING, JobStatus.IN_PROGRESS, JobStatus.COMPLETED)
96 .map(status-> vidAuditStatus(jobId, status.name(), status.equals(JobStatus.COMPLETED))).collect(toList());
97 assertThat(actualVidAudits, hasItemsFromCollection(expectedVidAudits));
99 List<JobAuditStatus> actualMsoAudits = getAuditStatuses(jobId, JobAuditStatus.SourceStatus.MSO.name());
100 List<JobAuditStatus> expectedMsoAudits = Stream.of("REQUESTED", "COMPLETE")
101 .map(status-> new JobAuditStatus(UUID.fromString(jobId),
103 JobAuditStatus.SourceStatus.MSO,
104 UUID.fromString("c0011670-0e1a-4b74-945d-8bf5aede1d9c"),
105 status.equals("COMPLETE") ? "Service Instance was created successfully." : null,
106 false)).collect(toList());
107 assertThat(actualMsoAudits, is(expectedMsoAudits));
112 @Test(expectedExceptions = HttpClientErrorException.class)
113 public void addBulkAndDeleteInProgress_deletionIsRejected(){
115 final Map<String, JobStatus> jobs = addBulkAllPendingButOneInProgress();
116 deleteOneJobHavingTheStatus(jobs, JobStatus.IN_PROGRESS);
117 } catch (HttpClientErrorException e) {
118 JsonAssert.assertJsonPartEquals(
119 "Service status does not allow deletion from the queue (Request id: null)",
120 e.getResponseBodyAsString(),
123 assertThat(e.getStatusCode(), is(HttpStatus.METHOD_NOT_ALLOWED));
130 public void addBulkAndDeletePending_deletedIsHiddenFromServiceInfoResults(){
131 Map<String, JobStatus> statusesBefore = addBulkAllPendingButOneInProgress();
133 final String deletedJob = deleteOneJobHavingTheStatus(statusesBefore, JobStatus.PENDING);
135 final Map<String, JobStatus> statusesNow = serviceListCall().getBody().stream()
136 .filter(si -> statusesBefore.keySet().contains(si.jobId))
137 .collect(toMap(si -> si.jobId, si -> si.jobStatus));
139 statusesBefore.remove(deletedJob);
140 assertThat("deleted job shall not be present in StatusInfo response", statusesNow, is(statusesBefore));
143 @Test(invocationCount = 3)
144 public void createBulkOfCreateInstancesWithSinglePreset_firstOneInProgressOtherArePending(){
145 final int bulkSize = 3;
146 Map<String, JobStatus> statusMap = addBulkAllPendingButOneInProgress(bulkSize);
147 Set<String> jobIds = statusMap.keySet();
149 final Map<JobStatus, List<ServiceInfo>> statuses = serviceListCall().getBody().stream()
150 .filter(si -> jobIds.contains(si.jobId))
151 .collect(groupingBy(ServiceInfo::getJobStatus));
153 // Variable "statuses" contains two lists by status:
154 // IN_PROGRESS: The ultimate first job - named with _001 - is always the only one in progress
155 // PENDING: The other two jobs - named with _002 and _003 - are the still pending
156 assertThat(jobIds, hasSize(bulkSize));
157 assertThat(statuses.get(JobStatus.IN_PROGRESS), hasSize(1));
159 assertThat(statuses.get(JobStatus.PENDING), hasSize(bulkSize - 1));
163 @Test(dataProvider = "trueAndFalse" )
164 public void whenServiceInBulkFailed_otherServicesAreStopped(Boolean isPresetForCreate){
165 Map<Keys, String> names = generateNames();
166 final int bulkSize = 3;
168 //if there is a preset for create, service shall failed during in_progress (upon get status)
169 //it there is no preset for create, service shall failed during pending (upon create request)
170 List<BasePreset> msoBulkPresets = isPresetForCreate ?
171 generateMsoCreateBulkPresets(bulkSize, names) :
173 ImmutableList<BasePreset> presets = new ImmutableList.Builder<BasePreset>()
174 .add(new PresetGetSessionSlotCheckIntervalGet())
175 .add(new PresetAAIGetSubscribersGet())
176 .add(PresetAAIGetCloudOwnersByCloudRegionId.PRESET_MTN3_TO_ATT_SABABA)
177 .addAll(msoBulkPresets)
178 .add(new PresetMSOOrchestrationRequestGet("FAILED"))
180 List<String> jobIds = createBulkOfMacroInstances(presets, false, bulkSize, names);
181 Assert.assertEquals(jobIds.size(),bulkSize);
182 boolean result = Wait.waitFor(x->{
183 List<ServiceInfo> serviceInfoList = serviceListCall().getBody();
184 Map<JobStatus, Long> statusCount = serviceInfoList.stream().filter(si->jobIds.contains(si.jobId)).collect(groupingBy(ServiceInfo::getJobStatus, counting()));
185 return Objects.equals(statusCount.get(JobStatus.FAILED), 1L) && Objects.equals(statusCount.get(JobStatus.STOPPED), 2L);
187 assertTrue(String.format("failed to get jobs [%s] to state of: 1 failed and 2 stopped ",
188 String.join(",", jobIds)),result);
192 public void createBulkOfAssignInstances(){
193 Map<Keys, String> names = generateNames();
194 final int bulkSize = 2;
195 ImmutableList<BasePreset> msoBulkPresets = IntStream.rangeClosed(0, bulkSize-1)
196 .mapToObj(i-> new PresetMSOAssignServiceInstanceGen2WithNames(names, i))
197 .collect(ImmutableList.toImmutableList());
198 ImmutableList<BasePreset> presets = new ImmutableList.Builder<BasePreset>()
199 .add(new PresetGetSessionSlotCheckIntervalGet())
200 .add(new PresetAAIGetSubscribersGet())
201 .add(PresetAAIGetCloudOwnersByCloudRegionId.PRESET_MTN3_TO_ATT_SABABA)
202 .addAll(msoBulkPresets)
203 .add(new PresetMSOOrchestrationRequestGet())
205 createBulkOfInstancesAndAssert(presets, true, bulkSize, JobStatus.COMPLETED, names);
209 public void tryToCreateBulkOfAssignInstancesErrorResponseFromMso(){
210 ImmutableList<BasePreset> presets = ImmutableList.of(
211 new PresetGetSessionSlotCheckIntervalGet(),
212 new PresetAAIGetSubscribersGet(),
213 new PresetMSOServiceInstanceGen2ErrorResponse(406));
215 List<String> jobIds = createBulkOfInstancesAndAssert(presets, true,1, JobStatus.FAILED, generateNames());
216 String jobId = jobIds.get(0);
217 List<JobAuditStatus> actualMsoAudits = getAuditStatuses(jobId, JobAuditStatus.SourceStatus.MSO.name());
218 JobAuditStatus expectedMsoAudit = new JobAuditStatus(UUID.fromString(jobId), "FAILED", JobAuditStatus.SourceStatus.MSO,
220 "Http Code:406, \"messageId\":\"SVC0002\",\"text\":\"JSON Object Mapping Request\"" ,
222 assertThat(actualMsoAudits.get(0), is(expectedMsoAudit));
226 public void whenGetLongErrorMessageFromMso_ThenAuditFirst2000Chars() {
227 Map<Keys, String> names = generateNames();
228 ImmutableList<BasePreset> presets = ImmutableList.of(
229 new PresetGetSessionSlotCheckIntervalGet(),
230 new PresetAAIGetSubscribersGet(),
231 new PresetMSOCreateServiceInstanceGen2WithNames(names, 0),
232 new PresetMSOOrchestrationRequestGet("FAILED", PresetMSOOrchestrationRequestGet.DEFAULT_REQUEST_ID, MSO_ERROR));
234 List<String> jobIds = createBulkOfInstancesAndAssert(presets, false, 1, JobStatus.FAILED, names);
235 String jobId = jobIds.get(0);
236 List<JobAuditStatus> actualMsoAudits = getAuditStatuses(jobId, JobAuditStatus.SourceStatus.MSO.name());
237 Optional<JobAuditStatus> jobAuditStatus = actualMsoAudits.stream().filter(x -> x.getJobStatus().equals("FAILED")).findFirst();
238 assertTrue(jobAuditStatus.isPresent());
239 assertThat(jobAuditStatus.get().getAdditionalInfo(), startsWith(MSO_BASE_ERROR));
240 assertThat(jobAuditStatus.get().getAdditionalInfo().length(), is(2000));
244 public void whenHideCompletedService_thenServiceNotReturnInServiceList(){
245 List<String> services = createBulkAndWaitForBeCompleted(2);
246 hideService(services.get(0));
247 List<String> serviceInfoList = serviceListCall().getBody().stream().map(ServiceInfo::getJobId).collect(toList());
248 assertThat(serviceInfoList, hasItem(services.get(1)));
249 assertThat(serviceInfoList, not(hasItem(services.get(0))));
253 public void errorResponseInGetStatusFromMso_getAuditStatusFromMso_errorMsgExistInAdditionalInfo(){
254 Map<Keys, String> names = generateNames();
255 ImmutableList<BasePreset> presets = new ImmutableList.Builder<BasePreset>()
256 .add(new PresetGetSessionSlotCheckIntervalGet())
257 .add(new PresetAAIGetSubscribersGet())
258 .add(PresetAAIGetCloudOwnersByCloudRegionId.PRESET_MTN3_TO_ATT_SABABA)
259 .add(new PresetMSOAssignServiceInstanceGen2WithNames(names, 0))
260 .add(new PresetMSOOrchestrationRequestGetErrorResponse(406))
263 final List<String> jobIds = createBulkOfInstancesAndAssert(presets, true,1, JobStatus.IN_PROGRESS, names);
264 String jobId = jobIds.get(0);
265 Wait.waitFor(y-> getAuditStatuses(jobId, JobAuditStatus.SourceStatus.MSO.name()).stream()
266 .anyMatch(si -> si.getJobStatus().equals("FAILED")),
268 List<JobAuditStatus> actualMsoAudits = getAuditStatuses(jobId, JobAuditStatus.SourceStatus.MSO.name());
269 List<JobAuditStatus> expectedMsoAudits = Stream.of("REQUESTED", "FAILED")
270 .map(status -> new JobAuditStatus(UUID.fromString(jobId),
272 JobAuditStatus.SourceStatus.MSO,
273 UUID.fromString("c0011670-0e1a-4b74-945d-8bf5aede1d9c"),
274 status.equals("FAILED") ? "Http Code:406, \"messageId\":\"SVC0002\",\"text\":\"JSON Object Mapping Request\"" : null,
275 false)).collect(toList());
276 assertThat(actualMsoAudits, is(expectedMsoAudits));
281 public void inProgressJobMoreThan24HoursIsFailedInVidAudit(){
282 addBulkPendingWithCustomList(Collections.singletonList(new PresetMSOOrchestrationRequestGet("IN_PROGRESS",24)));
284 AtomicReference<ServiceInfo> inProgressJob = new AtomicReference<>();
285 boolean isJobFound = Wait.waitFor(x->{
286 List<ServiceInfo> serviceInfoList = serviceListCall().getBody();
287 inProgressJob.set(serviceInfoList.stream().
288 filter(serviceInfo -> serviceInfo.serviceInstanceId.equals(PresetMSOOrchestrationRequestGet.DEFAULT_SERVICE_INSTANCE_ID) && serviceInfo.jobStatus.equals(JobStatus.FAILED))
291 return inProgressJob.get() != null;
294 org.junit.Assert.assertTrue("Job with DEFAULT_SERVICE_INSTANCE_ID and status FAILED should present", isJobFound);
296 verifyAuditStatuses(inProgressJob.get().jobId, Arrays.asList(JobStatus.PENDING.name(), JobStatus.IN_PROGRESS.name(),JobStatus.FAILED.name()), JobAuditStatus.SourceStatus.VID);
297 verifyAuditStatuses(inProgressJob.get().jobId, Arrays.asList("REQUESTED", "IN_PROGRESS"), JobAuditStatus.SourceStatus.MSO);
301 public void inProgressJobLessThan24HoursIsStillInProgressInVidAudit(){
302 addBulkPendingWithCustomList(Collections.singletonList(new PresetMSOOrchestrationRequestGet("IN_PROGRESS",23)));
304 AtomicReference<ServiceInfo> inProgressJob = new AtomicReference<>();
305 boolean isJobFound = Wait.waitFor(x->{
306 List<ServiceInfo> serviceInfoList = serviceListCall().getBody();
307 inProgressJob.set(serviceInfoList.stream().filter(serviceInfo -> serviceInfo.serviceInstanceId.equals(PresetMSOOrchestrationRequestGet.DEFAULT_SERVICE_INSTANCE_ID))
310 return inProgressJob.get() != null;
313 org.junit.Assert.assertTrue("Job with DEFAULT_SERVICE_INSTANCE_ID should present", isJobFound);
314 org.junit.Assert.assertEquals("Tested job status is not as expected", JobStatus.IN_PROGRESS, inProgressJob.get().getJobStatus());
316 verifyAuditStatuses(inProgressJob.get().jobId, Arrays.asList(JobStatus.PENDING.name(), JobStatus.IN_PROGRESS.name()), JobAuditStatus.SourceStatus.VID);
317 verifyAuditStatuses(inProgressJob.get().jobId, Arrays.asList("REQUESTED", "IN_PROGRESS"), JobAuditStatus.SourceStatus.MSO);
321 public void verifyAuditStatusOfInstanceGroupId(){
322 SimulatorApi.registerExpectationFromPreset(new PresetMSOOrchestrationRequestsManyInstanceStatusesGet(INSTANCE_GROUP_ID_LABEL, INSTANCE_GROUP_LABEL), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
323 final List<JobAuditStatus> expectedAuditStatusList = getAuditStatusesForInstance("VNFGROUP", "df305d54-75b4-431b-adb2-eb6b9e5460df");
324 verifyInstanceAuditStatuses(Arrays.asList(
325 new JobAuditStatus("groupTestName", "IN_PROGRESS", UUID.fromString("28502bd2-3aff-4a03-9f2b-5a0d1cb1ca24") , INSTANCE_GROUP_LABEL+" instance creation", null, INSTANCE_GROUP_LABEL),
326 new JobAuditStatus("groupTestName", "COMPLETE",UUID.fromString("28502bd2-3aff-4a03-9f2b-5a0d1cb1ca24") , INSTANCE_GROUP_LABEL+" instance creation", null, INSTANCE_GROUP_LABEL),
327 new JobAuditStatus("groupTestName", "IN_PROGRESS", UUID.fromString("f711f0ff-24b6-4d7f-9314-4b4eae15f48c") , INSTANCE_GROUP_LABEL+" instance deletion", null, INSTANCE_GROUP_LABEL),
328 new JobAuditStatus("groupTestName", "COMPLETE",UUID.fromString("f711f0ff-24b6-4d7f-9314-4b4eae15f48c") , INSTANCE_GROUP_LABEL+" instance deletion", null, INSTANCE_GROUP_LABEL)),
329 expectedAuditStatusList);
332 @Test(expectedExceptions = HttpClientErrorException.class)
333 public void verifyAuditStatusOfInstanceGroupId_notExistingVidType(){
335 getAuditStatusesForInstance("KUKU", "df305d54-75b4-431b-adb2-eb6b9e5460df");
336 } catch (HttpClientErrorException e){ //to verify the properiatary statusCode field
337 assertThat("Code is not as expected", HttpStatus.BAD_REQUEST.equals(e.getStatusCode()));
342 @Test(expectedExceptions = HttpServerErrorException.class)
343 public void verifyAuditStatusOfInstanceGroupId_notExistingMsoInstanceId(){
345 getAuditStatusesForInstance("VNFGROUP", "df305d54-75b4-431b-adb2-eb6b9e5460aa");
346 } catch (HttpServerErrorException e){ //to verify the properiatary statusCode field
347 assertThat("Code is not as expected", HttpStatus.INTERNAL_SERVER_ERROR.equals(e.getStatusCode()));
353 public static Object[][] macroAndALaCarteBulk(){
354 return new Object[][]{{CREATE_BULK_OF_MACRO_REQUEST}, {CREATE_BULK_OF_ALACARTE_REQUEST_WITH_VNF}};
357 @Test(dataProvider = "macroAndALaCarteBulk", expectedExceptions = HttpClientErrorException.class)
358 @FeatureTogglingTest(FLAG_1906_INSTANTIATION_API_USER_VALIDATION)
359 public void verifyCreateBulkOfInstancesUserPermissionValidation(String requestDetailsFileName) {
360 login(new UserCredentials("em1536000", "em1536000", null, null, null));
362 createBulkOfInstances(false, 1, Collections.EMPTY_MAP, requestDetailsFileName);
363 } catch (HttpClientErrorException e){
364 assertEquals("Code is not as expected", HttpStatus.FORBIDDEN.value(), e.getStatusCode().value());
374 public void deleteMacroService_getAuditStatus_verifyCompleted() {
375 String deleteServiceRequestId = UUID.randomUUID().toString();
376 String serviceInstanceId = "service-instance-id";
377 registerExpectationFromPresets(ImmutableList.of(
378 new PresetAAIGetSubscribersGet(),
379 new PresetMSODeleteMacroService(deleteServiceRequestId, serviceInstanceId),
380 new PresetMSOOrchestrationRequestGet(COMPLETE, deleteServiceRequestId)
381 ), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
383 List<String> uuids = createBulkOfInstances(false, 1, ImmutableMap.of(), DELETE_MACRO_SERVICE_FILE_NAME);
384 assertThat(uuids, IsCollectionWithSize.hasSize(1));
385 String jobId = uuids.get(0);
387 assertExpectedStatusAndServiceInfo(JobStatus.COMPLETED, jobId, PATIENCE_LEVEL.FAIL_FAST, new ServiceInfo(
388 "us16807000", JobStatus.COMPLETED, false,
389 "d61e6f2d-12fa-4cc2-91df-7c244011d6fc", "WayneHolland", "WATKINS",
390 "JAG1", "YUDFJULP-JAG1",
391 "092eb9e8e4b7412e8787dd091bc58e86", "USP-SIP-IC-24335-T-01",
393 "service-instance-type", null,
394 null, "InstanceName",
395 "f028b2e2-7080-4b13-91b2-94944d4c42d8", "Service with VRF", "5.0",
396 jobId, null, ServiceAction.DELETE, false)