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

Module: new GenericModule QoL class for class-based config deserializ… #15

Merged
merged 2 commits into from
May 5, 2021
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
59 changes: 59 additions & 0 deletions src/main/java/com/noleme/vault/parser/module/GenericModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.noleme.vault.parser.module;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.noleme.json.Json;
import com.noleme.json.JsonException;
import com.noleme.vault.container.definition.Definitions;
import com.noleme.vault.exception.VaultParserException;

/**
* @author Pierre LECERF (pierre@noleme.com)
* Created on 05/05/2021
*/
public class GenericModule<T> implements VaultModule
{
private final String identifier;
private final Class<T> type;
private final Processor<T> processor;

private static final ObjectMapper mapper = Json.newDefaultMapper()
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
;

/**
*
* @param identifier
* @param type
*/
public GenericModule(String identifier, Class<T> type, Processor<T> processor)
{
this.identifier = identifier;
this.type = type;
this.processor = processor;
}

@Override
public String identifier()
{
return this.identifier;
}

@Override
public void process(ObjectNode node, Definitions definitions) throws VaultParserException
{
try {
T config = Json.fromJson(mapper, node, this.type);
this.processor.process(config, definitions);
}
catch (JsonException e) {
throw new VaultParserException("The provided node could not be deserialized into an object of type "+this.type.getName(), e);
}
}

public interface Processor<T>
{
void process(T config, Definitions definitions) throws VaultParserException;
}
}
111 changes: 105 additions & 6 deletions src/test/java/com/noleme/vault/parser/ModuleTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.noleme.vault.parser;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.noleme.vault.container.Cellar;
import com.noleme.vault.container.definition.Definitions;
import com.noleme.vault.container.definition.ServiceProvider;
import com.noleme.vault.exception.VaultInjectionException;
import com.noleme.vault.factory.VaultFactory;
import com.noleme.vault.parser.module.CustomModule;
import com.noleme.vault.parser.module.GenericModule;
import com.noleme.vault.parser.module.VaultModule;
import com.noleme.vault.service.StringProvider;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
* @author Pierre Lecerf (plecerf@lumiomedical.com)
* Created on 2020/11/24
Expand All @@ -17,11 +24,103 @@ public class ModuleTest
@Test
void test() throws VaultInjectionException
{
VaultFactory.defaultParser.register(new CustomModule());
var container = new VaultFactory().populate(new Cellar(), "com/noleme/vault/parser/module/module.yml");
VaultParser parser = new VaultCompositeParser().register(new TestModule());
var cellar = new VaultFactory(parser).populate(new Cellar(), "com/noleme/vault/parser/module/module.yml");

makeAssertions(cellar);
}

@Test
void genericTest__shouldFail()
{
VaultParser parser = new VaultCompositeParser().register(new GenericModule<>("custom", NonMatchingTestConfig.class, (config, defs) -> {}));
Assertions.assertThrows(VaultInjectionException.class, () -> {
new VaultFactory(parser).populate(new Cellar(), "com/noleme/vault/parser/module/module.yml");
});
}

@Test
void genericTest() throws VaultInjectionException
{
VaultParser parser = new VaultCompositeParser().register(new GenericModule<>("custom", TestConfig.class, (config, defs) -> {
config.providers.forEach(id -> {
var def = new ServiceProvider(id, StringProvider.class.getName(), "build");
def.setMethodArgs(new Object[]{ config.value });

defs.getDefinitions().set(id, def);
});
}));
var cellar = new VaultFactory(parser).populate(new Cellar(), "com/noleme/vault/parser/module/module.yml");

makeAssertions(cellar);
}

@Test
void genericClassTest() throws VaultInjectionException
{
VaultParser parser = new VaultCompositeParser().register(new TypedTestModule());
var cellar = new VaultFactory(parser).populate(new Cellar(), "com/noleme/vault/parser/module/module.yml");

makeAssertions(cellar);
}

public static class TestModule implements VaultModule
{
@Override
public String identifier()
{
return "custom";
}

@Override
public void process(ObjectNode json, Definitions definitions)
{
String value = json.get("value").asText();

json.get("providers").elements().forEachRemaining(entry -> {
var id = entry.asText();
var def = new ServiceProvider(id, StringProvider.class.getName(), "build");
def.setMethodArgs(new Object[]{ value });

definitions.getDefinitions().set(id, def);
});
}
}

private static void makeAssertions(Cellar cellar)
{
Assertions.assertEquals("this_is_my_new_string", cellar.getService("my_provider.a", StringProvider.class).provide());
Assertions.assertEquals("this_is_my_new_string", cellar.getService("my_provider.b", StringProvider.class).provide());
Assertions.assertEquals("this_is_my_new_string", cellar.getService("my_provider.c", StringProvider.class).provide());
}

public static class TestConfig
{
public String value;
public List<String> providers = new ArrayList<>();
}

public static class NonMatchingTestConfig
{
public Long value;
public List<String> providers = new ArrayList<>();
}

public static class TypedTestModule extends GenericModule<TestConfig>
{
public TypedTestModule()
{
super("custom", TestConfig.class, TypedTestModule::process);
}

private static void process(TestConfig config, Definitions definitions)
{
config.providers.forEach(id -> {
var def = new ServiceProvider(id, StringProvider.class.getName(), "build");
def.setMethodArgs(new Object[]{ config.value });

Assertions.assertEquals("this_is_my_new_string", container.getService("my_provider.a", StringProvider.class).provide());
Assertions.assertEquals("this_is_my_new_string", container.getService("my_provider.b", StringProvider.class).provide());
Assertions.assertEquals("this_is_my_new_string", container.getService("my_provider.c", StringProvider.class).provide());
definitions.getDefinitions().set(id, def);
});
}
}
}
33 changes: 0 additions & 33 deletions src/test/java/com/noleme/vault/parser/module/CustomModule.java

This file was deleted.