From fff107f76fdf1a47394d3e5571bd00fdd6f3e650 Mon Sep 17 00:00:00 2001 From: ericgoebelbecker <@592Gbetz> Date: Tue, 13 Mar 2018 17:35:38 -0400 Subject: [PATCH 1/6] BAEL-1554 - Flips code --- .../com/baeldung/flips/ApplicationConfig.java | 15 ++++++ .../flips/controller/FlipController.java | 45 +++++++++++++++++ .../flips/controller/NewFlipController.java | 24 +++++++++ .../java/com/baeldung/flips/model/Thing.java | 14 ++++++ .../baeldung/flips/service/FlipService.java | 43 ++++++++++++++++ .../src/main/resources/application.properties | 3 ++ .../flips/controller/FlipControllerTest.java | 49 +++++++++++++++++++ 7 files changed, 193 insertions(+) create mode 100644 flips/src/main/java/com/baeldung/flips/ApplicationConfig.java create mode 100644 flips/src/main/java/com/baeldung/flips/controller/FlipController.java create mode 100644 flips/src/main/java/com/baeldung/flips/controller/NewFlipController.java create mode 100644 flips/src/main/java/com/baeldung/flips/model/Thing.java create mode 100644 flips/src/main/java/com/baeldung/flips/service/FlipService.java create mode 100644 flips/src/main/resources/application.properties create mode 100644 flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java diff --git a/flips/src/main/java/com/baeldung/flips/ApplicationConfig.java b/flips/src/main/java/com/baeldung/flips/ApplicationConfig.java new file mode 100644 index 000000000000..7001aeb99158 --- /dev/null +++ b/flips/src/main/java/com/baeldung/flips/ApplicationConfig.java @@ -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); + } +} \ No newline at end of file diff --git a/flips/src/main/java/com/baeldung/flips/controller/FlipController.java b/flips/src/main/java/com/baeldung/flips/controller/FlipController.java new file mode 100644 index 000000000000..f753bbfc420d --- /dev/null +++ b/flips/src/main/java/com/baeldung/flips/controller/FlipController.java @@ -0,0 +1,45 @@ +package com.baeldung.flips.controller; + +import com.baeldung.flips.model.Thing; +import com.baeldung.flips.service.FlipService; +import org.flips.annotation.FlipBean; +import org.flips.annotation.FlipOnEnvironmentProperty; +import org.flips.annotation.FlipOnProfiles; +import org.flips.annotation.FlipOnSpringExpression; +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.util.List; + +@RestController +public class FlipController { + + private FlipService flipService; + + @Autowired + public FlipController(FlipService flipService) { + this.flipService = flipService; + } + + @RequestMapping(value = "/things", method = RequestMethod.GET) + @FlipOnProfiles(activeProfiles = "dev") + public List getAllThings(){ + return flipService.getAllThings(); + } + + @RequestMapping(value = "/things/{id}", method = RequestMethod.GET) + @FlipOnEnvironmentProperty(property = "feature.thing.by.id", expectedValue = "Y") + public Thing getThingById(@PathVariable int id){ + return flipService.getThingById(id).orElse(new Thing("Not Found", -1)); + } + + @FlipBean(with = NewFlipController.class) + @FlipOnSpringExpression(expression = "@environment.getProperty('feature.new.thing') == 'Y'") + @RequestMapping(value = "/thing/new", method = RequestMethod.GET) + public Thing getNewThing(){ + return new Thing("Not Found", -1); + } +} \ No newline at end of file diff --git a/flips/src/main/java/com/baeldung/flips/controller/NewFlipController.java b/flips/src/main/java/com/baeldung/flips/controller/NewFlipController.java new file mode 100644 index 000000000000..a4fdc16ed4df --- /dev/null +++ b/flips/src/main/java/com/baeldung/flips/controller/NewFlipController.java @@ -0,0 +1,24 @@ +package com.baeldung.flips.controller; + +import com.baeldung.flips.model.Thing; +import com.baeldung.flips.service.FlipService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class NewFlipController { + + private FlipService flipService; + + @Autowired + public NewFlipController(FlipService flipService) { + this.flipService = flipService; + } + + @RequestMapping(value = "/alt/thing/new", method = RequestMethod.GET) + public Thing getNewThing(){ + return flipService.getNewThing(); + } +} \ No newline at end of file diff --git a/flips/src/main/java/com/baeldung/flips/model/Thing.java b/flips/src/main/java/com/baeldung/flips/model/Thing.java new file mode 100644 index 000000000000..4c7b8f3b2eb7 --- /dev/null +++ b/flips/src/main/java/com/baeldung/flips/model/Thing.java @@ -0,0 +1,14 @@ +package com.baeldung.flips.model; + + +import lombok.Data; +import lombok.RequiredArgsConstructor; + +@Data +@RequiredArgsConstructor +public class Thing { + + private final String name; + private final int id; + +} diff --git a/flips/src/main/java/com/baeldung/flips/service/FlipService.java b/flips/src/main/java/com/baeldung/flips/service/FlipService.java new file mode 100644 index 000000000000..d8c794be5b84 --- /dev/null +++ b/flips/src/main/java/com/baeldung/flips/service/FlipService.java @@ -0,0 +1,43 @@ +package com.baeldung.flips.service; + +import com.baeldung.flips.model.Thing; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Service +public class FlipService { + + + private final List things; + + public FlipService() { + + things = new ArrayList<>(); + + things.add(new Thing("Thing1", 1)); + things.add(new Thing("Thing2", 2)); + things.add(new Thing("Thing3", 3)); + things.add(new Thing("Thing4", 4)); + things.add(new Thing("Thing5", 5)); + things.add(new Thing("Thing6", 6)); + + } + + + + public List getAllThings() { + return things; + } + + public Optional getThingById(int id) { + return things.stream().filter(thing -> (thing.getId() == id)).findFirst(); + } + + public Thing getNewThing() { + return new Thing("New Thing!", 99); + } + +} \ No newline at end of file diff --git a/flips/src/main/resources/application.properties b/flips/src/main/resources/application.properties new file mode 100644 index 000000000000..977a71b96442 --- /dev/null +++ b/flips/src/main/resources/application.properties @@ -0,0 +1,3 @@ +feature.thing.by.id=Y +feature.new.thing=Y +logging.level.org.flips=info \ No newline at end of file diff --git a/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java b/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java new file mode 100644 index 000000000000..63b0376c39ec --- /dev/null +++ b/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java @@ -0,0 +1,49 @@ +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.flips.by.id=Y", + "feature.movie.statistics=Y" + }) +@AutoConfigureMockMvc +@ActiveProfiles("dev") +public class FlipControllerTest { + + @Autowired + private MockMvc mvc; + + @Test + public void shouldLoadAllThings() throws Exception{ + mvc.perform(MockMvcRequestBuilders.get("/things")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(6))); + } + + @Test + public void shouldGetThingById() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/things/1")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Thing1"))) + .andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(1))); + } + + @Test + public void getNewThing() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/thing/new")) + .andExpect(MockMvcResultMatchers.status().is(200)) + .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("New Thing!"))) + .andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(99))); + } +} \ No newline at end of file From 807fd5cadcb9e8790e88a4446c55b18d8f61aa8b Mon Sep 17 00:00:00 2001 From: ericgoebelbecker <@592Gbetz> Date: Wed, 14 Mar 2018 17:09:21 -0400 Subject: [PATCH 2/6] BAEL-1554 - Flips code, round 2 --- flips/README.md | 2 + flips/pom.xml | 65 +++++++++++++++++++ .../flips/controller/FlipController.java | 28 ++++++-- .../java/com/baeldung/flips/model/Thing.java | 2 - .../baeldung/flips/service/FlipService.java | 8 +++ .../src/main/resources/application.properties | 2 + .../flips/controller/FlipControllerTest.java | 26 +++++++- 7 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 flips/README.md create mode 100644 flips/pom.xml diff --git a/flips/README.md b/flips/README.md new file mode 100644 index 000000000000..071781ba8da9 --- /dev/null +++ b/flips/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Guide to Flips Library](http://www.baeldung.com/guide-to-flips-library) diff --git a/flips/pom.xml b/flips/pom.xml new file mode 100644 index 000000000000..e6f2ee1aa0a5 --- /dev/null +++ b/flips/pom.xml @@ -0,0 +1,65 @@ + + 4.0.0 + + flips + flips + 0.0.1-SNAPSHOT + jar + + flips + + + UTF-8 + 1.8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-web + 1.5.9.RELEASE + + + + org.springframework.boot + spring-boot-starter-test + 1.5.9.RELEASE + + + + com.github.feature-flip + flips-web + 1.0.1 + + + + org.projectlombok + lombok + + 1.16.18 + provided + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + diff --git a/flips/src/main/java/com/baeldung/flips/controller/FlipController.java b/flips/src/main/java/com/baeldung/flips/controller/FlipController.java index f753bbfc420d..6bc79b578abf 100644 --- a/flips/src/main/java/com/baeldung/flips/controller/FlipController.java +++ b/flips/src/main/java/com/baeldung/flips/controller/FlipController.java @@ -2,16 +2,14 @@ import com.baeldung.flips.model.Thing; import com.baeldung.flips.service.FlipService; -import org.flips.annotation.FlipBean; -import org.flips.annotation.FlipOnEnvironmentProperty; -import org.flips.annotation.FlipOnProfiles; -import org.flips.annotation.FlipOnSpringExpression; +import org.flips.annotation.*; 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 @@ -30,12 +28,34 @@ public List getAllThings(){ return flipService.getAllThings(); } + @RequestMapping(value = "/thing/{id}", method = RequestMethod.GET) + @FlipOnDaysOfWeek(daysOfWeek={DayOfWeek.MONDAY, DayOfWeek.WEDNESDAY}) + public Thing getThingByNewId(@PathVariable int id){ + return flipService.getThingById(id).orElse(new Thing("Not Found", -1)); + } + + + @RequestMapping(value = "/thing/last", method = RequestMethod.GET) + @FlipOnDateTime(cutoffDateTimeProperty = "last.active.after") + public Thing getLastThing(){ + return flipService.getLastThing(); + } + + @RequestMapping(value = "/thing/first", method = RequestMethod.GET) + @FlipOnDateTime(cutoffDateTimeProperty = "first.active.after") + public Thing getFirstThing(){ + return flipService.getLastThing(); + } + + @RequestMapping(value = "/things/{id}", method = RequestMethod.GET) @FlipOnEnvironmentProperty(property = "feature.thing.by.id", expectedValue = "Y") public Thing getThingById(@PathVariable int id){ return flipService.getThingById(id).orElse(new Thing("Not Found", -1)); } + + @FlipBean(with = NewFlipController.class) @FlipOnSpringExpression(expression = "@environment.getProperty('feature.new.thing') == 'Y'") @RequestMapping(value = "/thing/new", method = RequestMethod.GET) diff --git a/flips/src/main/java/com/baeldung/flips/model/Thing.java b/flips/src/main/java/com/baeldung/flips/model/Thing.java index 4c7b8f3b2eb7..725adda9d53a 100644 --- a/flips/src/main/java/com/baeldung/flips/model/Thing.java +++ b/flips/src/main/java/com/baeldung/flips/model/Thing.java @@ -7,8 +7,6 @@ @Data @RequiredArgsConstructor public class Thing { - private final String name; private final int id; - } diff --git a/flips/src/main/java/com/baeldung/flips/service/FlipService.java b/flips/src/main/java/com/baeldung/flips/service/FlipService.java index d8c794be5b84..f7392bedb6de 100644 --- a/flips/src/main/java/com/baeldung/flips/service/FlipService.java +++ b/flips/src/main/java/com/baeldung/flips/service/FlipService.java @@ -40,4 +40,12 @@ public Thing getNewThing() { return new Thing("New Thing!", 99); } + public Thing getLastThing() { + return things.get(things.size() -1); + } + + public Thing getFirstThing() { + return things.get(0); + } + } \ No newline at end of file diff --git a/flips/src/main/resources/application.properties b/flips/src/main/resources/application.properties index 977a71b96442..166a85bfe2d1 100644 --- a/flips/src/main/resources/application.properties +++ b/flips/src/main/resources/application.properties @@ -1,3 +1,5 @@ feature.thing.by.id=Y feature.new.thing=Y +last.active.after=2018-03-14T00:00:00Z +first.active.after=2999-03-15T00:00:00Z logging.level.org.flips=info \ No newline at end of file diff --git a/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java b/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java index 63b0376c39ec..a353b843b350 100644 --- a/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java +++ b/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java @@ -24,6 +24,30 @@ public class FlipControllerTest { @Autowired private MockMvc mvc; + + // @Test - comment this out after insuring that its the proper day of the week in FlipController! + public void givenValidDayOfWeek_APIAvailable() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/thing/1")) + .andExpect(MockMvcResultMatchers.status().is(200)) + .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Thing1"))) + .andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(1))); + } + + @Test + public void givenValidDate_APIAvailable() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/thing/last")) + .andExpect(MockMvcResultMatchers.status().is(200)) + .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Thing6"))) + .andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(6))); + } + + @Test + public void givenInvalidDate_APINotAvailable() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/thing/first")) + .andExpect(MockMvcResultMatchers.status().is(501)); + } + + @Test public void shouldLoadAllThings() throws Exception{ mvc.perform(MockMvcRequestBuilders.get("/things")) @@ -32,7 +56,7 @@ public void shouldLoadAllThings() throws Exception{ } @Test - public void shouldGetThingById() throws Exception { + public void givenPropertySet_APIAvailable() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/things/1")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Thing1"))) From 595f443e2e0fcb4d424ac94bb63cc4c3bd85f971 Mon Sep 17 00:00:00 2001 From: Eric Goebelbecker Date: Thu, 15 Mar 2018 20:33:22 -0400 Subject: [PATCH 3/6] BAEL-1554 - Guide to Flips --- flips/pom.xml | 2 +- .../flips/controller/FlipController.java | 30 +++++++------- .../flips/controller/NewFlipController.java | 24 ------------ .../java/com/baeldung/flips/model/Thing.java | 1 - .../baeldung/flips/service/FlipService.java | 15 ++++--- .../flips/service/NewFlipService.java | 17 ++++++++ .../flips/controller/FlipControllerTest.java | 39 ++++++++++--------- .../com/baeldung/flips/TestFlipsBean.java | 4 ++ pom.xml | 1 + 9 files changed, 65 insertions(+), 68 deletions(-) delete mode 100644 flips/src/main/java/com/baeldung/flips/controller/NewFlipController.java create mode 100644 flips/src/main/java/com/baeldung/flips/service/NewFlipService.java create mode 100644 libraries/src/main/java/com/baeldung/flips/TestFlipsBean.java diff --git a/flips/pom.xml b/flips/pom.xml index e6f2ee1aa0a5..ecc2af4a4710 100644 --- a/flips/pom.xml +++ b/flips/pom.xml @@ -20,7 +20,7 @@ org.springframework.boot spring-boot-starter-web - 1.5.9.RELEASE + 1.5.10.RELEASE diff --git a/flips/src/main/java/com/baeldung/flips/controller/FlipController.java b/flips/src/main/java/com/baeldung/flips/controller/FlipController.java index 6bc79b578abf..225bde92e18b 100644 --- a/flips/src/main/java/com/baeldung/flips/controller/FlipController.java +++ b/flips/src/main/java/com/baeldung/flips/controller/FlipController.java @@ -2,7 +2,10 @@ import com.baeldung.flips.model.Thing; import com.baeldung.flips.service.FlipService; -import org.flips.annotation.*; +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; @@ -24,42 +27,39 @@ public FlipController(FlipService flipService) { @RequestMapping(value = "/things", method = RequestMethod.GET) @FlipOnProfiles(activeProfiles = "dev") - public List getAllThings(){ + public List getAllThings() { return flipService.getAllThings(); } @RequestMapping(value = "/thing/{id}", method = RequestMethod.GET) - @FlipOnDaysOfWeek(daysOfWeek={DayOfWeek.MONDAY, DayOfWeek.WEDNESDAY}) - public Thing getThingByNewId(@PathVariable int id){ + @FlipOnDaysOfWeek(daysOfWeek = { + DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY, DayOfWeek.THURSDAY, + DayOfWeek.FRIDAY, DayOfWeek.SATURDAY, DayOfWeek.SUNDAY + }) + public Thing getThingByNewId(@PathVariable int id) { return flipService.getThingById(id).orElse(new Thing("Not Found", -1)); } - @RequestMapping(value = "/thing/last", method = RequestMethod.GET) @FlipOnDateTime(cutoffDateTimeProperty = "last.active.after") - public Thing getLastThing(){ + public Thing getLastThing() { return flipService.getLastThing(); } @RequestMapping(value = "/thing/first", method = RequestMethod.GET) @FlipOnDateTime(cutoffDateTimeProperty = "first.active.after") - public Thing getFirstThing(){ + public Thing getFirstThing() { return flipService.getLastThing(); } - @RequestMapping(value = "/things/{id}", method = RequestMethod.GET) @FlipOnEnvironmentProperty(property = "feature.thing.by.id", expectedValue = "Y") - public Thing getThingById(@PathVariable int id){ + public Thing getThingById(@PathVariable int id) { return flipService.getThingById(id).orElse(new Thing("Not Found", -1)); } - - - @FlipBean(with = NewFlipController.class) - @FlipOnSpringExpression(expression = "@environment.getProperty('feature.new.thing') == 'Y'") @RequestMapping(value = "/thing/new", method = RequestMethod.GET) - public Thing getNewThing(){ - return new Thing("Not Found", -1); + public Thing getNewThing() { + return flipService.getNewThing(); } } \ No newline at end of file diff --git a/flips/src/main/java/com/baeldung/flips/controller/NewFlipController.java b/flips/src/main/java/com/baeldung/flips/controller/NewFlipController.java deleted file mode 100644 index a4fdc16ed4df..000000000000 --- a/flips/src/main/java/com/baeldung/flips/controller/NewFlipController.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.flips.controller; - -import com.baeldung.flips.model.Thing; -import com.baeldung.flips.service.FlipService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class NewFlipController { - - private FlipService flipService; - - @Autowired - public NewFlipController(FlipService flipService) { - this.flipService = flipService; - } - - @RequestMapping(value = "/alt/thing/new", method = RequestMethod.GET) - public Thing getNewThing(){ - return flipService.getNewThing(); - } -} \ No newline at end of file diff --git a/flips/src/main/java/com/baeldung/flips/model/Thing.java b/flips/src/main/java/com/baeldung/flips/model/Thing.java index 725adda9d53a..07a2d51463fd 100644 --- a/flips/src/main/java/com/baeldung/flips/model/Thing.java +++ b/flips/src/main/java/com/baeldung/flips/model/Thing.java @@ -1,6 +1,5 @@ package com.baeldung.flips.model; - import lombok.Data; import lombok.RequiredArgsConstructor; diff --git a/flips/src/main/java/com/baeldung/flips/service/FlipService.java b/flips/src/main/java/com/baeldung/flips/service/FlipService.java index f7392bedb6de..2ccd9f0cba32 100644 --- a/flips/src/main/java/com/baeldung/flips/service/FlipService.java +++ b/flips/src/main/java/com/baeldung/flips/service/FlipService.java @@ -1,6 +1,8 @@ package com.baeldung.flips.service; import com.baeldung.flips.model.Thing; +import org.flips.annotation.FlipBean; +import org.flips.annotation.FlipOnSpringExpression; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -10,14 +12,11 @@ @Service public class FlipService { - private final List things; public FlipService() { - - things = new ArrayList<>(); - - things.add(new Thing("Thing1", 1)); + things = new ArrayList<>(); + things.add(new Thing("Thing1", 1)); things.add(new Thing("Thing2", 2)); things.add(new Thing("Thing3", 3)); things.add(new Thing("Thing4", 4)); @@ -26,8 +25,6 @@ public FlipService() { } - - public List getAllThings() { return things; } @@ -36,12 +33,14 @@ public Optional getThingById(int id) { return things.stream().filter(thing -> (thing.getId() == id)).findFirst(); } + @FlipBean(with = NewFlipService.class) + @FlipOnSpringExpression(expression = "(2 + 2) == 4") public Thing getNewThing() { return new Thing("New Thing!", 99); } public Thing getLastThing() { - return things.get(things.size() -1); + return things.get(things.size() - 1); } public Thing getFirstThing() { diff --git a/flips/src/main/java/com/baeldung/flips/service/NewFlipService.java b/flips/src/main/java/com/baeldung/flips/service/NewFlipService.java new file mode 100644 index 000000000000..562e8a61aa73 --- /dev/null +++ b/flips/src/main/java/com/baeldung/flips/service/NewFlipService.java @@ -0,0 +1,17 @@ +package com.baeldung.flips.service; + +import com.baeldung.flips.model.Thing; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Service +public class NewFlipService { + + public Thing getNewThing() { + return new Thing("Shiny New Thing!", 100); + } + +} \ No newline at end of file diff --git a/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java b/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java index a353b843b350..8961591b2cf4 100644 --- a/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java +++ b/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java @@ -12,20 +12,22 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; - @RunWith(SpringRunner.class) -@SpringBootTest(properties = {"feature.flips.by.id=Y", - "feature.movie.statistics=Y" - }) +@SpringBootTest(properties = { + "feature.thing.by.id=Y", + "feature.new.thing=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; + @Autowired private MockMvc mvc; - - // @Test - comment this out after insuring that its the proper day of the week in FlipController! + @Test public void givenValidDayOfWeek_APIAvailable() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/thing/1")) .andExpect(MockMvcResultMatchers.status().is(200)) @@ -47,27 +49,26 @@ public void givenInvalidDate_APINotAvailable() throws Exception { .andExpect(MockMvcResultMatchers.status().is(501)); } - @Test - public void shouldLoadAllThings() throws Exception{ + public void givenCorrectProfile_APIAvailable() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/things")) - .andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(6))); + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(6))); } @Test public void givenPropertySet_APIAvailable() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/things/1")) - .andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Thing1"))) - .andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(1))); + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Thing1"))) + .andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(1))); } @Test - public void getNewThing() throws Exception { + public void getValidExpression_FlipBean() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/thing/new")) - .andExpect(MockMvcResultMatchers.status().is(200)) - .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("New Thing!"))) - .andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(99))); + .andExpect(MockMvcResultMatchers.status().is(200)) + .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Shiny New Thing!"))) + .andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(100))); } } \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/flips/TestFlipsBean.java b/libraries/src/main/java/com/baeldung/flips/TestFlipsBean.java new file mode 100644 index 000000000000..e274e4ff0d24 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/flips/TestFlipsBean.java @@ -0,0 +1,4 @@ +package com.baeldung.flips; + +public class TestFlipsBean { +} diff --git a/pom.xml b/pom.xml index 3092c0fa40bd..1249926f9573 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,7 @@ feign + flips From 9deb016383cf38482939458a7c887da5f93620e8 Mon Sep 17 00:00:00 2001 From: Eric Goebelbecker Date: Thu, 15 Mar 2018 20:35:31 -0400 Subject: [PATCH 4/6] BAEL-1554 - update read me --- flips/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flips/README.md b/flips/README.md index 071781ba8da9..0c62173b6ac3 100644 --- a/flips/README.md +++ b/flips/README.md @@ -1,2 +1,2 @@ ### Relevant Articles: -- [Guide to Flips Library](http://www.baeldung.com/guide-to-flips-library) +- [Guide to Flips For Spring](http://www.baeldung.com/guide-to-flips-for-spring/) From f1df747746b1d4e475da5ed2ed705f9339f13585 Mon Sep 17 00:00:00 2001 From: Eric Goebelbecker Date: Fri, 16 Mar 2018 17:59:07 -0400 Subject: [PATCH 5/6] BAEL-1554 - rename "Thing" to "Foo" --- .../flips/controller/FlipController.java | 40 +++++++++---------- .../flips/model/{Thing.java => Foo.java} | 2 +- .../baeldung/flips/service/FlipService.java | 38 +++++++++--------- .../flips/service/NewFlipService.java | 10 ++--- .../src/main/resources/application.properties | 4 +- .../flips/controller/FlipControllerTest.java | 24 +++++------ 6 files changed, 57 insertions(+), 61 deletions(-) rename flips/src/main/java/com/baeldung/flips/model/{Thing.java => Foo.java} (89%) diff --git a/flips/src/main/java/com/baeldung/flips/controller/FlipController.java b/flips/src/main/java/com/baeldung/flips/controller/FlipController.java index 225bde92e18b..50458023b31d 100644 --- a/flips/src/main/java/com/baeldung/flips/controller/FlipController.java +++ b/flips/src/main/java/com/baeldung/flips/controller/FlipController.java @@ -1,6 +1,6 @@ package com.baeldung.flips.controller; -import com.baeldung.flips.model.Thing; +import com.baeldung.flips.model.Foo; import com.baeldung.flips.service.FlipService; import org.flips.annotation.FlipOnDateTime; import org.flips.annotation.FlipOnDaysOfWeek; @@ -25,41 +25,41 @@ public FlipController(FlipService flipService) { this.flipService = flipService; } - @RequestMapping(value = "/things", method = RequestMethod.GET) + @RequestMapping(value = "/foos", method = RequestMethod.GET) @FlipOnProfiles(activeProfiles = "dev") - public List getAllThings() { - return flipService.getAllThings(); + public List getAllFoos() { + return flipService.getAllFoos(); } - @RequestMapping(value = "/thing/{id}", method = RequestMethod.GET) + @RequestMapping(value = "/foo/{id}", method = RequestMethod.GET) @FlipOnDaysOfWeek(daysOfWeek = { DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY, DayOfWeek.THURSDAY, DayOfWeek.FRIDAY, DayOfWeek.SATURDAY, DayOfWeek.SUNDAY }) - public Thing getThingByNewId(@PathVariable int id) { - return flipService.getThingById(id).orElse(new Thing("Not Found", -1)); + public Foo getFooByNewId(@PathVariable int id) { + return flipService.getFooById(id).orElse(new Foo("Not Found", -1)); } - @RequestMapping(value = "/thing/last", method = RequestMethod.GET) + @RequestMapping(value = "/foo/last", method = RequestMethod.GET) @FlipOnDateTime(cutoffDateTimeProperty = "last.active.after") - public Thing getLastThing() { - return flipService.getLastThing(); + public Foo getLastFoo() { + return flipService.getLastFoo(); } - @RequestMapping(value = "/thing/first", method = RequestMethod.GET) + @RequestMapping(value = "/foo/first", method = RequestMethod.GET) @FlipOnDateTime(cutoffDateTimeProperty = "first.active.after") - public Thing getFirstThing() { - return flipService.getLastThing(); + public Foo getFirstFoo() { + return flipService.getLastFoo(); } - @RequestMapping(value = "/things/{id}", method = RequestMethod.GET) - @FlipOnEnvironmentProperty(property = "feature.thing.by.id", expectedValue = "Y") - public Thing getThingById(@PathVariable int id) { - return flipService.getThingById(id).orElse(new Thing("Not Found", -1)); + @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 = "/thing/new", method = RequestMethod.GET) - public Thing getNewThing() { - return flipService.getNewThing(); + @RequestMapping(value = "/foo/new", method = RequestMethod.GET) + public Foo getNewThing() { + return flipService.getNewFoo(); } } \ No newline at end of file diff --git a/flips/src/main/java/com/baeldung/flips/model/Thing.java b/flips/src/main/java/com/baeldung/flips/model/Foo.java similarity index 89% rename from flips/src/main/java/com/baeldung/flips/model/Thing.java rename to flips/src/main/java/com/baeldung/flips/model/Foo.java index 07a2d51463fd..d98abb79a9f8 100644 --- a/flips/src/main/java/com/baeldung/flips/model/Thing.java +++ b/flips/src/main/java/com/baeldung/flips/model/Foo.java @@ -5,7 +5,7 @@ @Data @RequiredArgsConstructor -public class Thing { +public class Foo { private final String name; private final int id; } diff --git a/flips/src/main/java/com/baeldung/flips/service/FlipService.java b/flips/src/main/java/com/baeldung/flips/service/FlipService.java index 2ccd9f0cba32..9f7fb325a5c2 100644 --- a/flips/src/main/java/com/baeldung/flips/service/FlipService.java +++ b/flips/src/main/java/com/baeldung/flips/service/FlipService.java @@ -1,6 +1,6 @@ package com.baeldung.flips.service; -import com.baeldung.flips.model.Thing; +import com.baeldung.flips.model.Foo; import org.flips.annotation.FlipBean; import org.flips.annotation.FlipOnSpringExpression; import org.springframework.stereotype.Service; @@ -12,39 +12,39 @@ @Service public class FlipService { - private final List things; + private final List foos; public FlipService() { - things = new ArrayList<>(); - things.add(new Thing("Thing1", 1)); - things.add(new Thing("Thing2", 2)); - things.add(new Thing("Thing3", 3)); - things.add(new Thing("Thing4", 4)); - things.add(new Thing("Thing5", 5)); - things.add(new Thing("Thing6", 6)); + 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 getAllThings() { - return things; + public List getAllFoos() { + return foos; } - public Optional getThingById(int id) { - return things.stream().filter(thing -> (thing.getId() == id)).findFirst(); + public Optional getFooById(int id) { + return foos.stream().filter(foo -> (foo.getId() == id)).findFirst(); } @FlipBean(with = NewFlipService.class) @FlipOnSpringExpression(expression = "(2 + 2) == 4") - public Thing getNewThing() { - return new Thing("New Thing!", 99); + public Foo getNewFoo() { + return new Foo("New Foo!", 99); } - public Thing getLastThing() { - return things.get(things.size() - 1); + public Foo getLastFoo() { + return foos.get(foos.size() - 1); } - public Thing getFirstThing() { - return things.get(0); + public Foo getFirstFoo() { + return foos.get(0); } } \ No newline at end of file diff --git a/flips/src/main/java/com/baeldung/flips/service/NewFlipService.java b/flips/src/main/java/com/baeldung/flips/service/NewFlipService.java index 562e8a61aa73..1dcda9b6ca81 100644 --- a/flips/src/main/java/com/baeldung/flips/service/NewFlipService.java +++ b/flips/src/main/java/com/baeldung/flips/service/NewFlipService.java @@ -1,17 +1,13 @@ package com.baeldung.flips.service; -import com.baeldung.flips.model.Thing; +import com.baeldung.flips.model.Foo; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - @Service public class NewFlipService { - public Thing getNewThing() { - return new Thing("Shiny New Thing!", 100); + public Foo getNewFoo() { + return new Foo("Shiny New Foo!", 100); } } \ No newline at end of file diff --git a/flips/src/main/resources/application.properties b/flips/src/main/resources/application.properties index 166a85bfe2d1..274896be1510 100644 --- a/flips/src/main/resources/application.properties +++ b/flips/src/main/resources/application.properties @@ -1,5 +1,5 @@ -feature.thing.by.id=Y -feature.new.thing=Y +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 \ No newline at end of file diff --git a/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java b/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java index 8961591b2cf4..1b8c78e2a4af 100644 --- a/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java +++ b/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java @@ -14,8 +14,8 @@ @RunWith(SpringRunner.class) @SpringBootTest(properties = { - "feature.thing.by.id=Y", - "feature.new.thing=Y", + "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" @@ -29,46 +29,46 @@ public class FlipControllerTest { @Test public void givenValidDayOfWeek_APIAvailable() throws Exception { - mvc.perform(MockMvcRequestBuilders.get("/thing/1")) + mvc.perform(MockMvcRequestBuilders.get("/foo/1")) .andExpect(MockMvcResultMatchers.status().is(200)) - .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Thing1"))) + .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("/thing/last")) + mvc.perform(MockMvcRequestBuilders.get("/foo/last")) .andExpect(MockMvcResultMatchers.status().is(200)) - .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Thing6"))) + .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("/thing/first")) + mvc.perform(MockMvcRequestBuilders.get("/foo/first")) .andExpect(MockMvcResultMatchers.status().is(501)); } @Test public void givenCorrectProfile_APIAvailable() throws Exception { - mvc.perform(MockMvcRequestBuilders.get("/things")) + 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("/things/1")) + mvc.perform(MockMvcRequestBuilders.get("/foos/1")) .andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Thing1"))) + .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("/thing/new")) + mvc.perform(MockMvcRequestBuilders.get("/foo/new")) .andExpect(MockMvcResultMatchers.status().is(200)) - .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Shiny New Thing!"))) + .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo("Shiny New Foo!"))) .andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(100))); } } \ No newline at end of file From 9bde50bb96a671a527d46e27b41463528ebf851a Mon Sep 17 00:00:00 2001 From: Eric Goebelbecker Date: Wed, 21 Mar 2018 09:18:46 -0400 Subject: [PATCH 6/6] BAEL-1554 - rename module to spring-4 --- pom.xml | 2 +- {flips => spring-4}/README.md | 0 {flips => spring-4}/pom.xml | 9 +++------ .../main/java/com/baeldung/flips/ApplicationConfig.java | 0 .../com/baeldung/flips/controller/FlipController.java | 0 .../src/main/java/com/baeldung/flips/model/Foo.java | 0 .../java/com/baeldung/flips/service/FlipService.java | 0 .../java/com/baeldung/flips/service/NewFlipService.java | 0 .../src/main/resources/application.properties | 0 .../baeldung/flips/controller/FlipControllerTest.java | 0 10 files changed, 4 insertions(+), 7 deletions(-) rename {flips => spring-4}/README.md (100%) rename {flips => spring-4}/pom.xml (95%) rename {flips => spring-4}/src/main/java/com/baeldung/flips/ApplicationConfig.java (100%) rename {flips => spring-4}/src/main/java/com/baeldung/flips/controller/FlipController.java (100%) rename {flips => spring-4}/src/main/java/com/baeldung/flips/model/Foo.java (100%) rename {flips => spring-4}/src/main/java/com/baeldung/flips/service/FlipService.java (100%) rename {flips => spring-4}/src/main/java/com/baeldung/flips/service/NewFlipService.java (100%) rename {flips => spring-4}/src/main/resources/application.properties (100%) rename {flips => spring-4}/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java (100%) diff --git a/pom.xml b/pom.xml index 1249926f9573..16d5c597f60d 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,6 @@ feign - flips @@ -147,6 +146,7 @@ testing-modules/selenium-junit-testng persistence-modules/solr spark-java + spring-4 spring-5 spring-5-reactive spring-5-mvc diff --git a/flips/README.md b/spring-4/README.md similarity index 100% rename from flips/README.md rename to spring-4/README.md diff --git a/flips/pom.xml b/spring-4/pom.xml similarity index 95% rename from flips/pom.xml rename to spring-4/pom.xml index ecc2af4a4710..8a57888e560c 100644 --- a/flips/pom.xml +++ b/spring-4/pom.xml @@ -2,12 +2,12 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - flips - flips + spring-4 + spring-4 0.0.1-SNAPSHOT jar - flips + spring-4 UTF-8 @@ -42,9 +42,6 @@ 1.16.18 provided - - - diff --git a/flips/src/main/java/com/baeldung/flips/ApplicationConfig.java b/spring-4/src/main/java/com/baeldung/flips/ApplicationConfig.java similarity index 100% rename from flips/src/main/java/com/baeldung/flips/ApplicationConfig.java rename to spring-4/src/main/java/com/baeldung/flips/ApplicationConfig.java diff --git a/flips/src/main/java/com/baeldung/flips/controller/FlipController.java b/spring-4/src/main/java/com/baeldung/flips/controller/FlipController.java similarity index 100% rename from flips/src/main/java/com/baeldung/flips/controller/FlipController.java rename to spring-4/src/main/java/com/baeldung/flips/controller/FlipController.java diff --git a/flips/src/main/java/com/baeldung/flips/model/Foo.java b/spring-4/src/main/java/com/baeldung/flips/model/Foo.java similarity index 100% rename from flips/src/main/java/com/baeldung/flips/model/Foo.java rename to spring-4/src/main/java/com/baeldung/flips/model/Foo.java diff --git a/flips/src/main/java/com/baeldung/flips/service/FlipService.java b/spring-4/src/main/java/com/baeldung/flips/service/FlipService.java similarity index 100% rename from flips/src/main/java/com/baeldung/flips/service/FlipService.java rename to spring-4/src/main/java/com/baeldung/flips/service/FlipService.java diff --git a/flips/src/main/java/com/baeldung/flips/service/NewFlipService.java b/spring-4/src/main/java/com/baeldung/flips/service/NewFlipService.java similarity index 100% rename from flips/src/main/java/com/baeldung/flips/service/NewFlipService.java rename to spring-4/src/main/java/com/baeldung/flips/service/NewFlipService.java diff --git a/flips/src/main/resources/application.properties b/spring-4/src/main/resources/application.properties similarity index 100% rename from flips/src/main/resources/application.properties rename to spring-4/src/main/resources/application.properties diff --git a/flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java b/spring-4/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java similarity index 100% rename from flips/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java rename to spring-4/src/test/java/com/baeldung/flips/controller/FlipControllerTest.java