aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/dal/AsyncInstantiationRepositoryTest.java
blob: 27be3fb5004798c163ddcdfb3b8cc7f2b2ad043a (plain)
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
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.dal;

import static java.util.stream.Collectors.toList;
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.onap.vid.job.Job.JobStatus.COMPLETED;

import com.google.common.collect.ImmutableList;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.inject.Inject;
import org.onap.portalsdk.core.service.DataAccessService;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.vid.config.DataSourceConfig;
import org.onap.vid.config.MockedAaiClientAndFeatureManagerConfig;
import org.onap.vid.job.Job;
import org.onap.vid.model.ResourceInfo;
import org.onap.vid.model.ServiceInfo;
import org.onap.vid.model.ServiceInfo.ServiceAction;
import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
import org.onap.vid.mso.rest.AsyncRequestStatus;
import org.onap.vid.mso.rest.RequestStatus;
import org.onap.vid.services.AsyncInstantiationBaseTest;
import org.onap.vid.utils.TimeUtils;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

@ContextConfiguration(classes = {DataSourceConfig.class, SystemProperties.class, MockedAaiClientAndFeatureManagerConfig.class})
public class AsyncInstantiationRepositoryTest extends AsyncInstantiationBaseTest {

    @Inject
    private DataAccessService dataAccessService;
    private AsyncInstantiationRepository asyncInstantiationRepository;

    @BeforeClass
    void initServicesInfoService() {
        asyncInstantiationRepository = new AsyncInstantiationRepository(dataAccessService);
        createInstanceParamsMaps();
        createNewTestServicesInfoWithServiceModelID();
    }

    private void createNewTestServicesInfoWithServiceModelID() {

        LocalDateTime NOW = LocalDateTime.now();

        addNewServiceInfoWithAction(UUID.randomUUID(), "abc", "0", NOW.minusYears(1L), NOW, COMPLETED, false, false,
            MODEL_UUID, ServiceAction.RESUME);
        addNewServiceInfoWithAction(UUID.randomUUID(), "abc", "1", NOW.minusYears(1L), NOW, COMPLETED, false, false,
            MODEL_UUID, ServiceAction.INSTANTIATE);
        addNewServiceInfoWithAction(UUID.randomUUID(), "abc", "2", NOW, NOW, COMPLETED, false, false,
            MODEL_UUID_2, ServiceAction.INSTANTIATE);
        addNewServiceInfoWithAction(UUID.randomUUID(), "abc", "3", NOW, NOW, COMPLETED, false, false,
            MODEL_UUID, ServiceAction.INSTANTIATE);
        addNewServiceInfoWithAction(UUID.randomUUID(), "abc", "hidden", NOW, NOW, COMPLETED, true, false,
            MODEL_UUID, ServiceAction.INSTANTIATE);
    }

    @DataProvider
    public static Object[][] listServicesByServiceModelIdDataProvider() {
        return new Object[][]{
            { "services info filtered by MODEL_UUID not hidden , ordered by newer first", MODEL_UUID, "3", "1" },
            { "services info filtered by MODEL_UUID2", MODEL_UUID_2, "2" },
        };
    }

    @Test(dataProvider = "listServicesByServiceModelIdDataProvider")
    public void testListServicesByServiceModelId(String desc, String modelUUID, String... expectedResult) {
        List<ServiceInfo> serviceInfoListResult = asyncInstantiationRepository.
            listInstantiatedServicesByServiceModelId(UUID.fromString(modelUUID));
        assertThat(desc, serviceInfoListResult.stream().map(ServiceInfo::getServiceInstanceName).collect(toList()),
            contains(expectedResult));
    }

    @Test
    public void whenFilterServiceByNotExistUUID_emptyListIsReturned() {
        List<ServiceInfo> serviceInfoListResult = asyncInstantiationRepository.listInstantiatedServicesByServiceModelId(UUID.randomUUID());
        assertThat(serviceInfoListResult, is(empty()));
    }

    @Test
    public void whenSaveNewRequest_thenRequestIsRetrieved() {
        ServiceInstantiation serviceInstantiation = generateALaCarteWithVnfsServiceInstantiationPayload();
        UUID jobUuid = UUID.randomUUID();
        asyncInstantiationRepository.addJobRequest(jobUuid, serviceInstantiation);
        ServiceInstantiation stored = asyncInstantiationRepository.getJobRequest(jobUuid);
        assertThat(stored, jsonEquals(serviceInstantiation).when(IGNORING_ARRAY_ORDER));
    }

    private AsyncRequestStatus createAsyncRequestStatus(String message, String requestState){
        RequestStatus requestStatus = new RequestStatus(requestState, message, TimeUtils.zonedDateTimeToString(ZonedDateTime.now()));
        AsyncRequestStatus.Request request = new AsyncRequestStatus.Request(requestStatus);
        return new AsyncRequestStatus(request);
    }

    @Test
    public void getResourceInfoByRootJobId_returnsMapOfjobIdResources(){
        UUID jobId1= UUID.randomUUID();
        UUID jobId2= UUID.randomUUID();
        AsyncRequestStatus errorMessage= createAsyncRequestStatus("MSO failed resource", "FAILED");
        List<ResourceInfo> requestInfoList= ImmutableList.of(
                new ResourceInfo("aaaaaa",jobId1, "64f3123a-f9a8-4591-b481-d662134bcb52", Job.JobStatus.COMPLETED, null),
                new ResourceInfo("bbbbbb",jobId1, "65f3123a-f9a8-4591-b481-kodj9ig87gdu", Job.JobStatus.COMPLETED_WITH_ERRORS, null),
                new ResourceInfo("dddddd",jobId1, null, Job.JobStatus.FAILED, null),
                new ResourceInfo("cccccc",jobId1, null, Job.JobStatus.FAILED, errorMessage),
                new ResourceInfo("eeeeee",jobId2, null, Job.JobStatus.FAILED, null),
                new ResourceInfo("ffffff",jobId2, "66f3123a-f9a8-4591-b481-ghfgh6767567", Job.JobStatus.COMPLETED, null)
        );
        for(ResourceInfo info: requestInfoList){
            asyncInstantiationRepository.saveResourceInfo(info);
        }
        Map<String, ResourceInfo> storedByTrackId = asyncInstantiationRepository.getResourceInfoByRootJobId(jobId1);
        assertThat(storedByTrackId.values(), hasSize(4));
        assertThat(storedByTrackId.get("aaaaaa").getInstanceId(), equalTo("64f3123a-f9a8-4591-b481-d662134bcb52"));
        assertThat(storedByTrackId.get("cccccc").getErrorMessage().request.requestStatus.getStatusMessage(), equalTo("MSO failed resource"));
        assertThat(storedByTrackId.get("cccccc").getErrorMessage().request.requestStatus.getRequestState(), equalTo("FAILED"));
        assertThat(storedByTrackId.get("dddddd").getErrorMessage(), equalTo(null));
        assertThat(storedByTrackId.values(),
            jsonEquals(requestInfoList.stream().filter(i -> i.getRootJobId().equals(jobId1)).collect(
                toList())).when(IGNORING_ARRAY_ORDER));
    }
}