From ebecef89c651fa1e7fddc0d0852cfc477f0c4d8a Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Wed, 3 Aug 2022 23:14:15 +0800 Subject: [PATCH] refactor: support configuration settings and configmap name (#2299) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core /milestone 2.0 #### What this PR does / why we need it: 主题支持配置 settingName 和 configMapName #### Which issue(s) this PR fixes: Fixes #2298 #### Special notes for your reviewer: /cc @halo-dev/sig-halo #### Does this PR introduce a user-facing change? ```release-note None ``` --- .../run/halo/app/core/extension/Theme.java | 27 +++++- .../halo/app/core/extension/ThemeTest.java | 82 +++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/test/java/run/halo/app/core/extension/ThemeTest.java diff --git a/src/main/java/run/halo/app/core/extension/Theme.java b/src/main/java/run/halo/app/core/extension/Theme.java index 8598d9839..9ecfe7789 100644 --- a/src/main/java/run/halo/app/core/extension/Theme.java +++ b/src/main/java/run/halo/app/core/extension/Theme.java @@ -4,6 +4,8 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.ToString; +import org.apache.commons.lang3.StringUtils; +import org.springframework.lang.NonNull; import run.halo.app.extension.AbstractExtension; import run.halo.app.extension.GVK; @@ -24,6 +26,7 @@ public class Theme extends AbstractExtension { @Data @ToString public static class ThemeSpec { + private static final String WILDCARD = "*"; @Schema(required = true, minLength = 1) private String displayName; @@ -39,9 +42,29 @@ public class Theme extends AbstractExtension { private String repo; - private String version = "*"; + private String version; - private String require = "*"; + private String require; + + private String settingName; + + private String configMapName; + + @NonNull + public String getVersion() { + if (StringUtils.isBlank(this.version)) { + return WILDCARD; + } + return version; + } + + @NonNull + public String getRequire() { + if (StringUtils.isBlank(this.require)) { + return WILDCARD; + } + return require; + } } @Data diff --git a/src/test/java/run/halo/app/core/extension/ThemeTest.java b/src/test/java/run/halo/app/core/extension/ThemeTest.java new file mode 100644 index 000000000..c8b4f27a4 --- /dev/null +++ b/src/test/java/run/halo/app/core/extension/ThemeTest.java @@ -0,0 +1,82 @@ +package run.halo.app.core.extension; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.json.JSONException; +import org.junit.jupiter.api.Test; +import org.skyscreamer.jsonassert.JSONAssert; +import run.halo.app.extension.Metadata; +import run.halo.app.infra.utils.JsonUtils; + +/** + * Tests for {@link Theme}. + * + * @author guqing + * @since 2.0.0 + */ +class ThemeTest { + + @Test + void constructor() throws JSONException { + Theme theme = new Theme(); + Metadata metadata = new Metadata(); + metadata.setName("test-theme"); + theme.setMetadata(metadata); + + + Theme.ThemeSpec themeSpec = new Theme.ThemeSpec(); + theme.setSpec(themeSpec); + themeSpec.setDisplayName("test-theme"); + + Theme.Author author = new Theme.Author(); + author.setName("test-author"); + author.setWebsite("https://test.com"); + themeSpec.setAuthor(author); + + themeSpec.setRepo("https://test.com"); + themeSpec.setLogo("https://test.com"); + themeSpec.setWebsite("https://test.com"); + themeSpec.setDescription("test-description"); + themeSpec.setConfigMapName("test-config-map"); + themeSpec.setSettingName("test-setting"); + + themeSpec.setVersion(null); + themeSpec.setRequire(null); + JSONAssert.assertEquals(""" + { + "spec": { + "displayName": "test-theme", + "author": { + "name": "test-author", + "website": "https://test.com" + }, + "description": "test-description", + "logo": "https://test.com", + "website": "https://test.com", + "repo": "https://test.com", + "version": "*", + "require": "*", + "settingName": "test-setting", + "configMapName": "test-config-map" + }, + "apiVersion": "theme.halo.run/v1alpha1", + "kind": "Theme", + "metadata": { + "name": "test-theme", + "labels": null, + "annotations": null, + "version": null, + "creationTimestamp": null, + "deletionTimestamp": null + } + } + """, + JsonUtils.objectToJson(theme), + true); + + themeSpec.setVersion("1.0.0"); + themeSpec.setRequire("2.0.0"); + assertThat(themeSpec.getVersion()).isEqualTo("1.0.0"); + assertThat(themeSpec.getRequire()).isEqualTo("2.0.0"); + } +} \ No newline at end of file