refactor: the structure of finders vo (#2400)

#### What type of PR is this?
/kind improvement
/area core
/milestone 2.0

#### What this PR does / why we need it:
统一主题端 VO 的结构 
#### Which issue(s) this PR fixes:
Fixes #2394

#### Special notes for your reviewer:
how to test it?
通过主题端调用 finder 方法来验证

/cc @halo-dev/sig-halo 
#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/2402/head
guqing 2022-09-09 17:44:11 +08:00 committed by GitHub
parent 1d73228b1f
commit 9dc7bc1729
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 327 additions and 393 deletions

View File

@ -2,6 +2,7 @@ package run.halo.app.theme.dialect;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
@ -35,7 +36,7 @@ public class PostTemplateHeadProcessor implements TemplateHeadProcessor {
.map(template -> (String) context.getVariable(POST_NAME_VARIABLE))
.map(postFinder::getByName)
.doOnNext(postVo -> {
List<Map<String, String>> htmlMetas = postVo.getHtmlMetas();
List<Map<String, String>> htmlMetas = postVo.getSpec().getHtmlMetas();
String metaHtml = headMetaBuilder(htmlMetas);
IModelFactory modelFactory = context.getModelFactory();
model.add(modelFactory.createText(metaHtml));
@ -44,6 +45,9 @@ public class PostTemplateHeadProcessor implements TemplateHeadProcessor {
}
private String headMetaBuilder(List<Map<String, String>> htmlMetas) {
if (htmlMetas == null) {
return StringUtils.EMPTY;
}
StringBuilder sb = new StringBuilder();
for (Map<String, String> htmlMeta : htmlMetas) {
sb.append("<meta");

View File

@ -76,14 +76,15 @@ public class CategoryFinderImpl implements CategoryFinder {
public List<CategoryTreeVo> listAsTree() {
List<CategoryVo> categoryVos = listAll();
Map<String, CategoryVo> nameIdentityMap = categoryVos.stream()
.collect(Collectors.toMap(CategoryVo::getName, Function.identity()));
.collect(Collectors.toMap(categoryVo -> categoryVo.getMetadata().getName(),
Function.identity()));
Map<String, CategoryTreeVo> treeVoMap = new HashMap<>();
// populate parentName
categoryVos.forEach(categoryVo -> {
final String parentName = categoryVo.getName();
final String parentName = categoryVo.getMetadata().getName();
treeVoMap.putIfAbsent(parentName, CategoryTreeVo.from(categoryVo));
List<String> children = categoryVo.getChildren();
List<String> children = categoryVo.getSpec().getChildren();
if (CollectionUtils.isEmpty(children)) {
return;
}
@ -91,7 +92,7 @@ public class CategoryFinderImpl implements CategoryFinder {
CategoryVo childrenVo = nameIdentityMap.get(childrenName);
CategoryTreeVo treeVo = CategoryTreeVo.from(childrenVo);
treeVo.setParentName(parentName);
treeVoMap.putIfAbsent(treeVo.getName(), treeVo);
treeVoMap.putIfAbsent(treeVo.getMetadata().getName(), treeVo);
});
});
nameIdentityMap.clear();
@ -102,7 +103,7 @@ public class CategoryFinderImpl implements CategoryFinder {
Map<String, List<CategoryTreeVo>> nameIdentityMap = list.stream()
.filter(item -> item.getParentName() != null)
.collect(Collectors.groupingBy(CategoryTreeVo::getParentName));
list.forEach(node -> node.setChildren(nameIdentityMap.get(node.getName())));
list.forEach(node -> node.setChildren(nameIdentityMap.get(node.getMetadata().getName())));
return list.stream()
.filter(v -> v.getParentName() == null)
.collect(Collectors.toList());

View File

@ -35,7 +35,7 @@ public class MenuFinderImpl implements MenuFinder {
@Override
public MenuVo getByName(String name) {
return listAsTree().stream()
.filter(menu -> menu.getName().equals(name))
.filter(menu -> menu.getMetadata().getName().equals(name))
.findAny()
.orElse(null);
}
@ -63,10 +63,15 @@ public class MenuFinderImpl implements MenuFinder {
List<MenuItemVo> menuItemVos = populateParentName(listAllMenuItem());
List<MenuItemVo> treeList = listToTree(menuItemVos);
Map<String, MenuItemVo> nameItemRootNodeMap = treeList.stream()
.collect(Collectors.toMap(MenuItemVo::getName, Function.identity()));
.collect(Collectors.toMap(item -> item.getMetadata().getName(), Function.identity()));
return listAll().stream()
.map(menuVo -> {
List<MenuItemVo> menuItems = menuVo.getMenuItemNames().stream()
LinkedHashSet<String> menuItemNames = menuVo.getSpec().getMenuItems();
if (menuItemNames == null) {
return menuVo.withMenuItems(List.of());
}
List<MenuItemVo> menuItems = menuItemNames.stream()
.map(nameItemRootNodeMap::get)
.filter(Objects::nonNull)
.toList();
@ -79,7 +84,7 @@ public class MenuFinderImpl implements MenuFinder {
Map<String, List<MenuItemVo>> nameIdentityMap = list.stream()
.filter(item -> item.getParentName() != null)
.collect(Collectors.groupingBy(MenuItemVo::getParentName));
list.forEach(node -> node.setChildren(nameIdentityMap.get(node.getName())));
list.forEach(node -> node.setChildren(nameIdentityMap.get(node.getMetadata().getName())));
// clear map to release memory
nameIdentityMap.clear();
return list.stream()
@ -101,28 +106,30 @@ public class MenuFinderImpl implements MenuFinder {
static List<MenuItemVo> populateParentName(List<MenuItemVo> menuItemVos) {
Map<String, MenuItemVo> nameIdentityMap = menuItemVos.stream()
.collect(Collectors.toMap(MenuItemVo::getName, Function.identity()));
.collect(Collectors.toMap(menuItem -> menuItem.getMetadata().getName(),
Function.identity()));
Map<String, MenuItemVo> treeVoMap = new HashMap<>();
// populate parentName
menuItemVos.forEach(menuItemVo -> {
final String parentName = menuItemVo.getName();
final String parentName = menuItemVo.getMetadata().getName();
treeVoMap.putIfAbsent(parentName, menuItemVo);
LinkedHashSet<String> children = menuItemVo.getChildrenNames();
LinkedHashSet<String> children = menuItemVo.getSpec().getChildren();
if (CollectionUtils.isEmpty(children)) {
return;
}
children.forEach(childrenName -> {
MenuItemVo childrenVo = nameIdentityMap.get(childrenName);
childrenVo.setParentName(parentName);
treeVoMap.putIfAbsent(childrenVo.getName(), childrenVo);
treeVoMap.putIfAbsent(childrenVo.getMetadata().getName(), childrenVo);
});
});
// clear map to release memory
nameIdentityMap.clear();
Function<MenuItemVo, Integer> priorityCmp = menuItem -> menuItem.getSpec().getPriority();
return treeVoMap.values()
.stream()
.sorted(Comparator.comparing(MenuItemVo::getPriority))
.sorted(Comparator.comparing(priorityCmp))
.toList();
}
}

View File

@ -1,39 +0,0 @@
package run.halo.app.theme.finders.vo;
import java.util.List;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.SuperBuilder;
/**
* Base class for category value object.
*
* @author guqing
* @since 2.0.0
*/
@Getter
@ToString
@SuperBuilder
public class BaseCategoryVo {
String name;
String displayName;
String slug;
String description;
String cover;
String template;
Integer priority;
String permalink;
List<String> posts;
public int getPostCount() {
return posts == null ? 0 : posts.size();
}
}

View File

@ -1,26 +0,0 @@
package run.halo.app.theme.finders.vo;
import java.util.Map;
import lombok.experimental.SuperBuilder;
import run.halo.app.core.extension.Comment;
@SuperBuilder
public class BaseCommentVo {
String name;
String raw;
String content;
Comment.CommentOwner owner;
String userAgent;
Integer priority;
Boolean top;
Boolean allowNotification;
Map<String, String> annotations;
}

View File

@ -1,61 +0,0 @@
package run.halo.app.theme.finders.vo;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.SuperBuilder;
import run.halo.app.core.extension.Post;
/**
* a base entity for post.
*
* @author guqing
* @since 2.0.0
*/
@Data
@ToString
@SuperBuilder
public abstract class BasePostVo {
String name;
String title;
String slug;
String owner;
String template;
String cover;
Boolean published;
Instant publishTime;
Boolean pinned;
Boolean allowComment;
Post.VisibleEnum visible;
Integer version;
Integer priority;
String excerpt;
List<Map<String, String>> htmlMetas;
String permalink;
List<Contributor> contributors;
Map<String, String> annotations;
static <T> List<T> nullSafe(List<T> t) {
return Objects.requireNonNullElse(t, List.of());
}
}

View File

@ -1,12 +1,13 @@
package run.halo.app.theme.finders.vo;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.SuperBuilder;
import org.springframework.util.Assert;
import run.halo.app.core.extension.Category;
import run.halo.app.extension.MetadataOperator;
/**
* A tree vo for {@link Category}.
@ -15,10 +16,16 @@ import run.halo.app.core.extension.Category;
* @since 2.0.0
*/
@Data
@SuperBuilder
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class CategoryTreeVo extends BaseCategoryVo {
@Builder
@ToString
@EqualsAndHashCode
public class CategoryTreeVo {
private MetadataOperator metadata;
private Category.CategorySpec spec;
private Category.CategoryStatus status;
private List<CategoryTreeVo> children;
@ -33,14 +40,10 @@ public class CategoryTreeVo extends BaseCategoryVo {
public static CategoryTreeVo from(CategoryVo category) {
Assert.notNull(category, "The category must not be null");
return CategoryTreeVo.builder()
.name(category.getName())
.displayName(category.getDisplayName())
.slug(category.getSlug())
.description(category.getDescription())
.cover(category.getCover())
.template(category.getTemplate())
.priority(category.getPriority())
.permalink(category.getPermalink())
.metadata(category.getMetadata())
.spec(category.getSpec())
.status(category.getStatus())
.children(List.of())
.build();
}
}

View File

@ -1,10 +1,10 @@
package run.halo.app.theme.finders.vo;
import java.util.List;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.experimental.SuperBuilder;
import run.halo.app.core.extension.Category;
import run.halo.app.extension.MetadataOperator;
/**
* A value object for {@link Category}.
@ -13,11 +13,15 @@ import run.halo.app.core.extension.Category;
* @since 2.0.0
*/
@Value
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
public class CategoryVo extends BaseCategoryVo {
@Builder
@EqualsAndHashCode
public class CategoryVo {
List<String> children;
MetadataOperator metadata;
Category.CategorySpec spec;
Category.CategoryStatus status;
/**
* Convert {@link Category} to {@link CategoryVo}.
@ -26,17 +30,10 @@ public class CategoryVo extends BaseCategoryVo {
* @return category value object
*/
public static CategoryVo from(Category category) {
Category.CategorySpec spec = category.getSpec();
return CategoryVo.builder()
.name(category.getMetadata().getName())
.displayName(spec.getDisplayName())
.slug(spec.getSlug())
.description(spec.getDescription())
.cover(spec.getCover())
.template(spec.getTemplate())
.priority(spec.getPriority())
.children(spec.getChildren())
.permalink(category.getStatusOrDefault().getPermalink())
.metadata(category.getMetadata())
.spec(category.getSpec())
.status(category.getStatus())
.build();
}
}

View File

@ -1,9 +1,10 @@
package run.halo.app.theme.finders.vo;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.experimental.SuperBuilder;
import run.halo.app.core.extension.Comment;
import run.halo.app.extension.MetadataOperator;
/**
* A value object for {@link Comment}.
@ -12,11 +13,13 @@ import run.halo.app.core.extension.Comment;
* @since 2.0.0
*/
@Value
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
public class CommentVo extends BaseCommentVo {
@Builder
@EqualsAndHashCode
public class CommentVo {
Comment.CommentSubjectRef subjectRef;
MetadataOperator metadata;
Comment.CommentSpec spec;
/**
* Convert {@link Comment} to {@link CommentVo}.
@ -25,18 +28,9 @@ public class CommentVo extends BaseCommentVo {
* @return a value object for {@link Comment}
*/
public static CommentVo from(Comment comment) {
Comment.CommentSpec spec = comment.getSpec();
return CommentVo.builder()
.name(comment.getMetadata().getName())
.subjectRef(spec.getSubjectRef())
.raw(spec.getRaw())
.content(spec.getContent())
.owner(spec.getOwner())
.userAgent(spec.getUserAgent())
.priority(spec.getPriority())
.top(spec.getTop())
.allowNotification(spec.getAllowNotification())
.annotations(comment.getMetadata().getAnnotations())
.metadata(comment.getMetadata())
.spec(comment.getSpec())
.build();
}
}

View File

@ -1,6 +1,7 @@
package run.halo.app.theme.finders.vo;
import lombok.Builder;
import lombok.ToString;
import lombok.Value;
import run.halo.app.core.extension.Snapshot;
@ -11,6 +12,7 @@ import run.halo.app.core.extension.Snapshot;
* @since 2.0.0
*/
@Value
@ToString
@Builder
public class ContentVo {

View File

@ -1,6 +1,7 @@
package run.halo.app.theme.finders.vo;
import lombok.Builder;
import lombok.ToString;
import lombok.Value;
import run.halo.app.core.extension.User;
@ -11,6 +12,7 @@ import run.halo.app.core.extension.User;
* @since 2.0.0
*/
@Value
@ToString
@Builder
public class Contributor {
String name;

View File

@ -1,10 +1,11 @@
package run.halo.app.theme.finders.vo;
import java.util.LinkedHashSet;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import run.halo.app.core.extension.MenuItem;
import run.halo.app.extension.MetadataOperator;
/**
* A value object for {@link MenuItem}.
@ -13,23 +14,20 @@ import run.halo.app.core.extension.MenuItem;
* @since 2.0.0
*/
@Data
@ToString
@Builder
public class MenuItemVo {
String name;
MetadataOperator metadata;
String parentName;
MenuItem.MenuItemSpec spec;
String displayName;
String href;
Integer priority;
LinkedHashSet<String> childrenNames;
MenuItem.MenuItemStatus status;
List<MenuItemVo> children;
String parentName;
/**
* Convert {@link MenuItem} to {@link MenuItemVo}.
*
@ -39,11 +37,10 @@ public class MenuItemVo {
public static MenuItemVo from(MenuItem menuItem) {
MenuItem.MenuItemStatus status = menuItem.getStatus();
return MenuItemVo.builder()
.name(menuItem.getMetadata().getName())
.priority(menuItem.getSpec().getPriority())
.childrenNames(menuItem.getSpec().getChildren())
.displayName(status.getDisplayName())
.href(status.getHref())
.metadata(menuItem.getMetadata())
.spec(menuItem.getSpec())
.status(status)
.children(List.of())
.build();
}
}

View File

@ -1,12 +1,12 @@
package run.halo.app.theme.finders.vo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.LinkedHashSet;
import java.util.List;
import lombok.Builder;
import lombok.ToString;
import lombok.Value;
import lombok.With;
import run.halo.app.core.extension.Menu;
import run.halo.app.extension.MetadataOperator;
/**
* A value object for {@link Menu}.
@ -15,15 +15,13 @@ import run.halo.app.core.extension.Menu;
* @since 2.0.0
*/
@Value
@ToString
@Builder
public class MenuVo {
String name;
MetadataOperator metadata;
String displayName;
@JsonIgnore
LinkedHashSet<String> menuItemNames;
Menu.Spec spec;
@With
List<MenuItemVo> menuItems;
@ -36,9 +34,9 @@ public class MenuVo {
*/
public static MenuVo from(Menu menu) {
return builder()
.name(menu.getMetadata().getName())
.displayName(menu.getSpec().getDisplayName())
.menuItemNames(menu.getSpec().getMenuItems())
.metadata(menu.getMetadata())
.spec(menu.getSpec())
.menuItems(List.of())
.build();
}
}

View File

@ -1,11 +1,13 @@
package run.halo.app.theme.finders.vo;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.SuperBuilder;
import lombok.ToString;
import org.springframework.util.Assert;
import run.halo.app.core.extension.Post;
import run.halo.app.extension.MetadataOperator;
/**
* A value object for {@link Post}.
@ -14,15 +16,24 @@ import run.halo.app.core.extension.Post;
* @since 2.0.0
*/
@Data
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
public class PostVo extends BasePostVo {
@Builder
@ToString
@EqualsAndHashCode
public class PostVo {
ContentVo content;
private MetadataOperator metadata;
List<CategoryVo> categories;
private Post.PostSpec spec;
List<TagVo> tags;
private Post.PostStatus status;
private ContentVo content;
private List<CategoryVo> categories;
private List<TagVo> tags;
private List<Contributor> contributors;
/**
* Convert {@link Post} to {@link PostVo}.
@ -35,25 +46,11 @@ public class PostVo extends BasePostVo {
Post.PostSpec spec = post.getSpec();
Post.PostStatus postStatus = post.getStatusOrDefault();
return PostVo.builder()
.name(post.getMetadata().getName())
.annotations(post.getMetadata().getAnnotations())
.title(spec.getTitle())
.cover(spec.getCover())
.allowComment(spec.getAllowComment())
.metadata(post.getMetadata())
.spec(spec)
.status(postStatus)
.categories(List.of())
.tags(List.of())
.owner(spec.getOwner())
.pinned(spec.getPinned())
.slug(spec.getSlug())
.htmlMetas(nullSafe(spec.getHtmlMetas()))
.published(spec.getPublished())
.publishTime(spec.getPublishTime())
.priority(spec.getPriority())
.version(spec.getVersion())
.visible(spec.getVisible())
.template(spec.getTemplate())
.permalink(postStatus.getPermalink())
.excerpt(postStatus.getExcerpt())
.contributors(List.of())
.content(new ContentVo(null, null))
.build();

View File

@ -1,9 +1,11 @@
package run.halo.app.theme.finders.vo;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.Value;
import lombok.experimental.SuperBuilder;
import run.halo.app.core.extension.Reply;
import run.halo.app.extension.MetadataOperator;
/**
* A value object for {@link Reply}.
@ -12,13 +14,14 @@ import run.halo.app.core.extension.Reply;
* @since 2.0.0
*/
@Value
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
public class ReplyVo extends BaseCommentVo {
@Builder
@ToString
@EqualsAndHashCode
public class ReplyVo {
String commentName;
MetadataOperator metadata;
String quoteReply;
Reply.ReplySpec spec;
/**
* Convert {@link Reply} to {@link ReplyVo}.
@ -29,17 +32,8 @@ public class ReplyVo extends BaseCommentVo {
public static ReplyVo from(Reply reply) {
Reply.ReplySpec spec = reply.getSpec();
return ReplyVo.builder()
.name(reply.getMetadata().getName())
.commentName(spec.getCommentName())
.quoteReply(spec.getQuoteReply())
.raw(spec.getRaw())
.content(spec.getContent())
.owner(spec.getOwner())
.userAgent(spec.getUserAgent())
.priority(spec.getPriority())
.top(spec.getTop())
.allowNotification(spec.getAllowNotification())
.annotations(reply.getMetadata().getAnnotations())
.metadata(reply.getMetadata())
.spec(spec)
.build();
}
}

View File

@ -1,12 +1,13 @@
package run.halo.app.theme.finders.vo;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.SuperBuilder;
import org.springframework.util.Assert;
import run.halo.app.core.extension.SinglePage;
import run.halo.app.extension.MetadataOperator;
/**
* A value object for {@link SinglePage}.
@ -15,12 +16,20 @@ import run.halo.app.core.extension.SinglePage;
* @since 2.0.0
*/
@Data
@SuperBuilder
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class SinglePageVo extends BasePostVo {
@Builder
@ToString
@EqualsAndHashCode
public class SinglePageVo {
ContentVo content;
private MetadataOperator metadata;
private SinglePage.SinglePageSpec spec;
private SinglePage.SinglePageStatus status;
private ContentVo content;
private List<Contributor> contributors;
/**
* Convert {@link SinglePage} to {@link SinglePageVo}.
@ -33,23 +42,9 @@ public class SinglePageVo extends BasePostVo {
SinglePage.SinglePageSpec spec = singlePage.getSpec();
SinglePage.SinglePageStatus pageStatus = singlePage.getStatus();
return SinglePageVo.builder()
.name(singlePage.getMetadata().getName())
.annotations(singlePage.getMetadata().getAnnotations())
.title(spec.getTitle())
.cover(spec.getCover())
.allowComment(spec.getAllowComment())
.owner(spec.getOwner())
.pinned(spec.getPinned())
.slug(spec.getSlug())
.htmlMetas(nullSafe(spec.getHtmlMetas()))
.published(spec.getPublished())
.publishTime(spec.getPublishTime())
.priority(spec.getPriority())
.version(spec.getVersion())
.visible(spec.getVisible())
.template(spec.getTemplate())
.permalink(pageStatus.getPermalink())
.excerpt(pageStatus.getExcerpt())
.metadata(singlePage.getMetadata())
.spec(spec)
.status(pageStatus)
.contributors(List.of())
.content(new ContentVo(null, null))
.build();

View File

@ -1,10 +1,9 @@
package run.halo.app.theme.finders.vo;
import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.Value;
import run.halo.app.core.extension.Tag;
import run.halo.app.extension.MetadataOperator;
/**
* A value object for {@link Tag}.
@ -13,21 +12,11 @@ import run.halo.app.core.extension.Tag;
@Builder
public class TagVo {
String name;
MetadataOperator metadata;
String displayName;
Tag.TagSpec spec;
String slug;
String color;
String cover;
String permalink;
List<String> posts;
Map<String, String> annotations;
Tag.TagStatus status;
/**
* Convert {@link Tag} to {@link TagVo}.
@ -39,14 +28,9 @@ public class TagVo {
Tag.TagSpec spec = tag.getSpec();
Tag.TagStatus status = tag.getStatusOrDefault();
return TagVo.builder()
.name(tag.getMetadata().getName())
.displayName(spec.getDisplayName())
.slug(spec.getSlug())
.color(spec.getColor())
.cover(spec.getCover())
.permalink(status.getPermalink())
.posts(status.getPosts())
.annotations(tag.getMetadata().getAnnotations())
.metadata(tag.getMetadata())
.spec(spec)
.status(status)
.build();
}
}

View File

@ -26,6 +26,8 @@ import org.thymeleaf.templateresolver.StringTemplateResolver;
import org.thymeleaf.templateresource.ITemplateResource;
import org.thymeleaf.templateresource.StringTemplateResource;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.Post;
import run.halo.app.extension.Metadata;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.theme.DefaultTemplateEnum;
@ -113,9 +115,13 @@ class HaloProcessorDialectTest {
List<Map<String, String>> htmlMetas = new ArrayList<>();
htmlMetas.add(ImmutableSortedMap.of("name", "post-meta-V1", "content", "post-meta-V1"));
htmlMetas.add(ImmutableSortedMap.of("name", "post-meta-V2", "content", "post-meta-V2"));
Post.PostSpec postSpec = new Post.PostSpec();
postSpec.setHtmlMetas(htmlMetas);
Metadata metadata = new Metadata();
metadata.setName("fake-post");
PostVo postVo = PostVo.builder()
.htmlMetas(htmlMetas)
.name("fake-post").build();
.spec(postSpec)
.metadata(metadata).build();
when(postFinder.getByName(eq("fake-post"))).thenReturn(postVo);
String result = templateEngine.process("post", context);

View File

@ -50,20 +50,27 @@ class CategoryFinderImplTest {
when(client.fetch(eq(Category.class), eq("hello")))
.thenReturn(Mono.just(category()));
CategoryVo categoryVo = categoryFinder.getByName("hello");
categoryVo.getMetadata().setCreationTimestamp(null);
JSONAssert.assertEquals("""
{
"name": "hello",
"displayName": "displayName-1",
"slug": "slug-1",
"description": "description-1",
"cover": "cover-1",
"template": "template-1",
"priority": 0,
"children": [
"C1",
"C2"
],
"postCount": 0
"metadata": {
"name": "hello",
"annotations": {
"K1": "V1"
}
},
"spec": {
"displayName": "displayName-1",
"slug": "slug-1",
"description": "description-1",
"cover": "cover-1",
"template": "template-1",
"priority": 0,
"children": [
"C1",
"C2"
]
}
}
""",
JsonUtils.objectToJson(categoryVo),
@ -80,7 +87,7 @@ class CategoryFinderImplTest {
.thenReturn(Mono.just(categories));
ListResult<CategoryVo> list = categoryFinder.list(1, 10);
assertThat(list.getItems()).hasSize(3);
assertThat(list.get().map(CategoryVo::getName).toList())
assertThat(list.get().map(categoryVo -> categoryVo.getMetadata().getName()).toList())
.isEqualTo(List.of("c3", "c2", "hello"));
}

View File

@ -54,72 +54,135 @@ class MenuFinderImplTest {
List<MenuVo> menuVos = menuFinder.listAsTree();
JSONAssert.assertEquals("""
[
{
"name": "D",
"displayName": "D",
"menuItems": [
{
"name": "E",
"priority": 0,
"childrenNames": [
"A",
"C"
],
"children": [
{
"name": "A",
"parentName": "E",
"priority": 0,
"childrenNames": [
"B"
],
"children": [
{
"name": "B",
"parentName": "A",
"priority": 0
}
]
},
{
"name": "C",
"parentName": "E",
"priority": 0
}
]
}
]
},
{
"name": "X",
"displayName": "X",
"menuItems": [
{
"name": "G",
"priority": 0
}
]
},
{
"name": "Y",
"displayName": "Y",
"menuItems": [
{
"name": "F",
"priority": 0,
"childrenNames": [
"H"
],
"children": [
{
"name": "H",
"parentName": "F",
"priority": 0
}
]
}
]
}
{
"metadata": {
"name": "D"
},
"spec": {
"displayName": "D",
"menuItems": [
"E"
]
},
"menuItems": [
{
"metadata": {
"name": "E"
},
"spec": {
"displayName": "E",
"priority": 0,
"children": [
"A",
"C"
]
},
"status": {},
"children": [
{
"metadata": {
"name": "A"
},
"spec": {
"displayName": "A",
"priority": 0,
"children": [
"B"
]
},
"status": {},
"children": [
{
"metadata": {
"name": "B"
},
"spec": {
"displayName": "B",
"priority": 0
},
"status": {},
"parentName": "A"
}
],
"parentName": "E"
},
{
"metadata": {
"name": "C"
},
"spec": {
"displayName": "C",
"priority": 0
},
"status": {},
"parentName": "E"
}
]
}
]
},
{
"metadata": {
"name": "X"
},
"spec": {
"displayName": "X",
"menuItems": [
"G"
]
},
"menuItems": [
{
"metadata": {
"name": "G"
},
"spec": {
"displayName": "G",
"priority": 0
},
"status": {}
}
]
},
{
"metadata": {
"name": "Y"
},
"spec": {
"displayName": "Y",
"menuItems": [
"F"
]
},
"menuItems": [
{
"metadata": {
"name": "F"
},
"spec": {
"displayName": "F",
"priority": 0,
"children": [
"H"
]
},
"status": {},
"children": [
{
"metadata": {
"name": "H"
},
"spec": {
"displayName": "H",
"priority": 0
},
"status": {},
"parentName": "F"
}
]
}
]
}
]
""",
JsonUtils.objectToJson(menuVos),

View File

@ -48,21 +48,28 @@ class TagFinderImplTest {
when(client.fetch(eq(Tag.class), eq("t1")))
.thenReturn(Mono.just(tag(1)));
TagVo tagVo = tagFinder.getByName("t1");
tagVo.getMetadata().setCreationTimestamp(null);
JSONAssert.assertEquals("""
{
"name": "t1",
"displayName": "displayName-1",
"slug": "slug-1",
"color": "color-1",
"cover": "cover-1",
"permalink": "permalink-1",
"posts": [
"p1",
"p2"
],
"annotations": {
"K1": "V1"
}
"metadata": {
"name": "t1",
"annotations": {
"K1": "V1"
}
},
"spec": {
"displayName": "displayName-1",
"slug": "slug-1",
"color": "color-1",
"cover": "cover-1"
},
"status": {
"permalink": "permalink-1",
"posts": [
"p1",
"p2"
]
}
}
""",
JsonUtils.objectToJson(tagVo),
@ -78,7 +85,9 @@ class TagFinderImplTest {
);
List<TagVo> tags = tagFinder.listAll();
assertThat(tags).hasSize(3);
assertThat(tags.stream().map(TagVo::getName).collect(Collectors.toList()))
assertThat(tags.stream()
.map(tag -> tag.getMetadata().getName())
.collect(Collectors.toList()))
.isEqualTo(List.of("t3", "t2", "t1"));
}