Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
Update to use Environment properties instead of enums
Browse files Browse the repository at this point in the history
Spring Boot users will prefer this style of configuration.

(Requires changes in togglz, submitted as a PR)
  • Loading branch information
dsyer committed Jul 5, 2017
1 parent d3c7367 commit ff78e7d
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 122 deletions.
5 changes: 0 additions & 5 deletions spring-boot-hello-world/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@
<artifactId>togglz-console</artifactId>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-junit</artifactId>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.togglz.core.Feature;
import org.togglz.core.manager.FeatureManager;
import org.togglz.core.util.NamedFeature;

@RestController
public class HelloWorldController {

private static final String GREETING = "Greetings from Spring Boot!";

private final FeatureManager manager;

@RequestMapping("/")
public static final Feature HELLO_WORLD = new NamedFeature("HELLO_WORLD");

public static final Feature REVERSE_GREETING = new NamedFeature("REVERSE_GREETING");

public HelloWorldController(FeatureManager manager) {
this.manager = manager;
}

@RequestMapping("/")
public ResponseEntity<?> index() {
if (MyFeatures.HELLO_WORLD.isActive()) {
if (manager.isActive(HELLO_WORLD)) {
StringBuilder sb = new StringBuilder(GREETING);
if (MyFeatures.REVERSE_GREETING.isActive()) {
if (manager.isActive(REVERSE_GREETING)) {
sb.reverse();
}
return ResponseEntity.ok().body(sb.toString());
Expand Down
20 changes: 0 additions & 20 deletions spring-boot-hello-world/src/main/java/sample/MyFeatures.java

This file was deleted.

7 changes: 4 additions & 3 deletions spring-boot-hello-world/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ logging:
level:
org.togglz: DEBUG
togglz:
feature-enums: sample.MyFeatures
features:
HELLO_WORLD: true
REVERSE_GREETING: false
HELLO_WORLD:
enabled: true
REVERSE_GREETING:
enabled: false
console:
secured: false
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,42 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.togglz.junit.TogglzRule;
import org.togglz.core.repository.FeatureState;
import org.togglz.core.repository.StateRepository;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloWorldControllerIntegrationTests {

@Rule
public TogglzRule togglzRule = TogglzRule.allDisabled(MyFeatures.class);
@Autowired
private StateRepository state;

@Autowired
private MockMvc mockMvc;

@Test
public void testHelloWorldFeatureDisabled() throws Exception {
togglzRule.disable(MyFeatures.HELLO_WORLD);
state.setFeatureState(new FeatureState(HelloWorldController.HELLO_WORLD, false));
mockMvc.perform(get("")).andExpect(status().isNotFound());
}

@Test
public void testHelloWorldFeatureEnabled() throws Exception {
togglzRule.enable(MyFeatures.HELLO_WORLD);
mockMvc.perform(get("")).andExpect(status().isOk())
.andExpect(content().string("Greetings from Spring Boot!"));
}

@Test
public void testHelloWorldFeatureAndReverseGreetingEnabled() throws Exception {
togglzRule.enable(MyFeatures.HELLO_WORLD);
togglzRule.enable(MyFeatures.REVERSE_GREETING);
state.setFeatureState(new FeatureState(HelloWorldController.REVERSE_GREETING, true));
mockMvc.perform(get("")).andExpect(status().isOk())
.andExpect(content().string("!tooB gnirpS morf sgniteerG"));
}
Expand Down
5 changes: 0 additions & 5 deletions spring-boot-no-feature-provider-or-feature-enums/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@
<artifactId>togglz-console</artifactId>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-junit</artifactId>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class HelloWorldController {
private static final String GREETING = "Greetings from Spring Boot!";

@RequestMapping("/")
public ResponseEntity index() {
public ResponseEntity<?> index() {
return ResponseEntity.ok().body(GREETING);
}
}
5 changes: 0 additions & 5 deletions spring-boot-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@
<artifactId>togglz-console</artifactId>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-junit</artifactId>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-security</artifactId>
Expand Down
9 changes: 0 additions & 9 deletions spring-boot-security/src/main/java/sample/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.togglz.core.manager.EnumBasedFeatureProvider;
import org.togglz.core.spi.FeatureProvider;

@SpringBootApplication
public class Application {

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

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package sample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.togglz.core.Feature;
import org.togglz.core.manager.FeatureManager;
import org.togglz.core.util.NamedFeature;

@RestController
public class HelloWorldController {

private static final String GREETING = "Greetings from Spring Boot!";

private FeatureManager featureManager;
private final FeatureManager manager;

@Autowired
public HelloWorldController(FeatureManager featureManager) {
this.featureManager = featureManager;
}
public static final Feature HELLO_WORLD = new NamedFeature("HELLO_WORLD");

public static final Feature REVERSE_GREETING = new NamedFeature("REVERSE_GREETING");

public HelloWorldController(FeatureManager manager) {
this.manager = manager;
}

@RequestMapping("/")
public ResponseEntity<?> index() {
if (featureManager.isActive(MyFeatures.HELLO_WORLD)) {
if (manager.isActive(HELLO_WORLD)) {
StringBuilder sb = new StringBuilder(GREETING);
if (featureManager.isActive(MyFeatures.REVERSE_GREETING)) {
if (manager.isActive(REVERSE_GREETING)) {
sb.reverse();
}
return ResponseEntity.ok().body(sb.toString());
Expand Down
20 changes: 0 additions & 20 deletions spring-boot-security/src/main/java/sample/MyFeatures.java

This file was deleted.

9 changes: 8 additions & 1 deletion spring-boot-security/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ togglz:
console:
feature-admin-authority: ROLE_ADMIN
feature-manager-name: My Feature Manager
features-file: classpath:/features.properties
features:
HELLO_WORLD:
enabled: true
REVERSE_GREETING:
enabled: true
strategy: username
param:
users: user2,user3
cache:
enable: true
time-to-live: 10000
4 changes: 0 additions & 4 deletions spring-boot-security/src/main/resources/features.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
Expand Down
5 changes: 0 additions & 5 deletions spring-boot-thymeleaf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@
<artifactId>togglz-console</artifactId>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-junit</artifactId>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-boot-starter</artifactId>
Expand Down
20 changes: 0 additions & 20 deletions spring-boot-thymeleaf/src/main/java/sample/MyFeatures.java

This file was deleted.

7 changes: 4 additions & 3 deletions spring-boot-thymeleaf/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ logging:
level:
org.togglz: DEBUG
togglz:
feature-enums: sample.MyFeatures
features:
HELLO_WORLD: true
REVERSE_GREETING: false
HELLO_WORLD:
enabled: true
REVERSE_GREETING:
enabled: false
console:
secured: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package sample;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.togglz.core.Feature;
import org.togglz.core.repository.FeatureState;
import org.togglz.core.repository.StateRepository;
import org.togglz.core.util.NamedFeature;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloWorldControllerIntegrationTests {

private static Feature HELLO_WORLD = new NamedFeature("HELLO_WORLD");

private static Feature REVERSE_GREETING = new NamedFeature("REVERSE_GREETING");

@Autowired
private StateRepository state;

@Autowired
private MockMvc mockMvc;

@Test
public void testHelloWorldFeatureDisabled() throws Exception {
state.setFeatureState(new FeatureState(HELLO_WORLD, false));
mockMvc.perform(get("")).andExpect(status().isOk())
.andExpect(content().string(not(containsString("Greetings from Spring Boot!"))));
}

@Test
public void testHelloWorldFeatureEnabled() throws Exception {
mockMvc.perform(get("")).andExpect(status().isOk())
.andExpect(content().string(containsString("Greetings from Spring Boot!")));
}

@Test
public void testHelloWorldFeatureAndReverseGreetingEnabled() throws Exception {
state.setFeatureState(new FeatureState(REVERSE_GREETING, true));
mockMvc.perform(get("")).andExpect(status().isOk())
.andExpect(content().string(containsString("!tooB gnirpS morf sgniteerG")));
}
}

0 comments on commit ff78e7d

Please sign in to comment.