2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2020 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.dcaegen2.platform.mod.web;
23 import org.onap.dcaegen2.platform.mod.model.basemicroservice.BaseMicroservice;
24 import org.onap.dcaegen2.platform.mod.model.exceptions.OperationNotAllowedException;
25 import org.onap.dcaegen2.platform.mod.model.exceptions.ResourceConflictException;
26 import org.onap.dcaegen2.platform.mod.model.restapi.MicroserviceCreateRequest;
27 import org.onap.dcaegen2.platform.mod.model.restapi.MicroserviceUpdateRequest;
28 import org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother;
29 import org.onap.dcaegen2.platform.mod.web.controller.BaseMicroserviceController;
30 import org.onap.dcaegen2.platform.mod.web.service.basemicroservice.MsService;
31 import org.hamcrest.Matchers;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.ArgumentMatchers;
36 import org.mockito.Mockito;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
39 import org.springframework.boot.test.mock.mockito.MockBean;
40 import org.springframework.http.MediaType;
41 import org.springframework.test.context.junit.jupiter.SpringExtension;
42 import org.springframework.test.web.servlet.MockMvc;
43 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
44 import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
46 import java.util.Arrays;
48 import static org.onap.dcaegen2.platform.mod.model.exceptions.ErrorMessages.MICROSERVICE_NAME_CONFLICT_MESSAGE;
50 @ExtendWith(SpringExtension.class)
51 @WebMvcTest(BaseMicroserviceController.class)
52 class BaseMicroserviceControllerTest {
55 private MockMvc mockMvc;
58 private MsService mockBaseMsService;
65 void test_GetAllBaseMicroservices_returnsListOfDTOs() throws Exception {
67 BaseMicroservice ms1 = new BaseMicroservice();
68 ms1.setName("HelloWorld1");
69 BaseMicroservice ms2 = new BaseMicroservice();
70 ms2.setName("HelloWorld2");
72 Mockito.when(mockBaseMsService.getAllMicroservices()).thenReturn(Arrays.asList(ms1, ms2));
75 mockMvc.perform(MockMvcRequestBuilders.get("/api/base-microservice")
76 .contentType(MediaType.APPLICATION_JSON))
77 .andExpect(MockMvcResultMatchers.status().isOk())
78 .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(2)));
82 void test_addBaseMicroservice_returnsMicroservice() throws Exception {
84 MicroserviceCreateRequest microserviceRequest = BaseMsObjectMother.createMockMsRequest();
87 BaseMicroservice microserviceDao = BaseMsObjectMother.createMockMsObject();
89 Mockito.when(mockBaseMsService.createMicroservice(microserviceRequest)).thenReturn(microserviceDao);
92 mockMvc.perform(MockMvcRequestBuilders.post("/api/base-microservice")
93 .contentType(MediaType.APPLICATION_JSON)
94 .content(BaseMsObjectMother.asJsonString(microserviceRequest))
95 .characterEncoding("utf-8"))
96 .andExpect(MockMvcResultMatchers.status().isCreated())
97 .andExpect((MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(BaseMsObjectMother.BASE_MS_ID))))
98 .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo(BaseMsObjectMother.BASE_MS_NAME)))
99 .andExpect(MockMvcResultMatchers.jsonPath("$.metadata.createdBy", Matchers.equalTo(BaseMsObjectMother.USER)));
103 void test_addBaseMicroserviceWithDuplicateName_shouldThrowConflictError() throws Exception{
105 MicroserviceCreateRequest microserviceRequest = BaseMsObjectMother.createMockMsRequest();
106 Mockito.when(mockBaseMsService.createMicroservice(ArgumentMatchers.any())).thenThrow(new ResourceConflictException(MICROSERVICE_NAME_CONFLICT_MESSAGE));
109 mockMvc.perform(MockMvcRequestBuilders.post("/api/base-microservice")
110 .contentType(MediaType.APPLICATION_JSON)
111 .content(BaseMsObjectMother.asJsonString(microserviceRequest)))
112 .andExpect(MockMvcResultMatchers.status().isConflict());
116 void test_updateBaseMicroserviceEndpoint() throws Exception{
117 MicroserviceUpdateRequest microserviceRequest = BaseMsObjectMother.createUpdateMsRequest();
118 String requestedMsId = "id-123";
120 mockMvc.perform(MockMvcRequestBuilders.patch(String.format(BaseMicroserviceController.API_BASE_MICROSERVICE + "/%s", requestedMsId))
121 .contentType(MediaType.APPLICATION_JSON)
122 .content(BaseMsObjectMother.asJsonString(microserviceRequest))
123 .characterEncoding("utf-8"))
124 .andExpect(MockMvcResultMatchers.status().isNoContent());
125 Mockito.verify(mockBaseMsService, Mockito.times(1)).updateMicroservice(requestedMsId, microserviceRequest);
129 void test_OperationNotAllowedExceptionThrows409() throws Exception{
130 MicroserviceUpdateRequest microserviceRequest = BaseMsObjectMother.createUpdateMsRequest();
131 String requestedMsId = "id-123";
132 Mockito.doThrow(new OperationNotAllowedException("")).
133 when(mockBaseMsService).updateMicroservice(requestedMsId, microserviceRequest);
135 mockMvc.perform(MockMvcRequestBuilders.patch(String.format(BaseMicroserviceController.API_BASE_MICROSERVICE + "/%s", requestedMsId))
136 .contentType(MediaType.APPLICATION_JSON)
137 .content(BaseMsObjectMother.asJsonString(microserviceRequest)))
138 .andExpect(MockMvcResultMatchers.status().isConflict());
142 void test_validateMsRequestShouldThrowCorrectResponse() throws Exception {
144 MicroserviceCreateRequest microserviceRequest = BaseMsObjectMother.createMockMsRequest();
145 microserviceRequest.setName(" ");
146 microserviceRequest.setTag("123");
147 microserviceRequest.setServiceName("123");
148 microserviceRequest.setUser(" ");
151 BaseMicroservice microserviceDao = BaseMsObjectMother.createMockMsObject();
153 Mockito.when(mockBaseMsService.createMicroservice(microserviceRequest)).thenReturn(microserviceDao);
156 mockMvc.perform(MockMvcRequestBuilders.post("/api/base-microservice")
157 .contentType(MediaType.APPLICATION_JSON)
158 .content(BaseMsObjectMother.asJsonString(microserviceRequest))
159 .characterEncoding("utf-8"))
160 .andExpect(MockMvcResultMatchers.status().isBadRequest())
161 .andExpect(MockMvcResultMatchers.jsonPath("$.message", Matchers.equalTo("Validation failed.")))
162 .andExpect(MockMvcResultMatchers.jsonPath("$.errors", Matchers.hasSize(4)))