Jean-Francois Leveque

Début d'implémentation recherche en TDD.

package org.legrog.entities;
import java.util.List;
/**
* Indexing/Search interface for IndexedPublisher
*/
......@@ -11,4 +13,12 @@ public interface PublisherSearchRepository {
* @return IndexedPublisher
*/
public IndexedPublisher save(IndexedPublisher indexedPublisher) throws IndexingException;
/**
* Searches for IndexedPublishers
*
* @param string String looked for in IndexedPublishers
* @return list of matching IndexedPublishers
*/
public List<IndexedPublisher> search(String string);
}
......
......@@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.io.IOException;
import java.util.List;
/**
* Implementation of PublisherSearchRepository using SolrJ
......@@ -40,4 +41,9 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
return indexedPublisher;
}
@Override
public List<IndexedPublisher> search(String string) {
return null;
}
}
......
package org.legrog.web.publisher;
import org.legrog.entities.PublisherVersion;
import javax.inject.Inject;
import java.util.List;
/**
* View behind publisherSearch.xhtml
*/
public class PublisherSearchView {
transient PublisherService publisherService;
/**
* Uses PublisherService to access search repository
*
* @param publisherService injected PublisherService
*/
@Inject
public PublisherSearchView(PublisherService publisherService) {
this.publisherService = publisherService;
}
PublisherSearchView() {
//no args constructor to make it proxyable
}
public List<PublisherVersion> search(String string) {
return publisherService.search(string);
}
}
package org.legrog.web.publisher;
import org.legrog.entities.IndexedPublisher;
import org.legrog.entities.Publisher;
import org.legrog.entities.PublisherAction;
import org.legrog.entities.PublisherVersion;
......@@ -72,4 +73,18 @@ public interface PublisherService {
* @return all PublisherAction on that PublisherVersion
*/
List<PublisherAction> getAllPublisherVersionActions(@NotNull PublisherVersion publisherVersion);
/**
*
* @param string String searched indexed publishers
* @return indexed publisher that matches the String
*/
List<PublisherVersion> search(@NotNull String string);
/**
*
* @param indexedPublishers IndexedPublisher List
* @return PublisherVersion List
*/
List<PublisherVersion> convert(List<IndexedPublisher> indexedPublishers);
}
\ No newline at end of file
......
......@@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashSet;
......@@ -137,4 +138,14 @@ public class PublisherServiceDefault implements PublisherService {
return publisherActionRepository.findByPublisherVersion(publisherVersion);
}
@Override
public List<PublisherVersion> search(@NotNull String string) {
return convert(publisherSearchRepository.search(string));
}
@Override
public List<PublisherVersion> convert(List<IndexedPublisher> indexedPublishers) {
return null;
}
}
......
<?xml version="1.0" encoding="UTF-8"?>
<!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">
<f:view>
<h:outputLabel value="Hello, world"/>
</f:view>
</html>
package org.legrog.web.publisher;
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.PublisherVersion;
import org.legrog.test.MockitoExtension;
import org.mockito.Mock;
import org.mockito.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 PublisherSearchView
*/
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
@DisplayName("Searches for an indexed publisher")
public class PublisherSearchViewTest {
private PublisherSearchView publisherSearchView;
private PublisherService publisherService;
@BeforeEach
public void setUp(@Mock PublisherService publisherService) {
this.publisherService = publisherService;
this.publisherSearchView = new PublisherSearchView(publisherService);
}
@Nested
@DisplayName("search method")
class SearchTests {
@Test
@DisplayName("when called, should delegate search to PublisherService with same string")
public void searchUsesPublisherService(@Mock PublisherService publisherService) {
publisherSearchView.search("1");
Mockito.verify(publisherService).search("1");
}
@Test
@DisplayName("when called, should return the answer it gets from PublisherService")
public void searchReturnsDataFromPublisherService(@Mock PublisherService publisherService) {
List<PublisherVersion> publisherVersionList = new ArrayList<>();
when(publisherService.search("2")).thenReturn(publisherVersionList);
assertThat(publisherSearchView.search("2")).isEqualTo(publisherVersionList);
}
}
}
......@@ -17,7 +17,9 @@ import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -51,8 +53,8 @@ public class PublisherServiceDefaultTest {
@Mock PublisherActionRepository publisherActionRepository,
@Mock PublisherSearchRepository publisherSearchRepository,
@Mock SharedService sharedService) throws Exception {
publisherServiceDefault = new PublisherServiceDefault(publisherRepository,
publisherVersionRepository, publisherActionRepository, publisherSearchRepository, sharedService);
publisherServiceDefault = Mockito.spy(new PublisherServiceDefault(publisherRepository,
publisherVersionRepository, publisherActionRepository, publisherSearchRepository, sharedService));
publisherVersion = new PublisherVersion();
publisherVersion1 = new PublisherVersion();
this.publisherRepository = publisherRepository;
......@@ -156,4 +158,43 @@ public class PublisherServiceDefaultTest {
}
@Nested
@DisplayName("search method")
class SearchTests {
@DisplayName("When called, should delegate search to PublisherSearchRepository")
@Test
public void testDelegateSearchToPublisherSearchRepository(@Mock PublisherSearchRepository publisherSearchRepository) {
publisherServiceDefault.search("3");
Mockito.verify(publisherSearchRepository).search("3");
}
@DisplayName("When getting IndexedPublishers from search, should convert them")
@Test
public void testConvertReturnedIndexedPublishers(@Mock PublisherSearchRepository publisherSearchRepository) {
List<IndexedPublisher> indexedPublishers = new ArrayList<>();
when(publisherSearchRepository.search("4")).thenReturn(indexedPublishers);
publisherServiceDefault.search("4");
verify(publisherServiceDefault, times(1)).convert(indexedPublishers);
}
@DisplayName("When called, should return the PublisherVersions it gets from convert")
@Test
public void testReturnFromConvert(@Mock PublisherSearchRepository publisherSearchRepository) {
List<PublisherVersion> publisherVersions = new ArrayList<>();
List<IndexedPublisher> indexedPublishers = new ArrayList<>();
Mockito.doReturn(publisherVersions).when(publisherServiceDefault).convert(indexedPublishers);
when(publisherSearchRepository.search("5")).thenReturn(indexedPublishers);
assertThat(publisherServiceDefault.search("5")).isEqualTo(publisherVersions);
}
}
@Nested
@DisplayName("convert method")
class ConvertTests {
}
}
......