I'm trying to integrate Togglz in a Spring Boot web application.
Since I didn't succeed with Togglz autoconfiguration (no FeatureManager
bean was created and, consequently, the ApplicationContext
was not created), I defined Togglz beans:
@Configuration
@EnableAutoConfiguration
public class TooglzAppCtxtConfig {
@Bean
public StateRepository stateRepository() throws IOException {
// Retrieve the configuration directory as Spring Resource...
Resource confDir = Application.getConfDir();
Resource applicationProperties = confDir
.createRelative("features.properties");
return new FileBasedStateRepository(applicationProperties.getFile());
}
@Bean
public UserProvider userProvider() {
return new NoOpUserProvider();
}
@Bean
public FeatureManager manager() throws IOException {
return new FeatureManagerBuilder()
.featureEnum(MyEnumFeatures.class)
.stateRepository(this.stateRepository())
.userProvider(this.userProvider()).build();
}
where MyEnumFeatures
enum is:
public enum MyEnumFeatures implements Feature {
@Label("Authorization Key")
AUTHORIZATION_KEY;
public boolean isActive() {
return FeatureContext.getFeatureManager().isActive(this);
}
}
My pom.xml
contains:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-console</artifactId>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-testing</artifactId>
<version>2.5.0.Final</version>
<scope>test</scope>
</dependency>
The file features.properties
(located in my configuration directory) contains the following line (the syntax has been taken from here):
AUTHORIZATION_KEY=true
The problem is that when I start a test, the feature is always disabled. By debugging, I discovered that the application loads a feature.properties
file under target/test-classes/conf/features.properties
containing:
#Fri Feb 16 14:01:15 CET 2018
AUTHORIZATION_KEY=false
that seems to be automatically generated. So, the feature is always disabled. The file is regenerated with the feature set to false
before the execution of each test case.
Moreover, I tried to modify my tests introducing the foolowing @Rule
:
@Rule
public TogglzRule togglzRule = TogglzRule.allEnabled(MyFeatures.class);
and enabling/disabling the feature at the beginning of each test case:
@Test
public void isavail_fileExists_Y() throws Exception {
togglzRule.enable(MyFeatures.AUTHORIZATION_KEY);
this.mockMvc.perform(get("/isavail?f=QCNjZWkvVGVzdC5wZGYjQA"))
.andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Y")));
}
Also this way, the feature is always disabled.
Where am I wrong? I need help.
I would like to have an explanation of which beans are involved in the process and how to configure them. The examples that I found here work, but it's not clear why: SpringBoot automagically configures something and I'm not able to understand where is the problem.
Thanks in advance.