-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4974ce8
commit c8bc08f
Showing
1 changed file
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.*; | ||
|
||
|
@@ -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. | ||
*/ | ||
|
@@ -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); | ||
} | ||
} |