forked from halo-dev/halo
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main'
- Loading branch information
Showing
17 changed files
with
463 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package run.halo.app.search; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Search service is used to search content. | ||
* | ||
* @author johnniang | ||
* @since 2.17.0 | ||
*/ | ||
public interface SearchService { | ||
|
||
/** | ||
* Perform search. | ||
* | ||
* @param option search option must not be null | ||
* @return search result | ||
*/ | ||
Mono<SearchResult> search(SearchOption option); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
application/src/main/java/run/halo/app/search/SearchServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package run.halo.app.search; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.validation.Validator; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Schedulers; | ||
import run.halo.app.infra.exception.RequestBodyValidationException; | ||
import run.halo.app.plugin.extensionpoint.ExtensionGetter; | ||
|
||
@Service | ||
public class SearchServiceImpl implements SearchService { | ||
|
||
private final Validator validator; | ||
|
||
private final ExtensionGetter extensionGetter; | ||
|
||
public SearchServiceImpl(Validator validator, ExtensionGetter extensionGetter) { | ||
this.validator = validator; | ||
this.extensionGetter = extensionGetter; | ||
} | ||
|
||
@Override | ||
public Mono<SearchResult> search(SearchOption option) { | ||
// validate the option | ||
var errors = validator.validateObject(option); | ||
if (errors.hasErrors()) { | ||
return Mono.error(new RequestBodyValidationException(errors)); | ||
} | ||
return extensionGetter.getEnabledExtension(SearchEngine.class) | ||
.filter(SearchEngine::available) | ||
.switchIfEmpty(Mono.error(SearchEngineUnavailableException::new)) | ||
.flatMap(searchEngine -> Mono.fromSupplier(() -> | ||
searchEngine.search(option) | ||
).subscribeOn(Schedulers.boundedElastic())); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ication/src/test/java/run/halo/app/plugin/DefaultPluginApplicationContextFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package run.halo.app.plugin; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.pf4j.PluginWrapper; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
import run.halo.app.search.SearchService; | ||
|
||
@SpringBootTest | ||
class DefaultPluginApplicationContextFactoryTest { | ||
|
||
@SpyBean | ||
SpringPluginManager pluginManager; | ||
|
||
DefaultPluginApplicationContextFactory factory; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
factory = new DefaultPluginApplicationContextFactory((SpringPluginManager) pluginManager); | ||
} | ||
|
||
@Test | ||
void shouldCreateCorrectly() { | ||
var pw = mock(PluginWrapper.class); | ||
when(pw.getPluginClassLoader()).thenReturn(this.getClass().getClassLoader()); | ||
var sp = mock(SpringPlugin.class); | ||
var pluginContext = new PluginContext.PluginContextBuilder() | ||
.name("fake-plugin") | ||
.version("1.0.0") | ||
.build(); | ||
when(sp.getPluginContext()).thenReturn(pluginContext); | ||
when(pw.getPlugin()).thenReturn(sp); | ||
when(pluginManager.getPlugin("fake-plugin")).thenReturn(pw); | ||
var context = factory.create("fake-plugin"); | ||
|
||
assertInstanceOf(PluginApplicationContext.class, context); | ||
assertNotNull(context.getBeanProvider(SearchService.class).getIfUnique()); | ||
// TODO Add more assertions here. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.