Skip to content

Commit 2b50c4c

Browse files
egoebelbeckermaibin
authored andcommitted
BAEL-1554 - Guide to Flips (eugenp#3827)
* BAEL-1554 - Flips code * BAEL-1554 - Flips code, round 2 * BAEL-1554 - Guide to Flips * BAEL-1554 - update read me * BAEL-1554 - rename "Thing" to "Foo"
1 parent d9e4ec2 commit 2b50c4c

11 files changed

Lines changed: 305 additions & 0 deletions

File tree

‎flips/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Relevant Articles:
2+
- [Guide to Flips For Spring](http://www.baeldung.com/guide-to-flips-for-spring/)

‎flips/pom.xml‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>flips</groupId>
6+
<artifactId>flips</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>flips</name>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
</properties>
17+
18+
<dependencies>
19+
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
<version>1.5.10.RELEASE</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-test</artifactId>
29+
<version>1.5.9.RELEASE</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.github.feature-flip</groupId>
34+
<artifactId>flips-web</artifactId>
35+
<version>1.0.1</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<!-- Check for the most recent available version: https://projectlombok.org/changelog.html -->
42+
<version>1.16.18</version>
43+
<scope>provided</scope>
44+
</dependency>
45+
46+
47+
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
<executions>
56+
<execution>
57+
<goals>
58+
<goal>repackage</goal>
59+
</goals>
60+
</execution>
61+
</executions>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.flips;
2+
3+
import org.flips.describe.config.FlipWebContextConfiguration;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Import;
7+
8+
@SpringBootApplication
9+
@Import(FlipWebContextConfiguration.class)
10+
public class ApplicationConfig {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(ApplicationConfig.class, args);
14+
}
15+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.flips.controller;
2+
3+
import com.baeldung.flips.model.Foo;
4+
import com.baeldung.flips.service.FlipService;
5+
import org.flips.annotation.FlipOnDateTime;
6+
import org.flips.annotation.FlipOnDaysOfWeek;
7+
import org.flips.annotation.FlipOnEnvironmentProperty;
8+
import org.flips.annotation.FlipOnProfiles;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
import java.time.DayOfWeek;
16+
import java.util.List;
17+
18+
@RestController
19+
public class FlipController {
20+
21+
private FlipService flipService;
22+
23+
@Autowired
24+
public FlipController(FlipService flipService) {
25+
this.flipService = flipService;
26+
}
27+
28+
@RequestMapping(value = "/foos", method = RequestMethod.GET)
29+
@FlipOnProfiles(activeProfiles = "dev")
30+
public List<Foo> getAllFoos() {
31+
return flipService.getAllFoos();
32+
}
33+
34+
@RequestMapping(value = "/foo/{id}", method = RequestMethod.GET)
35+
@FlipOnDaysOfWeek(daysOfWeek = {
36+
DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY, DayOfWeek.THURSDAY,
37+
DayOfWeek.FRIDAY, DayOfWeek.SATURDAY, DayOfWeek.SUNDAY
38+
})
39+
public Foo getFooByNewId(@PathVariable int id) {
40+
return flipService.getFooById(id).orElse(new Foo("Not Found", -1));
41+
}
42+
43+
@RequestMapping(value = "/foo/last", method = RequestMethod.GET)
44+
@FlipOnDateTime(cutoffDateTimeProperty = "last.active.after")
45+
public Foo getLastFoo() {
46+
return flipService.getLastFoo();
47+
}
48+
49+
@RequestMapping(value = "/foo/first", method = RequestMethod.GET)
50+
@FlipOnDateTime(cutoffDateTimeProperty = "first.active.after")
51+
public Foo getFirstFoo() {
52+
return flipService.getLastFoo();
53+
}
54+
55+
@RequestMapping(value = "/foos/{id}", method = RequestMethod.GET)
56+
@FlipOnEnvironmentProperty(property = "feature.foo.by.id", expectedValue = "Y")
57+
public Foo getFooById(@PathVariable int id) {
58+
return flipService.getFooById(id).orElse(new Foo("Not Found", -1));
59+
}
60+
61+
@RequestMapping(value = "/foo/new", method = RequestMethod.GET)
62+
public Foo getNewThing() {
63+
return flipService.getNewFoo();
64+
}
65+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.flips.model;
2+
3+
import lombok.Data;
4+
import lombok.RequiredArgsConstructor;
5+
6+
@Data
7+
@RequiredArgsConstructor
8+
public class Foo {
9+
private final String name;
10+
private final int id;
11+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.flips.service;
2+
3+
import com.baeldung.flips.model.Foo;
4+
import org.flips.annotation.FlipBean;
5+
import org.flips.annotation.FlipOnSpringExpression;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Optional;
11+
12+
@Service
13+
public class FlipService {
14+
15+
private final List<Foo> foos;
16+
17+
public FlipService() {
18+
foos = new ArrayList<>();
19+
foos.add(new Foo("Foo1", 1));
20+
foos.add(new Foo("Foo2", 2));
21+
foos.add(new Foo("Foo3", 3));
22+
foos.add(new Foo("Foo4", 4));
23+
foos.add(new Foo("Foo5", 5));
24+
foos.add(new Foo("Foo6", 6));
25+
26+
}
27+
28+
public List<Foo> getAllFoos() {
29+
return foos;
30+
}
31+
32+
public Optional<Foo> getFooById(int id) {
33+
return foos.stream().filter(foo -> (foo.getId() == id)).findFirst();
34+
}
35+
36+
@FlipBean(with = NewFlipService.class)
37+
@FlipOnSpringExpression(expression = "(2 + 2) == 4")
38+
public Foo getNewFoo() {
39+
return new Foo("New Foo!", 99);
40+
}
41+
42+
public Foo getLastFoo() {
43+
return foos.get(foos.size() - 1);
44+
}
45+
46+
public Foo getFirstFoo() {
47+
return foos.get(0);
48+
}
49+
50+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.flips.service;
2+
3+
import com.baeldung.flips.model.Foo;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class NewFlipService {
8+
9+
public Foo getNewFoo() {
10+
return new Foo("Shiny New Foo!", 100);
11+
}
12+
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
feature.foo.by.id=Y
2+
feature.new.foo=Y
3+
last.active.after=2018-03-14T00:00:00Z
4+
first.active.after=2999-03-15T00:00:00Z
5+
logging.level.org.flips=info
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.flips.controller;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.ActiveProfiles;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
13+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
14+
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest(properties = {
17+
"feature.foo.by.id=Y",
18+
"feature.new.foo=Y",
19+
"last.active.after=2018-03-14T00:00:00Z",
20+
"first.active.after=2999-03-15T00:00:00Z",
21+
"logging.level.org.flips=info"
22+
23+
})
24+
@AutoConfigureMockMvc
25+
@ActiveProfiles("dev")
26+
public class FlipControllerTest {
27+
28+
@Autowired private MockMvc mvc;
29+
30+
@Test
31+
public void givenValidDayOfWeek_APIAvailable() throws Exception {
32+
mvc.perform(MockMvcRequestBuilders.get("/foo/1"))
33+
.andExpect(MockMvcResultMatchers.status().is(200))
34+
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Foo1")))
35+
.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(1)));
36+
}
37+
38+
@Test
39+
public void givenValidDate_APIAvailable() throws Exception {
40+
mvc.perform(MockMvcRequestBuilders.get("/foo/last"))
41+
.andExpect(MockMvcResultMatchers.status().is(200))
42+
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Foo6")))
43+
.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(6)));
44+
}
45+
46+
@Test
47+
public void givenInvalidDate_APINotAvailable() throws Exception {
48+
mvc.perform(MockMvcRequestBuilders.get("/foo/first"))
49+
.andExpect(MockMvcResultMatchers.status().is(501));
50+
}
51+
52+
@Test
53+
public void givenCorrectProfile_APIAvailable() throws Exception {
54+
mvc.perform(MockMvcRequestBuilders.get("/foos"))
55+
.andExpect(MockMvcResultMatchers.status().isOk())
56+
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(6)));
57+
}
58+
59+
@Test
60+
public void givenPropertySet_APIAvailable() throws Exception {
61+
mvc.perform(MockMvcRequestBuilders.get("/foos/1"))
62+
.andExpect(MockMvcResultMatchers.status().isOk())
63+
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Foo1")))
64+
.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(1)));
65+
}
66+
67+
@Test
68+
public void getValidExpression_FlipBean() throws Exception {
69+
mvc.perform(MockMvcRequestBuilders.get("/foo/new"))
70+
.andExpect(MockMvcResultMatchers.status().is(200))
71+
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Shiny New Foo!")))
72+
.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(100)));
73+
}
74+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.flips;
2+
3+
public class TestFlipsBean {
4+
}

0 commit comments

Comments
 (0)