Jean-Francois Leveque

ReindexView et Test

1 package org.legrog.web.xyz; 1 package org.legrog.web.xyz;
2 2
3 +import org.legrog.web.account.AccountService;
4 +import org.legrog.web.publisher.PublisherService;
5 +
3 import javax.enterprise.context.RequestScoped; 6 import javax.enterprise.context.RequestScoped;
7 +import javax.inject.Inject;
4 import javax.inject.Named; 8 import javax.inject.Named;
5 import java.io.Serializable; 9 import java.io.Serializable;
6 10
...@@ -12,12 +16,23 @@ import java.io.Serializable; ...@@ -12,12 +16,23 @@ import java.io.Serializable;
12 public class ReindexView implements Serializable { 16 public class ReindexView implements Serializable {
13 private int indexedAccountsNumber; 17 private int indexedAccountsNumber;
14 private int indexedPublishersNumber; 18 private int indexedPublishersNumber;
19 + private PublisherService publisherService;
20 + private AccountService accountService;
21 +
22 + @Inject
23 + ReindexView(PublisherService publisherService, AccountService accountService) {
24 + this.publisherService = publisherService;
25 + this.accountService = accountService;
26 + }
15 27
16 ReindexView() { 28 ReindexView() {
17 //no args constructor to make it proxyable 29 //no args constructor to make it proxyable
18 } 30 }
19 31
20 - 32 + public void reindexAll() {
33 + indexedPublishersNumber = publisherService.reindexAllPublishers();
34 + indexedAccountsNumber = accountService.reindexAllAccounts();
35 + }
21 36
22 public int getIndexedAccountsNumber() { 37 public int getIndexedAccountsNumber() {
23 return indexedAccountsNumber; 38 return indexedAccountsNumber;
......
1 package org.legrog.web.xyz; 1 package org.legrog.web.xyz;
2 2
3 +import org.junit.jupiter.api.BeforeEach;
4 +import org.junit.jupiter.api.DisplayName;
5 +import org.junit.jupiter.api.Nested;
6 +import org.junit.jupiter.api.Test;
7 +import org.junit.jupiter.api.extension.ExtendWith;
8 +import org.junit.platform.runner.JUnitPlatform;
9 +import org.junit.runner.RunWith;
10 +import org.legrog.test.MockitoExtension;
11 +import org.legrog.web.account.AccountService;
12 +import org.legrog.web.publisher.PublisherService;
13 +import org.mockito.Mock;
14 +import org.mockito.Mockito;
15 +
16 +import static org.assertj.core.api.Assertions.assertThat;
17 +import static org.mockito.Mockito.when;
18 +
3 /** 19 /**
4 * Classe testing ReindexView 20 * Classe testing ReindexView
5 */ 21 */
22 +@RunWith(JUnitPlatform.class)
23 +@ExtendWith(MockitoExtension.class)
24 +@DisplayName("Reindexes data")
6 public class ReindexViewTest { 25 public class ReindexViewTest {
26 + private ReindexView reindexView;
27 + private PublisherService publisherService;
28 + private AccountService accountService;
29 +
30 + @BeforeEach
31 + public void setUp(@Mock PublisherService publisherService, @Mock AccountService accountService) {
32 + this.publisherService = publisherService;
33 + this.accountService = accountService;
34 + this.reindexView = new ReindexView(publisherService, accountService);
35 + }
36 +
37 + @Nested
38 + @DisplayName("reindexAll method")
39 + class ReindexAllTests {
40 +
41 + @DisplayName("when called, should call for reindexing of publishers and accounts")
42 + @Test
43 + public void reindexAllTest(@Mock PublisherService publisherService, @Mock AccountService accountService) {
44 + reindexView.reindexAll();
45 + Mockito.verify(publisherService).reindexAllPublishers();
46 + Mockito.verify(accountService).reindexAllAccounts();
47 + }
48 +
49 + @DisplayName("when called, should set its indexed numbers to values returned by reindexers")
50 + @Test
51 + public void setReturnIntegers(@Mock PublisherService publisherService, @Mock AccountService accountService) {
52 + when(publisherService.reindexAllPublishers()).thenReturn(1);
53 + when(accountService.reindexAllAccounts()).thenReturn(2);
54 + reindexView.reindexAll();
55 + assertThat(reindexView.getIndexedPublishersNumber()).isEqualTo(1);
56 + assertThat(reindexView.getIndexedAccountsNumber()).isEqualTo(2);
57 + }
58 +
59 + }
60 +
61 +
7 } 62 }
......