Skip to content

Commit

Permalink
Merge pull request #69 from nastiausenko/update-tests
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
egorsivenko authored Apr 21, 2024
2 parents 8f64fca + c60a912 commit cb93901
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
6 changes: 6 additions & 0 deletions src/test/java/com/linkurlshorter/urlshortener/TestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,4 +116,9 @@ public UserRepository userRepository() {
public AuthService authService() {
return mock(AuthService.class);
}

@Bean
public ShortLinkGenerator shortLinkGenerator() {
return new ShortLinkGenerator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
Expand All @@ -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")
Expand All @@ -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());
Expand All @@ -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())
Expand All @@ -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
Expand All @@ -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());
}
Expand All @@ -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, " +
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit cb93901

Please sign in to comment.