Jean-Francois Leveque

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

1 package org.legrog.entities; 1 package org.legrog.entities;
2 2
3 +import java.util.List;
4 +
3 /** 5 /**
4 * Indexing/Search interface for IndexedPublisher 6 * Indexing/Search interface for IndexedPublisher
5 */ 7 */
...@@ -11,4 +13,12 @@ public interface PublisherSearchRepository { ...@@ -11,4 +13,12 @@ public interface PublisherSearchRepository {
11 * @return IndexedPublisher 13 * @return IndexedPublisher
12 */ 14 */
13 public IndexedPublisher save(IndexedPublisher indexedPublisher) throws IndexingException; 15 public IndexedPublisher save(IndexedPublisher indexedPublisher) throws IndexingException;
16 +
17 + /**
18 + * Searches for IndexedPublishers
19 + *
20 + * @param string String looked for in IndexedPublishers
21 + * @return list of matching IndexedPublishers
22 + */
23 + public List<IndexedPublisher> search(String string);
14 } 24 }
......
...@@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; ...@@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory;
8 8
9 import javax.inject.Inject; 9 import javax.inject.Inject;
10 import java.io.IOException; 10 import java.io.IOException;
11 +import java.util.List;
11 12
12 /** 13 /**
13 * Implementation of PublisherSearchRepository using SolrJ 14 * Implementation of PublisherSearchRepository using SolrJ
...@@ -40,4 +41,9 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository ...@@ -40,4 +41,9 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
40 41
41 return indexedPublisher; 42 return indexedPublisher;
42 } 43 }
44 +
45 + @Override
46 + public List<IndexedPublisher> search(String string) {
47 + return null;
48 + }
43 } 49 }
......
1 +package org.legrog.web.publisher;
2 +
3 +import org.legrog.entities.PublisherVersion;
4 +
5 +import javax.inject.Inject;
6 +import java.util.List;
7 +
8 +/**
9 + * View behind publisherSearch.xhtml
10 + */
11 +public class PublisherSearchView {
12 +
13 + transient PublisherService publisherService;
14 +
15 + /**
16 + * Uses PublisherService to access search repository
17 + *
18 + * @param publisherService injected PublisherService
19 + */
20 + @Inject
21 + public PublisherSearchView(PublisherService publisherService) {
22 + this.publisherService = publisherService;
23 + }
24 +
25 + PublisherSearchView() {
26 + //no args constructor to make it proxyable
27 + }
28 +
29 + public List<PublisherVersion> search(String string) {
30 + return publisherService.search(string);
31 + }
32 +}
1 package org.legrog.web.publisher; 1 package org.legrog.web.publisher;
2 2
3 +import org.legrog.entities.IndexedPublisher;
3 import org.legrog.entities.Publisher; 4 import org.legrog.entities.Publisher;
4 import org.legrog.entities.PublisherAction; 5 import org.legrog.entities.PublisherAction;
5 import org.legrog.entities.PublisherVersion; 6 import org.legrog.entities.PublisherVersion;
...@@ -72,4 +73,18 @@ public interface PublisherService { ...@@ -72,4 +73,18 @@ public interface PublisherService {
72 * @return all PublisherAction on that PublisherVersion 73 * @return all PublisherAction on that PublisherVersion
73 */ 74 */
74 List<PublisherAction> getAllPublisherVersionActions(@NotNull PublisherVersion publisherVersion); 75 List<PublisherAction> getAllPublisherVersionActions(@NotNull PublisherVersion publisherVersion);
76 +
77 + /**
78 + *
79 + * @param string String searched indexed publishers
80 + * @return indexed publisher that matches the String
81 + */
82 + List<PublisherVersion> search(@NotNull String string);
83 +
84 + /**
85 + *
86 + * @param indexedPublishers IndexedPublisher List
87 + * @return PublisherVersion List
88 + */
89 + List<PublisherVersion> convert(List<IndexedPublisher> indexedPublishers);
75 } 90 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; ...@@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory;
8 8
9 import javax.ejb.Stateless; 9 import javax.ejb.Stateless;
10 import javax.inject.Inject; 10 import javax.inject.Inject;
11 +import javax.validation.constraints.NotNull;
11 import java.sql.Timestamp; 12 import java.sql.Timestamp;
12 import java.util.Date; 13 import java.util.Date;
13 import java.util.HashSet; 14 import java.util.HashSet;
...@@ -137,4 +138,14 @@ public class PublisherServiceDefault implements PublisherService { ...@@ -137,4 +138,14 @@ public class PublisherServiceDefault implements PublisherService {
137 return publisherActionRepository.findByPublisherVersion(publisherVersion); 138 return publisherActionRepository.findByPublisherVersion(publisherVersion);
138 } 139 }
139 140
141 + @Override
142 + public List<PublisherVersion> search(@NotNull String string) {
143 + return convert(publisherSearchRepository.search(string));
144 + }
145 +
146 + @Override
147 + public List<PublisherVersion> convert(List<IndexedPublisher> indexedPublishers) {
148 + return null;
149 + }
150 +
140 } 151 }
......
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
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"
7 + xmlns:f="http://xmlns.jcp.org/jsf/core">
8 +<f:view>
9 + <h:outputLabel value="Hello, world"/>
10 +</f:view>
11 +</html>
1 +package org.legrog.web.publisher;
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.PublisherVersion;
11 +import org.legrog.test.MockitoExtension;
12 +import org.mockito.Mock;
13 +import org.mockito.Mockito;
14 +
15 +import java.util.ArrayList;
16 +import java.util.List;
17 +
18 +import static org.assertj.core.api.Assertions.assertThat;
19 +import static org.mockito.Mockito.when;
20 +
21 +/**
22 + * Classe testant PublisherSearchView
23 + */
24 +@RunWith(JUnitPlatform.class)
25 +@ExtendWith(MockitoExtension.class)
26 +@DisplayName("Searches for an indexed publisher")
27 +public class PublisherSearchViewTest {
28 +
29 + private PublisherSearchView publisherSearchView;
30 + private PublisherService publisherService;
31 +
32 + @BeforeEach
33 + public void setUp(@Mock PublisherService publisherService) {
34 + this.publisherService = publisherService;
35 + this.publisherSearchView = new PublisherSearchView(publisherService);
36 + }
37 +
38 + @Nested
39 + @DisplayName("search method")
40 + class SearchTests {
41 +
42 + @Test
43 + @DisplayName("when called, should delegate search to PublisherService with same string")
44 + public void searchUsesPublisherService(@Mock PublisherService publisherService) {
45 + publisherSearchView.search("1");
46 + Mockito.verify(publisherService).search("1");
47 + }
48 +
49 + @Test
50 + @DisplayName("when called, should return the answer it gets from PublisherService")
51 + public void searchReturnsDataFromPublisherService(@Mock PublisherService publisherService) {
52 + List<PublisherVersion> publisherVersionList = new ArrayList<>();
53 + when(publisherService.search("2")).thenReturn(publisherVersionList);
54 + assertThat(publisherSearchView.search("2")).isEqualTo(publisherVersionList);
55 + }
56 + }
57 +}
...@@ -17,7 +17,9 @@ import org.mockito.Mockito; ...@@ -17,7 +17,9 @@ import org.mockito.Mockito;
17 import org.slf4j.Logger; 17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory; 18 import org.slf4j.LoggerFactory;
19 19
20 +import java.util.ArrayList;
20 import java.util.HashSet; 21 import java.util.HashSet;
22 +import java.util.List;
21 import java.util.Set; 23 import java.util.Set;
22 24
23 import static org.assertj.core.api.Assertions.assertThat; 25 import static org.assertj.core.api.Assertions.assertThat;
...@@ -51,8 +53,8 @@ public class PublisherServiceDefaultTest { ...@@ -51,8 +53,8 @@ public class PublisherServiceDefaultTest {
51 @Mock PublisherActionRepository publisherActionRepository, 53 @Mock PublisherActionRepository publisherActionRepository,
52 @Mock PublisherSearchRepository publisherSearchRepository, 54 @Mock PublisherSearchRepository publisherSearchRepository,
53 @Mock SharedService sharedService) throws Exception { 55 @Mock SharedService sharedService) throws Exception {
54 - publisherServiceDefault = new PublisherServiceDefault(publisherRepository, 56 + publisherServiceDefault = Mockito.spy(new PublisherServiceDefault(publisherRepository,
55 - publisherVersionRepository, publisherActionRepository, publisherSearchRepository, sharedService); 57 + publisherVersionRepository, publisherActionRepository, publisherSearchRepository, sharedService));
56 publisherVersion = new PublisherVersion(); 58 publisherVersion = new PublisherVersion();
57 publisherVersion1 = new PublisherVersion(); 59 publisherVersion1 = new PublisherVersion();
58 this.publisherRepository = publisherRepository; 60 this.publisherRepository = publisherRepository;
...@@ -156,4 +158,43 @@ public class PublisherServiceDefaultTest { ...@@ -156,4 +158,43 @@ public class PublisherServiceDefaultTest {
156 158
157 } 159 }
158 160
161 + @Nested
162 + @DisplayName("search method")
163 + class SearchTests {
164 +
165 + @DisplayName("When called, should delegate search to PublisherSearchRepository")
166 + @Test
167 + public void testDelegateSearchToPublisherSearchRepository(@Mock PublisherSearchRepository publisherSearchRepository) {
168 + publisherServiceDefault.search("3");
169 + Mockito.verify(publisherSearchRepository).search("3");
170 +
171 + }
172 +
173 + @DisplayName("When getting IndexedPublishers from search, should convert them")
174 + @Test
175 + public void testConvertReturnedIndexedPublishers(@Mock PublisherSearchRepository publisherSearchRepository) {
176 + List<IndexedPublisher> indexedPublishers = new ArrayList<>();
177 +
178 + when(publisherSearchRepository.search("4")).thenReturn(indexedPublishers);
179 + publisherServiceDefault.search("4");
180 + verify(publisherServiceDefault, times(1)).convert(indexedPublishers);
181 + }
182 +
183 + @DisplayName("When called, should return the PublisherVersions it gets from convert")
184 + @Test
185 + public void testReturnFromConvert(@Mock PublisherSearchRepository publisherSearchRepository) {
186 + List<PublisherVersion> publisherVersions = new ArrayList<>();
187 + List<IndexedPublisher> indexedPublishers = new ArrayList<>();
188 + Mockito.doReturn(publisherVersions).when(publisherServiceDefault).convert(indexedPublishers);
189 + when(publisherSearchRepository.search("5")).thenReturn(indexedPublishers);
190 + assertThat(publisherServiceDefault.search("5")).isEqualTo(publisherVersions);
191 + }
192 +
193 + }
194 +
195 + @Nested
196 + @DisplayName("convert method")
197 + class ConvertTests {
198 +
199 + }
159 } 200 }
......