Jean-Francois Leveque

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

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