Jean-Francois Leveque

Country (pages et backing beans) et index : réécriture et tests.

1 package org.legrog.web.xyz; 1 package org.legrog.web.xyz;
2 2
3 import org.legrog.entities.Country; 3 import org.legrog.entities.Country;
4 +import org.slf4j.Logger;
5 +import org.slf4j.LoggerFactory;
4 6
5 import javax.enterprise.context.RequestScoped; 7 import javax.enterprise.context.RequestScoped;
6 import javax.inject.Inject; 8 import javax.inject.Inject;
7 import javax.inject.Named; 9 import javax.inject.Named;
10 +import java.io.Serializable;
8 11
9 @Named 12 @Named
10 @RequestScoped 13 @RequestScoped
11 -public class AddCountryBean { 14 +public class AddCountryView implements Serializable {
12 - @Inject 15 + transient Logger logger = LoggerFactory.getLogger(getClass());
16 +
13 private SharedService sharedService; 17 private SharedService sharedService;
14 18
15 private String countryName; 19 private String countryName;
16 20
17 - public String getCountryName() { 21 + public AddCountryView() {
18 - return countryName;
19 } 22 }
20 23
21 - public void setCountryName(String countryName) { 24 + @Inject
22 - this.countryName = countryName; 25 + public AddCountryView(SharedService sharedService) {
26 + this.sharedService = sharedService;
23 } 27 }
24 28
25 - public String add() 29 + public void add() {
26 - {
27 Country country = new Country(); 30 Country country = new Country();
28 country.setCountryName(countryName); 31 country.setCountryName(countryName);
29 - sharedService.addCountry(country); 32 + country = sharedService.addCountry(country);
30 - return "success"; 33 + countryName = "";
34 + }
35 +
36 + public String getCountryName() {
37 + return countryName;
38 + }
39 +
40 + public void setCountryName(String countryName) {
41 + this.countryName = countryName;
31 } 42 }
32 43
33 } 44 }
......
...@@ -6,22 +6,23 @@ import javax.annotation.PostConstruct; ...@@ -6,22 +6,23 @@ import javax.annotation.PostConstruct;
6 import javax.enterprise.context.RequestScoped; 6 import javax.enterprise.context.RequestScoped;
7 import javax.inject.Inject; 7 import javax.inject.Inject;
8 import javax.inject.Named; 8 import javax.inject.Named;
9 +import java.io.Serializable;
9 import java.util.List; 10 import java.util.List;
10 11
11 @Named 12 @Named
12 @RequestScoped 13 @RequestScoped
13 -public class ListCountriesBean { 14 +public class ListCountriesView implements Serializable {
14 - @Inject 15 +
15 private SharedService sharedService; 16 private SharedService sharedService;
16 17
17 private List<Country> countries; 18 private List<Country> countries;
18 19
19 - public List<Country> getCountries() { 20 + public ListCountriesView() {
20 - return countries;
21 } 21 }
22 22
23 - public void setCountries(List<Country> countries) { 23 + @Inject
24 - this.countries = countries; 24 + public ListCountriesView(SharedService sharedService) {
25 + this.sharedService = sharedService;
25 } 26 }
26 27
27 @PostConstruct 28 @PostConstruct
...@@ -29,4 +30,12 @@ public class ListCountriesBean { ...@@ -29,4 +30,12 @@ public class ListCountriesBean {
29 countries = sharedService.getAllCountries(); 30 countries = sharedService.getAllCountries();
30 } 31 }
31 32
33 + public List<Country> getCountries() {
34 + return countries;
35 + }
36 +
37 + public void setCountries(List<Country> countries) {
38 + this.countries = countries;
39 + }
40 +
32 } 41 }
......
...@@ -6,7 +6,7 @@ import java.util.List; ...@@ -6,7 +6,7 @@ import java.util.List;
6 6
7 public interface SharedService { 7 public interface SharedService {
8 8
9 - void addCountry(Country country); 9 + Country addCountry(Country country);
10 10
11 List<Country> getAllCountries(); 11 List<Country> getAllCountries();
12 12
......
...@@ -30,8 +30,9 @@ public class SharedServiceSpring implements SharedService { ...@@ -30,8 +30,9 @@ public class SharedServiceSpring implements SharedService {
30 30
31 private List<DisplayNameMask> allDisplayNameMasks; 31 private List<DisplayNameMask> allDisplayNameMasks;
32 32
33 - public void addCountry(Country country) { 33 + public Country addCountry(Country country) {
34 countryRepository.save(country); 34 countryRepository.save(country);
35 + return country;
35 } 36 }
36 37
37 public List<Country> getAllCountries() { 38 public List<Country> getAllCountries() {
......
...@@ -3,20 +3,40 @@ ...@@ -3,20 +3,40 @@
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" 4 <html xmlns="http://www.w3.org/1999/xhtml"
5 xmlns:h="http://xmlns.jcp.org/jsf/html" 5 xmlns:h="http://xmlns.jcp.org/jsf/html"
6 - xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 6 + xmlns:jsf="http://xmlns.jcp.org/jsf">
7 - xmlns:f="http://xmlns.jcp.org/jsf/core">
8 <body> 7 <body>
9 -<h:form> 8 +<ul>
10 - <h:commandLink action="home"> 9 + <li>
11 - <h:outputText value="Menu principal"/> 10 + <a jsf:outcome="/index">Menu principal</a>
12 - </h:commandLink> 11 + </li>
13 - 12 + <li>Éditeurs
13 + <ul>
14 + <li>
15 + <a jsf:outcome="/publisher/listPublisherVersions">Liste des versions des éditeurs</a>
16 + </li>
17 + <li>
18 + <a jsf:outcome="/publisher/listPublisherActions">Liste des actions des éditeurs</a>
19 + </li>
20 + <li>
21 + <a jsf:outcome="/publisher/publisherVersion">Ajouter un éditeur</a>
22 + </li>
23 + </ul>
24 + </li>
25 + <li>Pays
26 + <ul>
27 + <li>
28 + <a jsf:outcome="listCountries">Liste des pays</a>
29 + </li>
30 + </ul>
31 + </li>
32 +</ul>
33 + <form action="" jsf:id="country">
14 <h:panelGrid columns="2"> 34 <h:panelGrid columns="2">
15 - <h:outputText value='Nom du pays'/> 35 + <label for="name">Nom du pays</label>
16 - <h:inputText value='#{addCountryBean.countryName}'/> 36 + <input type="text" id="name" jsf:value='#{addCountryView.countryName}'/>
17 - <h:outputText value='Add'/> 37 +
18 - <h:commandButton action="#{addCountryBean.add}" value="Add"/> 38 + <button jsf:action="#{addCountryView.add}">Ajouter</button>
19 </h:panelGrid> 39 </h:panelGrid>
20 -</h:form> 40 +</form>
21 </body> 41 </body>
22 </html> 42 </html>
......
...@@ -2,17 +2,37 @@ ...@@ -2,17 +2,37 @@
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" 4 <html xmlns="http://www.w3.org/1999/xhtml"
5 - xmlns:h="http://xmlns.jcp.org/jsf/html"
6 xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 5 xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
7 - xmlns:f="http://xmlns.jcp.org/jsf/core"> 6 + xmlns:jsf="http://xmlns.jcp.org/jsf">
8 <body> 7 <body>
9 -<h:form>
10 -<h:commandLink action="home">
11 - <h:outputText value="Menu principal"/>
12 -</h:commandLink>
13 -</h:form>
14 <ul> 8 <ul>
15 - <ui:repeat value="#{listCountriesBean.countries}" var="country"> 9 + <li>
10 + <a jsf:outcome="/index">Menu principal</a>
11 + </li>
12 + <li>Éditeurs
13 + <ul>
14 + <li>
15 + <a jsf:outcome="/publisher/listPublisherVersions">Liste des versions des éditeurs</a>
16 + </li>
17 + <li>
18 + <a jsf:outcome="/publisher/listPublisherActions">Liste des actions des éditeurs</a>
19 + </li>
20 + <li>
21 + <a jsf:outcome="/publisher/publisherVersion">Ajouter un éditeur</a>
22 + </li>
23 + </ul>
24 + </li>
25 + <li>Pays
26 + <ul>
27 + <li>
28 + <a jsf:outcome="addCountry">Ajouter un pays</a>
29 + </li>
30 + </ul>
31 + </li>
32 +</ul>
33 +
34 +<ul>
35 + <ui:repeat value="#{listCountriesView.countries}" var="country">
16 <li>#{country.countryName}</li> 36 <li>#{country.countryName}</li>
17 </ui:repeat> 37 </ui:repeat>
18 </ul> 38 </ul>
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
5 xmlns:jsf="http://xmlns.jcp.org/jsf"> 5 xmlns:jsf="http://xmlns.jcp.org/jsf">
6 <body> 6 <body>
7 <ul> 7 <ul>
8 + <li>Éditeurs
9 + <ul>
8 <li> 10 <li>
9 <a jsf:outcome="publisher/listPublisherVersions">Liste des versions des éditeurs</a> 11 <a jsf:outcome="publisher/listPublisherVersions">Liste des versions des éditeurs</a>
10 </li> 12 </li>
...@@ -14,6 +16,18 @@ ...@@ -14,6 +16,18 @@
14 <li> 16 <li>
15 <a jsf:outcome="publisher/publisherVersion">Ajouter un éditeur</a> 17 <a jsf:outcome="publisher/publisherVersion">Ajouter un éditeur</a>
16 </li> 18 </li>
19 + </ul>
20 + </li>
21 + <li>Pays
22 + <ul>
23 + <li>
24 + <a jsf:outcome="xyz/listCountries">Liste des pays</a>
25 + </li>
26 + <li>
27 + <a jsf:outcome="xyz/addCountry">Ajouter un pays</a>
28 + </li>
29 + </ul>
30 + </li>
17 </ul> 31 </ul>
18 </body> 32 </body>
19 </html> 33 </html>
...\ No newline at end of file ...\ No newline at end of file
......
1 +package org.legrog.web.xyz;
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.entities.Country;
11 +import org.legrog.test.MockitoExtension;
12 +import org.mockito.*;
13 +import static org.assertj.core.api.Assertions.assertThat;
14 +
15 +/**
16 + * Classe testant AddCountryView
17 + */
18 +@RunWith(JUnitPlatform.class)
19 +@ExtendWith(MockitoExtension.class)
20 +@DisplayName("Adds a country to the list")
21 +public class AddCountryViewTest {
22 +
23 + @Nested
24 + @DisplayName("add method")
25 + class addTests {
26 +
27 + private AddCountryView addCountryView;
28 +
29 + @Captor
30 + ArgumentCaptor<Country> countryArgumentCaptor;
31 +
32 + @BeforeEach
33 + public void setup(@Mock SharedService sharedServiceMock) {
34 + addCountryView = new AddCountryView(sharedServiceMock);
35 + addCountryView.setCountryName("Zanzibar");
36 + addCountryView.add();
37 + }
38 +
39 + @Test
40 + @DisplayName("uses the SharedService add with right argument")
41 + public void testSharedServiceAdd(@Mock SharedService sharedServiceMock) {
42 + Mockito.verify(sharedServiceMock).addCountry(countryArgumentCaptor.capture());
43 + Country country = countryArgumentCaptor.getValue();
44 + assertThat(country.getCountryName()).isEqualTo("Zanzibar");
45 + }
46 + }
47 +}
1 +package org.legrog.web.xyz;
2 +
3 +import org.junit.jupiter.api.Test;
4 +import org.junit.jupiter.api.BeforeEach;
5 +import org.junit.jupiter.api.DisplayName;
6 +import org.junit.jupiter.api.Nested;
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.entities.Country;
11 +import org.legrog.test.MockitoExtension;
12 +import org.mockito.*;
13 +
14 +import java.util.ArrayList;
15 +import java.util.List;
16 +
17 +import static org.assertj.core.api.Assertions.assertThat;
18 +import static org.mockito.Mockito.when;
19 +
20 +/**
21 + * Classe testant ListCountriesView
22 + */
23 +@RunWith(JUnitPlatform.class)
24 +@ExtendWith(MockitoExtension.class)
25 +@DisplayName("Shows the country list")
26 +public class ListCountriesViewTest {
27 + private ListCountriesView listCountriesView;
28 + private List<Country> countryList;
29 +
30 + @BeforeEach
31 + public void setup(@Mock SharedService sharedService) throws Exception {
32 + countryList = new ArrayList<Country>();
33 +
34 + listCountriesView = new ListCountriesView(sharedService);
35 + when(sharedService.getAllCountries()).thenReturn(countryList);
36 + }
37 +
38 + @Nested
39 + @DisplayName("init method")
40 + class initTests{
41 +
42 + @Test
43 + @DisplayName("should get all countries")
44 + public void getAllCountriesTest() {
45 + listCountriesView.init();
46 + assertThat(listCountriesView.getCountries()).isEqualTo(countryList);
47 + }
48 + }
49 +}
...\ No newline at end of file ...\ No newline at end of file