JR Utily

test (and refactor) of PublisherVersionView#add method

...@@ -11,7 +11,7 @@ import java.util.List; ...@@ -11,7 +11,7 @@ import java.util.List;
11 */ 11 */
12 public interface PublisherService { 12 public interface PublisherService {
13 13
14 - void addNewPublisher(PublisherVersion publisherVersion); 14 + PublisherVersion addNewPublisher(PublisherVersion publisherVersion);
15 15
16 void addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion); 16 void addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion);
17 17
......
...@@ -42,7 +42,7 @@ public class PublisherServiceSpring implements PublisherService { ...@@ -42,7 +42,7 @@ public class PublisherServiceSpring implements PublisherService {
42 PublisherServiceSpring() { 42 PublisherServiceSpring() {
43 } 43 }
44 44
45 - public void addNewPublisher(PublisherVersion publisherVersion) { 45 + public PublisherVersion addNewPublisher(PublisherVersion publisherVersion) {
46 Publisher publisher = new Publisher(); 46 Publisher publisher = new Publisher();
47 Set<PublisherVersion> publisherVersions = new HashSet<>(); 47 Set<PublisherVersion> publisherVersions = new HashSet<>();
48 publisherVersions.add(publisherVersion); 48 publisherVersions.add(publisherVersion);
...@@ -52,6 +52,7 @@ public class PublisherServiceSpring implements PublisherService { ...@@ -52,6 +52,7 @@ public class PublisherServiceSpring implements PublisherService {
52 publisherVersion.setPublisherVersionDatetime(new Timestamp(new Date().getTime())); 52 publisherVersion.setPublisherVersionDatetime(new Timestamp(new Date().getTime()));
53 this.savePublisherVersion(publisherVersion); 53 this.savePublisherVersion(publisherVersion);
54 this.savePublisher(publisher); 54 this.savePublisher(publisher);
55 + return publisherVersion;
55 } 56 }
56 57
57 public void addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion) { 58 public void addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion) {
......
...@@ -49,6 +49,7 @@ public class PublisherVersionView implements Serializable { ...@@ -49,6 +49,7 @@ public class PublisherVersionView implements Serializable {
49 private String publisherHistory; 49 private String publisherHistory;
50 private User publisherVersionAuthor; 50 private User publisherVersionAuthor;
51 private Timestamp publisherVersionDatetime; 51 private Timestamp publisherVersionDatetime;
52 +
52 private Publisher publisher; 53 private Publisher publisher;
53 54
54 private List<Country> availableCountries; 55 private List<Country> availableCountries;
...@@ -107,7 +108,8 @@ public class PublisherVersionView implements Serializable { ...@@ -107,7 +108,8 @@ public class PublisherVersionView implements Serializable {
107 } 108 }
108 } 109 }
109 110
110 - private void setValues(PublisherVersion publisherVersion) { 111 + private PublisherVersion getNewPublisherVersion() {
112 + PublisherVersion publisherVersion = new PublisherVersion();
111 publisherVersion.setPublisherActive(publisherActive); 113 publisherVersion.setPublisherActive(publisherActive);
112 publisherVersion.setPublisherAddressCountry(publisherAddressCountry); 114 publisherVersion.setPublisherAddressCountry(publisherAddressCountry);
113 publisherVersion.setPublisherAddressLocality(publisherAddressLocality); 115 publisherVersion.setPublisherAddressLocality(publisherAddressLocality);
...@@ -120,6 +122,7 @@ public class PublisherVersionView implements Serializable { ...@@ -120,6 +122,7 @@ public class PublisherVersionView implements Serializable {
120 publisherVersion.setPublisherStreetAddress(publisherStreetAddress); 122 publisherVersion.setPublisherStreetAddress(publisherStreetAddress);
121 publisherVersion.setPublisherTelephone(publisherTelephone); 123 publisherVersion.setPublisherTelephone(publisherTelephone);
122 publisherVersion.setPublisherURL(publisherURL); 124 publisherVersion.setPublisherURL(publisherURL);
125 + return publisherVersion;
123 } 126 }
124 127
125 private void getImmutableValues() { 128 private void getImmutableValues() {
...@@ -131,10 +134,9 @@ public class PublisherVersionView implements Serializable { ...@@ -131,10 +134,9 @@ public class PublisherVersionView implements Serializable {
131 } 134 }
132 135
133 public void add() { 136 public void add() {
134 - PublisherVersion publisherVersion = new PublisherVersion(); 137 + PublisherVersion publisherVersion = getNewPublisherVersion();
135 - setValues(publisherVersion);
136 138
137 - publisherService.addNewPublisher(publisherVersion); 139 + publisherVersion = publisherService.addNewPublisher(publisherVersion);
138 140
139 newPublisher = false; 141 newPublisher = false;
140 publisherVersionId = publisherVersion.getPublisherVersionId(); 142 publisherVersionId = publisherVersion.getPublisherVersionId();
...@@ -151,8 +153,7 @@ public class PublisherVersionView implements Serializable { ...@@ -151,8 +153,7 @@ public class PublisherVersionView implements Serializable {
151 publisherEmail, publisherHistory, publisherName, publisherPostalCode, publisherPostOfficeBoxNumber, 153 publisherEmail, publisherHistory, publisherName, publisherPostalCode, publisherPostOfficeBoxNumber,
152 publisherStreetAddress, publisherTelephone, publisherURL); 154 publisherStreetAddress, publisherTelephone, publisherURL);
153 155
154 - PublisherVersion publisherVersion = new PublisherVersion(); 156 + PublisherVersion publisherVersion = getNewPublisherVersion();
155 - setValues(publisherVersion);
156 157
157 logger.trace("milieu update, avant enregistrement, publisherVersion = {}", publisherVersion); 158 logger.trace("milieu update, avant enregistrement, publisherVersion = {}", publisherVersion);
158 publisherService.addVersionToPublisher(publisher, publisherVersion); 159 publisherService.addVersionToPublisher(publisher, publisherVersion);
...@@ -344,4 +345,12 @@ public class PublisherVersionView implements Serializable { ...@@ -344,4 +345,12 @@ public class PublisherVersionView implements Serializable {
344 public void setNewPublisher(boolean newPublisher) { 345 public void setNewPublisher(boolean newPublisher) {
345 this.newPublisher = newPublisher; 346 this.newPublisher = newPublisher;
346 } 347 }
348 +
349 + public Publisher getPublisher() {
350 + return publisher;
351 + }
352 +
353 + public void setPublisher(Publisher publisher) {
354 + this.publisher = publisher;
355 + }
347 } 356 }
......
...@@ -12,15 +12,16 @@ import org.legrog.entities.Publisher; ...@@ -12,15 +12,16 @@ import org.legrog.entities.Publisher;
12 import org.legrog.entities.PublisherVersion; 12 import org.legrog.entities.PublisherVersion;
13 import org.legrog.test.MockitoExtension; 13 import org.legrog.test.MockitoExtension;
14 import org.legrog.web.xyz.SharedService; 14 import org.legrog.web.xyz.SharedService;
15 -import org.mockito.Answers; 15 +import org.mockito.*;
16 -import org.mockito.Mock; 16 +import org.mockito.stubbing.Answer;
17 -import org.mockito.runners.MockitoJUnitRunner;
18 import org.slf4j.Logger; 17 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory; 18 import org.slf4j.LoggerFactory;
20 19
21 import java.util.List; 20 import java.util.List;
22 21
23 import static org.assertj.core.api.Assertions.assertThat; 22 import static org.assertj.core.api.Assertions.assertThat;
23 +import static org.mockito.ArgumentMatchers.any;
24 +import static org.mockito.ArgumentMatchers.anyInt;
24 import static org.mockito.Mockito.when; 25 import static org.mockito.Mockito.when;
25 26
26 /** 27 /**
...@@ -39,7 +40,7 @@ public class PublisherVersionViewTest { ...@@ -39,7 +40,7 @@ public class PublisherVersionViewTest {
39 // TODO tester add et update, mais je ne sais pas comment je suis censé faire 40 // TODO tester add et update, mais je ne sais pas comment je suis censé faire
40 41
41 @BeforeEach 42 @BeforeEach
42 - public void setUp(@Mock PublisherService publisherService, @Mock SharedService sharedService) throws Exception{ 43 + public void setUp(@Mock PublisherService publisherService, @Mock SharedService sharedService) throws Exception {
43 publisherVersionView = new PublisherVersionView(publisherService, sharedService); 44 publisherVersionView = new PublisherVersionView(publisherService, sharedService);
44 publisherVersionView.setEditMode(false); 45 publisherVersionView.setEditMode(false);
45 publisherVersionView.setNewPublisher(false); 46 publisherVersionView.setNewPublisher(false);
...@@ -194,4 +195,163 @@ public class PublisherVersionViewTest { ...@@ -194,4 +195,163 @@ public class PublisherVersionViewTest {
194 assertThat(publisherVersionView.getAvailableCountries()).isEqualTo(countries); 195 assertThat(publisherVersionView.getAvailableCountries()).isEqualTo(countries);
195 } 196 }
196 } 197 }
198 +
199 +
200 + @Nested
201 + @DisplayName("Add method")
202 + class addTests {
203 +
204 + @BeforeEach
205 + public void given(@Mock Publisher publisherMock) {
206 + publisherVersionView.setNewPublisher(true);
207 +
208 + publisherVersionView.setPublisherName("A");
209 + publisherVersionView.setPublisherStreetAddress("B");
210 + publisherVersionView.setPublisherPostalCode("C");
211 + publisherVersionView.setPublisherPostOfficeBoxNumber("D");
212 + publisherVersionView.setPublisherAddressRegion("E");
213 + publisherVersionView.setPublisherAddressLocality("F");
214 + publisherVersionView.setPublisherAddressCountry(new Country());
215 + publisherVersionView.setPublisherTelephone("G");
216 + publisherVersionView.setPublisherEmail("H");
217 + publisherVersionView.setPublisherURL("I");
218 + publisherVersionView.setPublisherActive(true);
219 + publisherVersionView.setPublisherHistory("J");
220 + publisherVersionView.setPublisher(publisherMock);
221 + }
222 +
223 + @Nested
224 + @DisplayName("given some input fields")
225 + class given {
226 +
227 + @Captor
228 + ArgumentCaptor<PublisherVersion> publisherVersionArgumentCaptor;
229 +
230 + @BeforeEach
231 + public void test(@Mock PublisherService publisherService, @Mock Publisher publisherMock, @Mock PublisherVersion publisherVersionMock) {
232 + // mock de l'appel dans Immutables values
233 + when(publisherService.getPublisherVersion(anyInt())).thenReturn(new PublisherVersion());
234 +
235 +
236 + when(publisherService.addNewPublisher(any(PublisherVersion.class))).thenReturn(publisherVersionMock);
237 + when(publisherVersionMock.getPublisher()).thenReturn(publisherMock);
238 + when(publisherVersionMock.getPublisherVersionId()).thenReturn(42);
239 + publisherVersionView.add();
240 + }
241 +
242 + @DisplayName("should create in the application a new publisher with its first version")
243 + @Test
244 + public void shouldSave(@Mock PublisherService publisherService) {
245 + Mockito.verify(publisherService).addNewPublisher(publisherVersionArgumentCaptor.capture());
246 + PublisherVersion publisherVersion = publisherVersionArgumentCaptor.getValue();
247 + assertThat(publisherVersion.getPublisherName()).isEqualTo("A");
248 + }
249 +
250 +
251 + @DisplayName("should put state in non edit mode")
252 + @Test
253 + public void shouldNonEdit() {
254 + assertThat(publisherVersionView.isEditMode()).isFalse();
255 + }
256 +
257 + @DisplayName("should put state in non new publisher with new ids")
258 + @Test
259 + public void shouldNonNewPublisher(@Mock Publisher publisherMock) {
260 +
261 + assertThat(publisherVersionView.isNewPublisher()).isFalse();
262 + assertThat(publisherVersionView.getPublisher()).isEqualTo(publisherMock);
263 + assertThat(publisherVersionView.getPublisherVersionId()).isEqualTo(42);
264 + }
265 +
266 + }
267 +
268 + }
269 +
270 + // J'ai l'impression qu'on pourrait utiliser de l'imbrication ici
271 + /*
272 + @DisplayName("Annulation de saisie de nouvelle version de saisie d'un éditeur existant")
273 + @Test
274 + public void testCancelNewVersionOfPublisher() {
275 + publisherVersionView.setNewPublisher(false);
276 + publisherVersionView.setPublisherVersionId(1);
277 +
278 + publisherVersionView.setPublisherName("1");
279 + publisherVersionView.setPublisherStreetAddress("2");
280 + publisherVersionView.setPublisherPostalCode("3");
281 + publisherVersionView.setPublisherPostOfficeBoxNumber("4");
282 + publisherVersionView.setPublisherAddressRegion("5");
283 + publisherVersionView.setPublisherAddressLocality("6");
284 + publisherVersionView.setPublisherAddressCountry(new Country());
285 + publisherVersionView.setPublisherTelephone("7");
286 + publisherVersionView.setPublisherEmail("8");
287 + publisherVersionView.setPublisherURL("9");
288 + publisherVersionView.setPublisherActive(false);
289 + publisherVersionView.setPublisherHistory("10");
290 +
291 + publisherVersionView.cancel();
292 +
293 + assertThat(publisherVersionView.getPublisherName()).isEqualTo("A");
294 + assertThat(publisherVersionView.getPublisherStreetAddress()).isEqualTo("B");
295 + assertThat(publisherVersionView.getPublisherPostalCode()).isEqualTo("C");
296 + assertThat(publisherVersionView.getPublisherPostOfficeBoxNumber()).isEqualTo("D");
297 + assertThat(publisherVersionView.getPublisherAddressRegion()).isEqualTo("E");
298 + assertThat(publisherVersionView.getPublisherAddressLocality()).isEqualTo("F");
299 + assertThat(publisherVersionView.getPublisherTelephone()).isEqualTo("G");
300 + assertThat(publisherVersionView.getPublisherEmail()).isEqualTo("H");
301 + assertThat(publisherVersionView.getPublisherURL()).isEqualTo("I");
302 + assertThat(publisherVersionView.getPublisherHistory()).isEqualTo("J");
303 + assertThat(publisherVersionView.isEditMode()).isFalse();
304 +
305 + }
306 +
307 + @Test
308 + @DisplayName("Test de la fonction de chargement des valeurs initiales pour leur affichage")
309 + public void testLoadData() {
310 + publisherVersionView.setNewPublisher(true);
311 + publisherVersionView.setPublisherVersionId(1);
312 + publisherVersionView.loadData();
313 + assertThat(publisherVersionView.getPublisherName()).isEqualTo("A");
314 + assertThat(publisherVersionView.getPublisherStreetAddress()).isEqualTo("B");
315 + assertThat(publisherVersionView.getPublisherPostalCode()).isEqualTo("C");
316 + assertThat(publisherVersionView.getPublisherPostOfficeBoxNumber()).isEqualTo("D");
317 + assertThat(publisherVersionView.getPublisherAddressRegion()).isEqualTo("E");
318 + assertThat(publisherVersionView.getPublisherAddressLocality()).isEqualTo("F");
319 + assertThat(publisherVersionView.getPublisherTelephone()).isEqualTo("G");
320 + assertThat(publisherVersionView.getPublisherEmail()).isEqualTo("H");
321 + assertThat(publisherVersionView.getPublisherURL()).isEqualTo("I");
322 + assertThat(publisherVersionView.isPublisherActive()).isTrue();
323 + assertThat(publisherVersionView.getPublisherHistory()).isEqualTo("J");
324 + assertThat(publisherVersionView.isNewPublisher()).isFalse();
325 + }
326 +
327 + @Test
328 + @DisplayName("Test initialisation formulaire pour nouvel éditeur")
329 + public void testLoadDataEmpty() {
330 + publisherVersionView.setNewPublisher(false);
331 + publisherVersionView.setPublisherVersionId(0);
332 + publisherVersionView.loadData();
333 + assertThat(publisherVersionView.isNewPublisher()).isTrue();
334 + assertThat(publisherVersionView.isEditMode()).isTrue();
335 + }
336 +
337 + @Nested
338 + @DisplayName("post construct method")
339 + class init {
340 +
341 + private List<Country> countries;
342 +
343 + @BeforeEach
344 + public void setUp(@Mock SharedService sharedService) {
345 + when(sharedService.getAllCountries()).thenReturn(countries);
346 + }
347 +
348 + @Test
349 + @DisplayName("should set lists of available masks, user roles, and user properties from shared service")
350 + public void testList(@Mock SharedService sharedService) {
351 + publisherVersionView.init();
352 + assertThat(publisherVersionView.getAvailableCountries()).isEqualTo(countries);
353 + }
354 + }
355 +}
356 + */
197 } 357 }
......