Skip to content

Commit

Permalink
Added unity test to VideoServiceImpl (#77)
Browse files Browse the repository at this point in the history
- Create test to VideoServiceImpl
- Chagend action name of create-release.yaml
  • Loading branch information
LauroSilveira authored Dec 18, 2023
1 parent 73c2064 commit 06c7e28
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
tags:
- 'v*'
jobs:
build:
create-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
tags: [ 'v*.*.*' ]

jobs:
docker:
docker-publish-image:
runs-on: ubuntu-latest
steps:
- name: Login to Docker Hub
Expand All @@ -17,5 +17,7 @@ jobs:
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: laurocorreia/alura-flix-api:latest
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.alura.aluraflixapi.domain.video.dto.UpdateVideoDto;
import com.alura.aluraflixapi.domain.video.dto.VideoDto;
import org.mapstruct.Mapper;
import org.springframework.data.domain.PageImpl;

import java.util.List;

Expand All @@ -15,9 +14,9 @@ public interface VideoMapper {

VideoDto mapToVideoDto(Video video);

UpdateVideoDto mapUpdateVideoDto(Video updateDto);
UpdateVideoDto mapToUpdateVideoDto(Video model);

Video mapUpdateVideoToModel(UpdateVideoDto updateVideoDto);
Video mapToModel(UpdateVideoDto updateVideoDto);

List<VideoDto> maptoList(List<Video> videos);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import com.alura.aluraflixapi.infraestructure.mapper.VideoMapper;
import com.alura.aluraflixapi.infraestructure.repository.CategoryRepository;
import com.alura.aluraflixapi.infraestructure.repository.VideoRepository;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
Expand All @@ -23,87 +25,86 @@
@Slf4j
public class VideoServiceImpl implements VideoService {

private static final String LOGGIN_PREFIX = "[VideoServiceImpl]";
private final VideoRepository videoRepository;
private final CategoryRepository categoryRepository;
private final VideoMapper videoMapper;

@Autowired
public VideoServiceImpl(VideoRepository videoRepository, VideoMapper videoMapper,
CategoryRepository categoryRepository) {
this.videoRepository = videoRepository;
this.videoMapper = videoMapper;
this.categoryRepository = categoryRepository;
}

@Override
public Page<VideoDto> getVideos(Pageable pageable) {
Page<Video> pages = videoRepository.findAll(pageable);

pages.get()
.forEach(video -> {
if (Objects.isNull(video.getCategory())) {
video.setCategory(Category.builder()
.id(null)
.rating(Rating.FREE.name())
.title(null)
.colorHex(null)
.build());
}
});

return new PageImpl<>(this.videoMapper.maptoList(pages.getContent()));
}

@Override
public VideoDto save(VideoDto dto) {
try {
log.info("{} Saving new video: {}", LOGGIN_PREFIX, dto.toString());
final var entityToPersist = videoMapper.mapToModel(dto);
final var entityPersisted = this.videoRepository.save(entityToPersist);
log.info("{} New Video saved", LOGGIN_PREFIX);
return videoMapper.mapToVideoDto(entityPersisted);
} catch (Exception e) {
throw new RuntimeException("Error to persist entity", e.getCause());
private static final String LOGGING_PREFIX = "[VideoServiceImpl]";
private final VideoRepository videoRepository;
private final CategoryRepository categoryRepository;
private final VideoMapper videoMapper;

@Autowired
public VideoServiceImpl(VideoRepository videoRepository, VideoMapper videoMapper,
CategoryRepository categoryRepository) {
this.videoRepository = videoRepository;
this.videoMapper = videoMapper;
this.categoryRepository = categoryRepository;
}
}

@Override
public UpdateVideoDto updateMovie(UpdateVideoDto dto) {
try {

final Video entity = videoMapper.mapUpdateVideoToModel(dto);
//The mapping framework does not handle cascading saves.
final var categorySaved = categoryRepository.save(entity.getCategory());
entity.setCategory(categorySaved);
videoRepository.save(entity);
return videoMapper.mapUpdateVideoDto(entity);
} catch (Exception e) {
log.error("Error to try Update entity by method Put {}", e.getMessage());
return null;

@Override
public Page<VideoDto> getVideos(Pageable pageable) {
Page<Video> pages = videoRepository.findAll(pageable);

pages.get()
.forEach(video -> {
if (Objects.isNull(video.getCategory())) {
video.setCategory(Category.builder()
.id(null)
.rating(Rating.FREE.name())
.title(null)
.colorHex(null)
.build());
}
});

return new PageImpl<>(this.videoMapper.maptoList(pages.getContent()));
}

@Override
public VideoDto save(VideoDto dto) {
try {
log.info("{} Saving new video: {}", LOGGING_PREFIX, dto.toString());
final var entityToPersist = videoMapper.mapToModel(dto);
final var entityPersisted = this.videoRepository.save(entityToPersist);
log.info("{} New Video saved", LOGGING_PREFIX);
return videoMapper.mapToVideoDto(entityPersisted);
} catch (Exception e) {
throw new RuntimeException("Error to persist entity", e.getCause());
}
}

}

@Override
public Optional<VideoDto> delete(String id) {
Optional<Video> entityToDelete = videoRepository.findById(id);
entityToDelete.ifPresent(videoRepository::delete);
return entityToDelete.map(videoMapper::mapToVideoDto);
}

@Override
public VideoDto getById(String id) {
return videoRepository.findById(id)
.map(videoMapper::mapToVideoDto)
.orElse(null);
}

@Override
public List<VideoDto> getVideosByTitle(String name) {
return this.videoRepository.findByTitleLike(name)
.stream()
.map(this.videoMapper::mapToVideoDto)
.toList();
}
@Override
public UpdateVideoDto updateMovie(UpdateVideoDto dto) {
try {

final Video entity = videoMapper.mapToModel(dto);
//The mapping framework does not handle cascading saves.
final var categorySaved = categoryRepository.save(entity.getCategory());
entity.setCategory(categorySaved);
videoRepository.save(entity);
return videoMapper.mapToUpdateVideoDto(entity);
} catch (Exception e) {
throw new RuntimeException("Error to update movie", e.getCause());
}

}

@Override
public Optional<VideoDto> delete(String id) {
Optional<Video> entityToDelete = videoRepository.findById(id);
entityToDelete.ifPresent(this.videoRepository::delete);
return entityToDelete.map(videoMapper::mapToVideoDto);
}

@Override
public VideoDto getById(String id) {
return videoRepository.findById(id)
.map(videoMapper::mapToVideoDto)
.orElse(null);
}

@Override
public List<VideoDto> getVideosByTitle(String name) {
return this.videoRepository.findByTitleLike(name)
.stream()
.map(this.videoMapper::mapToVideoDto)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package com.alura.aluraflixapi.infraestructure.service;

import com.alura.aluraflixapi.domain.video.Video;
import com.alura.aluraflixapi.domain.video.dto.UpdateVideoDto;
import com.alura.aluraflixapi.domain.video.dto.VideoDto;
import com.alura.aluraflixapi.infraestructure.mapper.VideoMapperImpl;
import com.alura.aluraflixapi.infraestructure.repository.CategoryRepository;
import com.alura.aluraflixapi.infraestructure.repository.VideoRepository;
import com.alura.aluraflixapi.jsonutils.ParseJson;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;

@ExtendWith(SpringExtension.class)
class VideoServiceImplTest extends ParseJson {

@MockBean
private VideoRepository videoRepository;
@SpyBean
private VideoMapperImpl videoMapper;
@MockBean
private CategoryRepository categoryRepository;
@SpyBean
private VideoServiceImpl videoService;

@Test
void getVideos_response_ok_test() {
//Given
final var jsonFile = getJsonFile("getAllVideos_response_ok.json");
final var videos = parseToJavaObject(jsonFile, Video[].class);
final Page<Video> pagesVideos = new PageImpl<>(Arrays.stream(videos).toList());

final var videosDto = this.videoMapper.maptoList(Arrays.stream(videos).toList());
when(this.videoRepository.findAll(any(Pageable.class))).thenReturn(pagesVideos);
when(this.videoMapper.maptoList(anyList())).thenReturn(videosDto);

//when
final var response = this.videoService.getVideos(Pageable.ofSize(10));

//Then
Assertions.assertThat(response).isNotNull();
Assertions.assertThat(response.getContent()).usingRecursiveComparison().isEqualTo(videosDto);
}

@Test
void save_response_ok_test() {
//Given
final var jsonFile = getJsonFile("create_video_response_ok.json");
final var videoDto = parseToJavaObject(jsonFile, VideoDto.class);
final var videoModel = this.videoMapper.mapToModel(videoDto);
when(this.videoMapper.mapToModel(any(VideoDto.class))).thenReturn(videoModel);
when(this.videoMapper.mapToVideoDto(any())).thenReturn(videoDto);

//When
final var videoCreated = this.videoService.save(videoDto);

//then
Assertions.assertThat(videoCreated).isNotNull();
Assertions.assertThat(videoCreated).usingRecursiveComparison().isEqualTo(videoDto);
}

@Test
void updateMovie_response_ok_test() {

//Given
final var jsonFile = getJsonFile("update_video_response_ok.json");
final var updateVideoDtoParsed = parseToJavaObject(jsonFile, UpdateVideoDto.class);
final var model = this.videoMapper.mapToModel(updateVideoDtoParsed);
final var updateVideoDto = this.videoMapper.mapToUpdateVideoDto(model);
when(this.videoMapper.mapToModel(any(UpdateVideoDto.class))).thenReturn(model);
when(this.videoMapper.mapToUpdateVideoDto(any())).thenReturn(updateVideoDto);

//When
final var response = this.videoService.updateMovie(updateVideoDtoParsed);

//Then
Assertions.assertThat(response).isNotNull();
Assertions.assertThat(response).usingRecursiveComparison().isEqualTo(updateVideoDtoParsed);
}

@Test
void delete_response_ok_test() {
//Given
final var jsonFile = getJsonFile("delete_video_response_ok.json");
final var model = parseToJavaObject(jsonFile, Video.class);
final var videoDto = this.videoMapper.mapToVideoDto(model);
when(this.videoRepository.findById(anyString())).thenReturn(Optional.of(model));
when(this.videoMapper.mapToVideoDto(model)).thenReturn(videoDto);

//When
final var response = this.videoService.delete("63680c011892283477b3e9b9");

//Then
Assertions.assertThat(response.get()).usingRecursiveComparison().isEqualTo(videoDto);
}

@Test
void getById_response_ok_test() {
//Given
final var jsonFile = getJsonFile("getById_video_response_ok.json");
final var model = parseToJavaObject(jsonFile, Video.class);
when(videoRepository.findById(anyString())).thenReturn(Optional.of(model));

//When
final var response = this.videoService.getById("63680c011892283477b3e9b9");

//Then
Assertions.assertThat(response).usingRecursiveComparison().isEqualTo(this.videoMapper.mapToVideoDto(model));
}

@Test
void getVideosByTitle_response_ok() {
//Given
final var jsonFile = getJsonFile("getById_video_response_ok.json");
final var video = parseToJavaObject(jsonFile, Video.class);
final var videoDtoExpected = this.videoMapper.mapToVideoDto(video);
when(this.videoRepository.findByTitleLike(anyString())).thenReturn(List.of(video));

//When
final var response = this.videoService.getVideosByTitle("Fantasy");

//Then
Assertions.assertThat(response.get(0)).usingRecursiveComparison().isEqualTo(videoDtoExpected);
}
}
Loading

0 comments on commit 06c7e28

Please sign in to comment.