Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix handling of null values #321

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #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");
}

}