Un test de plus pour le service avec le refactoring qui correspond et un petit b…
…ug résiduel en commenatire dans celui-ci.
Showing
3 changed files
with
43 additions
and
6 deletions
... | @@ -16,7 +16,7 @@ public interface PublisherService { | ... | @@ -16,7 +16,7 @@ public interface PublisherService { |
16 | 16 | ||
17 | PublisherVersion addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion); | 17 | PublisherVersion addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion); |
18 | 18 | ||
19 | - void validatePublisherVersion(PublisherVersion publisherVersion); | 19 | + PublisherAction validatePublisherVersion(PublisherVersion publisherVersion); |
20 | 20 | ||
21 | List<PublisherVersion> getAllPublisherVersions(); | 21 | List<PublisherVersion> getAllPublisherVersions(); |
22 | 22 | ... | ... |
... | @@ -72,7 +72,7 @@ public class PublisherServiceSpring implements PublisherService { | ... | @@ -72,7 +72,7 @@ public class PublisherServiceSpring implements PublisherService { |
72 | return publisherVersion; | 72 | return publisherVersion; |
73 | } | 73 | } |
74 | 74 | ||
75 | - public void validatePublisherVersion(PublisherVersion publisherVersion) { | 75 | + public PublisherAction validatePublisherVersion(PublisherVersion publisherVersion) { |
76 | Publisher publisher = publisherVersion.getPublisher(); | 76 | Publisher publisher = publisherVersion.getPublisher(); |
77 | publisher.setValidatedVersion(publisherVersion); | 77 | publisher.setValidatedVersion(publisherVersion); |
78 | PublisherAction publisherAction = new PublisherAction(); | 78 | PublisherAction publisherAction = new PublisherAction(); |
... | @@ -83,6 +83,7 @@ public class PublisherServiceSpring implements PublisherService { | ... | @@ -83,6 +83,7 @@ public class PublisherServiceSpring implements PublisherService { |
83 | publisherAction.setPublisher(publisher); | 83 | publisherAction.setPublisher(publisher); |
84 | this.savePublisher(publisher); | 84 | this.savePublisher(publisher); |
85 | publisherActionRepository.save(publisherAction); | 85 | publisherActionRepository.save(publisherAction); |
86 | + return publisherAction; | ||
86 | } | 87 | } |
87 | 88 | ||
88 | protected void savePublisherVersion(PublisherVersion publisherVersion) { | 89 | protected void savePublisherVersion(PublisherVersion publisherVersion) { | ... | ... |
1 | package org.legrog.web.publisher; | 1 | package org.legrog.web.publisher; |
2 | 2 | ||
3 | -import org.junit.Test; | 3 | +import org.junit.jupiter.api.Test; |
4 | import org.junit.jupiter.api.BeforeEach; | 4 | import org.junit.jupiter.api.BeforeEach; |
5 | import org.junit.jupiter.api.DisplayName; | 5 | import org.junit.jupiter.api.DisplayName; |
6 | import org.junit.jupiter.api.extension.ExtendWith; | 6 | import org.junit.jupiter.api.extension.ExtendWith; |
... | @@ -11,6 +11,12 @@ import org.legrog.test.MockitoExtension; | ... | @@ -11,6 +11,12 @@ import org.legrog.test.MockitoExtension; |
11 | import org.legrog.web.xyz.SharedService; | 11 | import org.legrog.web.xyz.SharedService; |
12 | import org.mockito.Mock; | 12 | import org.mockito.Mock; |
13 | 13 | ||
14 | +import javax.inject.Inject; | ||
15 | +import java.util.ArrayList; | ||
16 | +import java.util.HashSet; | ||
17 | +import java.util.List; | ||
18 | +import java.util.Set; | ||
19 | + | ||
14 | import static org.assertj.core.api.Assertions.assertThat; | 20 | import static org.assertj.core.api.Assertions.assertThat; |
15 | import static org.mockito.Mockito.*; | 21 | import static org.mockito.Mockito.*; |
16 | 22 | ||
... | @@ -29,6 +35,12 @@ public class PublisherServiceSpringTest { | ... | @@ -29,6 +35,12 @@ public class PublisherServiceSpringTest { |
29 | PublisherRepository publisherRepository; | 35 | PublisherRepository publisherRepository; |
30 | PublisherVersionRepository publisherVersionRepository; | 36 | PublisherVersionRepository publisherVersionRepository; |
31 | 37 | ||
38 | + @Inject | ||
39 | + ActionTypeRepository actionTypeRepository; | ||
40 | + | ||
41 | + @Mock | ||
42 | + PublisherVersion publisherVersionMock; | ||
43 | + | ||
32 | @BeforeEach | 44 | @BeforeEach |
33 | public void setUp(@Mock PublisherRepository publisherRepository, | 45 | public void setUp(@Mock PublisherRepository publisherRepository, |
34 | @Mock PublisherVersionRepository publisherVersionRepository, | 46 | @Mock PublisherVersionRepository publisherVersionRepository, |
... | @@ -44,7 +56,7 @@ public class PublisherServiceSpringTest { | ... | @@ -44,7 +56,7 @@ public class PublisherServiceSpringTest { |
44 | 56 | ||
45 | @DisplayName("Test ajout nouvel éditeur") | 57 | @DisplayName("Test ajout nouvel éditeur") |
46 | @Test | 58 | @Test |
47 | - public void testAddNewPublisher() { | 59 | + public void testAddNewPublisher(@Mock PublisherVersionRepository publisherVersionRepository) { |
48 | publisherServiceSpring.addNewPublisher(publisherVersion); | 60 | publisherServiceSpring.addNewPublisher(publisherVersion); |
49 | publisher = publisherVersion.getPublisher(); | 61 | publisher = publisherVersion.getPublisher(); |
50 | assertThat(publisher.getVersions()).containsExactly(publisherVersion); | 62 | assertThat(publisher.getVersions()).containsExactly(publisherVersion); |
... | @@ -55,13 +67,37 @@ public class PublisherServiceSpringTest { | ... | @@ -55,13 +67,37 @@ public class PublisherServiceSpringTest { |
55 | 67 | ||
56 | @DisplayName("Test ajout nouvelle version éditeur") | 68 | @DisplayName("Test ajout nouvelle version éditeur") |
57 | @Test | 69 | @Test |
58 | - public void testAddVersionToPublisher() { | 70 | + public void testAddVersionToPublisher(@Mock PublisherRepository publisherRepository, |
71 | + @Mock PublisherVersionRepository publisherVersionRepository) { | ||
59 | publisherServiceSpring.addNewPublisher(publisherVersion); | 72 | publisherServiceSpring.addNewPublisher(publisherVersion); |
60 | publisher = publisherVersion.getPublisher(); | 73 | publisher = publisherVersion.getPublisher(); |
61 | publisherServiceSpring.addVersionToPublisher(publisher, publisherVersion1); | 74 | publisherServiceSpring.addVersionToPublisher(publisher, publisherVersion1); |
62 | assertThat(publisherVersion1.getPublisher()).isEqualTo(publisher); | 75 | assertThat(publisherVersion1.getPublisher()).isEqualTo(publisher); |
63 | assertThat(publisher.getVersions()).contains(publisherVersion, publisherVersion1); | 76 | assertThat(publisher.getVersions()).contains(publisherVersion, publisherVersion1); |
64 | - verify(publisherRepository).save(publisher); | 77 | + verify(publisherRepository, times(2)).save(publisher); |
65 | verify(publisherVersionRepository).save(publisherVersion1); | 78 | verify(publisherVersionRepository).save(publisherVersion1); |
66 | } | 79 | } |
80 | + | ||
81 | + @DisplayName("Test validation version éditeur") | ||
82 | + @Test | ||
83 | + public void testValidateVersion(@Mock PublisherActionRepository publisherActionRepository) { | ||
84 | + Set<PublisherVersion> publisherVersions; | ||
85 | + | ||
86 | + publisher = new Publisher(); | ||
87 | + publisherVersion = new PublisherVersion(); | ||
88 | + | ||
89 | + publisherVersions = new HashSet<PublisherVersion>(); | ||
90 | + publisherVersions.add(publisherVersion); | ||
91 | + publisherVersions.add(publisherVersionMock); | ||
92 | + | ||
93 | + when(publisherVersionMock.getPublisher()).thenReturn(publisher); | ||
94 | + PublisherAction publisherAction; | ||
95 | + publisherAction = publisherServiceSpring.validatePublisherVersion(publisherVersionMock); | ||
96 | + verify(publisherActionRepository).save(publisherAction); | ||
97 | + assertThat(publisherAction.getPublisherVersion()).isEqualTo(publisherVersionMock); | ||
98 | + assertThat(publisherAction.getPublisher()).isEqualTo(publisher); | ||
99 | + assertThat(publisherAction.getPublisher().getValidatedVersion()).isEqualTo(publisherVersionMock); | ||
100 | +// TODO Régler ce problème de NPE | ||
101 | +// assertThat(publisherAction.getActionType()).isEqualTo(actionTypeRepository.findByActionTypeName("Validate")); | ||
102 | + } | ||
67 | } | 103 | } | ... | ... |
-
Please register or login to post a comment