-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscriptionRestControllerTest.java
More file actions
321 lines (280 loc) · 20.1 KB
/
SubscriptionRestControllerTest.java
File metadata and controls
321 lines (280 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package org.openpodcastapi.opa.subscriptions;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.openpodcastapi.opa.service.CustomUserDetails;
import org.openpodcastapi.opa.subscription.controller.SubscriptionRestController;
import org.openpodcastapi.opa.subscription.dto.BulkSubscriptionResponse;
import org.openpodcastapi.opa.subscription.dto.SubscriptionCreateDto;
import org.openpodcastapi.opa.subscription.dto.SubscriptionFailureDto;
import org.openpodcastapi.opa.subscription.dto.UserSubscriptionDto;
import org.openpodcastapi.opa.subscription.service.UserSubscriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.request.RequestDocumentation.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(SubscriptionRestController.class)
@AutoConfigureMockMvc
@AutoConfigureRestDocs(outputDir = "target/generated-snippets")
class SubscriptionRestControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockitoBean
private UserSubscriptionService subscriptionService;
@Test
void getAllSubscriptionsForUser_shouldReturnSubscriptions() throws Exception {
CustomUserDetails user = new CustomUserDetails(1L, UUID.randomUUID(), "alice", "alice@test.com", List.of(new SimpleGrantedAuthority("ROLE_USER")));
UserSubscriptionDto sub1 = new UserSubscriptionDto(UUID.randomUUID(), "test.com/feed1", Instant.now(), Instant.now(), true);
UserSubscriptionDto sub2 = new UserSubscriptionDto(UUID.randomUUID(), "test.com/feed2", Instant.now(), Instant.now(), true);
Page<UserSubscriptionDto> page = new PageImpl<>(List.of(sub1, sub2));
when(subscriptionService.getAllActiveSubscriptionsForUser(eq(user.id()), any(Pageable.class)))
.thenReturn(page);
mockMvc.perform(RestDocumentationRequestBuilders.get("/api/v1/subscriptions")
.with(authentication(new UsernamePasswordAuthenticationToken(user, "password", user.getAuthorities())))
.param("page", "0")
.param("size", "20"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.subscriptions.length()").value(2))
.andDo(document("subscriptions-list",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
queryParameters(
parameterWithName("page").description("The page number to fetch").optional(),
parameterWithName("size").description("The number of results to include on each page").optional(),
parameterWithName("includeUnsubscribed")
.optional()
.description("If true, includes unsubscribed feeds in the results. Defaults to false.")
),
responseFields(
fieldWithPath("subscriptions[].uuid").description("The UUID of the subscription").type(JsonFieldType.STRING),
fieldWithPath("subscriptions[].feedUrl").description("The feed URL of the subscription").type(JsonFieldType.STRING),
fieldWithPath("subscriptions[].createdAt").description("Creation timestamp of the subscription").type(JsonFieldType.STRING),
fieldWithPath("subscriptions[].updatedAt").description("Last update timestamp of the subscription").type(JsonFieldType.STRING),
fieldWithPath("subscriptions[].isSubscribed").description("Whether the user is subscribed to the feed").type(JsonFieldType.BOOLEAN),
fieldWithPath("page").description("Current page number").type(JsonFieldType.NUMBER),
fieldWithPath("size").description("Size of the page").type(JsonFieldType.NUMBER),
fieldWithPath("totalElements").description("Total number of subscriptions").type(JsonFieldType.NUMBER),
fieldWithPath("totalPages").description("Total number of pages").type(JsonFieldType.NUMBER),
fieldWithPath("first").description("Whether this is the first page").type(JsonFieldType.BOOLEAN),
fieldWithPath("last").description("Whether this is the last page").type(JsonFieldType.BOOLEAN),
fieldWithPath("numberOfElements").description("Number of subscriptions on the current page").type(JsonFieldType.NUMBER)
)
));
}
@Test
void getAllSubscriptionsForUser_shouldIncludeUnsubscribedWhenRequested() throws Exception {
CustomUserDetails user = new CustomUserDetails(
1L, UUID.randomUUID(), "alice", "alice@test.com",
List.of(new SimpleGrantedAuthority("ROLE_USER"))
);
UserSubscriptionDto sub1 = new UserSubscriptionDto(UUID.randomUUID(), "test.com/feed1", Instant.now(), Instant.now(), true);
UserSubscriptionDto sub2 = new UserSubscriptionDto(UUID.randomUUID(), "test.com/feed2", Instant.now(), Instant.now(), false);
Page<UserSubscriptionDto> page = new PageImpl<>(List.of(sub1, sub2));
when(subscriptionService.getAllSubscriptionsForUser(eq(user.id()), any(Pageable.class)))
.thenReturn(page);
mockMvc.perform(RestDocumentationRequestBuilders.get("/api/v1/subscriptions")
.with(authentication(new UsernamePasswordAuthenticationToken(user, "password", user.getAuthorities())))
.param("includeUnsubscribed", "true"))
.andExpect(status().isOk())
.andDo(document("subscriptions-list-with-unsubscribed",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
}
@Test
void getSubscriptionByUuid_shouldReturnSubscription() throws Exception {
CustomUserDetails user = new CustomUserDetails(1L, UUID.randomUUID(), "alice", "alice@test.com", List.of(new SimpleGrantedAuthority("ROLE_USER")));
UUID subscriptionUuid = UUID.randomUUID();
UserSubscriptionDto sub = new UserSubscriptionDto(subscriptionUuid, "test.com/feed1", Instant.now(), Instant.now(), true);
when(subscriptionService.getUserSubscriptionBySubscriptionUuid(subscriptionUuid, user.id()))
.thenReturn(sub);
mockMvc.perform(RestDocumentationRequestBuilders.get("/api/v1/subscriptions/{uuid}", subscriptionUuid)
.with(authentication(new UsernamePasswordAuthenticationToken(user, "password", user.getAuthorities()))))
.andExpect(status().isOk())
.andDo(document("subscription-get",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
pathParameters(
parameterWithName("uuid").description("UUID of the subscription to retrieve")
),
responseFields(
fieldWithPath("uuid").description("The UUID of the subscription").type(JsonFieldType.STRING),
fieldWithPath("feedUrl").description("The feed URL of the subscription").type(JsonFieldType.STRING),
fieldWithPath("createdAt").description("Creation timestamp").type(JsonFieldType.STRING),
fieldWithPath("updatedAt").description("Last update timestamp").type(JsonFieldType.STRING),
fieldWithPath("isSubscribed").description("Whether the user is subscribed to the feed").type(JsonFieldType.BOOLEAN)
)
));
}
@Test
void createUserSubscriptions_shouldReturnMixedResponse() throws Exception {
final CustomUserDetails user = new CustomUserDetails(1L, UUID.randomUUID(), "testuser", "test@test.com", List.of(new SimpleGrantedAuthority("ROLE_USER")));
final Instant timestamp = Instant.now();
final UUID goodFeedUUID = UUID.randomUUID();
final String BAD_UUID = "62ad30ce-aac0-4f0a-a811";
SubscriptionCreateDto dto1 = new SubscriptionCreateDto(goodFeedUUID.toString(), "test.com/feed1");
SubscriptionCreateDto dto2 = new SubscriptionCreateDto(BAD_UUID, "test.com/feed2");
BulkSubscriptionResponse response = new BulkSubscriptionResponse(
List.of(new UserSubscriptionDto(goodFeedUUID, "test.com/feed1", timestamp, timestamp, true)),
List.of(new SubscriptionFailureDto(BAD_UUID, "test.com/feed2", "invalid UUID format"))
);
when(subscriptionService.addSubscriptions(anyList(), eq(user.id())))
.thenReturn(response);
mockMvc.perform(post("/api/v1/subscriptions")
.with(authentication(new UsernamePasswordAuthenticationToken(user, "password", user.getAuthorities())))
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(List.of(dto1, dto2))))
.andExpect(status().isMultiStatus())
.andDo(document("subscriptions-bulk-create-mixed",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestFields(
fieldWithPath("[].uuid").description("The UUID of the subscription"),
fieldWithPath("[].feedUrl").description("The feed URL of the subscription to create")
),
responseFields(
fieldWithPath("success[]").description("List of feed URLs successfully added").type(JsonFieldType.ARRAY),
fieldWithPath("success[].uuid").description("The UUID of the feed").type(JsonFieldType.STRING),
fieldWithPath("success[].feedUrl").description("The feed URL").type(JsonFieldType.STRING),
fieldWithPath("success[].createdAt").description("The timestamp at which the subscription was created").type(JsonFieldType.STRING),
fieldWithPath("success[].updatedAt").description("The timestamp at which the subscription was updated").type(JsonFieldType.STRING),
fieldWithPath("success[].isSubscribed").description("Whether the user is subscribed to the feed").type(JsonFieldType.BOOLEAN),
fieldWithPath("failure[]").description("List of feed URLs that failed to add").type(JsonFieldType.ARRAY),
fieldWithPath("failure[].uuid").description("The UUID of the feed").type(JsonFieldType.STRING),
fieldWithPath("failure[].feedUrl").description("The feed URL").type(JsonFieldType.STRING),
fieldWithPath("failure[].message").description("The error message").type(JsonFieldType.STRING)
)
));
}
@Test
void createUserSubscription_shouldReturnSuccess() throws Exception {
final CustomUserDetails user = new CustomUserDetails(1L, UUID.randomUUID(), "testuser", "test@test.com", List.of(new SimpleGrantedAuthority("ROLE_USER")));
final UUID goodFeedUUID = UUID.randomUUID();
final Instant timestamp = Instant.now();
SubscriptionCreateDto dto = new SubscriptionCreateDto(goodFeedUUID.toString(), "test.com/feed1");
BulkSubscriptionResponse response = new BulkSubscriptionResponse(
List.of(new UserSubscriptionDto(goodFeedUUID, "test.com/feed1", timestamp, timestamp, true)),
List.of()
);
when(subscriptionService.addSubscriptions(anyList(), eq(user.id())))
.thenReturn(response);
mockMvc.perform(post("/api/v1/subscriptions")
.with(authentication(new UsernamePasswordAuthenticationToken(user, "password", user.getAuthorities())))
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(List.of(dto))))
.andExpect(status().is2xxSuccessful())
.andDo(document("subscriptions-bulk-create-success",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestFields(
fieldWithPath("[].uuid").description("The UUID of the subscription"),
fieldWithPath("[].feedUrl").description("The feed URL of the subscription to create")
),
responseFields(
fieldWithPath("success[]").description("List of feed URLs successfully added").type(JsonFieldType.ARRAY),
fieldWithPath("success[].uuid").description("The UUID of the feed").type(JsonFieldType.STRING),
fieldWithPath("success[].feedUrl").description("The feed URL").type(JsonFieldType.STRING),
fieldWithPath("success[].createdAt").description("The timestamp at which the subscription was created").type(JsonFieldType.STRING),
fieldWithPath("success[].updatedAt").description("The timestamp at which the subscription was updated").type(JsonFieldType.STRING),
fieldWithPath("success[].isSubscribed").description("Whether the user is subscribed to the feed").type(JsonFieldType.BOOLEAN),
fieldWithPath("failure[]").description("List of feed URLs that failed to add").type(JsonFieldType.ARRAY).ignored())));
}
@Test
void createUserSubscription_shouldReturnFailure() throws Exception {
final CustomUserDetails user = new CustomUserDetails(1L, UUID.randomUUID(), "testuser", "test@test.com", List.of(new SimpleGrantedAuthority("ROLE_USER")));
final String BAD_UUID = "62ad30ce-aac0-4f0a-a811";
SubscriptionCreateDto dto = new SubscriptionCreateDto(BAD_UUID, "test.com/feed2");
BulkSubscriptionResponse response = new BulkSubscriptionResponse(
List.of(),
List.of(new SubscriptionFailureDto(BAD_UUID, "test.com/feed2", "invalid UUID format"))
);
when(subscriptionService.addSubscriptions(anyList(), eq(user.id())))
.thenReturn(response);
mockMvc.perform(post("/api/v1/subscriptions")
.with(authentication(new UsernamePasswordAuthenticationToken(user, "password", user.getAuthorities())))
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(List.of(dto))))
.andExpect(status().isBadRequest())
.andDo(document("subscriptions-bulk-create-failure",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
responseFields(
fieldWithPath("success[]").description("List of feed URLs successfully added").type(JsonFieldType.ARRAY).ignored(),
fieldWithPath("failure[]").description("List of feed URLs that failed to add").type(JsonFieldType.ARRAY),
fieldWithPath("failure[].uuid").description("The UUID of the feed").type(JsonFieldType.STRING),
fieldWithPath("failure[].feedUrl").description("The feed URL").type(JsonFieldType.STRING),
fieldWithPath("failure[].message").description("The error message").type(JsonFieldType.STRING)
)));
}
@Test
void updateSubscriptionStatus_shouldReturnUpdatedSubscription() throws Exception {
CustomUserDetails user = new CustomUserDetails(
1L,
UUID.randomUUID(),
"alice",
"alice@test.com",
List.of(new SimpleGrantedAuthority("ROLE_USER"))
);
UUID subscriptionUuid = UUID.randomUUID();
boolean newStatus = false;
UserSubscriptionDto updatedSubscription = new UserSubscriptionDto(
subscriptionUuid,
"test.com/feed1",
Instant.now(),
Instant.now(),
newStatus
);
when(subscriptionService.unsubscribeUserFromFeed(subscriptionUuid, user.id()))
.thenReturn(updatedSubscription);
// Act & Assert
mockMvc.perform(RestDocumentationRequestBuilders.post("/api/v1/subscriptions/{uuid}/unsubscribe", subscriptionUuid)
.with(authentication(new UsernamePasswordAuthenticationToken(user, "password", user.getAuthorities())))
.with(csrf().asHeader())
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.uuid").value(subscriptionUuid.toString()))
.andExpect(jsonPath("$.feedUrl").value("test.com/feed1"))
.andExpect(jsonPath("$.isSubscribed").value(false))
.andDo(document("subscription-unsubscribe",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
pathParameters(
parameterWithName("uuid").description("UUID of the subscription to update")
),
responseFields(
fieldWithPath("uuid").description("The UUID of the subscription").type(JsonFieldType.STRING),
fieldWithPath("feedUrl").description("The feed URL of the subscription").type(JsonFieldType.STRING),
fieldWithPath("createdAt").description("When the subscription was created").type(JsonFieldType.STRING),
fieldWithPath("updatedAt").description("When the subscription was last updated").type(JsonFieldType.STRING),
fieldWithPath("isSubscribed").description("The updated subscription status").type(JsonFieldType.BOOLEAN)
)
));
}
}