Skip to content

Commit

Permalink
add tests for LinkService
Browse files Browse the repository at this point in the history
  • Loading branch information
nastiausenko committed Apr 17, 2024
1 parent 4974ce8 commit c8bc08f
Showing 1 changed file with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

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 Down Expand Up @@ -188,6 +188,66 @@ void findByShortLinkNotFoundTest() {
.isInstanceOf(NoLinkFoundByShortLinkException.class);
}

/**
* Test case for the {@link LinkService#findAllByUser(User)} method.
*/
@Test
void findByAllByUserTest() {
User user = User.builder()
.id(UUID.fromString("84991c79-f6a9-4b7b-b1b4-0d66c0b92c81"))
.email("[email protected]")
.password("password1")
.role(UserRole.USER)
.build();

List<Link> userLinks = Arrays.asList(
Link.builder().id(UUID.randomUUID()).user(user).build(),
Link.builder().id(UUID.randomUUID()).user(user).build(),
Link.builder().id(UUID.randomUUID()).user(user).build()
);

when(linkRepository.findAllByUser(user)).thenReturn(userLinks);
List<Link> foundLinks = linkService.findAllByUser(user);

assertThat(foundLinks).isNotNull().isEqualTo(userLinks);
verify(linkRepository, times(1)).findAllByUser(user);
}

/**
* Test case for the {@link LinkService#findAllByUser(User)} method when the provided user is null.
*/
@Test
void findAllByNullUserTest() {
assertThatThrownBy(() -> linkService.findAllByUser(null))
.isInstanceOf(NullLinkPropertyException.class);
}

/**
* Test case for the {@link LinkService#getLinkUsageStatsByUserId(UUID)} method.
*/
@Test
void getLinkUsageStatsByUserIdTest() {
UUID userId = UUID.randomUUID();
List<LinkStatisticsDto> expectedStats = Collections.singletonList(
new LinkStatisticsDto(UUID.randomUUID(), "shortLink1", 10)
);
when(linkRepository.getLinkUsageStatsForUser(userId)).thenReturn(expectedStats);

List<LinkStatisticsDto> actualStats = linkService.getLinkUsageStatsByUserId(userId);

assertThat(actualStats).isNotNull().isEqualTo(expectedStats);
verify(linkRepository, times(1)).getLinkUsageStatsForUser(userId);
}

/**
* Test case for the {@link LinkService#findAllByUser(User)} method when the provided user is null.
*/
@Test
void getLinkUsageStatsByUserIdNullTest() {
assertThatThrownBy(() -> linkService.getLinkUsageStatsByUserId(null))
.isInstanceOf(NullLinkPropertyException.class);
}

/**
* Test case for the {@link LinkService#deleteByShortLink(String)} method.
*/
Expand All @@ -207,4 +267,23 @@ 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);
}
}

0 comments on commit c8bc08f

Please sign in to comment.