Skip to content

Commit

Permalink
fix loading of properties (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
thmarx authored Oct 24, 2024
1 parent 76704fa commit 88ebd6d
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cms-core/configs/server.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server:
port: 1010
port: 8080
ip: "127.0.0.1"
env: dev
test: only in yaml
Expand Down
12 changes: 12 additions & 0 deletions cms-core/configs/site.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
id = "condation"
hostname = [ "condation.com", "localhost" ]
baseurl = "https://condation.com"

[template]
engine = "pebble"

[index]
persistent = true

[modules]
active = [ "pebble-module", "seo-module" ]
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ public Path getThemesFolder() {

@Override
public int serverPort() {
return configuration.getInteger("server.port", 8080);
// return configuration.getInteger("server.port", 8080);
var server = configuration.get("server", Server.class);
return server.port();
}

@Override
public String serverIp() {
return configuration.getString("server.ip", "127.0.0.1");
// return configuration.getString("server.ip", "127.0.0.1");
var server = configuration.get("server", Server.class);
return server.ip();
}

@Override
Expand All @@ -88,6 +92,7 @@ public boolean dev() {
return !Constants.Environments.PROD.equals(env());
}



public static record Server (int port, String ip) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.condation.cms.api.utils.FileUtils;
import com.condation.cms.core.configuration.reload.CronReload;
import com.condation.cms.api.eventbus.events.ConfigurationReloadEvent;
import com.condation.cms.core.configuration.properties.ExtendedServerProperties;
import com.condation.cms.core.scheduler.SingleCronJobScheduler;
import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -121,6 +122,14 @@ public void test_object () {
Assertions.assertThat(server.port).isEqualTo(1010);
}

@Test
public void test_properties () {
var serverProperties = new ExtendedServerProperties(configuration);

Assertions.assertThat(serverProperties.serverIp()).isEqualTo("127.0.0.1");
Assertions.assertThat(serverProperties.serverPort()).isEqualTo(1010);
}

@Data
@NoArgsConstructor
public static class Server {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.condation.cms.core.configuration;

/*-
* #%L
* tests
* %%
* Copyright (C) 2023 - 2024 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.condation.cms.core.configuration.configs.SimpleConfiguration;
import com.condation.cms.core.configuration.source.TomlConfigSource;
import com.condation.cms.core.configuration.source.YamlConfigSource;
import com.condation.cms.api.SiteProperties;
import com.condation.cms.api.eventbus.EventBus;
import com.condation.cms.api.scheduler.CronJobContext;
import com.condation.cms.api.scheduler.CronJobScheduler;
import com.condation.cms.api.utils.FileUtils;
import com.condation.cms.core.configuration.reload.CronReload;
import com.condation.cms.api.eventbus.events.ConfigurationReloadEvent;
import com.condation.cms.core.configuration.properties.ExtendedSiteProperties;
import com.condation.cms.core.scheduler.SingleCronJobScheduler;
import java.io.IOException;
import java.nio.file.Path;
import java.time.Duration;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

/**
*
* @author t.marx
*/
@ExtendWith(MockitoExtension.class)
public class SiteConfigurationTest {

SimpleConfiguration configuration;

Scheduler scheduler;
CronJobScheduler cronScheduler;

@Mock
SiteProperties siteProperties;

@Mock
EventBus eventBus;

private ExtendedSiteProperties extSiteProperties;

@BeforeEach
public void setup() throws IOException, SchedulerException {
scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
cronScheduler = new SingleCronJobScheduler(scheduler, new CronJobContext(), siteProperties);

configuration = SimpleConfiguration.builder(eventBus)
.id("test-config")
.reloadStrategy(new CronReload("0/10 * * * * ?", cronScheduler))
.addSource(YamlConfigSource.build(Path.of("configs/site.yaml")))
.addSource(TomlConfigSource.build(Path.of("configs/site.toml")))
.build();

extSiteProperties = new ExtendedSiteProperties(configuration);
}

@AfterEach
public void shutdown () throws SchedulerException {
scheduler.clear();
scheduler.shutdown();
}

@Test
public void test_hostname() {
var hostnames = extSiteProperties.hostnames();

Assertions.assertThat(hostnames).contains("localhost", "condation.com");
}


}
2 changes: 1 addition & 1 deletion cms-server/hosts/demo/site.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
id = "demo-site"
hostname = [ "localhost" ]
hostname = [ "localhost", "localhost2" ]
baseurl = "http://localhost:1010"
language = "en"
theme = "demo"
2 changes: 1 addition & 1 deletion cms-server/src/assembly/executable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<fileSet>
<directory>${project.basedir}</directory>
<includes>
<include>server.yaml</include>
<include>server.toml</include>
<include>log4j2.xml</include>
</includes>
</fileSet>
Expand Down
6 changes: 3 additions & 3 deletions distribution/build.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="distribution" default="distribution" basedir=".">
<property name="cms.version">7.0.0</property>
<property name="cms.version">7.1.1</property>

<property name="thymeleaf.version">v2.1.0</property>
<property name="thymeleaf.version">v3.0.0</property>

<target name="download-modules" description="--> download modules">
<echo>download template implementation modules</echo>
Expand All @@ -28,7 +28,7 @@
<copy todir="build/">
<resources>
<file file="temp/cms-server-${cms.version}/cms-server-${cms.version}.jar"/>
<file file="temp/cms-server-${cms.version}/server.yaml"/>
<file file="temp/cms-server-${cms.version}/server.toml"/>
<file file="temp/cms-server-${cms.version}/log4j2.xml"/>
<file file="../LICENSE"/>
</resources>
Expand Down

0 comments on commit 88ebd6d

Please sign in to comment.