From eb4456bba2d1ecb8abd90a09d534a1b1a305e9a4 Mon Sep 17 00:00:00 2001 From: nastiausenko Date: Sun, 21 Apr 2024 17:49:48 +0300 Subject: [PATCH 1/2] update LinkControllerTest --- .../urlshortener/TestConfig.java | 6 +++ .../urlshortener/link/LinkControllerTest.java | 50 +++++++++---------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/test/java/com/linkurlshorter/urlshortener/TestConfig.java b/src/test/java/com/linkurlshorter/urlshortener/TestConfig.java index 94abfed..ec17e8c 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/TestConfig.java +++ b/src/test/java/com/linkurlshorter/urlshortener/TestConfig.java @@ -5,6 +5,7 @@ import com.linkurlshorter.urlshortener.link.LinkInfoDtoMapper; import com.linkurlshorter.urlshortener.link.LinkRepository; import com.linkurlshorter.urlshortener.link.LinkService; +import com.linkurlshorter.urlshortener.link.ShortLinkGenerator; import com.linkurlshorter.urlshortener.security.CustomUserDetailsService; import com.linkurlshorter.urlshortener.user.UserRepository; import com.linkurlshorter.urlshortener.user.UserService; @@ -115,4 +116,9 @@ public UserRepository userRepository() { public AuthService authService() { return mock(AuthService.class); } + + @Bean + public ShortLinkGenerator shortLinkGenerator() { + return new ShortLinkGenerator(); + } } diff --git a/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerTest.java b/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerTest.java index bf43eac..f12c98c 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerTest.java @@ -117,25 +117,25 @@ void createLinkFailedTest() throws Exception { } /** - * Test case for the {@link LinkController#deleteLink(UUID)} method. + * Test case for the {@link LinkController#deleteLink(String)} method. */ @Test @WithMockUser void deleteLinkTest() throws Exception { when(userService.findByEmail(any())).thenReturn(user); - when(linkService.findById(link.getId())).thenReturn(link); - doNothing().when(linkService).deleteById(link.getId()); + when(linkService.findByShortLink(link.getShortLink())).thenReturn(link); + doNothing().when(linkService).deleteByShortLink(link.getShortLink()); ResultActions resultActions = mockMvc.perform(post("/api/V1/link/delete") .contentType(MediaType.APPLICATION_JSON) - .param("id", String.valueOf(link.getId()))); + .param("shortLink", String.valueOf(link.getShortLink()))); resultActions.andExpect(status().isOk()) .andExpect(jsonPath("$.error").value("ok")); } /** - * Test case for the {@link LinkController#deleteLink(UUID)} method when + * Test case for the {@link LinkController#deleteLink(String)} method when * the authenticated user does not have rights. */ @Test @@ -148,12 +148,12 @@ void deleteLinkForbiddenTest() throws Exception { .role(UserRole.USER) .build(); when(userService.findByEmail(any())).thenReturn(newUser); - when(linkService.findById(link.getId())).thenReturn(link); - doNothing().when(linkService).deleteById(link.getId()); + when(linkService.findByShortLink(link.getShortLink())).thenReturn(link); + doNothing().when(linkService).deleteByShortLink(link.getShortLink()); ResultActions resultActions = mockMvc.perform(post("/api/V1/link/delete") .contentType(MediaType.APPLICATION_JSON) - .param("id", String.valueOf(link.getId()))); + .param("shortLink", String.valueOf(link.getShortLink()))); resultActions.andExpect(status().isForbidden()); } @@ -165,9 +165,9 @@ void deleteLinkForbiddenTest() throws Exception { @WithMockUser void editLinkContentTest() throws Exception { when(userService.findByEmail(any())).thenReturn(user); - when(linkService.findById(link.getId())).thenReturn(link); + when(linkService.findByShortLink(link.getShortLink())).thenReturn(link); - EditLinkContentRequest request = new EditLinkContentRequest(link.getId(), "short-link-2"); + EditLinkContentRequest request = new EditLinkContentRequest(link.getShortLink(), "short-link-2"); when(linkService.update(link)).thenReturn(link); ResultActions resultActions = mockMvc.perform(post("/api/V1/link/edit/content") @@ -193,14 +193,14 @@ void editLinkContentForbiddenTest() throws Exception { .role(UserRole.USER) .build(); when(userService.findByEmail(any())).thenReturn(newUser); - when(linkService.findById(link.getId())).thenReturn(link); + when(linkService.findByShortLink(link.getShortLink())).thenReturn(link); - EditLinkContentRequest request = new EditLinkContentRequest(link.getId(), "short-link-2"); + EditLinkContentRequest request = new EditLinkContentRequest(link.getShortLink(), "short-link-2"); when(linkService.update(link)).thenReturn(link); ResultActions resultActions = mockMvc.perform(post("/api/V1/link/edit/content") .contentType(MediaType.APPLICATION_JSON) - .param("id", String.valueOf(link.getId())) + .param("shortLink", String.valueOf(link.getShortLink())) .content(objectMapper.writeValueAsString(request))); resultActions.andExpect(status().isForbidden()); @@ -215,14 +215,14 @@ void editLinkContentForbiddenTest() throws Exception { void editDeletedLinkContentTest() throws Exception { link.setStatus(LinkStatus.DELETED); when(userService.findByEmail(any())).thenReturn(user); - when(linkService.findById(link.getId())).thenReturn(link); + when(linkService.findByShortLink(link.getShortLink())).thenReturn(link); - EditLinkContentRequest request = new EditLinkContentRequest(link.getId(), "short-link-2"); + EditLinkContentRequest request = new EditLinkContentRequest(link.getShortLink(), "short-link-2"); when(linkService.update(link)).thenReturn(link); ResultActions resultActions = mockMvc.perform(post("/api/V1/link/edit/content") .contentType(MediaType.APPLICATION_JSON) - .param("id", String.valueOf(link.getId())) + .param("shortLink", String.valueOf(link.getShortLink())) .content(objectMapper.writeValueAsString(request))); resultActions.andExpect(status().isBadRequest()) @@ -231,25 +231,25 @@ void editDeletedLinkContentTest() throws Exception { } /** - * Test case for the {@link LinkController#refreshLink(UUID)} method. + * Test case for the {@link LinkController#refreshLink(String)} method. */ @Test @WithMockUser void refreshLinkTest() throws Exception { when(userService.findByEmail(any())).thenReturn(user); - when(linkService.findById(link.getId())).thenReturn(link); + when(linkService.findByShortLink(link.getShortLink())).thenReturn(link); when(linkService.update(link)).thenReturn(link); ResultActions resultActions = mockMvc.perform(post("/api/V1/link/edit/refresh") .contentType(MediaType.APPLICATION_JSON) - .param("id", String.valueOf(link.getId()))); + .param("shortLink", String.valueOf(link.getShortLink()))); resultActions.andExpect(status().isOk()) .andExpect(jsonPath("$.error").value("ok")); } /** - * Test case for the {@link LinkController#refreshLink(UUID)} method when + * Test case for the {@link LinkController#refreshLink(String)} method when * the authenticated user does not have rights. */ @Test @@ -262,12 +262,12 @@ void refreshLinkForbiddenTest() throws Exception { .role(UserRole.USER) .build(); when(userService.findByEmail(any())).thenReturn(newUser); - when(linkService.findById(link.getId())).thenReturn(link); + when(linkService.findByShortLink(link.getShortLink())).thenReturn(link); when(linkService.update(link)).thenReturn(link); ResultActions resultActions = mockMvc.perform(post("/api/V1/link/edit/refresh") .contentType(MediaType.APPLICATION_JSON) - .param("id", String.valueOf(link.getId()))); + .param("shortLink", String.valueOf(link.getShortLink()))); resultActions.andExpect(status().isForbidden()); } @@ -281,12 +281,12 @@ void refreshLinkForbiddenTest() throws Exception { void refreshDeletedLinkTest() throws Exception { link.setStatus(LinkStatus.DELETED); when(userService.findByEmail(any())).thenReturn(user); - when(linkService.findById(link.getId())).thenReturn(link); + when(linkService.findByShortLink(link.getShortLink())).thenReturn(link); when(linkService.update(link)).thenReturn(link); ResultActions resultActions = mockMvc.perform(post("/api/V1/link/edit/refresh") .contentType(MediaType.APPLICATION_JSON) - .param("id", String.valueOf(link.getId()))); + .param("shortLink", String.valueOf(link.getShortLink()))); resultActions.andExpect(status().isBadRequest()) .andExpect(jsonPath("$.message").value("The link has already been deleted, " + @@ -301,7 +301,6 @@ void refreshDeletedLinkTest() throws Exception { @WithMockUser void getInfoByShortLinkTest() throws Exception { when(userService.findByEmail(any())).thenReturn(user); - when(linkService.findById(any())).thenReturn(link); when(linkService.findByShortLink(link.getShortLink())).thenReturn(link); ResultActions resultActions = mockMvc.perform(get("/api/V1/link/info") @@ -327,7 +326,6 @@ void getInfoByShortLinkForbiddenTest() throws Exception { .role(UserRole.USER) .build(); when(userService.findByEmail(any())).thenReturn(newUser); - when(linkService.findById(any())).thenReturn(link); when(linkService.findByShortLink(link.getShortLink())).thenReturn(link); ResultActions resultActions = mockMvc.perform(get("/api/V1/link/info") From c60a9121fd4e213c919e8d1b86f7de5d438b9e80 Mon Sep 17 00:00:00 2001 From: nastiausenko Date: Sun, 21 Apr 2024 18:02:04 +0300 Subject: [PATCH 2/2] update LinkControllerIntegrationTest --- .../link/LinkControllerIntegrationTest.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java b/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java index ce43865..fe6b492 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java @@ -24,8 +24,6 @@ import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import java.util.UUID; - import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -102,22 +100,22 @@ void createShortLinkFailsWhenUrlIsInvalid(String url) throws Exception { @Test void deleteLinkWorksCorrectly() throws Exception { - UUID id = UUID.fromString("3053e49b-6da3-4389-9d06-23b2d57b6f25"); - mockMvc.perform(post(baseUrl + "delete" + "?id=" + id) + String shortLink = "short-link-1"; + mockMvc.perform(post(baseUrl + "delete" + "?shortLink=" + shortLink) .contentType(MediaType.APPLICATION_JSON) .header("Authorization", token)) .andExpect(status().isOk()) .andExpect(jsonPath("$.error").value("ok")); } @Test - void deleteLinkFailsWhenIdIsInvalid() throws Exception { - UUID id = UUID.randomUUID(); - mockMvc.perform(post(baseUrl + "delete" + "?id=" + id) + void deleteLinkFailsWhenShortLinkIsInvalid() throws Exception { + String shortLink = "short"; + mockMvc.perform(post(baseUrl + "delete" + "?shortLink=" + shortLink) .contentType(MediaType.APPLICATION_JSON) .header("Authorization", token)) .andExpect(status().is4xxClientError()) .andExpect(jsonPath("$.statusCode").value(404)) - .andExpect(jsonPath("$.message").value("No link by provided id found")) + .andExpect(jsonPath("$.message").value("No link by provided short link found")) .andExpect(jsonPath("$.path").value("/api/V1/link/delete")); } @Test @@ -137,8 +135,8 @@ void deleteLinkFailsWhenUserHasNoRightsForThisLink() throws Exception { String contentAsString = mvcResult.getResponse().getContentAsString(); JSONObject jsonObject = new JSONObject(contentAsString); this.token = "Bearer " + jsonObject.getString("jwtToken"); - UUID id = UUID.fromString("3053e49b-6da3-4389-9d06-23b2d57b6f25"); - mockMvc.perform(post(baseUrl + "delete" + "?id=" + id) + String shortLink = "short-link-1"; + mockMvc.perform(post(baseUrl + "delete" + "?shortLink=" + shortLink) .contentType(MediaType.APPLICATION_JSON) .header("Authorization", token)) .andExpect(status().is4xxClientError())