Skip to content

Commit

Permalink
Support contextprovider as a method parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
rameshmalla committed Jan 8, 2024
1 parent 5d47c2f commit 4c224a8
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import org.zalando.baigan.repository.ConfigurationRepository;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Suppliers.memoize;
Expand Down Expand Up @@ -57,7 +60,13 @@ public void setBeanFactory(final BeanFactory beanFactory) throws BeansException
@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) {
final String key = createKey(getClass(proxy), method);
final Object result = getConfig(key);

final List<ContextProvider> contextProviders = Arrays.stream(args)
.filter(ContextProvider.class::isInstance)
.map(ContextProvider.class::cast)
.collect(Collectors.toList());

final Object result = getConfig(key,contextProviders);
if (result == null) {
LOG.warn("Configuration not found for key: {}", key);
return null;
Expand All @@ -76,7 +85,7 @@ private Class<?> getClass(final Object proxy) {
return interfaces[0];
}

private Object getConfig(final String key) {
private Object getConfig(final String key, final List<ContextProvider> contextProviders) {

final Optional<Configuration> optional = configurationRepository.get().get(key);
if (!optional.isPresent()) {
Expand All @@ -95,6 +104,11 @@ private Object getConfig(final String key) {
context.put(param, provider.getContextParam(param));
}

contextProviders.forEach(provider ->
provider.getProvidedContexts()
.forEach(param -> context.put(param, provider.getContextParam(param)))
);

return conditionsProcessor.get().process(optional.get(), context);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.zalando.baigan.e2e.configs;

import org.zalando.baigan.annotation.BaiganConfig;
import org.zalando.baigan.e2e.filerepo.CustomContextProvider;

import java.util.List;
import java.util.Map;
Expand All @@ -11,6 +12,7 @@ public interface SomeConfiguration {
SomeConfigObject someConfig();
String someValue();
Boolean isThisTrue();
Boolean toggleFlag(CustomContextProvider customContextProvider);
Map<UUID, List<SomeConfigObject>> topLevelGenerics();
List<String> configList();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.zalando.baigan.e2e.filerepo;

import org.jetbrains.annotations.NotNull;
import org.zalando.baigan.context.ContextProvider;

import java.util.Set;

public class CustomContextProvider implements ContextProvider {

private final Set<String> PARAMS = Set.of("appdomain");

private final String appDomain;

public CustomContextProvider(String appDomain) {
this.appDomain = appDomain;
}

@Override
public String getContextParam(@NotNull final String name) {
return appDomain;
}

@Override
public Set<String> getProvidedContexts() {
return PARAMS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.zalando.baigan.e2e.filerepo;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.zalando.baigan.BaiganSpringContext;
import org.zalando.baigan.annotation.ConfigurationServiceScan;
import org.zalando.baigan.e2e.configs.SomeConfiguration;
import org.zalando.baigan.repository.FileSystemConfigurationRepository;
import org.zalando.baigan.repository.RepositoryFactory;

import java.time.Duration;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {FileSystemConfigurationContextProviderEnd2EndTest.RepoConfig.class})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class FileSystemConfigurationContextProviderEnd2EndTest {

@Autowired
private SomeConfiguration someConfiguration;

private static final Duration CONFIG_REFRESH_INTERVAL = Duration.ofMillis(100);

@Test
public void testConfigurationsWithContext() {
assertThat(someConfiguration.toggleFlag(new CustomContextProvider("1")), equalTo(true));
assertThat(someConfiguration.toggleFlag(new CustomContextProvider("2")), equalTo(false));
assertThat(someConfiguration.toggleFlag(null), equalTo(false));
}

@ConfigurationServiceScan(basePackageClasses = SomeConfiguration.class)
@Testcontainers
@ComponentScan(basePackageClasses = {BaiganSpringContext.class})
static class RepoConfig {

@Bean
FileSystemConfigurationRepository configurationRepository(RepositoryFactory repositoryFactory) {
return repositoryFactory.fileSystemConfigurationRepository()
.fileName(FileSystemConfigurationContextProviderEnd2EndTest.class.getClassLoader().getResource("test-config.json").getPath())
.refreshInterval(CONFIG_REFRESH_INTERVAL)
.objectMapper(new ObjectMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false))
.build();
}
}
}
52 changes: 52 additions & 0 deletions src/test/resources/test-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"alias": "some.non.existing.config",
"defaultValue": "an irrelevant value"
},
{
"alias": "some.configuration.is.this.true",
"defaultValue": false,
"conditions": [
{
"value": true,
"conditionType": {
"onValue": "1",
"type": "Equals"
},
"paramName": "appdomain"
}
]
},
{
"alias": "some.configuration.some.value",
"defaultValue": "some value"
},
{
"alias": "some.configuration.some.config",
"defaultValue": {
"config_key": "a value",
"extra_field": "objectMapper configured to not fail for unknown properties"
}
},
{
"alias": "some.configuration.toggle.flag",
"defaultValue": false,
"conditions": [
{
"value": true,
"conditionType": {
"onValue": "1",
"type": "Equals"
},
"paramName": "appdomain"
}
]
},
{
"alias": "some.configuration.config.list",
"defaultValue": [
"A",
"B"
]
}
]

0 comments on commit 4c224a8

Please sign in to comment.