Introduce FeatureManager to ResourceCommand
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / InstanceGroupCommandTest.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 java.util.function.Function.identity;
24 import static java.util.stream.Collectors.toMap;
25 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.eq;
28 import static org.mockito.Mockito.only;
29 import static org.mockito.Mockito.same;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import com.google.common.collect.ImmutableMap;
34 import java.util.Optional;
35 import java.util.Set;
36 import org.apache.commons.beanutils.BeanUtils;
37 import org.mockito.Answers;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.vid.job.JobAdapter;
42 import org.onap.vid.job.JobsBrokerService;
43 import org.onap.vid.job.impl.JobSharedData;
44 import org.onap.vid.model.Action;
45 import org.onap.vid.model.RequestReferencesContainer;
46 import org.onap.vid.model.serviceInstantiation.InstanceGroup;
47 import org.onap.vid.mso.RestMsoImplementation;
48 import org.onap.vid.mso.model.ModelInfo;
49 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
50 import org.springframework.http.HttpMethod;
51 import org.testng.annotations.BeforeMethod;
52 import org.testng.annotations.DataProvider;
53 import org.testng.annotations.Test;
54 import org.togglz.core.manager.FeatureManager;
55
56 public class InstanceGroupCommandTest {
57
58     @Mock(answer = Answers.RETURNS_MOCKS)
59     RestMsoImplementation restMso;
60
61     @Mock InstanceGroup instanceGroupRequest;
62
63     @Mock(answer = Answers.RETURNS_MOCKS)
64     MsoResultHandlerService msoResultHandlerService;
65
66     @Mock(answer = Answers.RETURNS_MOCKS)
67     MsoRequestBuilder msoRequestBuilder;
68
69     @Mock WatchChildrenJobsBL watchChildrenJobsBL;
70
71     @Mock(answer = Answers.RETURNS_MOCKS)
72     AsyncInstantiationBusinessLogic asyncInstantiationBL;
73
74     @Mock(answer = Answers.RETURNS_MOCKS)
75     JobsBrokerService jobsBrokerService;
76
77     @Mock(answer = Answers.RETURNS_MOCKS)
78     JobAdapter jobAdapter;
79
80     @Mock InProgressStatusService inProgressStatusService;
81
82     @Mock FeatureManager featureManager;
83
84     @InjectMocks
85     private InstanceGroupCommand command;
86
87     @BeforeMethod
88     public void initMocks() {
89         command = null;
90         MockitoAnnotations.initMocks(this);
91     }
92     @DataProvider
93     public static Object[][] testApis() {
94         return new Object[][]{
95                 {"VNF_API"}, {null}};
96     }
97     @Test(dataProvider = "testApis")
98     public void createMyself_callsMso(String testApi) {
99         final ModelInfo serviceModelInfo = setRandomStrings(new ModelInfo());
100         final String serviceInstanceId = "service-instance-id";
101         final String userId = "ff3223";
102
103         when(instanceGroupRequest.getAction()).thenReturn(Action.Delete);
104
105         JobSharedData sharedData = new JobSharedData(
106                 null, userId, instanceGroupRequest, testApi);
107         command.init(sharedData, ImmutableMap.of(
108                 "resourceModelInfos", ImmutableMap.of("SERVICE_MODEL_INFO", serviceModelInfo),
109                 "resourceInstancesIds", ImmutableMap.of("SERVICE_INSTANCE_ID", serviceInstanceId)
110         ));
111
112         command.createMyself();
113
114         verify(msoRequestBuilder).generateInstanceGroupInstantiationRequest(
115                 same(instanceGroupRequest), eq(serviceModelInfo), eq(serviceInstanceId), eq(userId), eq(testApi));
116         verify(restMso, only()).restCall(eq(HttpMethod.POST), eq(RequestReferencesContainer.class), any(), any(), eq(Optional.empty()));
117     }
118
119     private ModelInfo setRandomStrings(ModelInfo object) {
120         try {
121             Set<String> fields = BeanUtils.describe(object).keySet();
122             BeanUtils.populate(object,
123                     fields.stream().collect(toMap(identity(), s -> randomAlphanumeric(4))));
124             return object;
125         } catch (Exception e) {
126             throw new RuntimeException(e);
127         }
128     }
129 }