Distributing Blueprint to DCAE Dashboard Issue-ID: DCAEGEN2-2385>
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / BaseMicroserviceControllerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
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
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.dcaegen2.platform.mod.web;
22
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;
45
46 import java.util.Arrays;
47
48 import static org.onap.dcaegen2.platform.mod.model.exceptions.ErrorMessages.MICROSERVICE_NAME_CONFLICT_MESSAGE;
49
50 @ExtendWith(SpringExtension.class)
51 @WebMvcTest(BaseMicroserviceController.class)
52 class BaseMicroserviceControllerTest {
53
54     @Autowired
55     private MockMvc mockMvc;
56
57     @MockBean
58     private MsService mockBaseMsService;
59
60     @BeforeEach
61     void setUp() {
62     }
63
64     @Test
65     void test_GetAllBaseMicroservices_returnsListOfDTOs() throws Exception {
66         //arrange
67         BaseMicroservice ms1 = new BaseMicroservice();
68         ms1.setName("HelloWorld1");
69         BaseMicroservice ms2 = new BaseMicroservice();
70         ms2.setName("HelloWorld2");
71
72         Mockito.when(mockBaseMsService.getAllMicroservices()).thenReturn(Arrays.asList(ms1, ms2));
73
74         //act/assert
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)));
79     }
80
81     @Test
82     void test_addBaseMicroservice_returnsMicroservice() throws Exception {
83         //arrange
84         MicroserviceCreateRequest microserviceRequest = BaseMsObjectMother.createMockMsRequest();
85
86         //response
87         BaseMicroservice microserviceDao = BaseMsObjectMother.createMockMsObject();
88
89         Mockito.when(mockBaseMsService.createMicroservice(microserviceRequest)).thenReturn(microserviceDao);
90
91         //act/assert
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)));
100     }
101
102     @Test
103     void test_addBaseMicroserviceWithDuplicateName_shouldThrowConflictError() throws Exception{
104         //arrange
105         MicroserviceCreateRequest microserviceRequest = BaseMsObjectMother.createMockMsRequest();
106         Mockito.when(mockBaseMsService.createMicroservice(ArgumentMatchers.any())).thenThrow(new ResourceConflictException(MICROSERVICE_NAME_CONFLICT_MESSAGE));
107
108         //act/assert
109         mockMvc.perform(MockMvcRequestBuilders.post("/api/base-microservice")
110                 .contentType(MediaType.APPLICATION_JSON)
111                 .content(BaseMsObjectMother.asJsonString(microserviceRequest)))
112                 .andExpect(MockMvcResultMatchers.status().isConflict());
113     }
114
115     @Test
116     void test_updateBaseMicroserviceEndpoint() throws Exception{
117         MicroserviceUpdateRequest microserviceRequest = BaseMsObjectMother.createUpdateMsRequest();
118         String requestedMsId = "id-123";
119
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);
126     }
127
128     @Test
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);
134
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());
139     }
140
141     @Test
142     void test_validateMsRequestShouldThrowCorrectResponse() throws Exception {
143         //arrange
144         MicroserviceCreateRequest microserviceRequest = BaseMsObjectMother.createMockMsRequest();
145         microserviceRequest.setName(" ");
146         microserviceRequest.setTag("123");
147         microserviceRequest.setServiceName("123");
148         microserviceRequest.setUser(" ");
149
150         //response
151         BaseMicroservice microserviceDao = BaseMsObjectMother.createMockMsObject();
152
153         Mockito.when(mockBaseMsService.createMicroservice(microserviceRequest)).thenReturn(microserviceDao);
154
155         //act/assert
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)))
163                 ;
164     }
165 }