Update license headers
[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.impl.JobSharedData;
30 import org.onap.vid.model.RequestReferencesContainer;
31 import org.onap.vid.model.serviceInstantiation.InstanceGroup;
32 import org.onap.vid.mso.RestMsoImplementation;
33 import org.onap.vid.mso.model.ModelInfo;
34 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
35 import org.springframework.http.HttpMethod;
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.Test;
38
39 import java.util.Optional;
40 import java.util.Set;
41
42 import static java.util.function.Function.identity;
43 import static java.util.stream.Collectors.toMap;
44 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
45 import static org.mockito.ArgumentMatchers.any;
46 import static org.mockito.Mockito.*;
47
48 public class InstanceGroupCommandTest {
49
50     @Mock(answer = Answers.RETURNS_MOCKS)
51     RestMsoImplementation restMso;
52
53     @Mock InstanceGroup instanceGroupRequest;
54
55     @Mock(answer = Answers.RETURNS_MOCKS)
56     MsoResultHandlerService msoResultHandlerService;
57
58     @Mock WatchChildrenJobsBL watchChildrenJobsBL;
59
60     @Mock(answer = Answers.RETURNS_MOCKS)
61     AsyncInstantiationBusinessLogic asyncInstantiationBL;
62
63     @Mock InProgressStatusService inProgressStatusService;
64
65     @InjectMocks
66     private InstanceGroupCommand command;
67
68     @BeforeMethod
69     public void initMocks() {
70         command = null;
71         MockitoAnnotations.initMocks(this);
72     }
73
74     @Test
75     public void createMyself_callsMso() {
76         final ModelInfo serviceModelInfo = setRandomStrings(new ModelInfo());
77         final String serviceInstanceId = "service-instance-id";
78         final String userId = "ff3223";
79
80         command.init(new JobSharedData(
81                 null, userId, instanceGroupRequest
82         ), ImmutableMap.of(
83                 "resourceModelInfos", ImmutableMap.of("SERVICE_MODEL_INFO", serviceModelInfo),
84                 "resourceInstancesIds", ImmutableMap.of("SERVICE_INSTANCE_ID", serviceInstanceId)
85         ));
86
87         command.createMyself();
88
89         verify(asyncInstantiationBL).generateInstanceGroupInstantiationRequest(
90                 same(instanceGroupRequest), eq(serviceModelInfo), eq(serviceInstanceId), eq(userId));
91         verify(restMso, only()).restCall(eq(HttpMethod.POST), eq(RequestReferencesContainer.class), any(), any(), eq(Optional.empty()));
92
93     }
94
95     private ModelInfo setRandomStrings(ModelInfo object) {
96         try {
97             Set<String> fields = BeanUtils.describe(object).keySet();
98             BeanUtils.populate(object,
99                     fields.stream().collect(toMap(identity(), s -> randomAlphanumeric(4))));
100             return object;
101         } catch (Exception e) {
102             throw new RuntimeException(e);
103         }
104     }
105 }