1515import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
1616import org .springframework .data .domain .Page ;
1717import org .springframework .data .domain .PageImpl ;
18- import org .springframework .data .domain .PageRequest ;
18+ import org .springframework .data .domain .Pageable ;
1919import org .springframework .http .MediaType ;
2020import org .springframework .restdocs .mockmvc .RestDocumentationRequestBuilders ;
2121import org .springframework .restdocs .payload .JsonFieldType ;
2828import java .util .List ;
2929import java .util .UUID ;
3030
31- import static org .mockito .ArgumentMatchers .anyList ;
32- import static org .mockito .ArgumentMatchers .eq ;
31+ import static org .mockito .ArgumentMatchers .*;
3332import static org .mockito .Mockito .when ;
3433import static org .springframework .restdocs .mockmvc .MockMvcRestDocumentation .document ;
3534import static org .springframework .restdocs .mockmvc .RestDocumentationRequestBuilders .post ;
@@ -60,9 +59,9 @@ void getAllSubscriptionsForUser_shouldReturnSubscriptions() throws Exception {
6059
6160 UserSubscriptionDto sub1 = new UserSubscriptionDto (UUID .randomUUID (), "test.com/feed1" , Instant .now (), Instant .now (), true );
6261 UserSubscriptionDto sub2 = new UserSubscriptionDto (UUID .randomUUID (), "test.com/feed2" , Instant .now (), Instant .now (), true );
62+ Page <UserSubscriptionDto > page = new PageImpl <>(List .of (sub1 , sub2 ));
6363
64- Page <UserSubscriptionDto > page = new PageImpl <>(List .of (sub1 , sub2 ), PageRequest .of (0 , 2 ), 2 );
65- when (subscriptionService .getAllSubscriptionsForUser (user .id (), PageRequest .of (0 , 20 )))
64+ when (subscriptionService .getAllActiveSubscriptionsForUser (eq (user .id ()), any (Pageable .class )))
6665 .thenReturn (page );
6766
6867 mockMvc .perform (RestDocumentationRequestBuilders .get ("/api/v1/subscriptions" )
@@ -76,7 +75,10 @@ void getAllSubscriptionsForUser_shouldReturnSubscriptions() throws Exception {
7675 preprocessResponse (prettyPrint ()),
7776 queryParameters (
7877 parameterWithName ("page" ).description ("The page number to fetch" ).optional (),
79- parameterWithName ("size" ).description ("The number of results to include on each page" ).optional ()
78+ parameterWithName ("size" ).description ("The number of results to include on each page" ).optional (),
79+ parameterWithName ("includeUnsubscribed" )
80+ .optional ()
81+ .description ("If true, includes unsubscribed feeds in the results. Defaults to false." )
8082 ),
8183 responseFields (
8284 fieldWithPath ("subscriptions[].uuid" ).description ("The UUID of the subscription" ).type (JsonFieldType .STRING ),
@@ -95,6 +97,30 @@ void getAllSubscriptionsForUser_shouldReturnSubscriptions() throws Exception {
9597 ));
9698 }
9799
100+ @ Test
101+ void getAllSubscriptionsForUser_shouldIncludeUnsubscribedWhenRequested () throws Exception {
102+ CustomUserDetails user = new CustomUserDetails (
103+ 1L , UUID .randomUUID (), "alice" , "alice@test.com" ,
104+ List .of (new SimpleGrantedAuthority ("ROLE_USER" ))
105+ );
106+
107+ UserSubscriptionDto sub1 = new UserSubscriptionDto (UUID .randomUUID (), "test.com/feed1" , Instant .now (), Instant .now (), true );
108+ UserSubscriptionDto sub2 = new UserSubscriptionDto (UUID .randomUUID (), "test.com/feed2" , Instant .now (), Instant .now (), false );
109+ Page <UserSubscriptionDto > page = new PageImpl <>(List .of (sub1 , sub2 ));
110+
111+ when (subscriptionService .getAllSubscriptionsForUser (eq (user .id ()), any (Pageable .class )))
112+ .thenReturn (page );
113+
114+ mockMvc .perform (RestDocumentationRequestBuilders .get ("/api/v1/subscriptions" )
115+ .with (authentication (new UsernamePasswordAuthenticationToken (user , "password" , user .getAuthorities ())))
116+ .param ("includeUnsubscribed" , "true" ))
117+ .andExpect (status ().isOk ())
118+ .andDo (document ("subscriptions-list-with-unsubscribed" ,
119+ preprocessRequest (prettyPrint ()),
120+ preprocessResponse (prettyPrint ())));
121+ }
122+
123+
98124 @ Test
99125 void getSubscriptionByUuid_shouldReturnSubscription () throws Exception {
100126 CustomUserDetails user = new CustomUserDetails (1L , UUID .randomUUID (), "alice" , "alice@test.com" , List .of (new SimpleGrantedAuthority ("ROLE_USER" )));
@@ -243,4 +269,53 @@ void createUserSubscription_shouldReturnFailure() throws Exception {
243269 fieldWithPath ("failure[].message" ).description ("The error message" ).type (JsonFieldType .STRING )
244270 )));
245271 }
272+
273+ @ Test
274+ void updateSubscriptionStatus_shouldReturnUpdatedSubscription () throws Exception {
275+ CustomUserDetails user = new CustomUserDetails (
276+ 1L ,
277+ UUID .randomUUID (),
278+ "alice" ,
279+ "alice@test.com" ,
280+ List .of (new SimpleGrantedAuthority ("ROLE_USER" ))
281+ );
282+
283+ UUID subscriptionUuid = UUID .randomUUID ();
284+ boolean newStatus = false ;
285+
286+ UserSubscriptionDto updatedSubscription = new UserSubscriptionDto (
287+ subscriptionUuid ,
288+ "test.com/feed1" ,
289+ Instant .now (),
290+ Instant .now (),
291+ newStatus
292+ );
293+
294+ when (subscriptionService .unsubscribeUserFromFeed (subscriptionUuid , user .id ()))
295+ .thenReturn (updatedSubscription );
296+
297+ // Act & Assert
298+ mockMvc .perform (RestDocumentationRequestBuilders .post ("/api/v1/subscriptions/{uuid}/unsubscribe" , subscriptionUuid )
299+ .with (authentication (new UsernamePasswordAuthenticationToken (user , "password" , user .getAuthorities ())))
300+ .with (csrf ().asHeader ())
301+ .accept (MediaType .APPLICATION_JSON ))
302+ .andExpect (status ().isOk ())
303+ .andExpect (jsonPath ("$.uuid" ).value (subscriptionUuid .toString ()))
304+ .andExpect (jsonPath ("$.feedUrl" ).value ("test.com/feed1" ))
305+ .andExpect (jsonPath ("$.isSubscribed" ).value (false ))
306+ .andDo (document ("subscription-unsubscribe" ,
307+ preprocessRequest (prettyPrint ()),
308+ preprocessResponse (prettyPrint ()),
309+ pathParameters (
310+ parameterWithName ("uuid" ).description ("UUID of the subscription to update" )
311+ ),
312+ responseFields (
313+ fieldWithPath ("uuid" ).description ("The UUID of the subscription" ).type (JsonFieldType .STRING ),
314+ fieldWithPath ("feedUrl" ).description ("The feed URL of the subscription" ).type (JsonFieldType .STRING ),
315+ fieldWithPath ("createdAt" ).description ("When the subscription was created" ).type (JsonFieldType .STRING ),
316+ fieldWithPath ("updatedAt" ).description ("When the subscription was last updated" ).type (JsonFieldType .STRING ),
317+ fieldWithPath ("isSubscribed" ).description ("The updated subscription status" ).type (JsonFieldType .BOOLEAN )
318+ )
319+ ));
320+ }
246321}
0 commit comments