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