Skip to content

Commit

Permalink
Merge pull request #88 from nastiausenko/update-tests
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
IvanShalaev1990 authored Apr 24, 2024
2 parents 2204746 + 479785b commit 7747499
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

/**
Expand Down Expand Up @@ -120,26 +119,6 @@ public void updateRedisLink(String shortLink, Link link) {
}
}

/**
* Retrieves a link entity by its ID.
*
* @param id The ID of the link entity to retrieve.
* @return The retrieved link entity.
* @throws NullLinkPropertyException If the 'id' parameter is null.
* @throws NoLinkFoundByIdException If no link is found with the given ID.
* @throws DeletedLinkException If the retrieved link has been marked as deleted.
*/
public Link findById(UUID id) { // **Unused method**
if (Objects.isNull(id)) {
throw new NullLinkPropertyException();
}
Link link = linkRepository.findById(id).orElseThrow(NoLinkFoundByIdException::new);
if (link.getStatus() == LinkStatus.DELETED) {
throw new DeletedLinkException();
}
return link;
}

/**
* Retrieves a link entity by its short link.
*
Expand Down Expand Up @@ -195,16 +174,6 @@ public void deleteByShortLink(String shortLink) {
linkRepository.save(link);
}

public void deleteById(UUID id) { // **Unused method**
if (Objects.isNull(id)) {
throw new NullLinkPropertyException();
}
linkRepository.deleteById(id);
}
public Optional<Link> findByUniqueShortLink(String shortLink) {
return linkRepository.findByShortLink(shortLink);
}

/**
* Searches for a unique existing link by a short link and returns true if such link exists, false otherwise
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ spring.datasource.password=${POSTGRES_PASSWORD:password}
spring.flyway.locations=classpath:db/migration/prod

##Redis
spring.redis.host=${REDIS_HOST:localhost}
spring.redis.port=${REDIS_PORT:6379}
spring.data.redis.host=${REDIS_HOST:localhost}
spring.data.redis.port=${REDIS_PORT:6379}

Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ void createLinkFailedTest() throws Exception {
resultActions.andExpect(status().isInternalServerError());
}

/**
* Test case for the {@link LinkController#createLink(CreateLinkRequest)} method when
* an error occurs during the short link generation process.
*/
@Test
@WithMockUser
void createLinkInternalErrorTest() throws Exception {
when(userService.findByEmail(any())).thenReturn(user);
when(linkService.doesLinkExist(any())).thenReturn(true);

CreateLinkRequest request = new CreateLinkRequest("https://www.example.com", null);

ResultActions resultActions = mockMvc.perform(post("/api/V1/link/create")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)));

resultActions.andExpect(status().isInternalServerError());
}

