PublisherVersionViewTest.java 15.9 KB
package org.legrog.web.publisher;

import org.junit.jupiter.api.Nested;
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.Country;
import org.legrog.entities.Publisher;
import org.legrog.entities.PublisherVersion;
import org.legrog.test.MockitoExtension;
import org.legrog.web.xyz.SharedService;
import org.mockito.*;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;

/**
 * Classe testant PublisherVersionView.
 */
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
@DisplayName("Création et visualisation de version d'éditeur")
public class PublisherVersionViewTest {
    Logger logger = LoggerFactory.getLogger(getClass());

    private PublisherVersionView publisherVersionView;

    private PublisherVersion publisherVersion;

    // TODO tester add et update, mais je ne sais pas comment je suis censé faire

    @BeforeEach
    public void setUp(@Mock PublisherService publisherService, @Mock SharedService sharedService) throws Exception {
        publisherVersionView = new PublisherVersionView(publisherService, sharedService);
        publisherVersionView.setEditMode(false);
        publisherVersionView.setNewPublisher(false);

        publisherVersion = new PublisherVersion();
        publisherVersion.setPublisherName("A");
        publisherVersion.setPublisherStreetAddress("B");
        publisherVersion.setPublisherPostalCode("C");
        publisherVersion.setPublisherPostOfficeBoxNumber("D");
        publisherVersion.setPublisherAddressRegion("E");
        publisherVersion.setPublisherAddressLocality("F");
        publisherVersion.setPublisherAddressCountry(new Country());
        publisherVersion.setPublisherTelephone("G");
        publisherVersion.setPublisherEmail("H");
        publisherVersion.setPublisherURL("I");
        publisherVersion.setPublisherActive(true);
        publisherVersion.setPublisherHistory("J");
        publisherVersion.setPublisher(new Publisher());

        when(publisherService.getPublisherVersion(1)).thenReturn(publisherVersion);

    }

    @Test
    @DisplayName("depends on Shared Service and User Service")
    public void testDependencies() {
        assertThat(publisherVersionView).isNotNull();
    }

    @DisplayName("Passage en mode édition")
    @Test
    public void testEdit() {
        assertThat(publisherVersionView.isEditMode()).isFalse();
        publisherVersionView.edit();
        assertThat(publisherVersionView.isEditMode()).isTrue();
    }

    @DisplayName("Annulation des données saisie pour un nouvel éditeur")
    @Test
    public void testCancelNewPublisher() {
        publisherVersionView.setNewPublisher(true);

        publisherVersionView.setPublisherName("A");
        publisherVersionView.setPublisherStreetAddress("B");
        publisherVersionView.setPublisherPostalCode("C");
        publisherVersionView.setPublisherPostOfficeBoxNumber("D");
        publisherVersionView.setPublisherAddressRegion("E");
        publisherVersionView.setPublisherAddressLocality("F");
        publisherVersionView.setPublisherAddressCountry(new Country());
        publisherVersionView.setPublisherTelephone("G");
        publisherVersionView.setPublisherEmail("H");
        publisherVersionView.setPublisherURL("I");
        publisherVersionView.setPublisherActive(true);
        publisherVersionView.setPublisherHistory("J");

        publisherVersionView.cancel();

        assertThat(publisherVersionView.getPublisherName()).isNull();
        assertThat(publisherVersionView.getPublisherStreetAddress()).isNull();
        assertThat(publisherVersionView.getPublisherPostalCode()).isNull();
        assertThat(publisherVersionView.getPublisherPostOfficeBoxNumber()).isNull();
        assertThat(publisherVersionView.getPublisherAddressRegion()).isNull();
        assertThat(publisherVersionView.getPublisherAddressLocality()).isNull();
        assertThat(publisherVersionView.getPublisherTelephone()).isNull();
        assertThat(publisherVersionView.getPublisherEmail()).isNull();
        assertThat(publisherVersionView.getPublisherURL()).isNull();
        assertThat(publisherVersionView.getPublisherHistory()).isNull();
        assertThat(publisherVersionView.isPublisherActive()).isFalse();
        assertThat(publisherVersionView.isEditMode()).isTrue();
    }

    @DisplayName("Annulation de saisie de nouvelle version de saisie d'un éditeur existant")
    @Test
    public void testCancelNewVersionOfPublisher() {
        publisherVersionView.setNewPublisher(false);
        publisherVersionView.setPublisherVersionId(1);

        publisherVersionView.setPublisherName("1");
        publisherVersionView.setPublisherStreetAddress("2");
        publisherVersionView.setPublisherPostalCode("3");
        publisherVersionView.setPublisherPostOfficeBoxNumber("4");
        publisherVersionView.setPublisherAddressRegion("5");
        publisherVersionView.setPublisherAddressLocality("6");
        publisherVersionView.setPublisherAddressCountry(new Country());
        publisherVersionView.setPublisherTelephone("7");
        publisherVersionView.setPublisherEmail("8");
        publisherVersionView.setPublisherURL("9");
        publisherVersionView.setPublisherActive(false);
        publisherVersionView.setPublisherHistory("10");

        publisherVersionView.cancel();

        assertThat(publisherVersionView.getPublisherName()).isEqualTo("A");
        assertThat(publisherVersionView.getPublisherStreetAddress()).isEqualTo("B");
        assertThat(publisherVersionView.getPublisherPostalCode()).isEqualTo("C");
        assertThat(publisherVersionView.getPublisherPostOfficeBoxNumber()).isEqualTo("D");
        assertThat(publisherVersionView.getPublisherAddressRegion()).isEqualTo("E");
        assertThat(publisherVersionView.getPublisherAddressLocality()).isEqualTo("F");
        assertThat(publisherVersionView.getPublisherTelephone()).isEqualTo("G");
        assertThat(publisherVersionView.getPublisherEmail()).isEqualTo("H");
        assertThat(publisherVersionView.getPublisherURL()).isEqualTo("I");
        assertThat(publisherVersionView.getPublisherHistory()).isEqualTo("J");
        assertThat(publisherVersionView.isEditMode()).isFalse();

    }

