55import com .somemore .center .dto .request .PreferItemCreateRequestDto ;
66import com .somemore .center .dto .response .PreferItemCreateResponseDto ;
77import com .somemore .center .usecase .command .CreatePreferItemUseCase ;
8+ import com .somemore .center .usecase .command .DeletePreferItemUseCase ;
89import com .somemore .global .exception .BadRequestException ;
910import org .junit .jupiter .api .DisplayName ;
1011import org .junit .jupiter .api .Test ;
1415import java .util .UUID ;
1516
1617import static org .mockito .BDDMockito .given ;
18+ import static org .mockito .Mockito .doThrow ;
1719import static org .mockito .Mockito .verify ;
20+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .delete ;
1821import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
1922import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
2023import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
@@ -24,6 +27,9 @@ class PreferItemCommandApiControllerTest extends ControllerTestSupport {
2427 @ MockBean
2528 private CreatePreferItemUseCase createPreferItemUseCase ;
2629
30+ @ MockBean
31+ private DeletePreferItemUseCase deletePreferItemUseCase ;
32+
2733
2834 @ DisplayName ("기관은 선호물품을 등록할 수 있다. (controller)" )
2935 @ Test
@@ -80,4 +86,69 @@ void registerPreferItem_Fail_WhenCenterNotExists() throws Exception {
8086 // verify
8187 verify (createPreferItemUseCase ).createPreferItem (userId , requestDto );
8288 }
89+
90+ @ DisplayName ("기관은 등록한 선호물품을 삭제할 수 있다. (controller)" )
91+ @ Test
92+ @ WithMockCustomUser (role = "CENTER" )
93+ void deletePreferItem () throws Exception {
94+ // given
95+ UUID userId = UUID .fromString ("123e4567-e89b-12d3-a456-426614174000" );
96+ Long preferItemId = 1L ;
97+
98+ // when & then
99+ mockMvc .perform (delete ("/api/preferItem/{preferItemId}" , preferItemId )
100+ .principal (userId ::toString ))
101+ .andExpect (status ().isOk ())
102+ .andExpect (jsonPath ("$.code" ).value (200 ))
103+ .andExpect (jsonPath ("$.message" ).value ("선호 물품 삭제 성공" ));
104+
105+ // verify
106+ verify (deletePreferItemUseCase ).deletePreferItem (userId , preferItemId );
107+ }
108+
109+ @ DisplayName ("선호물품을 등록한 기관이 아니라면 선호물품을 삭제할 수 없다. (controller)" )
110+ @ Test
111+ @ WithMockCustomUser (role = "CENTER" )
112+ void deletePreferItem_Fail_WhenUnauthorized () throws Exception {
113+ // given
114+ UUID userId = UUID .fromString ("123e4567-e89b-12d3-a456-426614174000" );
115+ Long preferItemId = 1L ;
116+
117+ doThrow (new BadRequestException ("다른 기관의 선호 물품은 삭제할 수 없습니다." ))
118+ .when (deletePreferItemUseCase )
119+ .deletePreferItem (userId , preferItemId );
120+
121+ // when & then
122+ mockMvc .perform (delete ("/api/preferItem/{preferItemId}" , preferItemId )
123+ .principal (userId ::toString ))
124+ .andExpect (status ().isBadRequest ())
125+ .andExpect (jsonPath ("$.status" ).value (400 ))
126+ .andExpect (jsonPath ("$.detail" ).value ("다른 기관의 선호 물품은 삭제할 수 없습니다." ));
127+
128+ // verify
129+ verify (deletePreferItemUseCase ).deletePreferItem (userId , preferItemId );
130+ }
131+
132+ @ DisplayName ("존재하지 않는 선호물품을 삭제할 수 없다. (controller)" )
133+ @ Test
134+ @ WithMockCustomUser (role = "CENTER" )
135+ void deletePreferItem_Fail_WhenPreferItemNotExists () throws Exception {
136+ // given
137+ UUID userId = UUID .fromString ("123e4567-e89b-12d3-a456-426614174000" );
138+ Long preferItemId = 1L ;
139+
140+ doThrow (new BadRequestException ("존재하지 않는 선호 물품입니다." ))
141+ .when (deletePreferItemUseCase )
142+ .deletePreferItem (userId , preferItemId );
143+
144+ // when & then
145+ mockMvc .perform (delete ("/api/preferItem/{preferItemId}" , preferItemId )
146+ .principal (userId ::toString ))
147+ .andExpect (status ().isBadRequest ())
148+ .andExpect (jsonPath ("$.status" ).value (400 ))
149+ .andExpect (jsonPath ("$.detail" ).value ("존재하지 않는 선호 물품입니다." ));
150+
151+ // verify
152+ verify (deletePreferItemUseCase ).deletePreferItem (userId , preferItemId );
153+ }
83154}
0 commit comments