/**
* Test case for the {@link LinkController#deleteLink(String)} method.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.linkurlshorter.urlshortener.link;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkurlshorter.urlshortener.user.User;
import com.linkurlshorter.urlshortener.user.UserRole;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -16,7 +18,6 @@

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

Expand All @@ -39,6 +40,9 @@ class LinkServiceTest {
@Mock
private Jedis jedis;

@Mock
private ObjectMapper mapper;

private Link link;

/**
Expand Down Expand Up @@ -115,50 +119,63 @@ void updateDeletedLinkTest() {
}

/**
* Test case for the {@link LinkService#findById(UUID)} method.
* Test case for the {@link LinkService#updateRedisShortLink(String, String)} method.
*/
@Test
void findByIdTest() {
when(linkRepository.findById(link.getId())).thenReturn(Optional.ofNullable(link));
Link foundLink = linkService.findById(link.getId());
void updateRedisShortLinkTest() {
String shortLink = link.getShortLink();
String newShortLink = "short-link-2";

assertThat(foundLink).isNotNull().isEqualTo(link);
verify(linkRepository, times(1)).findById(link.getId());
when(jedisPool.getResource()).thenReturn(jedis);
when(jedis.exists(anyString())).thenReturn(true);
linkService.updateRedisShortLink(shortLink, newShortLink);

verify(jedis, times(1)).rename(shortLink, newShortLink);
}

/**
* Test case for the {@link LinkService#findById(UUID)} method when the link with provided id
* does not exist.
* Test case for the {@link LinkService#updateRedisLink(String, Link)} method.
*/
@Test
void findByIdNotFoundTest() {
UUID nonExistentUserId = UUID.randomUUID();
when(linkRepository.findById(nonExistentUserId)).thenReturn(Optional.empty());
void updateRedisLinkTest() throws JsonProcessingException {
String shortLink = link.getShortLink();

assertThatThrownBy(() -> linkService.findById(nonExistentUserId))
.isInstanceOf(NoLinkFoundByIdException.class);
when(jedisPool.getResource()).thenReturn(jedis);
when(jedis.exists(anyString())).thenReturn(true);
linkService.updateRedisLink(shortLink, link);

verify(jedis, times(1)).set(shortLink, mapper.writeValueAsString(link));
}

/**
* Test case for the {@link LinkService#findById(UUID)} method when the link with provided id
* is deleted.
* Test case for the {@link LinkService#getLongLinkFromShortLink(String)} method.
*/
@Test
void findByIdDeletedTest() {
link.setStatus(LinkStatus.DELETED);
when(linkRepository.findById(link.getId())).thenReturn(Optional.of(link));
void getLongLinkFromShortLinkTest() throws JsonProcessingException {
when(jedisPool.getResource()).thenReturn(jedis);
when(jedis.exists(anyString())).thenReturn(true);
when(jedis.get(anyString())).thenReturn("{}");
when(mapper.readValue(anyString(), eq(Link.class))).thenReturn(link);

assertThatThrownBy(() -> linkService.findById(link.getId()))
.isInstanceOf(DeletedLinkException.class);
String actualLongLink = linkService.getLongLinkFromShortLink(link.getShortLink());

assertThat(actualLongLink).isEqualTo(link.getLongLink());
}

/**
* Test case for the {@link LinkService#findById(UUID)} method when the provided id is null.
* Test case for the {@link LinkService#getLongLinkFromShortLink(String)} method when link status
* is inactive.
*/
@Test
void findByNullIdTest() {
assertThatThrownBy(() -> linkService.findById(null))
.isInstanceOf(NullLinkPropertyException.class);
void getLongLinkFromShortLinkInactiveTest() throws JsonProcessingException {
link.setStatus(LinkStatus.INACTIVE);
when(jedisPool.getResource()).thenReturn(jedis);
when(jedis.exists(anyString())).thenReturn(true);
when(jedis.get(anyString())).thenReturn("{}");
when(mapper.readValue(anyString(), eq(Link.class))).thenReturn(link);

assertThatThrownBy(() -> linkService.getLongLinkFromShortLink(link.getShortLink()))
.isInstanceOf(InactiveLinkException.class);
}

/**
Expand Down Expand Up @@ -189,10 +206,22 @@ void findByNullShortLinkTest() {
*/
@Test
void findByShortLinkNotFoundTest() {
assertThatThrownBy(() -> linkService.deleteByShortLink("https://link/short"))
assertThatThrownBy(() -> linkService.findByShortLink("short-link-2"))
.isInstanceOf(NoLinkFoundByShortLinkException.class);
}

/**
* Test case for the {@link LinkService#findByShortLink(String)} method when the
* provided short link is deleted.
*/
@Test
void findByShortLinkDeletedTest() {
when(linkRepository.findByShortLink(link.getShortLink())).thenReturn(Optional.of(link));
link.setStatus(LinkStatus.DELETED);
assertThatThrownBy(() -> linkService.findByShortLink(link.getShortLink()))
.isInstanceOf(DeletedLinkException.class);
}

/**
* Test case for the {@link LinkService#findAllByUserId(UUID)} method.
*/
Expand Down Expand Up @@ -268,34 +297,4 @@ void deleteByNullShortLinkTest() {
assertThatThrownBy(() -> linkService.deleteByShortLink(null))
.isInstanceOf(NullLinkPropertyException.class);
}

/**
* Test case for the {@link LinkService#deleteById(UUID)} method.
*/
@Test
void deleteByIdTest() {
assertAll(() -> linkService.deleteById(link.getId()));
verify(linkRepository, times(1)).deleteById(link.getId());
}

/**
* Test case for the {@link LinkService#deleteById(UUID)} method when the
* provided id is null.
*/
@Test
void deleteByNullIdTest() {
assertThatThrownBy(() -> linkService.deleteById(null))
.isInstanceOf(NullLinkPropertyException.class);
}


/**
* Test case for the {@link LinkService#doesLinkExist(String)} method.
*/
@Test
void findByExistUniqueLinkTest() {
when(linkRepository.findByShortLink(link.getShortLink())).thenReturn(Optional.ofNullable(link));
linkService.doesLinkExist(link.getShortLink());
assertThat(LinkStatus.ACTIVE).isEqualTo(link.getStatus());
}
}

0 comments on commit 7747499

Please sign in to comment.