1+ package me .oldboy .integration .controllers .api_user_details_scenario ;
2+ 3+ import com .fasterxml .jackson .core .type .TypeReference ;
4+ import com .fasterxml .jackson .databind .ObjectMapper ;
5+ import lombok .SneakyThrows ;
6+ import me .oldboy .config .test_data_source .TestContainerInit ;
7+ import me .oldboy .dto .contact_dto .ContactReadDto ;
8+ import me .oldboy .integration .annotation .IT ;
9+ import org .junit .jupiter .api .BeforeEach ;
10+ import org .junit .jupiter .api .Test ;
11+ import org .springframework .beans .factory .annotation .Autowired ;
12+ import org .springframework .security .test .context .support .WithUserDetails ;
13+ import org .springframework .test .web .servlet .MockMvc ;
14+ import org .springframework .test .web .servlet .MvcResult ;
15+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
16+ import org .springframework .web .context .WebApplicationContext ;
17+ 18+ import java .util .List ;
19+ 20+ import static me .oldboy .test_constant .TestConstantFields .*;
21+ import static org .assertj .core .api .Assertions .assertThat ;
22+ import static org .junit .jupiter .api .Assertions .assertAll ;
23+ import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
24+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
25+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
26+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
27+ 28+ @ IT
29+ class ContactControllerIT extends TestContainerInit {
30+ 31+ @ Autowired
32+ private WebApplicationContext webApplicationContext ;
33+ private MockMvc mockMvc ;
34+ 35+ @ BeforeEach
36+ void setUp (){
37+ mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext )
38+ .apply (springSecurity ())
39+ .build ();
40+ }
41+ 42+ @ Test
43+ @ SneakyThrows
44+ @ WithUserDetails (value = EXIST_EMAIL , userDetailsServiceBeanName = "clientDetailsService" )
45+ void getContactDetails_ShouldReturnContactReadDto_Test () {
46+ MvcResult result = mockMvc .perform (get ("/api/myContact" ))
47+ .andExpect (status ().isOk ())
48+ .andReturn ();
49+ 50+ String strResponse = result .getResponse ().getContentAsString ();
51+ /* Проверяем наличие DTO полей в ответе (он явно не пустой) */
52+ assertAll (
53+ () -> assertThat (strResponse .contains ("city" )).isTrue (),
54+ () -> assertThat (strResponse .contains ("postalCode" )).isTrue (),
55+ () -> assertThat (strResponse .contains ("address" )).isTrue (),
56+ () -> assertThat (strResponse .contains ("building" )).isTrue (),
57+ () -> assertThat (strResponse .contains ("apartment" )).isTrue (),
58+ () -> assertThat (strResponse .contains ("homePhone" )).isTrue (),
59+ () -> assertThat (strResponse .contains ("mobilePhone" )).isTrue ()
60+ );
61+ }
62+ 63+ /* Ниже два теста проверяют работу @PreAuthorize("hasRole('ADMIN')") над методом, ну и сам метод в случае успеха */
64+ @ Test
65+ @ SneakyThrows
66+ @ WithUserDetails (value = EXIST_EMAIL , userDetailsServiceBeanName = "clientDetailsService" )
67+ void getAllContacts_ShouldReturnDtoList_AdminAuth_Test () {
68+ MvcResult result = mockMvc .perform (get ("/api/user-contacts" ))
69+ .andExpect (status ().isOk ())
70+ .andReturn ();
71+ 72+ String strResultResponse = result .getResponse ().getContentAsString ();
73+ 74+ List <ContactReadDto > listFromResponse = new ObjectMapper ()
75+ .readValue (strResultResponse , new TypeReference <List <ContactReadDto >>() {});
76+ 77+ assertThat (listFromResponse .size ()).isGreaterThan (1 );
78+ }
79+ 80+ @ Test
81+ @ SneakyThrows
82+ @ WithUserDetails (value = EXIST_EMAIL_WITH_READ_AUTH , userDetailsServiceBeanName = "clientDetailsService" )
83+ void getAllContacts_ShouldReturnBadRequest_NotAdminAuth_Test () {
84+ mockMvc .perform (get ("/api/user-contacts" ))
85+ .andExpect (status ().isBadRequest ())
86+ .andExpect (content ().string ("{\" exceptionMsg\" :\" Access Denied\" }" ));
87+ }
88+ 89+ /* Ниже тесты проверяют работу @PostAuthorize("#myCity == returnObject.city") над методом, ну и сам метод в случае успеха */
90+ @ Test
91+ @ SneakyThrows
92+ @ WithUserDetails (value = EXIST_EMAIL , userDetailsServiceBeanName = "clientDetailsService" )
93+ void getContactsIfConditionIsGood_ShouldReturnOneRecordOfExistingCity_Test () {
94+ String testCity = "Lovervill" ;
95+ 96+ MvcResult result = mockMvc .perform (get ("/api/user-contact-with-condition/" + testCity ))
97+ .andExpect (status ().isOk ())
98+ .andReturn ();
99+ 100+ String strRes = result .getResponse ().getContentAsString ();
101+ 102+ ContactReadDto respDto = new ObjectMapper ().readValue (strRes , ContactReadDto .class );
103+ 104+ assertThat (respDto .getCity ()).isEqualTo (testCity );
105+ }
106+ 107+ @ Test
108+ @ SneakyThrows
109+ @ WithUserDetails (value = EXIST_EMAIL , userDetailsServiceBeanName = "clientDetailsService" )
110+ void getContactsIfConditionIsGood_ShouldReturnBadRequest_NotExistingCity_Test () {
111+ String testCity = "Ufa" ;
112+ 113+ mockMvc .perform (get ("/api/user-contact-with-condition/" + testCity ))
114+ .andExpect (status ().isBadRequest ())
115+ .andExpect (content ().string ("" ));
116+ }
117+ }
0 commit comments