2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.job.command;
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;
33 import com.google.common.collect.ImmutableMap;
34 import java.util.Optional;
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;
56 public class InstanceGroupCommandTest {
58 @Mock(answer = Answers.RETURNS_MOCKS)
59 RestMsoImplementation restMso;
61 @Mock InstanceGroup instanceGroupRequest;
63 @Mock(answer = Answers.RETURNS_MOCKS)
64 MsoResultHandlerService msoResultHandlerService;
66 @Mock(answer = Answers.RETURNS_MOCKS)
67 MsoRequestBuilder msoRequestBuilder;
69 @Mock WatchChildrenJobsBL watchChildrenJobsBL;
71 @Mock(answer = Answers.RETURNS_MOCKS)
72 AsyncInstantiationBusinessLogic asyncInstantiationBL;
74 @Mock(answer = Answers.RETURNS_MOCKS)
75 JobsBrokerService jobsBrokerService;
77 @Mock(answer = Answers.RETURNS_MOCKS)
78 JobAdapter jobAdapter;
80 @Mock InProgressStatusService inProgressStatusService;
82 @Mock FeatureManager featureManager;
85 private InstanceGroupCommand command;
88 public void initMocks() {
90 MockitoAnnotations.initMocks(this);
93 public static Object[][] testApis() {
94 return new Object[][]{
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";
103 when(instanceGroupRequest.getAction()).thenReturn(Action.Delete);
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)
112 command.createMyself();
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()));
119 private ModelInfo setRandomStrings(ModelInfo object) {
121 Set<String> fields = BeanUtils.describe(object).keySet();
122 BeanUtils.populate(object,
123 fields.stream().collect(toMap(identity(), s -> randomAlphanumeric(4))));
125 } catch (Exception e) {
126 throw new RuntimeException(e);