Skip to content
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
4 changes: 4 additions & 0 deletions libraries/src/main/java/com/baeldung/flips/TestFlipsBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.baeldung.flips;

public class TestFlipsBean {
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<module>testing-modules/selenium-junit-testng</module>
<module>persistence-modules/solr</module>
<module>spark-java</module>
<module>spring-4</module>
<module>spring-5</module>
<module>spring-5-reactive</module>
<module>spring-5-mvc</module>
Expand Down
2 changes: 2 additions & 0 deletions spring-4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Relevant Articles:
- [Guide to Flips For Spring](http://www.baeldung.com/guide-to-flips-for-spring/)
62 changes: 62 additions & 0 deletions spring-4/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>spring-4</groupId>
<artifactId>spring-4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-4</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>

<dependency>
<groupId>com.github.feature-flip</groupId>
<artifactId>flips-web</artifactId>
<version>1.0.1</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!-- Check for the most recent available version: https://projectlombok.org/changelog.html -->
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
15 changes: 15 additions & 0 deletions spring-4/src/main/java/com/baeldung/flips/ApplicationConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.flips;

import org.flips.describe.config.FlipWebContextConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@Import(FlipWebContextConfiguration.class)
public class ApplicationConfig {

public static void main(String[] args) {
SpringApplication.run(ApplicationConfig.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.baeldung.flips.controller;

import com.baeldung.flips.model.Foo;
import com.baeldung.flips.service.FlipService;
import org.flips.annotation.FlipOnDateTime;
import org.flips.annotation.FlipOnDaysOfWeek;
import org.flips.annotation.FlipOnEnvironmentProperty;
import org.flips.annotation.FlipOnProfiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.DayOfWeek;
import java.util.List;

@RestController
public class FlipController {

private FlipService flipService;

@Autowired
public FlipController(FlipService flipService) {
this.flipService = flipService;
}

@RequestMapping(value = "/foos", method = RequestMethod.GET)
@FlipOnProfiles(activeProfiles = "dev")
public List<Foo> getAllFoos() {
return flipService.getAllFoos();
}

@RequestMapping(value = "/foo/{id}", method = RequestMethod.GET)
@FlipOnDaysOfWeek(daysOfWeek = {
DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY, DayOfWeek.THURSDAY,
DayOfWeek.FRIDAY, DayOfWeek.SATURDAY, DayOfWeek.SUNDAY
})
public Foo getFooByNewId(@PathVariable int id) {
return flipService.getFooById(id).orElse(new Foo("Not Found", -1));
}

@RequestMapping(value = "/foo/last", method = RequestMethod.GET)
@FlipOnDateTime(cutoffDateTimeProperty = "last.active.after")
public Foo getLastFoo() {
return flipService.getLastFoo();
}

@RequestMapping(value = "/foo/first", method = RequestMethod.GET)
@FlipOnDateTime(cutoffDateTimeProperty = "first.active.after")
public Foo getFirstFoo() {
return flipService.getLastFoo();
}

@RequestMapping(value = "/foos/{id}", method = RequestMethod.GET)
@FlipOnEnvironmentProperty(property = "feature.foo.by.id", expectedValue = "Y")
public Foo getFooById(@PathVariable int id) {
return flipService.getFooById(id).orElse(new Foo("Not Found", -1));
}

@RequestMapping(value = "/foo/new", method = RequestMethod.GET)
public Foo getNewThing() {
return flipService.getNewFoo();
}
}
11 changes: 11 additions & 0 deletions spring-4/src/main/java/com/baeldung/flips/model/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.flips.model;

import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor
public class Foo {
private final String name;
private final int id;
}
50 changes: 50 additions & 0 deletions spring-4/src/main/java/com/baeldung/flips/service/FlipService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.baeldung.flips.service;

import com.baeldung.flips.model.Foo;
import org.flips.annotation.FlipBean;
import org.flips.annotation.FlipOnSpringExpression;
import org.springframework.stereotype.Service;

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

@Service
public class FlipService {

private final List<Foo> foos;

public FlipService() {
foos = new ArrayList<>();
foos.add(new Foo("Foo1", 1));
foos.add(new Foo("Foo2", 2));
foos.add(new Foo("Foo3", 3));
foos.add(new Foo("Foo4", 4));
foos.add(new Foo("Foo5", 5));
foos.add(new Foo("Foo6", 6));

}

public List<Foo> getAllFoos() {
return foos;
}

public Optional<Foo> getFooById(int id) {
return foos.stream().filter(foo -> (foo.getId() == id)).findFirst();
}

@FlipBean(with = NewFlipService.class)
@FlipOnSpringExpression(expression = "(2 + 2) == 4")
public Foo getNewFoo() {
return new Foo("New Foo!", 99);
}

public Foo getLastFoo() {
return foos.get(foos.size() - 1);
}

public Foo getFirstFoo() {
return foos.get(0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.flips.service;

import com.baeldung.flips.model.Foo;
import org.springframework.stereotype.Service;

@Service
public class NewFlipService {

public Foo getNewFoo() {
return new Foo("Shiny New Foo!", 100);
}

}
5 changes: 5 additions & 0 deletions spring-4/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
feature.foo.by.id=Y
feature.new.foo=Y
last.active.after=2018-03-14T00:00:00Z
first.active.after=2999-03-15T00:00:00Z
logging.level.org.flips=info
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.baeldung.flips.controller;

import org.hamcrest.Matchers;
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.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest(properties = {
"feature.foo.by.id=Y",
"feature.new.foo=Y",
"last.active.after=2018-03-14T00:00:00Z",
"first.active.after=2999-03-15T00:00:00Z",
"logging.level.org.flips=info"

})
@AutoConfigureMockMvc
@ActiveProfiles("dev")
public class FlipControllerTest {

@Autowired private MockMvc mvc;

@Test
public void givenValidDayOfWeek_APIAvailable() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/foo/1"))
.andExpect(MockMvcResultMatchers.status().is(200))
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Foo1")))
.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(1)));
}

@Test
public void givenValidDate_APIAvailable() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/foo/last"))
.andExpect(MockMvcResultMatchers.status().is(200))
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Foo6")))
.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(6)));
}

@Test
public void givenInvalidDate_APINotAvailable() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/foo/first"))
.andExpect(MockMvcResultMatchers.status().is(501));
}

@Test
public void givenCorrectProfile_APIAvailable() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/foos"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(6)));
}

@Test
public void givenPropertySet_APIAvailable() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/foos/1"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Foo1")))
.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(1)));
}

@Test
public void getValidExpression_FlipBean() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/foo/new"))
.andExpect(MockMvcResultMatchers.status().is(200))
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Shiny New Foo!")))
.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(100)));
}
}