Merge "Add AsyncInstantiationController tests"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / AsyncInstantiationControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.vid.controller;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyNoMoreInteractions;
29 import static org.mockito.Mockito.when;
30 import static org.onap.vid.controller.AsyncInstantiationController.ASYNC_INSTANTIATION;
31 import static org.springframework.http.MediaType.APPLICATION_JSON;
32 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
33 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
34 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
35 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
36
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import java.io.Serializable;
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.UUID;
43 import java.util.stream.Collectors;
44
45 import org.checkerframework.checker.units.qual.A;
46 import org.jeasy.random.EasyRandom;
47 import org.jeasy.random.EasyRandomParameters;
48 import org.jeasy.random.FieldPredicates;
49 import org.jeasy.random.randomizers.misc.BooleanRandomizer;
50 import org.jeasy.random.randomizers.text.StringRandomizer;
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.mockito.ArgumentCaptor;
54 import org.onap.portalsdk.core.util.SystemProperties;
55 import org.onap.vid.dal.AsyncInstantiationRepository;
56 import org.onap.vid.model.ServiceInfo;
57 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
58 import org.onap.vid.mso.MsoResponseWrapper2;
59 import org.onap.vid.roles.RoleProvider;
60 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
61 import org.onap.vid.services.AuditService;
62 import org.onap.vid.utils.SystemPropertiesWrapper;
63 import org.springframework.test.web.servlet.MockMvc;
64 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
65 import org.togglz.core.manager.FeatureManager;
66
67 public class AsyncInstantiationControllerTest {
68
69     private static final long STATIC_SEED = 5336L;
70     private final EasyRandomParameters parameters = new EasyRandomParameters()
71         .randomize(Boolean.class, new BooleanRandomizer(STATIC_SEED))
72         .randomize(String.class, new StringRandomizer(4, 4, STATIC_SEED))
73         .excludeField(FieldPredicates.ofType(Serializable.class))
74         .collectionSizeRange(1, 1);
75     private final EasyRandom modelGenerator = new EasyRandom(parameters);
76     private final ObjectMapper objectMapper = new ObjectMapper();
77
78     private MockMvc mockMvc;
79     private AsyncInstantiationBusinessLogic instantiationBusinessLogic;
80     private AsyncInstantiationRepository asyncInstantiationRepository;
81     private SystemPropertiesWrapper propertiesWrapper;
82     private AuditService auditService;
83
84     @Before
85     public void setUp() {
86         instantiationBusinessLogic = mock(AsyncInstantiationBusinessLogic.class);
87         RoleProvider roleProvider = mock(RoleProvider.class);
88         FeatureManager featureManager = mock(FeatureManager.class);
89         propertiesWrapper = mock(SystemPropertiesWrapper.class);
90         auditService = mock(AuditService.class);
91         asyncInstantiationRepository = mock(AsyncInstantiationRepository.class);
92         AsyncInstantiationController asyncInstantiationController = new AsyncInstantiationController(
93             instantiationBusinessLogic, asyncInstantiationRepository, roleProvider, featureManager, propertiesWrapper, auditService
94         );
95
96         mockMvc = MockMvcBuilders.standaloneSetup(asyncInstantiationController).build();
97     }
98
99     @Test
100     public void shouldReturnAllServiceInfos() throws Exception {
101         List<ServiceInfo> serviceInfos = modelGenerator.objects(ServiceInfo.class, 3).collect(Collectors.toList());
102         when(instantiationBusinessLogic.getAllServicesInfo()).thenReturn(serviceInfos);
103
104         mockMvc.perform(get("/" + ASYNC_INSTANTIATION))
105             .andExpect(content().json(asJson(serviceInfos)));
106
107         verify(instantiationBusinessLogic).getAllServicesInfo();
108         verifyNoMoreInteractions(instantiationBusinessLogic);
109     }
110
111     @Test
112     public void shouldRetryJobsWithGivenUuid() throws Exception {
113         when(propertiesWrapper.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("some user");
114
115         List<UUID> expectedUuids = new ArrayList<>();
116         expectedUuids.add(UUID.fromString("c195c600-a162-4655-9d88-d1a44518c4b5"));
117         expectedUuids.add(UUID.fromString("1a7ee2b5-ac2b-4dc7-a2a6-22e5d3b33d79"));
118         MsoResponseWrapper2<List<UUID>> expectedResponse = new MsoResponseWrapper2<>(200, expectedUuids);
119
120         ServiceInstantiation serviceInstantiation = modelGenerator.nextObject(ServiceInstantiation.class);
121
122         ArgumentCaptor<ServiceInstantiation> svcInstCaptor = ArgumentCaptor.forClass(ServiceInstantiation.class);
123         ArgumentCaptor<UUID> uuidCaptor = ArgumentCaptor.forClass(UUID.class);
124
125         when(instantiationBusinessLogic.retryJob(
126             svcInstCaptor.capture(),
127             uuidCaptor.capture(),
128             any()
129         )).thenReturn(expectedUuids);
130
131         mockMvc.perform(
132             post("/" + ASYNC_INSTANTIATION + "/retryJobWithChangedData/{jobId}",
133                 "804d26c3-fbe9-426c-8eff-25c6ab18fdcf")
134                 .content(asJson(serviceInstantiation))
135                 .contentType(APPLICATION_JSON))
136             .andExpect(content().json(asJson(expectedResponse)));
137
138         assertThat(svcInstCaptor.getValue().getInstanceId()).isEqualTo(serviceInstantiation.getInstanceId());
139         assertThat(uuidCaptor.getValue()).isEqualTo(UUID.fromString("804d26c3-fbe9-426c-8eff-25c6ab18fdcf"));
140     }
141
142     @Test
143     public void shouldDeleteJob() throws Exception {
144         mockMvc.perform(
145             delete("/" + ASYNC_INSTANTIATION + "/job/{jobId}", "804d26c3-fbe9-426c-8eff-25c6ab18fdcf"));
146
147         verify(instantiationBusinessLogic).deleteJob(eq(UUID.fromString("804d26c3-fbe9-426c-8eff-25c6ab18fdcf")));
148     }
149
150     @Test
151     public void shouldHideServiceJob() throws Exception {
152         mockMvc.perform(
153             post("/" + ASYNC_INSTANTIATION + "/hide/{jobId}", "804d26c3-fbe9-426c-8eff-25c6ab18fdcf"));
154
155         verify(instantiationBusinessLogic).hideServiceInfo(eq(UUID.fromString("804d26c3-fbe9-426c-8eff-25c6ab18fdcf")));
156     }
157
158     @Test
159     public void shouldRetryJob() throws Exception {
160         when(propertiesWrapper.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("some user");
161
162         List<UUID> expectedUuids = new ArrayList<>();
163         expectedUuids.add(UUID.fromString("c195c600-a162-4655-9d88-d1a44518c4b5"));
164         expectedUuids.add(UUID.fromString("1a7ee2b5-ac2b-4dc7-a2a6-22e5d3b33d79"));
165         MsoResponseWrapper2<List<UUID>> expectedResponse = new MsoResponseWrapper2<>(200, expectedUuids);
166
167         when(instantiationBusinessLogic.retryJob(eq(UUID.fromString("804d26c3-fbe9-426c-8eff-25c6ab18fdcf")), any()))
168             .thenReturn(expectedUuids);
169
170         mockMvc.perform(
171             post("/" + ASYNC_INSTANTIATION + "/retry/{jobId}", "804d26c3-fbe9-426c-8eff-25c6ab18fdcf"))
172             .andExpect(content().json(asJson(expectedResponse)));
173     }
174
175     private <T> String asJson(T object) throws JsonProcessingException {
176         return objectMapper.writeValueAsString(object);
177     }
178 }