1616
1717package com .exadel .frs .controller ;
1818
19+ import static com .exadel .frs .system .global .Constants .ADMIN ;
20+ import static com .exadel .frs .utils .TestUtils .buildUser ;
21+ import static java .util .UUID .randomUUID ;
22+ import static org .mockito .ArgumentMatchers .any ;
23+ import static org .mockito .ArgumentMatchers .anyLong ;
24+ import static org .mockito .ArgumentMatchers .eq ;
25+ import static org .mockito .Mockito .doNothing ;
26+ import static org .mockito .Mockito .when ;
27+ import static org .springframework .http .MediaType .APPLICATION_JSON ;
28+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
29+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .user ;
30+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .delete ;
31+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
32+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
33+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .put ;
34+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
35+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
1936import com .exadel .frs .commonservice .entity .Model ;
2037import com .exadel .frs .commonservice .repository .ModelStatisticRepository ;
2138import com .exadel .frs .dto .ui .ModelCreateDto ;
2744import com .exadel .frs .system .security .config .ResourceServerConfig ;
2845import com .exadel .frs .system .security .config .WebSecurityConfig ;
2946import com .fasterxml .jackson .databind .ObjectMapper ;
47+ import java .util .List ;
48+ import lombok .SneakyThrows ;
3049import lombok .val ;
3150import org .junit .jupiter .api .Test ;
51+ import org .junit .jupiter .params .ParameterizedTest ;
52+ import org .junit .jupiter .params .provider .ValueSource ;
3253import org .springframework .beans .factory .annotation .Autowired ;
3354import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
3455import org .springframework .boot .test .mock .mockito .MockBean ;
3556import org .springframework .context .annotation .ComponentScan ;
3657import org .springframework .context .annotation .FilterType ;
3758import org .springframework .test .web .servlet .MockMvc ;
3859
39- import java .util .List ;
40-
41- import static com .exadel .frs .system .global .Constants .ADMIN ;
42- import static com .exadel .frs .utils .TestUtils .buildUser ;
43- import static java .util .UUID .randomUUID ;
44- import static org .mockito .ArgumentMatchers .*;
45- import static org .mockito .Mockito .doNothing ;
46- import static org .mockito .Mockito .when ;
47- import static org .springframework .http .MediaType .APPLICATION_JSON ;
48- import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
49- import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .user ;
50- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
51- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
52- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
53-
5460@ WebMvcTest (controllers = ModelController .class ,
5561 excludeFilters = @ ComponentScan .Filter (
5662 type = FilterType .ASSIGNABLE_TYPE ,
@@ -92,12 +98,12 @@ void shouldReturnMessageAndCodeWhenModelNameIsMissingOnUpdate() throws Exception
9298 .contentType (APPLICATION_JSON );
9399
94100 mockMvc .perform (updateRequest .content (mapper .writeValueAsString (bodyWithEmptyName )))
95- .andExpect (status ().isBadRequest ())
96- .andExpect (content ().string (expectedContent ));
101+ .andExpect (status ().isBadRequest ())
102+ .andExpect (content ().string (expectedContent ));
97103
98104 mockMvc .perform (updateRequest .content (mapper .writeValueAsString (bodyWithNoName )))
99- .andExpect (status ().isBadRequest ())
100- .andExpect (content ().string (expectedContent ));
105+ .andExpect (status ().isBadRequest ())
106+ .andExpect (content ().string (expectedContent ));
101107 }
102108
103109 @ Test
@@ -136,8 +142,8 @@ void shouldReturnModel() throws Exception {
136142 when (modelService .getModelDto (eq (APP_GUID ), eq (MODEL_GUID ), anyLong ())).thenReturn (responseDto );
137143
138144 mockMvc .perform (request )
139- .andExpect (status ().isOk ())
140- .andExpect (content ().string (mapper .writeValueAsString (responseDto )));
145+ .andExpect (status ().isOk ())
146+ .andExpect (content ().string (mapper .writeValueAsString (responseDto )));
141147 }
142148
143149 @ Test
@@ -153,11 +159,12 @@ void shouldReturnModels() throws Exception {
153159 when (modelService .getModels (eq (APP_GUID ), anyLong ())).thenReturn (List .of (responseDto , responseDto ));
154160
155161 mockMvc .perform (request )
156- .andExpect (status ().isOk ())
157- .andExpect (content ().string (mapper .writeValueAsString (List .of (responseDto , responseDto ))));
162+ .andExpect (status ().isOk ())
163+ .andExpect (content ().string (mapper .writeValueAsString (List .of (responseDto , responseDto ))));
158164 }
159165
160- @ Test
166+ @ ParameterizedTest
167+ @ ValueSource (strings = {MODEL_NAME , "_model_-.[]" })
161168 void shouldReturnCreatedModel () throws Exception {
162169 val createDto = new ModelCreateDto ();
163170 createDto .setName (MODEL_NAME );
@@ -170,8 +177,8 @@ void shouldReturnCreatedModel() throws Exception {
170177 .content (mapper .writeValueAsString (createDto ));
171178
172179 val model = Model .builder ()
173- .name (MODEL_NAME )
174- .build ();
180+ .name (MODEL_NAME )
181+ .build ();
175182
176183 val responseDto = new ModelResponseDto ();
177184 responseDto .setName (MODEL_NAME );
@@ -180,8 +187,8 @@ void shouldReturnCreatedModel() throws Exception {
180187 when (modelMapper .toResponseDto (any (Model .class ), eq (APP_GUID ))).thenReturn (responseDto );
181188
182189 mockMvc .perform (createRequest )
183- .andExpect (status ().isCreated ())
184- .andExpect (content ().string (mapper .writeValueAsString (responseDto )));
190+ .andExpect (status ().isCreated ())
191+ .andExpect (content ().string (mapper .writeValueAsString (responseDto )));
185192 }
186193
187194 @ Test
@@ -196,8 +203,8 @@ void shouldReturnUpdatedModel() throws Exception {
196203 .content (mapper .writeValueAsString (updateDto ));
197204
198205 val model = Model .builder ()
199- .name (MODEL_NAME )
200- .build ();
206+ .name (MODEL_NAME )
207+ .build ();
201208
202209 val responseDto = new ModelResponseDto ();
203210 responseDto .setName (MODEL_NAME );
@@ -206,8 +213,8 @@ void shouldReturnUpdatedModel() throws Exception {
206213 when (modelMapper .toResponseDto (any (Model .class ), eq (APP_GUID ))).thenReturn (responseDto );
207214
208215 mockMvc .perform (createRequest )
209- .andExpect (status ().isOk ())
210- .andExpect (content ().string (mapper .writeValueAsString (responseDto )));
216+ .andExpect (status ().isOk ())
217+ .andExpect (content ().string (mapper .writeValueAsString (responseDto )));
211218 }
212219
213220 @ Test
@@ -228,8 +235,8 @@ void shouldReturnUpdatedWithApiKeyModel() throws Exception {
228235 when (modelService .getModelDto (eq (APP_GUID ), eq (MODEL_GUID ), anyLong ())).thenReturn (responseDto );
229236
230237 mockMvc .perform (request )
231- .andExpect (status ().isOk ())
232- .andExpect (content ().string (mapper .writeValueAsString (responseDto )));
238+ .andExpect (status ().isOk ())
239+ .andExpect (content ().string (mapper .writeValueAsString (responseDto )));
233240 }
234241
235242 @ Test
@@ -245,6 +252,22 @@ void shouldReturnOkWhenDeleteModel() throws Exception {
245252 doNothing ().when (modelService ).deleteModel (eq (APP_GUID ), eq (MODEL_GUID ), anyLong ());
246253
247254 mockMvc .perform (request )
248- .andExpect (status ().isOk ());
255+ .andExpect (status ().isOk ());
256+ }
257+
258+ @ Test
259+ @ SneakyThrows
260+ void shouldReturnErrorMessageWhenNameContainsSpecialCharactersOnCreateNewModel () {
261+ val bodyWithEmptyName = new ModelCreateDto ();
262+ bodyWithEmptyName .setName ("\\ new;model//" );
263+ bodyWithEmptyName .setType ("RECOGNITION" );
264+
265+ mockMvc .perform (post (ADMIN + "/app/" + APP_GUID + "/model" )
266+ .with (csrf ())
267+ .with (user (buildUser ()))
268+ .contentType (APPLICATION_JSON ).content (mapper .writeValueAsString (bodyWithEmptyName )))
269+ .andExpect (status ().isBadRequest ())
270+ .andExpect (content ().string ("{\" message\" :\" The name cannot contain the following special characters: ';', '/', '\\ \\ '\" ," +
271+ "\" code\" :36}" ));
249272 }
250- }
273+ }
0 commit comments