From 95da2cf19ebb513d67ae8e503a74042c767d9d1b Mon Sep 17 00:00:00 2001 From: Thorsten Marx Date: Sun, 3 Nov 2024 16:54:13 +0100 Subject: [PATCH] fix handling of null values --- .../condation/cms/filesystem/FileSystem.java | 10 ++ .../metadata/persistent/QueryHelper.java | 9 ++ .../filesystem/PresistentFileSystemTest.java | 2 - .../PersistentFileTaxonomiesTest.java | 115 ++++++++++++++++++ 4 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 cms-filesystem/src/test/java/com/condation/cms/filesystem/taxonomy/PersistentFileTaxonomiesTest.java diff --git a/cms-filesystem/src/main/java/com/condation/cms/filesystem/FileSystem.java b/cms-filesystem/src/main/java/com/condation/cms/filesystem/FileSystem.java index 5d7d8a4b..8861ff12 100644 --- a/cms-filesystem/src/main/java/com/condation/cms/filesystem/FileSystem.java +++ b/cms-filesystem/src/main/java/com/condation/cms/filesystem/FileSystem.java @@ -53,6 +53,8 @@ import java.util.Optional; import java.util.function.BiFunction; import java.util.function.Function; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.regex.Pattern; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -112,6 +114,14 @@ public void shutdown() { if (fileWatcher != null) { fileWatcher.stop(); } + if (metaData != null) { + try { + metaData.close(); + } catch (IOException ex) { + log.error("", ex); + throw new RuntimeException(ex); + } + } } @Override diff --git a/cms-filesystem/src/main/java/com/condation/cms/filesystem/metadata/persistent/QueryHelper.java b/cms-filesystem/src/main/java/com/condation/cms/filesystem/metadata/persistent/QueryHelper.java index b36e8e65..ba972901 100644 --- a/cms-filesystem/src/main/java/com/condation/cms/filesystem/metadata/persistent/QueryHelper.java +++ b/cms-filesystem/src/main/java/com/condation/cms/filesystem/metadata/persistent/QueryHelper.java @@ -49,6 +49,15 @@ public class QueryHelper { public static void exists (BooleanQuery.Builder queryBuilder, String field, Object value) { + + if (value == null) { + queryBuilder.add( + TermRangeQuery.newStringRange(field, null, null, true, true), + BooleanClause.Occur.FILTER); + + return; + } + if (value.getClass().isArray()) { value = ((Object[])value)[1]; } diff --git a/cms-filesystem/src/test/java/com/condation/cms/filesystem/PresistentFileSystemTest.java b/cms-filesystem/src/test/java/com/condation/cms/filesystem/PresistentFileSystemTest.java index f5931751..9982a846 100644 --- a/cms-filesystem/src/test/java/com/condation/cms/filesystem/PresistentFileSystemTest.java +++ b/cms-filesystem/src/test/java/com/condation/cms/filesystem/PresistentFileSystemTest.java @@ -22,8 +22,6 @@ * #L% */ -import com.condation.cms.filesystem.FileSystem; -import com.condation.cms.filesystem.MetaData; import com.condation.cms.api.eventbus.EventBus; import com.condation.cms.api.utils.FileUtils; import java.io.IOException; diff --git a/cms-filesystem/src/test/java/com/condation/cms/filesystem/taxonomy/PersistentFileTaxonomiesTest.java b/cms-filesystem/src/test/java/com/condation/cms/filesystem/taxonomy/PersistentFileTaxonomiesTest.java new file mode 100644 index 00000000..a4d3065e --- /dev/null +++ b/cms-filesystem/src/test/java/com/condation/cms/filesystem/taxonomy/PersistentFileTaxonomiesTest.java @@ -0,0 +1,115 @@ +package com.condation.cms.filesystem.taxonomy; + +/*- + * #%L + * cms-filesystem + * %% + * 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 + * . + * #L% + */ +import com.condation.cms.api.configuration.Configuration; +import com.condation.cms.api.configuration.configs.TaxonomyConfiguration; +import com.condation.cms.api.db.taxonomy.Taxonomy; +import com.condation.cms.api.eventbus.EventBus; +import com.condation.cms.api.utils.FileUtils; +import com.condation.cms.filesystem.FileSystem; +import com.condation.cms.filesystem.MetaData; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +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 PersistentFileTaxonomiesTest { + + static FileSystem fileSystem; + + static FileTaxonomies taxonomies; + + @BeforeAll + public static void setup() throws IOException { + var config = new Configuration(); + var tags = new Taxonomy("Tags", "tags", "taxonomy.tags"); + tags.setArray(true); + config.add(TaxonomyConfiguration.class, new TaxonomyConfiguration(new ConcurrentHashMap<>( + Map.of( + "kategorien", new Taxonomy("Kategorie", "kategorien", "taxonomy.category"), + "tags", tags + ) + ))); + + var eventBus = Mockito.mock(EventBus.class); + + fileSystem = new FileSystem(Path.of("src/test/resources"), eventBus, (file) -> { + try { + return new Yaml().load(Files.readString(file)); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + fileSystem.init(MetaData.Type.PERSISTENT); + + taxonomies = new FileTaxonomies(config, fileSystem); + } + + @AfterAll + public static void close() throws IOException { + fileSystem.shutdown(); + + if (Files.exists(Path.of("src/test/resources/data"))) { + FileUtils.deleteFolder(Path.of("src/test/resources/data")); + } + } + + @Test + public void test_slug() throws IOException { + Assertions.assertThat(taxonomies.forSlug("tags")).isPresent(); + Assertions.assertThat(taxonomies.forSlug("author")).isEmpty(); + } + + @Test + public void test_values() throws IOException { + var tags = taxonomies.forSlug("tags").get(); + var values = taxonomies.values(tags); + + Assertions.assertThat(values).containsExactlyInAnyOrder("eins", "zwei", "drei"); + +// Assertions.assertThat(tags.getValues()).containsOnlyKeys("eins", "zwei"); +// Assertions.assertThat(tags.getValues().get("eins").title).isEqualTo("Eins"); +// Assertions.assertThat(tags.getValues().get("zwei").title).isEqualTo("Zwei"); + } + + @Test + public void test_with_value() throws IOException { + var tags = taxonomies.forSlug("tags").get(); + taxonomies.withValue(tags, "eins"); + + taxonomies.withValue(tags, "drei"); + } + +}