forked from halo-dev/halo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewNameResolverTest.java
108 lines (93 loc) · 3.6 KB
/
ViewNameResolverTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package run.halo.app.theme.router;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.reactive.function.server.MockServerRequest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import run.halo.app.theme.ThemeContext;
import run.halo.app.theme.ThemeResolver;
/**
* Tests for {@link ViewNameResolver}.
*
* @author guqing
* @since 2.0.0
*/
@ExtendWith(SpringExtension.class)
class ViewNameResolverTest {
@Mock
private ThemeResolver themeResolver;
@Mock
private ThymeleafProperties thymeleafProperties;
@InjectMocks
private ViewNameResolver viewNameResolver;
@TempDir
private File themePath;
@BeforeEach
void setUp() throws IOException {
when(thymeleafProperties.getSuffix()).thenReturn(ThymeleafProperties.DEFAULT_SUFFIX);
var templatesPath = themePath.toPath().resolve("templates");
if (!Files.exists(templatesPath)) {
Files.createDirectory(templatesPath);
}
Files.createFile(templatesPath.resolve("post_news.html"));
Files.createFile(templatesPath.resolve("post_docs.html"));
when(themeResolver.getTheme(any()))
.thenReturn(Mono.fromSupplier(() -> ThemeContext.builder()
.name("fake-theme")
.path(themePath.toPath())
.active(true)
.build())
);
}
@Test
void resolveViewNameOrDefault() throws URISyntaxException {
ServerWebExchange exchange = Mockito.mock(ServerWebExchange.class);
MockServerRequest request = MockServerRequest.builder()
.uri(new URI("/")).method(HttpMethod.GET)
.exchange(exchange)
.build();
viewNameResolver.resolveViewNameOrDefault(request, "post_news", "post")
.as(StepVerifier::create)
.expectNext("post_news")
.verifyComplete();
// post_docs.html
String viewName = "post_docs" + thymeleafProperties.getSuffix();
viewNameResolver.resolveViewNameOrDefault(request, viewName, "post")
.as(StepVerifier::create)
.expectNext(viewName)
.verifyComplete();
viewNameResolver.resolveViewNameOrDefault(request, "post_nothing", "post")
.as(StepVerifier::create)
.expectNext("post")
.verifyComplete();
}
@Test
void processName() {
var suffix = thymeleafProperties.getSuffix();
assertThat(viewNameResolver.computeResourceName("post_news"))
.isEqualTo("post_news" + suffix);
assertThat(
viewNameResolver.computeResourceName("post_news" + suffix))
.isEqualTo("post_news" + suffix);
assertThat(viewNameResolver.computeResourceName("post_news.test"))
.isEqualTo("post_news.test" + suffix);
assertThat(viewNameResolver.computeResourceName(null)).isNull();
}
}