Jean-Francois Leveque

Un test de plus pour le service avec le refactoring qui correspond et un petit b…

…ug résiduel en commenatire dans celui-ci.
......@@ -16,7 +16,7 @@ public interface PublisherService {
PublisherVersion addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion);
void validatePublisherVersion(PublisherVersion publisherVersion);
PublisherAction validatePublisherVersion(PublisherVersion publisherVersion);
List<PublisherVersion> getAllPublisherVersions();
......
......@@ -72,7 +72,7 @@ public class PublisherServiceSpring implements PublisherService {
return publisherVersion;
}
public void validatePublisherVersion(PublisherVersion publisherVersion) {
public PublisherAction validatePublisherVersion(PublisherVersion publisherVersion) {
Publisher publisher = publisherVersion.getPublisher();
publisher.setValidatedVersion(publisherVersion);
PublisherAction publisherAction = new PublisherAction();
......@@ -83,6 +83,7 @@ public class PublisherServiceSpring implements PublisherService {
publisherAction.setPublisher(publisher);
this.savePublisher(publisher);
publisherActionRepository.save(publisherAction);
return publisherAction;
}
protected void savePublisherVersion(PublisherVersion publisherVersion) {
......
package org.legrog.web.publisher;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtendWith;
......@@ -11,6 +11,12 @@ import org.legrog.test.MockitoExtension;
import org.legrog.web.xyz.SharedService;
import org.mockito.Mock;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
......@@ -29,6 +35,12 @@ public class PublisherServiceSpringTest {
PublisherRepository publisherRepository;
PublisherVersionRepository publisherVersionRepository;
@Inject
ActionTypeRepository actionTypeRepository;
@Mock
PublisherVersion publisherVersionMock;
@BeforeEach
public void setUp(@Mock PublisherRepository publisherRepository,
@Mock PublisherVersionRepository publisherVersionRepository,
......@@ -44,7 +56,7 @@ public class PublisherServiceSpringTest {
@DisplayName("Test ajout nouvel éditeur")
@Test
public void testAddNewPublisher() {
public void testAddNewPublisher(@Mock PublisherVersionRepository publisherVersionRepository) {
publisherServiceSpring.addNewPublisher(publisherVersion);
publisher = publisherVersion.getPublisher();
assertThat(publisher.getVersions()).containsExactly(publisherVersion);
......@@ -55,13 +67,37 @@ public class PublisherServiceSpringTest {
@DisplayName("Test ajout nouvelle version éditeur")
@Test
public void testAddVersionToPublisher() {
public void testAddVersionToPublisher(@Mock PublisherRepository publisherRepository,
@Mock PublisherVersionRepository publisherVersionRepository) {
publisherServiceSpring.addNewPublisher(publisherVersion);
publisher = publisherVersion.getPublisher();
publisherServiceSpring.addVersionToPublisher(publisher, publisherVersion1);
assertThat(publisherVersion1.getPublisher()).isEqualTo(publisher);
assertThat(publisher.getVersions()).contains(publisherVersion, publisherVersion1);
verify(publisherRepository).save(publisher);
verify(publisherRepository, times(2)).save(publisher);
verify(publisherVersionRepository).save(publisherVersion1);
}
@DisplayName("Test validation version éditeur")
@Test
public void testValidateVersion(@Mock PublisherActionRepository publisherActionRepository) {
Set<PublisherVersion> publisherVersions;
publisher = new Publisher();
publisherVersion = new PublisherVersion();
publisherVersions = new HashSet<PublisherVersion>();
publisherVersions.add(publisherVersion);
publisherVersions.add(publisherVersionMock);
when(publisherVersionMock.getPublisher()).thenReturn(publisher);
PublisherAction publisherAction;
publisherAction = publisherServiceSpring.validatePublisherVersion(publisherVersionMock);
verify(publisherActionRepository).save(publisherAction);
assertThat(publisherAction.getPublisherVersion()).isEqualTo(publisherVersionMock);
assertThat(publisherAction.getPublisher()).isEqualTo(publisher);
assertThat(publisherAction.getPublisher().getValidatedVersion()).isEqualTo(publisherVersionMock);
// TODO Régler ce problème de NPE
// assertThat(publisherAction.getActionType()).isEqualTo(actionTypeRepository.findByActionTypeName("Validate"));
}
}
......