    @Test
    @DisplayName("Test de la fonction de chargement des valeurs initiales pour leur affichage")
    public void testLoadData() {
        publisherVersionView.setNewPublisher(true);
        publisherVersionView.setPublisherVersionId(1);
        publisherVersionView.loadData();
        assertThat(publisherVersionView.getPublisherName()).isEqualTo("A");
        assertThat(publisherVersionView.getPublisherStreetAddress()).isEqualTo("B");
        assertThat(publisherVersionView.getPublisherPostalCode()).isEqualTo("C");
        assertThat(publisherVersionView.getPublisherPostOfficeBoxNumber()).isEqualTo("D");
        assertThat(publisherVersionView.getPublisherAddressRegion()).isEqualTo("E");
        assertThat(publisherVersionView.getPublisherAddressLocality()).isEqualTo("F");
        assertThat(publisherVersionView.getPublisherTelephone()).isEqualTo("G");
        assertThat(publisherVersionView.getPublisherEmail()).isEqualTo("H");
        assertThat(publisherVersionView.getPublisherURL()).isEqualTo("I");
        assertThat(publisherVersionView.isPublisherActive()).isTrue();
        assertThat(publisherVersionView.getPublisherHistory()).isEqualTo("J");
        assertThat(publisherVersionView.isNewPublisher()).isFalse();
    }

    @Test
    @DisplayName("Test initialisation formulaire pour nouvel éditeur")
    public void testLoadDataEmpty() {
        publisherVersionView.setNewPublisher(false);
        publisherVersionView.setPublisherVersionId(0);
        publisherVersionView.loadData();
        assertThat(publisherVersionView.isNewPublisher()).isTrue();
        assertThat(publisherVersionView.isEditMode()).isTrue();
    }

    @Nested
    @DisplayName("post construct method")
    class init {

        private List<Country> countries;

        @BeforeEach
        public void setUp(@Mock SharedService sharedService) {
            when(sharedService.getAllCountries()).thenReturn(countries);
        }

        @Test
        @DisplayName("should set lists of available masks, user roles, and user properties from shared service")
        public void testList(@Mock SharedService sharedService) {
            publisherVersionView.init();
            assertThat(publisherVersionView.getAvailableCountries()).isEqualTo(countries);
        }
    }


    @Nested
    @DisplayName("Add method")
    class addTests {

        @BeforeEach
        public void given(@Mock Publisher publisherMock) {
            publisherVersionView.setNewPublisher(true);

            publisherVersionView.setPublisherName("A");
            publisherVersionView.setPublisherStreetAddress("B");
            publisherVersionView.setPublisherPostalCode("C");
            publisherVersionView.setPublisherPostOfficeBoxNumber("D");
            publisherVersionView.setPublisherAddressRegion("E");
            publisherVersionView.setPublisherAddressLocality("F");
            publisherVersionView.setPublisherAddressCountry(new Country());
            publisherVersionView.setPublisherTelephone("G");
            publisherVersionView.setPublisherEmail("H");
            publisherVersionView.setPublisherURL("I");
            publisherVersionView.setPublisherActive(true);
            publisherVersionView.setPublisherHistory("J");
            publisherVersionView.setPublisher(publisherMock);
        }

        @Nested
        @DisplayName("given some input fields")
        class given {

            @Captor
            ArgumentCaptor<PublisherVersion> publisherVersionArgumentCaptor;

            @BeforeEach
            public void test(@Mock PublisherService publisherService, @Mock Publisher publisherMock, @Mock PublisherVersion publisherVersionMock) {
                // mock de l'appel dans Immutables values
                when(publisherService.getPublisherVersion(anyInt())).thenReturn(new PublisherVersion());


                when(publisherService.addNewPublisher(any(PublisherVersion.class))).thenReturn(publisherVersionMock);
                when(publisherVersionMock.getPublisher()).thenReturn(publisherMock);
                when(publisherVersionMock.getPublisherVersionId()).thenReturn(42);
                publisherVersionView.add();
            }

