Skip to content

Commit

Permalink
#213 add integration-test module to test reloading of configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Jun 24, 2024
1 parent 6329752 commit f96d9b6
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public <T extends Config> T get(Class<T> configClass) {

public void reload(Class<? extends Config> configClass) {
try {
var env = get(ServerConfiguration.class).serverProperties().env();
log.debug("reload config + " + configClass.getSimpleName());
if (configClass.equals(SiteConfiguration.class)) {
var env = get(ServerConfiguration.class).serverProperties().env();
new SiteConfigurationLoader(hostBase, env).reload((SiteConfiguration)configs.get(configClass));
} else if (configClass.equals(TaxonomyConfiguration.class)) {
new TaxonomyConfigurationLoader(hostBase).reload((TaxonomyConfiguration)configs.get(configClass));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,22 @@ private void init_files () throws IOException {
}

public void init() throws IOException {
init(1, 1, TimeUnit.MINUTES);
}

public void init(int initialDelay, int delay, TimeUnit timeUnit) throws IOException {
init_files();

// setup scheduler
scheduler.scheduleWithFixedDelay(this, 1, 1, TimeUnit.MINUTES);
scheduler.scheduleWithFixedDelay(this, initialDelay, delay, timeUnit);
}

private void addPathToWatch(final Path configFile, final Class<? extends Config> configClass) throws IOException {
if (!Files.exists(configFile)) {
return;
}
watched_configurations.add(
new ConfigurationResource(configFile, configClass, Files.getLastModifiedTime(configFile).toMillis())
new ConfigurationResource(configFile, configClass, 0)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -39,8 +40,6 @@ public class FileSystemTest {

static FileSystem fileSystem;



@BeforeAll
static void setup() throws IOException {

Expand All @@ -55,6 +54,11 @@ static void setup() throws IOException {
});
fileSystem.init();
}

@AfterAll
static void shutdown () {
fileSystem.shutdown();
}

@Test
public void test_seconday_index() throws IOException {
Expand Down
23 changes: 23 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>5.1.0</version>
</parent>
<artifactId>integration-tests</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-api</artifactId>
</dependency>
<dependency>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-filesystem</artifactId>
</dependency>
</dependencies>

</project>
9 changes: 9 additions & 0 deletions integration-tests/reload/config/taxonomy.tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## YAML Template.
---
values:
- id: blue
title: Blau
- id: red
title: Rot
- id: orange
title: Orange
10 changes: 10 additions & 0 deletions integration-tests/reload/config/taxonomy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## YAML Template.
---
taxonomies:
- title: Kategorie
slug: kategorien
field: taxonomy.category
- title: Tags
slug: tags
field: taxonomy.tags
array: true
9 changes: 9 additions & 0 deletions integration-tests/reload/taxonomies/taxonomy.tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## YAML Template.
---
values:
- id: blue
title: Blau
- id: red
title: Rot
- id: orange
title: Orange
10 changes: 10 additions & 0 deletions integration-tests/reload/taxonomies/taxonomy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## YAML Template.
---
taxonomies:
- title: Kategorie
slug: kategorien
field: taxonomy.category
- title: Tags
slug: tags
field: taxonomy.tags
array: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.github.thmarx.cms.integration.tests;

/*-
* #%L
* integration-tests
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* 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.github.thmarx.cms.api.ServerProperties;
import com.github.thmarx.cms.api.configuration.Configuration;
import com.github.thmarx.cms.api.configuration.ConfigurationManagement;
import com.github.thmarx.cms.api.configuration.configs.ServerConfiguration;
import com.github.thmarx.cms.api.eventbus.Event;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.api.eventbus.EventListener;
import com.github.thmarx.cms.filesystem.FileDB;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

/**
*
* @author t.marx
*/
public class ConfigurationManagementReloadTest {

static FileDB db;

static Configuration configuration;

static ConfigurationManagement configurationManagement;

static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

static MockEventBus eventBus = new MockEventBus();

@BeforeAll
static void setup() throws IOException {

var serverProps = Mockito.mock(ServerProperties.class);

Mockito.when(serverProps.env()).thenReturn("dev");

configuration = new Configuration(Path.of("reload/"));
configuration.add(ServerConfiguration.class, new ServerConfiguration(serverProps));

db = new FileDB(Path.of("reload/"), eventBus, (path) -> Map.of(), configuration);
db.init();

configurationManagement = new ConfigurationManagement(db, configuration, scheduler, eventBus);
configurationManagement.init(0, 5, TimeUnit.SECONDS);
}

@BeforeEach
void prepare_clean_run () throws IOException {
Files.deleteIfExists(Path.of("reload/config/taxonomy.yaml"));
Files.deleteIfExists(Path.of("reload/config/taxonomy.tags.yaml"));
}

@AfterAll
static void shutdown() throws Exception {
db.close();
scheduler.shutdown();
}

@Test
void test_taxonomy() throws Exception {

eventBus.reset();
Files.copy(
Path.of("reload/taxonomies/taxonomy.yaml"),
Path.of("reload/config/taxonomy.yaml")
);
Files.copy(
Path.of("reload/taxonomies/taxonomy.tags.yaml"),
Path.of("reload/config/taxonomy.tags.yaml")
);

configurationManagement.reload();

Thread.sleep(Duration.ofSeconds(6));

Assertions.assertThat(eventBus.pubCounter).isEqualTo(2);
}

static class MockEventBus implements EventBus {

private int pubCounter = 0;

public void reset() {
pubCounter = 0;
}

@Override
public <T extends Event> void syncPublish(T event) {
}

@Override
public <T extends Event> void publish(T event) {
pubCounter++;
}

@Override
public <T extends Event> void register(Class<T> eventClass, EventListener<T> listener) {
}

@Override
public <T extends Event> void unregister(Class<T> eventClass, EventListener<T> listener) {
}

@Override
public void unregister(EventListener listener) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.github.thmarx.cms.integration.tests;

/*-
* #%L
* integration-tests
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* 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.github.thmarx.cms.api.configuration.Configuration;
import com.github.thmarx.cms.api.configuration.configs.TaxonomyConfiguration;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.filesystem.FileSystem;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.yaml.snakeyaml.Yaml;

/**
*
* @author t.marx
*/
public class ConfigurationReloadTest {

static FileSystem fileSystem;

static Configuration configuration;

@BeforeAll
static void setup() throws IOException {

Files.deleteIfExists(Path.of("reload/config/taxonomy.yaml"));
Files.deleteIfExists(Path.of("reload/config/taxonomy.tags.yaml"));

var eventBus = Mockito.mock(EventBus.class);

fileSystem = new FileSystem(Path.of("reload/"), eventBus, (file) -> {
try {
return new Yaml().load(Files.readString(file));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
fileSystem.init();

configuration = new Configuration(fileSystem.base());
}

@AfterAll
static void shutdown () {
fileSystem.shutdown();
}

@Test
void test_taxonomy () throws IOException {

TaxonomyConfiguration config = configuration.get(TaxonomyConfiguration.class);

Assertions.assertThat(config.getTaxonomies()).isEmpty();

Files.copy(
Path.of("reload/taxonomies/taxonomy.yaml"),
Path.of("reload/config/taxonomy.yaml")
);
Files.copy(
Path.of("reload/taxonomies/taxonomy.tags.yaml"),
Path.of("reload/config/taxonomy.tags.yaml")
);

configuration.reload(TaxonomyConfiguration.class);

Assertions.assertThat(config.getTaxonomies()).isNotEmpty();
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<module>cms-content</module>
<module>cms-extensions</module>
<module>cms-auth</module>
<module>integration-tests</module>
</modules>
<inceptionYear>2023</inceptionYear>
<organization>
Expand Down

0 comments on commit f96d9b6

Please sign in to comment.