Introduce FeatureManager to ResourceCommand
[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 import org.togglz.core.manager.FeatureManager;
58
59 public class MacroServiceCommandTest {
60
61     @Mock
62     private InProgressStatusService inProgressStatusService;
63
64     @Mock
65     private WatchChildrenJobsBL watchChildrenJobsB;
66
67     @Mock
68     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
69
70     @Mock
71     private JobsBrokerService jobsBrokerService;
72
73     @Mock
74     private MsoRequestBuilder msoRequestBuilder;
75
76     @Mock
77     private MsoResultHandlerService msoResultHandlerService;
78
79     @Mock
80     private JobAdapter jobAdapter;
81
82     @Mock
83     private RestMsoImplementation restMso;
84
85     @Mock
86     private AuditService auditService;
87
88     @Mock
89     FeatureManager featureManager;
90
91     @InjectMocks
92     private MacroServiceCommand macroServiceCommand;
93
94     @DataProvider
95     public static Object[][] modelCategoryPre1806DataProvider() {
96         return new Object[][]{
97                 {IS_5G_PROVIDER_NETWORK_MODEL, false},
98                 {IS_5G_FABRIC_CONFIGURATION_MODEL, false},
99                 {Transport, true},
100                 {SERVICE_WITH_COLLECTION_RESOURCE, true},
101                 {INFRASTRUCTURE_VPN, true},
102                 {OTHER, false},
103         };
104     }
105
106     @BeforeClass
107     public void initMocks() {
108         MockitoAnnotations.initMocks(this);
109     }
110
111     @Test(dataProvider = "modelCategoryPre1806DataProvider")
112     public void testShouldUsePre1806Request(VidNotions.ModelCategory modelCategory, boolean expectedResult) {
113         ServiceInstantiation serviceInstantiation = mock(ServiceInstantiation.class);
114         VidNotions vidNotions = mock(VidNotions.class);
115         when(serviceInstantiation.getVidNotions()).thenReturn(vidNotions);
116         when(vidNotions.getModelCategory()).thenReturn(modelCategory);
117         assertEquals(macroServiceCommand.shouldUsePre1806Request(serviceInstantiation), expectedResult);
118     }
119
120     @DataProvider
121     public static Object[][] MsoFilteredRequestsDataProvider() {
122         return new Object[][]{
123                 {Collections.EMPTY_LIST},
124                 {ImmutableList.of(new AsyncRequestStatus.Request())}
125         };
126     }
127
128     @Test(dataProvider = "MsoFilteredRequestsDataProvider")
129     public void givenResumeAction_whenCantRetrieveRequestIdFromMSO_thenJobIsFailed(List<AsyncRequestStatus.Request> requests) {
130         String instanceId = UUID.randomUUID().toString();
131         BaseResource baseResource = mock(BaseResource.class);
132         when(baseResource.getInstanceId()).thenReturn(instanceId);
133         when(baseResource.getAction()).thenReturn(Action.Resume);
134         macroServiceCommand.init(new JobSharedData(null, null, baseResource, null));
135         when(auditService.retrieveRequestsFromMsoByServiceIdAndRequestTypeAndScope(eq(instanceId), any(), any()))
136                 .thenReturn(requests);
137         assertEquals(macroServiceCommand.resumeMyself(), Job.JobStatus.FAILED);
138     }
139
140 }