Skip to content

Commit

Permalink
Merge branch 'main' into refactor/2820
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby authored Dec 12, 2022
2 parents 51b3394 + 52906f2 commit 085302c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public HandlerFunction<ServerResponse> getHandler(SystemSetting.ThemeRouteRules
String name) {
return request -> {
Map<String, Object> model = new HashMap<>();
model.put("name", name);
model.put("groupVersionKind", gvk);
model.put("plural", getPlural());
model.put(ModelConst.TEMPLATE_ID, DefaultTemplateEnum.SINGLE_PAGE.getValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package run.halo.app.theme.router.strategy;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;
import static run.halo.app.theme.DefaultTemplateEnum.SINGLE_PAGE;

import java.util.Map;
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 org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.content.SinglePage;
import run.halo.app.extension.GroupVersionKind;
import run.halo.app.extension.Metadata;
import run.halo.app.theme.finders.SinglePageFinder;
import run.halo.app.theme.finders.vo.SinglePageVo;
import run.halo.app.theme.router.ViewNameResolver;

/**
* Tests for {@link SinglePageRouteStrategy}.
Expand All @@ -28,9 +37,13 @@ class SinglePageRouteStrategyTest extends RouterStrategyTestSuite {
@Mock
private SinglePageFinder singlePageFinder;

@Mock
private ViewNameResolver viewNameResolver;

@InjectMocks
private SinglePageRouteStrategy strategy;


@Override
public void setUp() {
lenient().when(viewResolver.resolveViewName(eq(SINGLE_PAGE.getValue()), any()))
Expand Down Expand Up @@ -82,6 +95,43 @@ void shouldResponse200IfSlugNameContainsChineseChars() {
.expectStatus().isOk();
}

@Test
void ensureModel() {
// fix gh-2912
Metadata metadata = new Metadata();
metadata.setName("fake-name");
SinglePageVo singlePageVo = SinglePageVo.builder()
.metadata(metadata)
.spec(new SinglePage.SinglePageSpec())
.build();

when(singlePageFinder.getByName(eq("fake-name"))).thenReturn(Mono.just(singlePageVo));
permalinkHttpGetRouter.insert("/fake-slug",
strategy.getHandler(getThemeRouteRules(), "fake-name"));

when(viewNameResolver.resolveViewNameOrDefault(any(), any(), any()))
.thenReturn(Mono.just("page"));
when(viewResolver.resolveViewName(eq(SINGLE_PAGE.getValue()), any()))
.thenReturn(Mono.just(new EmptyView() {
@Override
public Mono<Void> render(Map<String, ?> model, MediaType contentType,
ServerWebExchange exchange) {
assertThat(model.get("name")).isEqualTo("fake-name");
assertThat(model.get("plural")).isEqualTo("singlepages");
assertThat(model.get("_templateId")).isEqualTo("page");
assertThat(model.get("singlePage")).isEqualTo(singlePageVo);
assertThat(model.get("groupVersionKind")).isEqualTo(
GroupVersionKind.fromExtension(SinglePage.class));
return Mono.empty();
}
}));
createClient().get()
.uri("/fake-slug")
.exchange()
.expectStatus().isOk()
.expectBody();
}

WebTestClient createClient() {
RouterFunction<ServerResponse> routeFunction = getRouterFunction();
return getWebTestClient(routeFunction);
Expand Down

0 comments on commit 085302c

Please sign in to comment.