Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / MacroServiceCommandTest.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.job.command;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 import static org.onap.vid.model.VidNotions.ModelCategory.INFRASTRUCTURE_VPN;
28 import static org.onap.vid.model.VidNotions.ModelCategory.IS_5G_FABRIC_CONFIGURATION_MODEL;
29 import static org.onap.vid.model.VidNotions.ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL;
30 import static org.onap.vid.model.VidNotions.ModelCategory.OTHER;
31 import static org.onap.vid.model.VidNotions.ModelCategory.SERVICE_WITH_COLLECTION_RESOURCE;
32 import static org.onap.vid.model.VidNotions.ModelCategory.Transport;
33 import static org.testng.AssertJUnit.assertEquals;
34
35 import com.google.common.collect.ImmutableList;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.UUID;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.vid.job.Job;
43 import org.onap.vid.job.JobAdapter;
44 import org.onap.vid.job.JobsBrokerService;
45 import org.onap.vid.job.impl.JobSharedData;
46 import org.onap.vid.model.Action;
47 import org.onap.vid.model.VidNotions;
48 import org.onap.vid.model.serviceInstantiation.BaseResource;
49 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
50 import org.onap.vid.mso.RestMsoImplementation;
51 import org.onap.vid.mso.rest.AsyncRequestStatus;
52 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
53 import org.onap.vid.services.AuditService;
54 import org.testng.annotations.BeforeClass;
55 import org.testng.annotations.DataProvider;
56 import org.testng.annotations.Test;
57
58 public class MacroServiceCommandTest {
59
60     @Mock
61     private InProgressStatusService inProgressStatusService;
62
63     @Mock
64     private WatchChildrenJobsBL watchChildrenJobsB;
65
66     @Mock
67     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
68
69     @Mock
70     private JobsBrokerService jobsBrokerService;
71
72     @Mock
73     private MsoRequestBuilder msoRequestBuilder;
74
75     @Mock
76     private MsoResultHandlerService msoResultHandlerService;
77
78     @Mock
79     private JobAdapter jobAdapter;
80
81     @Mock
82     private RestMsoImplementation restMso;
83
84     @Mock
85     private AuditService auditService;
86
87     @InjectMocks
88     private MacroServiceCommand macroServiceCommand;
89
90     @DataProvider
91     public static Object[][] modelCategoryPre1806DataProvider() {
92         return new Object[][]{
93                 {IS_5G_PROVIDER_NETWORK_MODEL, false},
94                 {IS_5G_FABRIC_CONFIGURATION_MODEL, false},
95                 {Transport, true},
96                 {SERVICE_WITH_COLLECTION_RESOURCE, true},
97                 {INFRASTRUCTURE_VPN, true},
98                 {OTHER, false},
99         };
100     }
101
102     @BeforeClass
103     public void initMocks() {
104         MockitoAnnotations.initMocks(this);
105     }
106
107     @Test(dataProvider = "modelCategoryPre1806DataProvider")
108     public void testShouldUsePre1806Request(VidNotions.ModelCategory modelCategory, boolean expectedResult) {
109         ServiceInstantiation serviceInstantiation = mock(ServiceInstantiation.class);
110         VidNotions vidNotions = mock(VidNotions.class);
111         when(serviceInstantiation.getVidNotions()).thenReturn(vidNotions);
112         when(vidNotions.getModelCategory()).thenReturn(modelCategory);
113         assertEquals(macroServiceCommand.shouldUsePre1806Request(serviceInstantiation), expectedResult);
114     }
115
116     @DataProvider
117     public static Object[][] MsoFilteredRequestsDataProvider() {
118         return new Object[][]{
119                 {Collections.EMPTY_LIST},
120                 {ImmutableList.of(new AsyncRequestStatus.Request())}
121         };
122     }
123
124     @Test(dataProvider = "MsoFilteredRequestsDataProvider")
125     public void givenResumeAction_whenCantRetrieveRequestIdFromMSO_thenJobIsFailed(List<AsyncRequestStatus.Request> requests) {
126         String instanceId = UUID.randomUUID().toString();
127         BaseResource baseResource = mock(BaseResource.class);
128         when(baseResource.getInstanceId()).thenReturn(instanceId);
129         when(baseResource.getAction()).thenReturn(Action.Resume);
130         macroServiceCommand.init(new JobSharedData(null, null, baseResource, null));
131         when(auditService.retrieveRequestsFromMsoByServiceIdAndRequestTypeAndScope(eq(instanceId), any(), any()))
132                 .thenReturn(requests);
133         assertEquals(macroServiceCommand.resumeMyself(), Job.JobStatus.FAILED);
134     }
135
136 }