Skip to content

Commit

Permalink
28/12: write test for AlbumService
Browse files Browse the repository at this point in the history
  • Loading branch information
thuan committed Dec 28, 2023
1 parent 0fed027 commit e35c6ac
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 39 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/spotify/app/model/Album.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@ public void removeSong(Song song) {
}
}
}

public Album(Long id, String name) {
this.id = id;
this.name = name;
}

public Album(String name) {
this.name = name;
}
}
38 changes: 20 additions & 18 deletions src/main/java/com/spotify/app/service/AlbumService.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ public void addSong(Long albumId, Long songId) {
}

public void removeSong(Long albumId, Long songId) {
Album album = get(albumId);
Song song = getSongBySongId(songId);
Album album = albumRepository.
findById(albumId).
orElseThrow(() ->
new ResourceNotFoundException(String.format("album with id [%d] not found", albumId)));
Song song = songRepository.
findById(songId).
orElseThrow(() ->
new ResourceNotFoundException(String.format("song with id %d not found", songId))) ;
album.removeSong(song);
albumRepository.save(album);
}
Expand All @@ -136,7 +142,9 @@ public List<AlbumResponse> findAll() {


public Long addAlbum(Long userId, AlbumRequest request) {
User user = getUserByUserId(userId);
User user = userRepository.
findById(userId).
orElseThrow(() -> new ResourceNotFoundException(String.format("user %d not found", userId)));
Album album = albumRequestMapper.dtoToEntity(request);
album.setReleaseDate(LocalDateTime.now());
album.setUser(user);
Expand All @@ -145,7 +153,11 @@ public Long addAlbum(Long userId, AlbumRequest request) {
}

public void updateAlbum(Long albumId, AlbumRequest request) {
Album album = get(albumId);
Album album = albumRepository.
findById(albumId).
orElseThrow(() ->
new ResourceNotFoundException(String.format("album with id [%d] not found", albumId)));

if(!request.name().equals(album.getName())){
album.setName(request.name());
}
Expand All @@ -157,19 +169,6 @@ public List<AlbumResponse> findAlbumByUserId(Long userId) {
return albums.stream().map(albumResponseMapper::albumToAlbumResponse).toList();
}

public User getUserByUserId(Long userId) {
return userRepository.
findById(userId).
orElseThrow(() -> new ResourceNotFoundException(String.format("user %d not found", userId)));
}


public Song getSongBySongId(Long songId) {
return songRepository.
findById(songId).
orElseThrow(() ->
new ResourceNotFoundException(String.format("song with id %d not found",songId))) ;
}

public String convertTotalTime(List<AlbumSong> albumSongs) {
long totalTime = albumSongs.stream()
Expand Down Expand Up @@ -200,7 +199,10 @@ public AlbumResponse findByIdByAdmin(Long albumId) {
}

public String updateStatusAlbum(Long albumId) {
Album album = get(albumId);
Album album = albumRepository.
findById(albumId).
orElseThrow(() ->
new ResourceNotFoundException(String.format("album with id [%d] not found", albumId)));
album.setStatus(!album.isStatus());
albumRepository.saveAndFlush(album);
String status = !album.isStatus() ? "enabled" : "disabled";
Expand Down
201 changes: 180 additions & 21 deletions src/test/java/com/spotify/app/album/AlbumServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@


import com.spotify.app.dto.AlbumDTO;
import com.spotify.app.dto.request.AlbumRequest;
import com.spotify.app.dto.response.SongResponse;
import com.spotify.app.dto.response.UserNoAssociationResponse;
import com.spotify.app.exception.ResourceNotFoundException;
import com.spotify.app.mapper.AlbumMapper;
import com.spotify.app.mapper.AlbumRequestMapper;
import com.spotify.app.mapper.AlbumResponseMapper;
Expand All @@ -22,6 +24,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -31,7 +34,7 @@
import java.util.Optional;

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

Expand Down Expand Up @@ -126,46 +129,202 @@ void canGetAlbumById () {
assertThat(actual).isEqualTo(album);
}

// add song
// @Test
// public void canAddSong () {
// // given
// Long albumId = 2L;
// Long songId = 1L;
// Album album2 = new Album(albumId);
// Song song3 = new Song(songId);
// AlbumSong albumSong = new AlbumSong(album2, song3);
//
// List<AlbumSong> albumSongList = List.of(albumSong);
// album2.setAlbumSongList(albumSongList);
// // when
// when(albumRepository.findById(albumId)).thenReturn(Optional.of(album2));
// when(songRepository.findById(songId)).thenReturn(Optional.of(song3));
// albumService.addSong(albumId, songId);
// }
@Test
public void canAddSong () {
Long albumId = 1L;
Long songId = 2L;

// Mocking the Album and Song
Album album = new Album();
album.setId(albumId);

Song song = new Song();
song.setId(songId);

// Mocking the repository responses
Mockito.when(albumRepository.findById(albumId)).thenReturn(Optional.of(album));
Mockito.when(songRepository.findById(songId)).thenReturn(Optional.of(song));
Mockito.when(albumRepository.save(Mockito.any(Album.class))).thenReturn(album);

// Calling the service method
albumService.addSong(albumId, songId);

// Verifying that the repository methods were called
Mockito.verify(albumRepository, Mockito.times(1)).findById(albumId);
Mockito.verify(songRepository, Mockito.times(1)).findById(songId);
Mockito.verify(albumRepository, Mockito.times(1)).save(album);
}

@Test
public void shouldThrowExceptionWithNonexistentAlbum () {
Long albumId = 1L;
Long songId = 2L;
Mockito.when(albumRepository.findById(albumId)).thenReturn(Optional.empty());

assertThrows(ResourceNotFoundException.class, () -> {
albumService.addSong(albumId, songId);
});
Mockito.verify(albumRepository, Mockito.times(1)).findById(albumId);
Mockito.verify(songRepository, Mockito.never()).findById(albumId);
Mockito.verify(albumRepository, Mockito.never()).save(Mockito.any(Album.class));

}
// remove song
@Test
public void canRemoveSong () {
public void canRemoveSong() {
// Sample data
Long albumId = 1L;
Long songId = 2L;

// Mocking the Album and Song
Album album = new Album();
album.setId(albumId);

Song song = new Song();
song.setId(songId);

// Mocking the repository responses
Mockito.when(albumRepository.findById(albumId)).thenReturn(Optional.of(album));
Mockito.when(songRepository.findById(songId)).thenReturn(Optional.of(song));
Mockito.when(albumRepository.save(Mockito.any(Album.class))).thenReturn(album);

// Calling the service method
albumService.removeSong(albumId, songId);

// Verifying that the repository methods were called
Mockito.verify(albumRepository, Mockito.times(1)).findById(albumId);
Mockito.verify(songRepository, Mockito.times(1)).findById(songId);
Mockito.verify(albumRepository, Mockito.times(1)).save(album);
}

@Test
public void cannotRemoveSongWithNonexistentAlbum() {
// Sample data
Long albumId = 1L;
Long songId = 2L;

// Mocking the repository responses
Mockito.when(albumRepository.findById(albumId)).thenReturn(Optional.empty());

// Calling the service method and expecting an exception
assertThrows(ResourceNotFoundException.class, () -> {
albumService.removeSong(albumId, songId);
});

// Verifying that the repository methods were called
Mockito.verify(albumRepository, Mockito.times(1)).findById(albumId);
Mockito.verify(songRepository, Mockito.never()).findById(songId);
Mockito.verify(albumRepository, Mockito.never()).save(Mockito.any(Album.class));
}

// add album

@Test
public void canAddAlbum () {
// given
Long userId = 1L;
Long albumId= 1L;
AlbumRequest request = new AlbumRequest("album_name");
Album album1 = new Album(albumId,"album_name");
User user1 = new User(userId);

// when
Mockito.when(userRepository.findById(userId)).thenReturn(Optional.of(user1));
Mockito.when(albumRequestMapper.dtoToEntity(request)).thenReturn(album1);
Mockito.when(albumRepository.save(album1)).thenReturn(album1);
var actualId = albumService.addAlbum(userId, request);

// then
assertEquals(actualId, albumId);
Mockito.verify(userRepository, Mockito.times(1)).findById(userId);
Mockito.verify(albumRepository, Mockito.times(1)).save(Mockito.any(Album.class));
}

@Test
public void cannotAddAlbumWithNonexistentUser() {
// Sample data
Long userId = 1L;
String albumName = "albumName";
AlbumRequest albumRequest = new AlbumRequest(albumName);

// Mocking the UserRepository response
Mockito.when(userRepository.findById(userId)).thenReturn(Optional.empty());

// Calling the service method and expecting an exception
assertThrows(ResourceNotFoundException.class, () -> {
albumService.addAlbum(userId, albumRequest);
});

// Verifying that the repository methods were called
Mockito.verify(userRepository, Mockito.times(1)).findById(userId);
Mockito.verify(albumRequestMapper, Mockito.never()).dtoToEntity(Mockito.any(AlbumRequest.class));
Mockito.verify(albumRepository, Mockito.never()).save(Mockito.any(Album.class));
}
// remove album
@Test
public void canRemoveAlbum () {
public void canUpdateAlbum () {
// given
Long albumId = 1L;
String updatedName = "Updated Album Name";
Album existingAlbum = new Album();
existingAlbum.setId(albumId);
existingAlbum.setName("Old Album Name");
AlbumRequest request = new AlbumRequest(updatedName);

// when
Mockito.when(albumRepository.findById(albumId)).thenReturn(Optional.of(existingAlbum));
Mockito.when(albumRepository.save(Mockito.any(Album.class))).thenReturn(existingAlbum);
albumService.updateAlbum(albumId, request);
// then
Mockito.verify(albumRepository, Mockito.times(1)).findById(albumId);
Mockito.verify(albumRepository, Mockito.times(1)).save(existingAlbum);
assertEquals(updatedName, existingAlbum.getName());

}

@Test
public void cannotUpdateAlbumWithNonexistentAlbum() {
// Sample data
Long albumId = 1L;
String updatedName = "Updated Album Name";

// Mocking the AlbumRepository response
Mockito.when(albumRepository.findById(albumId)).thenReturn(Optional.empty());

// Calling the service method and expecting an exception
assertThrows(ResourceNotFoundException.class, () -> {
AlbumRequest updateRequest = new AlbumRequest(updatedName);
albumService.updateAlbum(albumId, updateRequest);
});

// Verifying that the repository methods were called
Mockito.verify(albumRepository, Mockito.times(1)).findById(albumId);
Mockito.verify(albumRepository, Mockito.never()).save(Mockito.any(Album.class));
}

// update status
@Test
public void canUpdateStatus () {
Long albumId = 1L;

// Mocking the AlbumRepository response
Album existingAlbum = new Album();
existingAlbum.setId(albumId);
existingAlbum.setStatus(true);

Mockito.when(albumRepository.findById(albumId)).thenReturn(Optional.of(existingAlbum));
existingAlbum.setStatus(false);
Mockito.when(albumRepository.saveAndFlush(Mockito.any(Album.class))).thenAnswer(invocation -> invocation.getArgument(0));

// Calling the service method
String result = albumService.updateStatusAlbum(albumId);

// Verifying that the repository methods were called
Mockito.verify(albumRepository, Mockito.times(1)).findById(albumId);
Mockito.verify(albumRepository, Mockito.times(1)).saveAndFlush(existingAlbum);

// Verifying the result
String expectedStatusMessage = "album with id: 1 is disabled"; // Assuming initial status is true
assertEquals(expectedStatusMessage, result);
}


Expand Down

0 comments on commit e35c6ac

Please sign in to comment.