            @DisplayName("should create in the application a new publisher with its first version")
            @Test
            public void shouldSave(@Mock PublisherService publisherService) {
                Mockito.verify(publisherService).addNewPublisher(publisherVersionArgumentCaptor.capture());
                PublisherVersion publisherVersion = publisherVersionArgumentCaptor.getValue();
                assertThat(publisherVersion.getPublisherName()).isEqualTo("A");
            }


            @DisplayName("should put state in non edit mode")
            @Test
            public void shouldNonEdit() {
                assertThat(publisherVersionView.isEditMode()).isFalse();
            }

            @DisplayName("should put state in non new publisher with new ids")
            @Test
            public void shouldNonNewPublisher(@Mock Publisher publisherMock) {

                assertThat(publisherVersionView.isNewPublisher()).isFalse();
                assertThat(publisherVersionView.getPublisher()).isEqualTo(publisherMock);
                assertThat(publisherVersionView.getPublisherVersionId()).isEqualTo(42);
            }

        }

    }

    // J'ai l'impression qu'on pourrait utiliser de l'imbrication ici
    /*
    @DisplayName("Annulation de saisie de nouvelle version de saisie d'un éditeur existant")
    @Test
    public void testCancelNewVersionOfPublisher() {
        publisherVersionView.setNewPublisher(false);
        publisherVersionView.setPublisherVersionId(1);

        publisherVersionView.setPublisherName("1");
        publisherVersionView.setPublisherStreetAddress("2");
        publisherVersionView.setPublisherPostalCode("3");
        publisherVersionView.setPublisherPostOfficeBoxNumber("4");
        publisherVersionView.setPublisherAddressRegion("5");
        publisherVersionView.setPublisherAddressLocality("6");
        publisherVersionView.setPublisherAddressCountry(new Country());
        publisherVersionView.setPublisherTelephone("7");
        publisherVersionView.setPublisherEmail("8");
        publisherVersionView.setPublisherURL("9");
        publisherVersionView.setPublisherActive(false);
        publisherVersionView.setPublisherHistory("10");

        publisherVersionView.cancel();

        assertThat(publisherVersionView.getPublisherName()).isEqualTo("A");
        assertThat(publisherVersionView.getPublisherStreetAddress()).isEqualTo("B");
        assertThat(publisherVersionView.getPublisherPostalCode()).isEqualTo("C");
        assertThat(publisherVersionView.getPublisherPostOfficeBoxNumber()).isEqualTo("D");
        assertThat(publisherVersionView.getPublisherAddressRegion()).isEqualTo("E");
        assertThat(publisherVersionView.getPublisherAddressLocality()).isEqualTo("F");
        assertThat(publisherVersionView.getPublisherTelephone()).isEqualTo("G");
        assertThat(publisherVersionView.getPublisherEmail()).isEqualTo("H");
        assertThat(publisherVersionView.getPublisherURL()).isEqualTo("I");
        assertThat(publisherVersionView.getPublisherHistory()).isEqualTo("J");
        assertThat(publisherVersionView.isEditMode()).isFalse();

    }

    @Test
    @DisplayName("Test de la fonction de chargement des valeurs initiales pour leur affichage")
    public void testLoadData() {
        publisherVersionView.setNewPublisher(true);
        publisherVersionView.setPublisherVersionId(1);
        publisherVersionView.loadData();
        assertThat(publisherVersionView.getPublisherName()).isEqualTo("A");
        assertThat(publisherVersionView.getPublisherStreetAddress()).isEqualTo("B");
        assertThat(publisherVersionView.getPublisherPostalCode()).isEqualTo("C");
        assertThat(publisherVersionView.getPublisherPostOfficeBoxNumber()).isEqualTo("D");
        assertThat(publisherVersionView.getPublisherAddressRegion()).isEqualTo("E");
        assertThat(publisherVersionView.getPublisherAddressLocality()).isEqualTo("F");
        assertThat(publisherVersionView.getPublisherTelephone()).isEqualTo("G");
        assertThat(publisherVersionView.getPublisherEmail()).isEqualTo("H");
        assertThat(publisherVersionView.getPublisherURL()).isEqualTo("I");
        assertThat(publisherVersionView.isPublisherActive()).isTrue();
        assertThat(publisherVersionView.getPublisherHistory()).isEqualTo("J");
        assertThat(publisherVersionView.isNewPublisher()).isFalse();
    }

    @Test
    @DisplayName("Test initialisation formulaire pour nouvel éditeur")
    public void testLoadDataEmpty() {
        publisherVersionView.setNewPublisher(false);
        publisherVersionView.setPublisherVersionId(0);
        publisherVersionView.loadData();
        assertThat(publisherVersionView.isNewPublisher()).isTrue();
        assertThat(publisherVersionView.isEditMode()).isTrue();
    }

    @Nested
    @DisplayName("post construct method")
    class init {

        private List<Country> countries;

        @BeforeEach
        public void setUp(@Mock SharedService sharedService) {
            when(sharedService.getAllCountries()).thenReturn(countries);
        }

        @Test
        @DisplayName("should set lists of available masks, user roles, and user properties from shared service")
        public void testList(@Mock SharedService sharedService) {
            publisherVersionView.init();
            assertThat(publisherVersionView.getAvailableCountries()).isEqualTo(countries);
        }
    }
}
    */
}