PublisherVersionViewTest.java 16.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
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("Creates new versions of a publisher, and shows existing with history of actions on it")
public class PublisherVersionViewTest {
    Logger logger = LoggerFactory.getLogger(getClass());

    private PublisherVersionView publisherVersionView;

    private PublisherVersion publisherVersion;
    private PublisherService publisherService;
    private SharedService sharedService;

    @Mock
    PublisherVersion publisherVersionMock;

    @BeforeEach
    public void setUp(@Mock PublisherService publisherService, @Mock SharedService sharedService) throws Exception {
        this.publisherService = publisherService;
        this.sharedService = sharedService;
        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.setPublisherHistory("J");
        publisherVersion.setPublisher(new Publisher());

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

    }

    @Test
    @DisplayName("when constructed, should have right dependencies : Shared Service and Publisher Service")
    public void testDependencies() {
        assertThat(publisherVersionView).isNotNull();
        assertThat(publisherVersionView.publisherService).isEqualTo(publisherService);
        assertThat(publisherVersionView.sharedService).isEqualTo(sharedService);
    }

    @DisplayName("when edit is clicked, should switch to edit mode")
    @Test
    public void testEdit() {
        assertThat(publisherVersionView.isEditMode()).isFalse();
        publisherVersionView.edit();
        assertThat(publisherVersionView.isEditMode()).isTrue();
    }

    @DisplayName("when cancel is clicked for an new publisher, should clean new form data and switch to non-edit mode")
    @Test
    public void testCancelNewPublisher() {
        publisherVersionView.setNewPublisher(true);

        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.isEditMode()).isTrue();
    }

    @DisplayName("whan cancel is clicked for a publisher update, should revert to original data and switch to non-edit mode")
    @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.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();

    }

    @Nested
    @DisplayName("loadData method")
    class loadDataTests{

        @Test
        @DisplayName("Loads all actions on it")
        public void testLoadActions(@Mock PublisherService publisherService) {
            publisherVersionView.setPublisherVersionId(1);
            publisherVersionView.loadData();
            Mockito.verify(publisherService).getAllPublisherVersionActions(publisherVersion);
        }

        @Test
        @DisplayName("when called for existing publisher, should get the rigtht data into the form")
        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.getPublisherHistory()).isEqualTo("J");
            assertThat(publisherVersionView.isNewPublisher()).isFalse();
        }

        @Test
        @DisplayName("when called for new publisher, should set form for new version")
        public void testLoadDataEmpty() {
            publisherVersionView.setNewPublisher(false);
            publisherVersionView.setPublisherVersionId(0);
            publisherVersionView.loadData();
            assertThat(publisherVersionView.isNewPublisher()).isTrue();
            assertThat(publisherVersionView.isEditMode()).isTrue();
        }

    }

    @Nested
    @DisplayName("getImmutableValues method")
    class getImmutableValuesTests{
        @BeforeEach
        @DisplayName("given a way to get a null")
        public void given(@Mock PublisherService publisherService) {
            when(publisherService.addNewPublisher(any())).thenReturn(publisherVersionMock);
            when(publisherVersionMock.getPublisherVersionId()).thenReturn(2);
            when(publisherService.getPublisherVersion(2)).thenReturn(null);
        }
        @Test
        @DisplayName("throws an IllegalStateException")
        public void testUnexcpetedNull() {
            assertThrows(IllegalStateException.class, () -> publisherVersionView.add());
        }
    }

    @Nested
    @DisplayName("save method")
    class SaveTests{
        PublisherVersionView publisherVersionViewForSave;
        public boolean addCalled = false;
        public boolean updateCalled = false;

        @BeforeEach
        public void given() {
            publisherVersionViewForSave = new PublisherVersionView() {

                @Override
                public void add() {
                    addCalled = true;
                }

                @Override
                public void update() {
                    updateCalled = true;
                }

            };
        }

        @Test
        @DisplayName("when view is for a new publisher, should delegate to add() method")
        public void testSaveAdd() {
            publisherVersionViewForSave.setNewPublisher(true);
            publisherVersionViewForSave.save();
            assertThat(addCalled).isTrue();
        }

        @Test
        @DisplayName("when view is not for a new publisher, should delegate to update() method")
        public void testSaveUpdate() {
            publisherVersionViewForSave.setNewPublisher(false);
            publisherVersionViewForSave.save();
            assertThat(updateCalled).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("Update method")
    class UpdateTests {

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

            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.setPublisherHistory("10");
            publisherVersionView.setPublisher(publisherMock);
        }

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

            @Captor
            ArgumentCaptor<PublisherVersion> publisherVersionArgumentCaptor;

            @Captor
            ArgumentCaptor<Publisher> publisherArgumentCaptor;

            @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.addVersionToPublisher(any(Publisher.class), any(PublisherVersion.class))).thenReturn(publisherVersionMock);
                when(publisherVersionMock.getPublisher()).thenReturn(publisherMock);
                when(publisherVersionMock.getPublisherVersionId()).thenReturn(13);

                publisherVersionView.update();
            }

            @DisplayName("should add a new version to a publisher")
            @Test
            public void shouldAddNewVersion(@Mock PublisherService publisherService) {
                Mockito.verify(publisherService).addVersionToPublisher(publisherArgumentCaptor.capture(), publisherVersionArgumentCaptor.capture());
                PublisherVersion publisherVersion = publisherVersionArgumentCaptor.getValue();
                assertThat(publisherVersion.getPublisherName()).isEqualTo("1");
            }

            @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(13);
            }

        }

    }

    @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.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 a new publisher in the application 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);
            }

        }

    }

}