Skip to content

Commit

Permalink
#98 refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Dec 3, 2023
1 parent f653674 commit 08f3569
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 32 deletions.
22 changes: 22 additions & 0 deletions cms-api/src/main/java/com/github/thmarx/cms/api/db/Content.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package com.github.thmarx.cms.api.db;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 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 java.nio.file.Path;
import java.util.List;
import java.util.Optional;
Expand Down
22 changes: 22 additions & 0 deletions cms-api/src/main/java/com/github/thmarx/cms/api/db/DB.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package com.github.thmarx.cms.api.db;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 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%
*/

/**
*
* @author thmar
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
package com.github.thmarx.cms.api.db;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 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 java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
*
* @author thmar
*/
public interface DBFileSystem {
Path resolve(String path);

String loadContent(final Path file) throws IOException;

List<String> loadLines(final Path file) throws IOException;

String loadContent(final Path file, final Charset charset) throws IOException;

List<String> loadLines(final Path file, final Charset charset) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package com.github.thmarx.cms.filesystem;

/*-
* #%L
* cms-filesystem
* %%
* Copyright (C) 2023 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.db.Content;
import java.nio.file.Path;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package com.github.thmarx.cms.filesystem;

/*-
* #%L
* cms-filesystem
* %%
* Copyright (C) 2023 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.db.Content;
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.api.db.DBFileSystem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,22 @@ public Path resolve(String path) {
return hostBaseDirectory.resolve(path);
}

@Override
public String loadContent(final Path file) throws IOException {
return loadContent(file, StandardCharsets.UTF_8);
}

@Override
public List<String> loadLines(final Path file) throws IOException {
return loadLines(file, StandardCharsets.UTF_8);
}

@Override
public String loadContent(final Path file, final Charset charset) throws IOException {
return Files.readString(file, charset);
}

@Override
public List<String> loadLines(final Path file, final Charset charset) throws IOException {
return Files.readAllLines(file, charset);
}
Expand Down
13 changes: 7 additions & 6 deletions cms-server/src/test/java/com/github/thmarx/cms/SectionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.github.thmarx.cms.filesystem.MetaData;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.api.template.TemplateEngine;
import com.github.thmarx.cms.filesystem.FileDB;
import com.github.thmarx.cms.template.TemplateEngineTest;
import com.github.thmarx.cms.theme.DefaultTheme;
import java.io.IOException;
Expand All @@ -48,33 +49,33 @@ public class SectionsTest extends TemplateEngineTest {

static ContentRenderer contentRenderer;
static MarkdownRenderer markdownRenderer;
static FileSystem fileSystem;
static FileDB db;

@BeforeAll
public static void beforeClass() throws IOException {
var contentParser = new ContentParser();
fileSystem = new FileSystem(Path.of("hosts/test/"), new DefaultEventBus(), (file) -> {
db = new FileDB(Path.of("hosts/test/"), new DefaultEventBus(), (file) -> {
try {
return contentParser.parseMeta(file);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
fileSystem.init();
db.init();
markdownRenderer = TestHelper.getRenderer();
TemplateEngine templates = new TestTemplateEngine(fileSystem);
TemplateEngine templates = new TestTemplateEngine(db);

contentRenderer = new ContentRenderer(contentParser,
() -> templates,
fileSystem,
db,
new SiteProperties(Map.of()),
() -> new MockModuleManager()
);
}

@Test
public void test_sections() throws IOException {
List<MetaData.MetaNode> listSections = fileSystem.listSections(fileSystem.resolve("content/page.md"));
List<MetaData.MetaNode> listSections = db.getContent().listSections(db.getFileSystem().resolve("content/page.md"));
Assertions.assertThat(listSections).hasSize(4);

Map<String, List<ContentRenderer.Section>> renderSections = contentRenderer.renderSections(listSections, requestContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* #L%
*/
import com.github.thmarx.cms.api.template.TemplateEngine;
import com.github.thmarx.cms.filesystem.FileSystem;
import com.github.thmarx.cms.filesystem.FileDB;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
Expand All @@ -39,7 +39,7 @@ public class TestTemplateEngine implements TemplateEngine {

private StringSubstitutor stringSubstitutor = new StringSubstitutor();

private final FileSystem fileSystem;
private final FileDB db;

@Override
public void invalidateCache() {
Expand All @@ -53,7 +53,7 @@ public String render(final String template, Model model) throws IOException {
values.putAll(model.values);
values.put("meta.title", ((Map<String, Object>)model.values.getOrDefault("meta", Map.of())).getOrDefault("title", "<no title>"));

String templateContent = fileSystem.loadContent(fileSystem.resolve("templates").resolve(template), StandardCharsets.UTF_8);
String templateContent = db.getFileSystem().loadContent(db.getFileSystem().resolve("templates").resolve(template), StandardCharsets.UTF_8);

StringSubstitutor sub = new StringSubstitutor(values);
return sub.replace(templateContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
* #L%
*/

import com.github.thmarx.cms.content.ContentParser;
import com.github.thmarx.cms.filesystem.FileSystem;
import com.github.thmarx.cms.eventbus.DefaultEventBus;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
import com.github.thmarx.cms.TestHelper;
import com.github.thmarx.cms.TestTemplateEngine;
import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.eventbus.DefaultEventBus;
import com.github.thmarx.cms.filesystem.FileSystem;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.api.template.TemplateEngine;
import com.github.thmarx.cms.filesystem.FileDB;
import com.github.thmarx.cms.template.TemplateEngineTest;
import com.github.thmarx.modules.api.ModuleManager;
import java.io.IOException;
Expand All @@ -56,19 +57,19 @@ public class ContentRendererNGTest extends TemplateEngineTest {
@BeforeAll
public static void beforeClass () throws IOException {
var contentParser = new ContentParser();
final FileSystem fileSystem = new FileSystem(Path.of("hosts/test/"), new DefaultEventBus(), (file) -> {
final FileDB db = new FileDB(Path.of("hosts/test/"), new DefaultEventBus(), (file) -> {
try {
return contentParser.parseMeta(file);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
markdownRenderer = TestHelper.getRenderer();
TemplateEngine templates = new TestTemplateEngine(fileSystem);
TemplateEngine templates = new TestTemplateEngine(db);

contentRenderer = new ContentRenderer(contentParser,
() -> templates,
fileSystem,
db,
new SiteProperties(Map.of()),
() -> moduleManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.github.thmarx.cms.TestHelper;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.eventbus.DefaultEventBus;
import com.github.thmarx.cms.filesystem.FileDB;
import com.github.thmarx.cms.filesystem.FileSystem;
import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -42,27 +43,27 @@
public class NodeListFunctionBuilderNGTest {

static NodeListFunctionBuilder nodeList;
static FileSystem fileSystem;
static FileDB db;

static ContentParser parser = new ContentParser();
static MarkdownRenderer markdownRenderer = TestHelper.getRenderer();

@BeforeAll
static void setup () throws IOException {

fileSystem = new FileSystem(Path.of("hosts/test"), new DefaultEventBus(), (file) -> {
db = new FileDB(Path.of("hosts/test"), new DefaultEventBus(), (file) -> {
try {
return parser.parseMeta(file);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
fileSystem.init();
nodeList = new NodeListFunctionBuilder(fileSystem, fileSystem.resolve("content/").resolve("index.md"), parser, markdownRenderer);
db.init();
nodeList = new NodeListFunctionBuilder(db, db.getFileSystem().resolve("content/").resolve("index.md"), parser, markdownRenderer);
}
@AfterAll
static void close () {
fileSystem.shutdown();
static void close () throws Exception {
db.close();
}

@Test
Expand Down Expand Up @@ -145,7 +146,7 @@ void list_asterix () {

@Test
void test_from_subfolder () {
var nodeList = new NodeListFunctionBuilder(fileSystem, fileSystem.resolve("content/nodelist2/index.md"), parser, markdownRenderer);
var nodeList = new NodeListFunctionBuilder(db, db.getFileSystem().resolve("content/nodelist2/index.md"), parser, markdownRenderer);
Page<Node> page = nodeList.from("./sub_folder/*").page(1).size(10).list();
var nodeUris = page.getItems().stream().map(Node::path).collect(Collectors.toList());
Assertions.assertThat(nodeUris)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.github.thmarx.cms.TestHelper;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.eventbus.DefaultEventBus;
import com.github.thmarx.cms.filesystem.FileDB;
import com.github.thmarx.cms.filesystem.FileSystem;
import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -42,21 +43,21 @@
public class NavigationFunctionNGTest {

static NavigationFunction navigationFunction;
private static FileSystem fileSystem;
private static FileDB db;
static MarkdownRenderer markdownRenderer = TestHelper.getRenderer();

@BeforeAll
static void init() throws IOException {
var contentParser = new ContentParser();
fileSystem = new FileSystem(Path.of("hosts/test"), new DefaultEventBus(), (file) -> {
db = new FileDB(Path.of("hosts/test"), new DefaultEventBus(), (file) -> {
try {
return contentParser.parseMeta(file);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
fileSystem.init();
navigationFunction = new NavigationFunction(fileSystem, Path.of("hosts/test/content/nav/index.md"), new ContentParser(),
db.init();
navigationFunction = new NavigationFunction(db, Path.of("hosts/test/content/nav/index.md"), new ContentParser(),
markdownRenderer);
}

Expand Down Expand Up @@ -101,7 +102,7 @@ public void test_visibility() {
@Test
public void test_path() {

var sut = new NavigationFunction(fileSystem, Path.of("hosts/test/content/nav3/folder1/index.md"), new ContentParser(),
var sut = new NavigationFunction(db, Path.of("hosts/test/content/nav3/folder1/index.md"), new ContentParser(),
markdownRenderer);

List<NavNode> path = sut.path();
Expand Down
Loading

0 comments on commit 08f3569

Please sign in to comment.