Untitled

mail@pastecode.io avatar
unknown
java
a year ago
12 kB
4
Indexable
Never
package ba.com.zira.sensubean.test.suites;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import ba.com.zira.commons.exception.ApiException;
import ba.com.zira.commons.message.request.EntityRequest;
import ba.com.zira.commons.message.request.FilterRequest;
import ba.com.zira.commons.message.response.PayloadResponse;
import ba.com.zira.commons.model.PagedData;
import ba.com.zira.commons.model.QueryConditionPage;
import ba.com.zira.sensubean.api.ReviewService;
import ba.com.zira.sensubean.api.model.review.Review;
import ba.com.zira.sensubean.api.model.review.ReviewCreateRequest;
import ba.com.zira.sensubean.api.model.review.ReviewUpdateRequest;
import ba.com.zira.sensubean.core.impl.ReviewServiceImpl;
import ba.com.zira.sensubean.core.utils.LookupService;
import ba.com.zira.sensubean.dao.ReviewDAO;
import ba.com.zira.sensubean.dao.model.GameEntity;
import ba.com.zira.sensubean.dao.model.ReviewEntity;
import ba.com.zira.sensubean.dao.model.ReviewFormulaEntity;
import ba.com.zira.sensubean.mapper.ReviewMapper;
import ba.com.zira.sensubean.test.configuration.ApplicationTestConfiguration;
import ba.com.zira.sensubean.test.configuration.BasicTestConfiguration;

@ContextConfiguration(classes = ApplicationTestConfiguration.class)
public class ReviewServiceTest extends BasicTestConfiguration {

    @Autowired
    private ReviewMapper reviewMapper;

    private ReviewDAO reviewDAO;
    private ReviewService reviewService;
    private LookupService lookupService;

    @BeforeMethod
    public void beforeMethod() throws ApiException {
        this.reviewDAO = Mockito.mock(ReviewDAO.class);
        this.lookupService = Mockito.mock(LookupService.class);
        this.reviewService = new ReviewServiceImpl(lookupService, reviewDAO, reviewMapper);
    }

    @Test(enabled = true)
    public void testFindReview() {
        try {

            GameEntity game = new GameEntity();
            game.setId(1L);
            game.setFullName("Test game");

            ReviewFormulaEntity reviewFormula = new ReviewFormulaEntity();
            reviewFormula.setId(1L);
            reviewFormula.setName("Test review formula");

            List<ReviewEntity> entities = new ArrayList<>();

            ReviewEntity firstReviewEntity = new ReviewEntity();
            firstReviewEntity.setId(1L);
            firstReviewEntity.setTitle("Review Title 1");
            firstReviewEntity.setText("Review Content 1");
            firstReviewEntity.setGame(game);
            firstReviewEntity.setReviewFormula(reviewFormula);

            ReviewEntity secondReviewEntity = new ReviewEntity();
            secondReviewEntity.setId(2L);
            secondReviewEntity.setTitle("Review Title 2");
            secondReviewEntity.setText("Review Content 2");
            secondReviewEntity.setGame(game);
            secondReviewEntity.setReviewFormula(reviewFormula);

            entities.add(firstReviewEntity);
            entities.add(secondReviewEntity);

            PagedData<ReviewEntity> pagedEntities = new PagedData<>();
            pagedEntities.setRecords(entities);

            Map<String, Object> filterCriteria = new HashMap<>();
            QueryConditionPage queryConditionPage = new QueryConditionPage();
            FilterRequest filterRequest = new FilterRequest(filterCriteria, queryConditionPage);

            Mockito.when(reviewDAO.findAll(filterRequest.getFilter())).thenReturn(pagedEntities);

            List<Review> reviewFindResponse = reviewService.find(filterRequest).getPayload();

            List<Review> response = new ArrayList<>();

            var firstResponse = new Review();
            firstResponse.setId(1L);
            firstResponse.setTitle("Review Title 1");
            firstResponse.setText("Review Content 1");
            firstResponse.setGameId(game.getId());
            firstResponse.setReviewFormulaId(reviewFormula.getId());
            firstResponse.setGameName(game.getFullName());
            firstResponse.setReviewFormulaName(reviewFormula.getName());

            var secondResponse = new Review();
            secondResponse.setId(2L);
            secondResponse.setTitle("Review Title 2");
            secondResponse.setText("Review Content 2");
            secondResponse.setGameId(game.getId());
            secondResponse.setReviewFormulaId(reviewFormula.getId());
            secondResponse.setGameName(game.getFullName());
            secondResponse.setReviewFormulaName(reviewFormula.getName());

            response.add(firstResponse);
            response.add(secondResponse);

            Assertions.assertThat(reviewFindResponse).as("Check if both lists have the same elements in the same order")
                    .isEqualTo(response);

        } catch (Exception e) {
            Assert.fail("API Exception occurred: " + e.getMessage());
        }
    }

