PublisherServiceDefaultTest.java 6.3 KB
package org.legrog.web.publisher;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.legrog.entities.*;
import org.legrog.test.MockitoExtension;
import org.legrog.web.xyz.SharedService;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

/**
 * Classe testant PublisherServiceDefault.
 */
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
@DisplayName("Service layer for all publisher-related data")
public class PublisherServiceDefaultTest {
    Logger logger = LoggerFactory.getLogger(getClass());

    PublisherServiceDefault publisherServiceDefault;
    PublisherVersion publisherVersion;
    PublisherVersion publisherVersion1;
    Publisher publisher;
    PublisherRepository publisherRepository;
    PublisherVersionRepository publisherVersionRepository;

    @Mock
    PublisherVersion publisherVersionMock;

    @Captor
    ArgumentCaptor<IndexedPublisher> indexedPublisherArgumentCaptor;

    @BeforeEach
    public void setUp(@Mock PublisherRepository publisherRepository,
                      @Mock PublisherVersionRepository publisherVersionRepository,
                      @Mock PublisherActionRepository publisherActionRepository,
                      @Mock PublisherSearchRepository publisherSearchRepository,
                      @Mock SharedService sharedService) throws Exception {
        publisherServiceDefault = new PublisherServiceDefault(publisherRepository,
                publisherVersionRepository, publisherActionRepository, publisherSearchRepository, sharedService);
        publisherVersion = new PublisherVersion();
        publisherVersion1 = new PublisherVersion();
        this.publisherRepository = publisherRepository;
    }

    @DisplayName("When a new publisher is added, both Publisher and PublisherVersion should be saved in the right state")
    @Test
    public void testAddNewPublisher(@Mock PublisherVersionRepository publisherVersionRepository) {
        publisherServiceDefault.addNewPublisher(publisherVersion);
        publisher = publisherVersion.getPublisher();
        assertThat(publisher.getVersions()).containsExactly(publisherVersion);
        assertThat(publisherVersion.getPublisher()).isEqualTo(publisher);
        verify(publisherRepository).save(publisher);
        verify(publisherVersionRepository).save(publisherVersion);
    }

    @DisplayName("When a new version of a publisher is added, setting it up, attaching it to publisher and saving both")
    @Test
    public void testAddVersionToPublisher(@Mock PublisherRepository publisherRepository,
                                          @Mock PublisherVersionRepository publisherVersionRepository) {
        publisherServiceDefault.addNewPublisher(publisherVersion);
        publisher = publisherVersion.getPublisher();
        publisherServiceDefault.addVersionToPublisher(publisher, publisherVersion1);
        assertThat(publisherVersion1.getPublisher()).isEqualTo(publisher);
        assertThat(publisher.getVersions()).contains(publisherVersion, publisherVersion1);
        verify(publisherRepository, times(2)).save(publisher);
        verify(publisherVersionRepository).save(publisherVersion1);
    }

    @DisplayName("When a PublisherVersion is validated, it should be the right one")
    @Test
    public void testValidateVersionRight() {
        Set<PublisherVersion> publisherVersions;

        publisher = new Publisher();
        publisherVersion = new PublisherVersion();
        publisherVersions = new HashSet<>();
        publisherVersions.add(publisherVersion);
        publisherVersions.add(publisherVersionMock);
        publisher.setVersions(publisherVersions);

        when(publisherVersionMock.getPublisher()).thenReturn(publisher);
        publisherServiceDefault.validatePublisherVersion(publisherVersionMock);
        assertThat(publisher.getValidatedVersion()).isEqualTo(publisherVersionMock);
    }

    @DisplayName("When a publisherVersion is validated, the indexed IndexedPublisher should have all attributes right")
    @Test
    public void testValidateIndexPublisher(@Mock PublisherSearchRepository publisherSearchRepository) {

        publisher = new Publisher();
        publisher.setPublisherId(111);

        when(publisherVersionMock.getPublisher()).thenReturn(publisher);
        when(publisherVersionMock.getPublisherName()).thenReturn("nom");
        when(publisherVersionMock.getPublisherHistory()).thenReturn("histoire");
        publisherServiceDefault.validatePublisherVersion(publisherVersionMock);
        try {
            Mockito.verify(publisherSearchRepository).save(indexedPublisherArgumentCaptor.capture());
        } catch (IndexingException e) {
            logger.error("IndexingException {}", e);
        }
        IndexedPublisher indexedPublisher = indexedPublisherArgumentCaptor.getValue();
        assertThat(indexedPublisher.getPublisherId()).isEqualTo(111);
        assertThat(indexedPublisher.getPublisherName()).isEqualTo("nom");
        assertThat(indexedPublisher.getPublisherHistory()).isEqualTo("histoire");
    }

    @DisplayName("When a validate creates a PublisherAction it should be saved and have all attributes right")
    @Test
    public void testValidateCreateAction(@Mock PublisherActionRepository publisherActionRepository) {
        PublisherAction publisherAction;
        ActionType actionType = ActionType.VALIDATE;

        publisher = new Publisher();

        when(publisherVersionMock.getPublisher()).thenReturn(publisher);
        publisherAction = publisherServiceDefault.validatePublisherVersion(publisherVersionMock);
        verify(publisherActionRepository).save(publisherAction);
        assertThat(publisherAction.getPublisherVersion()).isEqualTo(publisherVersionMock);
        assertThat(publisherAction.getPublisher()).isEqualTo(publisher);
        assertThat(publisherAction.getPublisher().getValidatedVersion()).isEqualTo(publisherVersionMock);
        assertThat(publisherAction.getActionType()).isEqualTo(actionType);
    }

}