Showing
1 changed file
with
89 additions
and
70 deletions
1 | package org.legrog.web.publisher; | 1 | package org.legrog.web.publisher; |
2 | 2 | ||
3 | +import org.junit.jupiter.api.Nested; | ||
3 | import org.junit.jupiter.api.Test; | 4 | import org.junit.jupiter.api.Test; |
4 | import org.junit.jupiter.api.BeforeEach; | 5 | import org.junit.jupiter.api.BeforeEach; |
5 | import org.junit.jupiter.api.DisplayName; | 6 | import org.junit.jupiter.api.DisplayName; |
... | @@ -57,84 +58,102 @@ public class PublisherServiceDefaultTest { | ... | @@ -57,84 +58,102 @@ public class PublisherServiceDefaultTest { |
57 | this.publisherRepository = publisherRepository; | 58 | this.publisherRepository = publisherRepository; |
58 | } | 59 | } |
59 | 60 | ||
60 | - @DisplayName("When a new publisher is added, both Publisher and PublisherVersion should be saved in the right state") | 61 | + @Nested |
61 | - @Test | 62 | + @DisplayName("addNewPublisher method") |
62 | - public void testAddNewPublisher(@Mock PublisherVersionRepository publisherVersionRepository) { | 63 | + class AddNewPublisherTests { |
63 | - publisherServiceDefault.addNewPublisher(publisherVersion); | 64 | + |
64 | - publisher = publisherVersion.getPublisher(); | 65 | + @DisplayName("When a new publisher is added, both Publisher and PublisherVersion should be saved in the right state") |
65 | - assertThat(publisher.getVersions()).containsExactly(publisherVersion); | 66 | + @Test |
66 | - assertThat(publisherVersion.getPublisher()).isEqualTo(publisher); | 67 | + public void testAddNewPublisher(@Mock PublisherVersionRepository publisherVersionRepository) { |
67 | - verify(publisherRepository).save(publisher); | 68 | + publisherServiceDefault.addNewPublisher(publisherVersion); |
68 | - verify(publisherVersionRepository).save(publisherVersion); | 69 | + publisher = publisherVersion.getPublisher(); |
69 | - } | 70 | + assertThat(publisher.getVersions()).containsExactly(publisherVersion); |
71 | + assertThat(publisherVersion.getPublisher()).isEqualTo(publisher); | ||
72 | + verify(publisherRepository).save(publisher); | ||
73 | + verify(publisherVersionRepository).save(publisherVersion); | ||
74 | + } | ||
70 | 75 | ||
71 | - @DisplayName("When a new version of a publisher is added, setting it up, attaching it to publisher and saving both") | ||
72 | - @Test | ||
73 | - public void testAddVersionToPublisher(@Mock PublisherRepository publisherRepository, | ||
74 | - @Mock PublisherVersionRepository publisherVersionRepository) { | ||
75 | - publisherServiceDefault.addNewPublisher(publisherVersion); | ||
76 | - publisher = publisherVersion.getPublisher(); | ||
77 | - publisherServiceDefault.addVersionToPublisher(publisher, publisherVersion1); | ||
78 | - assertThat(publisherVersion1.getPublisher()).isEqualTo(publisher); | ||
79 | - assertThat(publisher.getVersions()).contains(publisherVersion, publisherVersion1); | ||
80 | - verify(publisherRepository, times(2)).save(publisher); | ||
81 | - verify(publisherVersionRepository).save(publisherVersion1); | ||
82 | } | 76 | } |
83 | 77 | ||
84 | - @DisplayName("When a PublisherVersion is validated, it should be the right one") | 78 | + @Nested |
85 | - @Test | 79 | + @DisplayName("addVersionToPublisher method") |
86 | - public void testValidateVersionRight() { | 80 | + class AddVersionToPublisherTests { |
87 | - Set<PublisherVersion> publisherVersions; | 81 | + |
82 | + @DisplayName("When a new version of a publisher is added, setting it up, attaching it to publisher and saving both") | ||
83 | + @Test | ||
84 | + public void testAddVersionToPublisher(@Mock PublisherRepository publisherRepository, | ||
85 | + @Mock PublisherVersionRepository publisherVersionRepository) { | ||
86 | + publisherServiceDefault.addNewPublisher(publisherVersion); | ||
87 | + publisher = publisherVersion.getPublisher(); | ||
88 | + publisherServiceDefault.addVersionToPublisher(publisher, publisherVersion1); | ||
89 | + assertThat(publisherVersion1.getPublisher()).isEqualTo(publisher); | ||
90 | + assertThat(publisher.getVersions()).contains(publisherVersion, publisherVersion1); | ||
91 | + verify(publisherRepository, times(2)).save(publisher); | ||
92 | + verify(publisherVersionRepository).save(publisherVersion1); | ||
93 | + } | ||
88 | 94 | ||
89 | - publisher = new Publisher(); | ||
90 | - publisherVersion = new PublisherVersion(); | ||
91 | - publisherVersions = new HashSet<>(); | ||
92 | - publisherVersions.add(publisherVersion); | ||
93 | - publisherVersions.add(publisherVersionMock); | ||
94 | - publisher.setVersions(publisherVersions); | ||
95 | - | ||
96 | - when(publisherVersionMock.getPublisher()).thenReturn(publisher); | ||
97 | - publisherServiceDefault.validatePublisherVersion(publisherVersionMock); | ||
98 | - assertThat(publisher.getValidatedVersion()).isEqualTo(publisherVersionMock); | ||
99 | } | 95 | } |
100 | 96 | ||
101 | - @DisplayName("When a publisherVersion is validated, the indexed IndexedPublisher should have all attributes right") | 97 | + @Nested |
102 | - @Test | 98 | + @DisplayName("addVersionToPublisher method") |
103 | - public void testValidateIndexPublisher(@Mock PublisherSearchRepository publisherSearchRepository) { | 99 | + class ValidatePublisherVersionTests { |
104 | - | 100 | + |
105 | - publisher = new Publisher(); | 101 | + @DisplayName("When a PublisherVersion is validated, it should be the right one") |
106 | - publisher.setPublisherId(111); | 102 | + @Test |
107 | - | 103 | + public void testValidateVersionRight() { |
108 | - when(publisherVersionMock.getPublisher()).thenReturn(publisher); | 104 | + Set<PublisherVersion> publisherVersions; |
109 | - when(publisherVersionMock.getPublisherName()).thenReturn("nom"); | 105 | + |
110 | - when(publisherVersionMock.getPublisherHistory()).thenReturn("histoire"); | 106 | + publisher = new Publisher(); |
111 | - publisherServiceDefault.validatePublisherVersion(publisherVersionMock); | 107 | + publisherVersion = new PublisherVersion(); |
112 | - try { | 108 | + publisherVersions = new HashSet<>(); |
113 | - Mockito.verify(publisherSearchRepository).save(indexedPublisherArgumentCaptor.capture()); | 109 | + publisherVersions.add(publisherVersion); |
114 | - } catch (IndexingException e) { | 110 | + publisherVersions.add(publisherVersionMock); |
115 | - logger.error("IndexingException {}", e); | 111 | + publisher.setVersions(publisherVersions); |
112 | + | ||
113 | + when(publisherVersionMock.getPublisher()).thenReturn(publisher); | ||
114 | + publisherServiceDefault.validatePublisherVersion(publisherVersionMock); | ||
115 | + assertThat(publisher.getValidatedVersion()).isEqualTo(publisherVersionMock); | ||
116 | + } | ||
117 | + | ||
118 | + @DisplayName("When a publisherVersion is validated, the indexed IndexedPublisher should have all attributes right") | ||
119 | + @Test | ||
120 | + public void testValidateIndexPublisher(@Mock PublisherSearchRepository publisherSearchRepository) { | ||
121 | + | ||
122 | + publisher = new Publisher(); | ||
123 | + publisher.setPublisherId(111); | ||
124 | + | ||
125 | + when(publisherVersionMock.getPublisher()).thenReturn(publisher); | ||
126 | + when(publisherVersionMock.getPublisherName()).thenReturn("nom"); | ||
127 | + when(publisherVersionMock.getPublisherHistory()).thenReturn("histoire"); | ||
128 | + publisherServiceDefault.validatePublisherVersion(publisherVersionMock); | ||
129 | + try { | ||
130 | + Mockito.verify(publisherSearchRepository).save(indexedPublisherArgumentCaptor.capture()); | ||
131 | + } catch (IndexingException e) { | ||
132 | + logger.error("IndexingException {}", e); | ||
133 | + } | ||
134 | + IndexedPublisher indexedPublisher = indexedPublisherArgumentCaptor.getValue(); | ||
135 | + assertThat(indexedPublisher.getPublisherId()).isEqualTo(111); | ||
136 | + assertThat(indexedPublisher.getPublisherName()).isEqualTo("nom"); | ||
137 | + assertThat(indexedPublisher.getPublisherHistory()).isEqualTo("histoire"); | ||
138 | + } | ||
139 | + | ||
140 | + @DisplayName("When a validate creates a PublisherAction it should be saved and have all attributes right") | ||
141 | + @Test | ||
142 | + public void testValidateCreateAction(@Mock PublisherActionRepository publisherActionRepository) { | ||
143 | + PublisherAction publisherAction; | ||
144 | + ActionType actionType = ActionType.VALIDATE; | ||
145 | + | ||
146 | + publisher = new Publisher(); | ||
147 | + | ||
148 | + when(publisherVersionMock.getPublisher()).thenReturn(publisher); | ||
149 | + publisherAction = publisherServiceDefault.validatePublisherVersion(publisherVersionMock); | ||
150 | + verify(publisherActionRepository).save(publisherAction); | ||
151 | + assertThat(publisherAction.getPublisherVersion()).isEqualTo(publisherVersionMock); | ||
152 | + assertThat(publisherAction.getPublisher()).isEqualTo(publisher); | ||
153 | + assertThat(publisherAction.getPublisher().getValidatedVersion()).isEqualTo(publisherVersionMock); | ||
154 | + assertThat(publisherAction.getActionType()).isEqualTo(actionType); | ||
116 | } | 155 | } |
117 | - IndexedPublisher indexedPublisher = indexedPublisherArgumentCaptor.getValue(); | ||
118 | - assertThat(indexedPublisher.getPublisherId()).isEqualTo(111); | ||
119 | - assertThat(indexedPublisher.getPublisherName()).isEqualTo("nom"); | ||
120 | - assertThat(indexedPublisher.getPublisherHistory()).isEqualTo("histoire"); | ||
121 | - } | ||
122 | 156 | ||
123 | - @DisplayName("When a validate creates a PublisherAction it should be saved and have all attributes right") | ||
124 | - @Test | ||
125 | - public void testValidateCreateAction(@Mock PublisherActionRepository publisherActionRepository) { | ||
126 | - PublisherAction publisherAction; | ||
127 | - ActionType actionType = ActionType.VALIDATE; | ||
128 | - | ||
129 | - publisher = new Publisher(); | ||
130 | - | ||
131 | - when(publisherVersionMock.getPublisher()).thenReturn(publisher); | ||
132 | - publisherAction = publisherServiceDefault.validatePublisherVersion(publisherVersionMock); | ||
133 | - verify(publisherActionRepository).save(publisherAction); | ||
134 | - assertThat(publisherAction.getPublisherVersion()).isEqualTo(publisherVersionMock); | ||
135 | - assertThat(publisherAction.getPublisher()).isEqualTo(publisher); | ||
136 | - assertThat(publisherAction.getPublisher().getValidatedVersion()).isEqualTo(publisherVersionMock); | ||
137 | - assertThat(publisherAction.getActionType()).isEqualTo(actionType); | ||
138 | } | 157 | } |
139 | 158 | ||
140 | } | 159 | } | ... | ... |
-
Please register or login to post a comment