fix: page stats does not subscribe in finder (#3003)

#### What type of PR is this?
/kind bug
/area core
#### What this PR does / why we need it:
修复自定义页面 Finder 中 getByName 方法没有返回访问量的问题
此问题为重构 #2918 时引入。

#### Which issue(s) this PR fixes:

Fixes #2992

#### Special notes for your reviewer:
/cc @halo-dev/sig-halo 
#### Does this PR introduce a user-facing change?
```release-note
None
```
pull/3011/head^2
guqing 2022-12-20 22:56:29 +08:00 committed by GitHub
parent dee496b349
commit 9d6bcdf072
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 3 deletions

View File

@ -61,9 +61,12 @@ public class SinglePageFinderImpl implements SinglePageFinder {
SinglePageVo pageVo = SinglePageVo.from(page);
pageVo.setContributors(List.of());
pageVo.setContent(ContentVo.empty());
populateStats(pageVo);
return pageVo;
})
.flatMap(singlePageVo -> fetchStats(singlePageVo)
.doOnNext(singlePageVo::setStats)
.thenReturn(singlePageVo)
)
.flatMap(this::populateContributors)
.flatMap(page -> content(pageName)
.doOnNext(page::setContent)
@ -94,7 +97,7 @@ public class SinglePageFinderImpl implements SinglePageFinder {
pageVo.setContributors(List.of());
return pageVo;
})
.flatMap(lp -> populateStats(lp).doOnNext(lp::setStats).thenReturn(lp))
.flatMap(lp -> fetchStats(lp).doOnNext(lp::setStats).thenReturn(lp))
.concatMap(this::populateContributors)
.collectList()
.map(pageVos -> new ListResult<>(list.getPage(), list.getSize(), list.getTotal(),
@ -104,7 +107,7 @@ public class SinglePageFinderImpl implements SinglePageFinder {
.defaultIfEmpty(new ListResult<>(0, 0, 0, List.of()));
}
<T extends ListedSinglePageVo> Mono<StatsVo> populateStats(T pageVo) {
<T extends ListedSinglePageVo> Mono<StatsVo> fetchStats(T pageVo) {
String name = pageVo.getMetadata().getName();
return counterService.getByName(MeterUtils.nameOf(SinglePage.class, name))
.map(counter -> StatsVo.builder()

View File

@ -0,0 +1,79 @@
package run.halo.app.theme.finders.impl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import run.halo.app.content.ContentService;
import run.halo.app.core.extension.content.SinglePage;
import run.halo.app.extension.Metadata;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.metrics.CounterService;
import run.halo.app.theme.finders.ContributorFinder;
/**
* Tests for {@link SinglePageFinderImpl}.
*
* @author guqing
* @since 2.0.1
*/
@ExtendWith(MockitoExtension.class)
class SinglePageFinderImplTest {
@Mock
private ReactiveExtensionClient client;
@Mock
private ContentService contentService;
@Mock
private ContributorFinder contributorFinder;
@Mock
private CounterService counterService;
@InjectMocks
private SinglePageFinderImpl singlePageFinder;
@Test
void getByName() {
// fix gh-2992
String fakePageName = "fake-page";
SinglePage singlePage = new SinglePage();
singlePage.setMetadata(new Metadata());
singlePage.getMetadata().setName(fakePageName);
singlePage.setSpec(new SinglePage.SinglePageSpec());
singlePage.getSpec().setOwner("fake-owner");
singlePage.getSpec().setReleaseSnapshot("fake-release");
singlePage.setStatus(new SinglePage.SinglePageStatus());
when(client.fetch(eq(SinglePage.class), eq(fakePageName)))
.thenReturn(Mono.just(singlePage));
when(counterService.getByName(anyString())).thenReturn(Mono.empty());
when(contributorFinder.getContributor(anyString())).thenReturn(Mono.empty());
when(contentService.getContent(anyString())).thenReturn(Mono.empty());
singlePageFinder.getByName(fakePageName)
.as(StepVerifier::create)
.consumeNextWith(page -> {
assertThat(page.getStats()).isNotNull();
assertThat(page.getContent()).isNotNull();
})
.verifyComplete();
verify(client, times(2)).fetch(SinglePage.class, fakePageName);
verify(counterService).getByName(anyString());
verify(contentService).getContent(anyString());
verify(contributorFinder).getContributor(anyString());
}
}