    @Test(enabled = true)
    public void testCreateReview() {
        try {
            GameEntity game = new GameEntity();
            game.setId(1L);
            game.setFullName("Test game");

            ReviewFormulaEntity reviewFormula = new ReviewFormulaEntity();
            reviewFormula.setId(1L);
            reviewFormula.setName("Test review formula");

            EntityRequest<ReviewCreateRequest> request = new EntityRequest<>();

            ReviewCreateRequest createRequest = new ReviewCreateRequest();
            createRequest.setTitle("New Review Title");
            createRequest.setText("New Review Content");
            createRequest.setGameId(game.getId());
            createRequest.setReviewFormulaId(reviewFormula.getId());

            request.setEntity(createRequest);

            ReviewEntity createdReviewEntity = new ReviewEntity();
            createdReviewEntity.setTitle("New Review Title");
            createdReviewEntity.setText("New Review Content");
            createdReviewEntity.setGame(game);
            createdReviewEntity.setReviewFormula(reviewFormula);

            Review expectedReview = new Review();
            expectedReview.setTitle("New Review Title");
            expectedReview.setText("New Review Content");
            expectedReview.setGameId(game.getId());
            expectedReview.setReviewFormulaId(reviewFormula.getId());

            Mockito.when(reviewDAO.persist(createdReviewEntity)).thenReturn(null);

            PayloadResponse<Review> reviewFindResponse = reviewService.create(request);

            Assertions.assertThat(reviewFindResponse.getPayload()).as("Check all fields").usingRecursiveComparison()
                    .ignoringFields("created", "createdBy", "modified", "modifiedBy").isEqualTo(expectedReview);

        } catch (Exception e) {
            Assert.fail("API Exception occurred: " + e.getMessage());
        }
    }

    @Test(enabled = true)
    public void testUpdateReview() {
        try {

            GameEntity game = new GameEntity();
            game.setId(1L);
            game.setFullName("Test game");

            GameEntity gameUpdate = new GameEntity();
            gameUpdate.setId(2L);
            gameUpdate.setFullName("Test game");

            ReviewFormulaEntity reviewFormula = new ReviewFormulaEntity();
            reviewFormula.setId(1L);
            reviewFormula.setName("Test review formula");

            EntityRequest<ReviewUpdateRequest> request = new EntityRequest<>();

            ReviewEntity reviewEnitity = new ReviewEntity();
            reviewEnitity.setId(1L);
            reviewEnitity.setTitle("New Review Title");
            reviewEnitity.setText("New Review Content");
            reviewEnitity.setGame(game);
            reviewEnitity.setReviewFormula(reviewFormula);

            Review review = new Review();

            review.setId(1L);
            review.setTitle("Update Review Title");
            review.setText("Update Review Content");
            review.setGameId(gameUpdate.getId());
            review.setReviewFormulaId(reviewFormula.getId());
            review.setGameName(gameUpdate.getFullName());
            review.setReviewFormulaName(reviewFormula.getName());

            ReviewUpdateRequest updateReviewRequest = new ReviewUpdateRequest();

            updateReviewRequest.setId(1L);
            updateReviewRequest.setGameId(gameUpdate.getId());
            updateReviewRequest.setTitle("Update Review Title");
            updateReviewRequest.setText("Update Review Content");
            updateReviewRequest.setReviewFormulaId(reviewFormula.getId());

            request.setEntity(updateReviewRequest);

            Mockito.when(reviewDAO.findByPK(request.getEntity().getId())).thenReturn(reviewEnitity);

            Mockito.doNothing().when(reviewDAO).merge(reviewEnitity);

            var reviewUpdateResponse = reviewService.update(request);

            Assertions.assertThat(reviewUpdateResponse.getPayload()).as("Check all fields").usingRecursiveComparison()
                    .ignoringFields("created", "createdBy", "modified", "modifiedBy").isEqualTo(review);

        } catch (Exception e) {
            Assert.fail("API Exception occurred: " + e.getMessage());
        }
    }

    @Test(enabled = true)
    public void testDeleteReview() {
        try {
            EntityRequest<Long> request = new EntityRequest<Long>();

            request.setEntity(2L);

            Mockito.doNothing().when(reviewDAO).removeByPK(request.getEntity());

            reviewService.delete(request);
        } catch (Exception e) {
            Assert.fail("API Exception occurred: " + e.getMessage());
        }
    }

    @Test(enabled = true)
    public void testFindReviewById() {
        try {

            GameEntity game = new GameEntity();
            game.setId(1L);
            game.setFullName("Test game");

            ReviewFormulaEntity reviewFormula = new ReviewFormulaEntity();
            reviewFormula.setId(1L);
            reviewFormula.setName("Test review formula");

            EntityRequest<Long> request = new EntityRequest<Long>();
            request.setEntity(1L);

            var reviewEntity = new ReviewEntity();
            reviewEntity.setId(1L);
            reviewEntity.setTitle("Title");
            reviewEntity.setText("Text");
            reviewEntity.setGame(game);
            reviewEntity.setReviewFormula(reviewFormula);

            var review = new Review();
            review.setId(1L);
            review.setTitle("Title");
            review.setText("Text");
            review.setGameId(game.getId());
            review.setGameName(game.getFullName());
            review.setReviewFormulaId(reviewFormula.getId());
            review.setReviewFormulaName(reviewFormula.getName());

            Mockito.when(reviewDAO.findByPK(request.getEntity())).thenReturn(reviewEntity);
            PayloadResponse<Review> reviewPayloadResponse = reviewService.findById(request);

            Assertions.assertThat(reviewPayloadResponse.getPayload()).as("Check all elements")
                    .overridingErrorMessage("All elements should be equal!").usingRecursiveComparison()
                    .ignoringFields("created", "createdBy", "modified", "modifiedBy", "imageUrl").isEqualTo(review);

        } catch (Exception e) {
            Assert.fail("API Exception occurred: " + e.getMessage());
        }
    }

}