baronKarza
baronKarza

Reputation: 523

Togglz + SpringBoot: features always disabled

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.

Upvotes: 3

Views: 1645

Answers (1)

wasdueyesterday
wasdueyesterday

Reputation: 11

I'm experiencing similar problem as the FeatureOptions.MY_FEATURE_1.isActive() always returns false when I expect it to be true.

So my setup is pretty much following the quickstart guide and I made some minor adjustment. Thanks to the help of an dzone article, I got it to work. It appears that what's missing the inject of the feature provider into the spring beans. This means if I have a SpringConfiguration.java, I need to add this code:

@Bean
@SuppressWarnings("unchecked")
public FeatureProvider featureProvider() {
    return new EnumBasedFeatureProvider(FeatureOptions.class);
}

After adding this, the code for checking the flag isActive() now returns as expected (true)

I noticed that the post was from 2018 so my answer doesn't help much. It's perhaps better to ask Togglz site to add this to their guide.

Note that if you rely on auto config provided by the starter, then it appears that you can skip making those config, and skip registering the FeatureProvider bean. See the official guide here.

Cheers

Upvotes: 1

Related Questions