JR Utily

test (and refactor) of PublisherVersionView#add method

......@@ -11,7 +11,7 @@ import java.util.List;
*/
public interface PublisherService {
void addNewPublisher(PublisherVersion publisherVersion);
PublisherVersion addNewPublisher(PublisherVersion publisherVersion);
void addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion);
......
......@@ -42,7 +42,7 @@ public class PublisherServiceSpring implements PublisherService {
PublisherServiceSpring() {
}
public void addNewPublisher(PublisherVersion publisherVersion) {
public PublisherVersion addNewPublisher(PublisherVersion publisherVersion) {
Publisher publisher = new Publisher();
Set<PublisherVersion> publisherVersions = new HashSet<>();
publisherVersions.add(publisherVersion);
......@@ -52,6 +52,7 @@ public class PublisherServiceSpring implements PublisherService {
publisherVersion.setPublisherVersionDatetime(new Timestamp(new Date().getTime()));
this.savePublisherVersion(publisherVersion);
this.savePublisher(publisher);
return publisherVersion;
}
public void addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion) {
......
......@@ -49,6 +49,7 @@ public class PublisherVersionView implements Serializable {
private String publisherHistory;
private User publisherVersionAuthor;
private Timestamp publisherVersionDatetime;
private Publisher publisher;
private List<Country> availableCountries;
......@@ -107,7 +108,8 @@ public class PublisherVersionView implements Serializable {
}
}
private void setValues(PublisherVersion publisherVersion) {
private PublisherVersion getNewPublisherVersion() {
PublisherVersion publisherVersion = new PublisherVersion();
publisherVersion.setPublisherActive(publisherActive);
publisherVersion.setPublisherAddressCountry(publisherAddressCountry);
publisherVersion.setPublisherAddressLocality(publisherAddressLocality);
......@@ -120,6 +122,7 @@ public class PublisherVersionView implements Serializable {
publisherVersion.setPublisherStreetAddress(publisherStreetAddress);
publisherVersion.setPublisherTelephone(publisherTelephone);
publisherVersion.setPublisherURL(publisherURL);
return publisherVersion;
}
private void getImmutableValues() {
......@@ -131,10 +134,9 @@ public class PublisherVersionView implements Serializable {
}
public void add() {
PublisherVersion publisherVersion = new PublisherVersion();
setValues(publisherVersion);
PublisherVersion publisherVersion = getNewPublisherVersion();
publisherService.addNewPublisher(publisherVersion);
publisherVersion = publisherService.addNewPublisher(publisherVersion);
newPublisher = false;
publisherVersionId = publisherVersion.getPublisherVersionId();
......@@ -151,8 +153,7 @@ public class PublisherVersionView implements Serializable {
publisherEmail, publisherHistory, publisherName, publisherPostalCode, publisherPostOfficeBoxNumber,
publisherStreetAddress, publisherTelephone, publisherURL);
PublisherVersion publisherVersion = new PublisherVersion();
setValues(publisherVersion);
PublisherVersion publisherVersion = getNewPublisherVersion();
logger.trace("milieu update, avant enregistrement, publisherVersion = {}", publisherVersion);
publisherService.addVersionToPublisher(publisher, publisherVersion);
......@@ -344,4 +345,12 @@ public class PublisherVersionView implements Serializable {
public void setNewPublisher(boolean newPublisher) {
this.newPublisher = newPublisher;
}
public Publisher getPublisher() {
return publisher;
}
public void setPublisher(Publisher publisher) {
this.publisher = publisher;
}
}
......
......@@ -12,15 +12,16 @@ import org.legrog.entities.Publisher;
import org.legrog.entities.PublisherVersion;
import org.legrog.test.MockitoExtension;
import org.legrog.web.xyz.SharedService;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.*;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
/**
......@@ -39,7 +40,7 @@ public class PublisherVersionViewTest {
// TODO tester add et update, mais je ne sais pas comment je suis censé faire
@BeforeEach
public void setUp(@Mock PublisherService publisherService, @Mock SharedService sharedService) throws Exception{
public void setUp(@Mock PublisherService publisherService, @Mock SharedService sharedService) throws Exception {
publisherVersionView = new PublisherVersionView(publisherService, sharedService);
publisherVersionView.setEditMode(false);
publisherVersionView.setNewPublisher(false);
......@@ -194,4 +195,163 @@ public class PublisherVersionViewTest {
assertThat(publisherVersionView.getAvailableCountries()).isEqualTo(countries);
}
}
}
\ No newline at end of file
@Nested
@DisplayName("Add method")
class addTests {
@BeforeEach
public void given(@Mock Publisher publisherMock) {
publisherVersionView.setNewPublisher(true);
publisherVersionView.setPublisherName("A");
publisherVersionView.setPublisherStreetAddress("B");
publisherVersionView.setPublisherPostalCode("C");
publisherVersionView.setPublisherPostOfficeBoxNumber("D");
publisherVersionView.setPublisherAddressRegion("E");
publisherVersionView.setPublisherAddressLocality("F");
publisherVersionView.setPublisherAddressCountry(new Country());
publisherVersionView.setPublisherTelephone("G");
publisherVersionView.setPublisherEmail("H");
publisherVersionView.setPublisherURL("I");
publisherVersionView.setPublisherActive(true);
publisherVersionView.setPublisherHistory("J");
publisherVersionView.setPublisher(publisherMock);
}
@Nested
@DisplayName("given some input fields")
class given {
@Captor
ArgumentCaptor<PublisherVersion> publisherVersionArgumentCaptor;
@BeforeEach
public void test(@Mock PublisherService publisherService, @Mock Publisher publisherMock, @Mock PublisherVersion publisherVersionMock) {
// mock de l'appel dans Immutables values
when(publisherService.getPublisherVersion(anyInt())).thenReturn(new PublisherVersion());
when(publisherService.addNewPublisher(any(PublisherVersion.class))).thenReturn(publisherVersionMock);
when(publisherVersionMock.getPublisher()).thenReturn(publisherMock);
when(publisherVersionMock.getPublisherVersionId()).thenReturn(42);
publisherVersionView.add();
}
@DisplayName("should create in the application a new publisher with its first version")
@Test
public void shouldSave(@Mock PublisherService publisherService) {
Mockito.verify(publisherService).addNewPublisher(publisherVersionArgumentCaptor.capture());
PublisherVersion publisherVersion = publisherVersionArgumentCaptor.getValue();
assertThat(publisherVersion.getPublisherName()).isEqualTo("A");
}
@DisplayName("should put state in non edit mode")
@Test
public void shouldNonEdit() {
assertThat(publisherVersionView.isEditMode()).isFalse();
}
@DisplayName("should put state in non new publisher with new ids")
@Test
public void shouldNonNewPublisher(@Mock Publisher publisherMock) {
assertThat(publisherVersionView.isNewPublisher()).isFalse();
assertThat(publisherVersionView.getPublisher()).isEqualTo(publisherMock);
assertThat(publisherVersionView.getPublisherVersionId()).isEqualTo(42);
}
}
}
// J'ai l'impression qu'on pourrait utiliser de l'imbrication ici
/*
@DisplayName("Annulation de saisie de nouvelle version de saisie d'un éditeur existant")
@Test
public void testCancelNewVersionOfPublisher() {
publisherVersionView.setNewPublisher(false);
publisherVersionView.setPublisherVersionId(1);
publisherVersionView.setPublisherName("1");
publisherVersionView.setPublisherStreetAddress("2");
publisherVersionView.setPublisherPostalCode("3");
publisherVersionView.setPublisherPostOfficeBoxNumber("4");
publisherVersionView.setPublisherAddressRegion("5");
publisherVersionView.setPublisherAddressLocality("6");
publisherVersionView.setPublisherAddressCountry(new Country());
publisherVersionView.setPublisherTelephone("7");
publisherVersionView.setPublisherEmail("8");
publisherVersionView.setPublisherURL("9");
publisherVersionView.setPublisherActive(false);
publisherVersionView.setPublisherHistory("10");
publisherVersionView.cancel();
assertThat(publisherVersionView.getPublisherName()).isEqualTo("A");
assertThat(publisherVersionView.getPublisherStreetAddress()).isEqualTo("B");
assertThat(publisherVersionView.getPublisherPostalCode()).isEqualTo("C");
assertThat(publisherVersionView.getPublisherPostOfficeBoxNumber()).isEqualTo("D");
assertThat(publisherVersionView.getPublisherAddressRegion()).isEqualTo("E");
assertThat(publisherVersionView.getPublisherAddressLocality()).isEqualTo("F");
assertThat(publisherVersionView.getPublisherTelephone()).isEqualTo("G");
assertThat(publisherVersionView.getPublisherEmail()).isEqualTo("H");
assertThat(publisherVersionView.getPublisherURL()).isEqualTo("I");
assertThat(publisherVersionView.getPublisherHistory()).isEqualTo("J");
assertThat(publisherVersionView.isEditMode()).isFalse();
}
@Test
@DisplayName("Test de la fonction de chargement des valeurs initiales pour leur affichage")
public void testLoadData() {
publisherVersionView.setNewPublisher(true);
publisherVersionView.setPublisherVersionId(1);
publisherVersionView.loadData();
assertThat(publisherVersionView.getPublisherName()).isEqualTo("A");
assertThat(publisherVersionView.getPublisherStreetAddress()).isEqualTo("B");
assertThat(publisherVersionView.getPublisherPostalCode()).isEqualTo("C");
assertThat(publisherVersionView.getPublisherPostOfficeBoxNumber()).isEqualTo("D");
assertThat(publisherVersionView.getPublisherAddressRegion()).isEqualTo("E");
assertThat(publisherVersionView.getPublisherAddressLocality()).isEqualTo("F");
assertThat(publisherVersionView.getPublisherTelephone()).isEqualTo("G");
assertThat(publisherVersionView.getPublisherEmail()).isEqualTo("H");
assertThat(publisherVersionView.getPublisherURL()).isEqualTo("I");
assertThat(publisherVersionView.isPublisherActive()).isTrue();
assertThat(publisherVersionView.getPublisherHistory()).isEqualTo("J");
assertThat(publisherVersionView.isNewPublisher()).isFalse();
}
@Test
@DisplayName("Test initialisation formulaire pour nouvel éditeur")
public void testLoadDataEmpty() {
publisherVersionView.setNewPublisher(false);
publisherVersionView.setPublisherVersionId(0);
publisherVersionView.loadData();
assertThat(publisherVersionView.isNewPublisher()).isTrue();
assertThat(publisherVersionView.isEditMode()).isTrue();
}
@Nested
@DisplayName("post construct method")
class init {
private List<Country> countries;
@BeforeEach
public void setUp(@Mock SharedService sharedService) {
when(sharedService.getAllCountries()).thenReturn(countries);
}
@Test
@DisplayName("should set lists of available masks, user roles, and user properties from shared service")
public void testList(@Mock SharedService sharedService) {
publisherVersionView.init();
assertThat(publisherVersionView.getAvailableCountries()).isEqualTo(countries);
}
}
}
*/
}
......