Add queryable store with SQLite implementation

This adds the new "queryable store" API, that allows complex
queries and is backed by SQLite. This new API can be used
for entities annotated with the new QueryableType annotation.
This commit is contained in:
Rene Pfeuffer
2025-04-01 16:18:04 +02:00
parent d5362d634b
commit ada575d871
235 changed files with 10154 additions and 252 deletions

View File

@@ -0,0 +1,150 @@
/*
* Copyright (c) 2020 - present Cloudogu GmbH
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3.
*
* 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 Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package sonia.scm.store;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import sonia.scm.plugin.QueryableTypeDescriptor;
import sonia.scm.security.UUIDKeyGenerator;
import sonia.scm.store.sqlite.SQLiteQueryableStoreFactory;
import sonia.scm.util.IOUtil;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.reflect.Constructor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import static java.util.Arrays.stream;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Loads {@link QueryableTypes} into a JUnit test suite.
* <br/>
* This extension also includes support for {@link Nested} classes: {@link QueryableTypes} attached to a nested class
* are loaded before the types of its parent.
*/
public class QueryableStoreExtension implements ParameterResolver, BeforeEachCallback, AfterEachCallback {
private final ObjectMapper mapper = getObjectMapper();
private final Set<Class<?>> storeFactoryClasses = new HashSet<>();
private Path tempDirectory;
private Collection<QueryableTypeDescriptor> queryableTypeDescriptors;
private SQLiteQueryableStoreFactory storeFactory;
private static ObjectMapper getObjectMapper() {
return new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(new JavaTimeModule())
.configure(JsonParser.Feature.IGNORE_UNDEFINED, true)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
@Override
public void beforeEach(ExtensionContext context) throws IOException {
tempDirectory = Files.createTempDirectory("test");
String connectionString = "jdbc:sqlite:" + tempDirectory.toString() + "/test.db";
queryableTypeDescriptors = new ArrayList<>();
addDescriptors(context);
storeFactory = new SQLiteQueryableStoreFactory(
connectionString,
mapper,
new UUIDKeyGenerator(),
queryableTypeDescriptors
);
}
@Override
public void afterEach(ExtensionContext context) throws IOException {
IOUtil.delete(tempDirectory.toFile());
}
private void addDescriptors(ExtensionContext context) {
context.getTestClass().ifPresent(
testClass -> {
QueryableTypes annotation = testClass.getAnnotation(QueryableTypes.class);
if (annotation != null) {
queryableTypeDescriptors.addAll(stream(
annotation
.value()
).map(this::createDescriptor).toList());
}
}
);
context.getParent().ifPresent(this::addDescriptors);
}
private QueryableTypeDescriptor createDescriptor(Class<?> clazz) {
QueryableTypeDescriptor descriptor = mock(QueryableTypeDescriptor.class);
QueryableType queryableAnnotation = clazz.getAnnotation(QueryableType.class);
when(descriptor.getTypes()).thenReturn(stream(queryableAnnotation.value()).map(Class::getName).toArray(String[]::new));
lenient().when(descriptor.getClazz()).thenReturn(clazz.getName());
when(descriptor.getName()).thenReturn(queryableAnnotation.name());
try {
Class<?> storeFactoryClass = Class.forName(clazz.getName() + "StoreFactory");
storeFactoryClasses.add(storeFactoryClass);
} catch (ClassNotFoundException e) {
throw new RuntimeException("class for store factory not found", e);
}
return descriptor;
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Class<?> requestedParameterType = parameterContext.getParameter().getType();
return requestedParameterType.equals(QueryableStoreFactory.class)
|| storeFactoryClasses.contains(requestedParameterType);
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Class<?> requestedParameterType = parameterContext.getParameter().getType();
if (requestedParameterType.equals(QueryableStoreFactory.class)) {
return storeFactory;
} else if (storeFactoryClasses.contains(requestedParameterType)) {
try {
Constructor<?> constructor = requestedParameterType.getDeclaredConstructor(QueryableStoreFactory.class);
constructor.setAccessible(true);
return constructor.newInstance(storeFactory);
} catch (Exception e) {
throw new RuntimeException("failed to instantiate store factory", e);
}
} else {
throw new ParameterResolutionException("unsupported parameter type");
}
}
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
public @interface QueryableTypes {
Class<?>[] value();
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2020 - present Cloudogu GmbH
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3.
*
* 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 Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package sonia.scm.store;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(QueryableStoreExtension.class)
@QueryableStoreExtension.QueryableTypes(Spaceship.class)
class QueryableStoreExtensionTest {
@Test
void shouldProvideQueryableStoreFactory(QueryableStoreFactory storeFactory) {
QueryableMutableStore<Spaceship> store = storeFactory.getMutable(Spaceship.class);
store.put(new Spaceship("Heart Of Gold"));
assertEquals(1, store.getAll().size());
}
@Test
void shouldProvideTypeRelatedStoreFactory(SpaceshipStoreFactory storeFactory) {
QueryableMutableStore<Spaceship> store = storeFactory.getMutable();
store.put(new Spaceship("Heart Of Gold"));
assertEquals(1, store.getAll().size());
}
}
@QueryableType
class Spaceship {
String name;
Spaceship() {
}
Spaceship(String name) {
this.name = name;
}
}