Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / test / java / org / onap / vid / dal / AsyncInstantiationRepositoryTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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  */
20
21 package org.onap.vid.dal;
22
23 import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
24 import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
27 import static org.hamcrest.core.IsEqual.equalTo;
28
29 import com.google.common.collect.ImmutableList;
30 import java.time.ZonedDateTime;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.UUID;
34 import java.util.stream.Collectors;
35 import javax.inject.Inject;
36 import org.onap.portalsdk.core.service.DataAccessService;
37 import org.onap.portalsdk.core.util.SystemProperties;
38 import org.onap.vid.config.DataSourceConfig;
39 import org.onap.vid.config.MockedAaiClientAndFeatureManagerConfig;
40 import org.onap.vid.job.Job;
41 import org.onap.vid.model.ResourceInfo;
42 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
43 import org.onap.vid.mso.rest.AsyncRequestStatus;
44 import org.onap.vid.mso.rest.RequestStatus;
45 import org.onap.vid.services.AsyncInstantiationBaseTest;
46 import org.onap.vid.utils.TimeUtils;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.testng.annotations.BeforeClass;
49 import org.testng.annotations.Test;
50
51 @ContextConfiguration(classes = {DataSourceConfig.class, SystemProperties.class, MockedAaiClientAndFeatureManagerConfig.class})
52 public class AsyncInstantiationRepositoryTest extends AsyncInstantiationBaseTest {
53
54     @Inject
55     private DataAccessService dataAccessService;
56
57     @BeforeClass
58     void initServicesInfoService() {
59         createInstanceParamsMaps();
60     }
61
62     @Test
63     public void whenSaveNewRequest_thenRequestIsRetrieved() {
64         AsyncInstantiationRepository underTest = new AsyncInstantiationRepository(dataAccessService);
65         ServiceInstantiation serviceInstantiation = generateALaCarteWithVnfsServiceInstantiationPayload();
66         UUID jobUuid = UUID.randomUUID();
67         underTest.addJobRequest(jobUuid, serviceInstantiation);
68         ServiceInstantiation stored = underTest.getJobRequest(jobUuid);
69         assertThat(stored, jsonEquals(serviceInstantiation).when(IGNORING_ARRAY_ORDER));
70     }
71
72     private AsyncRequestStatus createAsyncRequestStatus(String message, String requestState){
73         RequestStatus requestStatus = new RequestStatus(requestState, message, TimeUtils.zonedDateTimeToString(ZonedDateTime.now()));
74         AsyncRequestStatus.Request request = new AsyncRequestStatus.Request(requestStatus);
75         return new AsyncRequestStatus(request);
76     }
77
78     @Test
79     public void getResourceInfoByRootJobId_returnsMapOfjobIdResources(){
80         AsyncInstantiationRepository underTest = new AsyncInstantiationRepository(dataAccessService);
81         UUID jobId1= UUID.randomUUID();
82         UUID jobId2= UUID.randomUUID();
83         AsyncRequestStatus errorMessage= createAsyncRequestStatus("MSO failed resource", "FAILED");
84         List<ResourceInfo> requestInfoList= ImmutableList.of(
85                 new ResourceInfo("aaaaaa",jobId1, "64f3123a-f9a8-4591-b481-d662134bcb52", Job.JobStatus.COMPLETED, null),
86                 new ResourceInfo("bbbbbb",jobId1, "65f3123a-f9a8-4591-b481-kodj9ig87gdu", Job.JobStatus.COMPLETED_WITH_ERRORS, null),
87                 new ResourceInfo("dddddd",jobId1, null, Job.JobStatus.FAILED, null),
88                 new ResourceInfo("cccccc",jobId1, null, Job.JobStatus.FAILED, errorMessage),
89                 new ResourceInfo("eeeeee",jobId2, null, Job.JobStatus.FAILED, null),
90                 new ResourceInfo("ffffff",jobId2, "66f3123a-f9a8-4591-b481-ghfgh6767567", Job.JobStatus.COMPLETED, null)
91         );
92         for(ResourceInfo info: requestInfoList){
93             underTest.saveResourceInfo(info);
94         }
95         Map<String, ResourceInfo> storedByTrackId = underTest.getResourceInfoByRootJobId(jobId1);
96         assertThat(storedByTrackId.values(), hasSize(4));
97         assertThat(storedByTrackId.get("aaaaaa").getInstanceId(), equalTo("64f3123a-f9a8-4591-b481-d662134bcb52"));
98         assertThat(storedByTrackId.get("cccccc").getErrorMessage().request.requestStatus.getStatusMessage(), equalTo("MSO failed resource"));
99         assertThat(storedByTrackId.get("cccccc").getErrorMessage().request.requestStatus.getRequestState(), equalTo("FAILED"));
100         assertThat(storedByTrackId.get("dddddd").getErrorMessage(), equalTo(null));
101         assertThat(storedByTrackId.values(),  jsonEquals(requestInfoList.stream().filter(i-> i.getRootJobId().equals(jobId1)).collect(Collectors.toList())).when(IGNORING_ARRAY_ORDER));
102     }
103 }