From a0d82c25e34260e7bbdfa043596e66f00903c130 Mon Sep 17 00:00:00 2001 From: Mohammed Sarfaraz Khan Date: Tue, 4 Jul 2017 11:07:03 +0530 Subject: [PATCH 1/4] Review Comment Incorporation --- .../spring/drools/app/ApplicationRunner.java | 4 +- .../baeldung/spring/drools/model/Fare.java | 3 + .../spring/drools/model/TaxiRide.java | 12 ++-- .../service/TaxiFareCalculatorService.java | 6 +- .../drools/service/TaxiFareConfiguration.java | 11 ++-- .../src/main/resources/TAXI_FARE_RULE.drl | 12 ++-- ...iFareCalculatorServiceIntegrationTest.java | 64 +++++++++---------- 7 files changed, 54 insertions(+), 58 deletions(-) diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/app/ApplicationRunner.java b/spring-drools/src/main/java/com/baeldung/spring/drools/app/ApplicationRunner.java index ce4b49451a..f36fa0495b 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/app/ApplicationRunner.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/app/ApplicationRunner.java @@ -12,10 +12,10 @@ public class ApplicationRunner { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(TaxiFareConfiguration.class); - TaxiFareCalculatorService orderService = context.getBean(TaxiFareCalculatorService.class); + TaxiFareCalculatorService orderService = (TaxiFareCalculatorService) context.getBean(TaxiFareCalculatorService.class); TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(true); + taxiRide.setIsNightSurcharge(true); taxiRide.setDistanceInMile(190L); Fare rideFare = new Fare(); orderService.calculateFare(taxiRide, rideFare); diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java b/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java index 86044b281a..09be3044fa 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java @@ -4,10 +4,12 @@ public class Fare { private Long nightSurcharge; private Long rideFare; + private Long totalFare; public Fare() { nightSurcharge = 0L; rideFare = 0L; + totalFare = 0L; } public Long getNightSurcharge() { @@ -29,4 +31,5 @@ public class Fare { public Long getTotalFare() { return nightSurcharge + rideFare; } + } diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/model/TaxiRide.java b/spring-drools/src/main/java/com/baeldung/spring/drools/model/TaxiRide.java index 6dc08bbb60..8dc0f373b1 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/model/TaxiRide.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/model/TaxiRide.java @@ -1,16 +1,16 @@ package com.baeldung.spring.drools.model; public class TaxiRide { - - private Boolean bNightSurcharge; + + private Boolean isNightSurcharge; private Long distanceInMile; - public Boolean getbNightSurcharge() { - return bNightSurcharge; + public Boolean getIsNightSurcharge() { + return isNightSurcharge; } - public void setbNightSurcharge(Boolean bNightSurcharge) { - this.bNightSurcharge = bNightSurcharge; + public void setIsNightSurcharge(Boolean isNightSurcharge) { + this.isNightSurcharge = isNightSurcharge; } public Long getDistanceInMile() { diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareCalculatorService.java b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareCalculatorService.java index 0407eff5d9..c2c5b399df 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareCalculatorService.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareCalculatorService.java @@ -3,17 +3,19 @@ package com.baeldung.spring.drools.service; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; import com.baeldung.spring.drools.model.Fare; import com.baeldung.spring.drools.model.TaxiRide; +@Service public class TaxiFareCalculatorService { @Autowired - private KieContainer kieContainer; + private KieContainer kContainer; public Long calculateFare(TaxiRide taxiRide, Fare rideFare) { - KieSession kieSession = kieContainer.newKieSession(); + KieSession kieSession = kContainer.newKieSession(); kieSession.setGlobal("rideFare", rideFare); kieSession.insert(taxiRide); kieSession.fireAllRules(); diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java index 8da1d4b992..e7b614694e 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java @@ -5,15 +5,16 @@ import org.kie.api.builder.KieBuilder; import org.kie.api.builder.KieFileSystem; import org.kie.api.builder.KieModule; import org.kie.api.runtime.KieContainer; -import org.kie.api.runtime.KieSession; import org.kie.internal.io.ResourceFactory; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration +@ComponentScan("com.baeldung.spring.drools.service") public class TaxiFareConfiguration { - private static final String drlFile = "TAXI_FARE_RULE.drl"; + public static final String drlFile = "TAXI_FARE_RULE.drl"; @Bean public KieContainer kieContainer() { @@ -26,10 +27,6 @@ public class TaxiFareConfiguration { KieModule kieModule = kieBuilder.getKieModule(); return kieServices.newKieContainer(kieModule.getReleaseId()); - } - - @Bean - public TaxiFareCalculatorService taxiFareCalculatorService() { - return new TaxiFareCalculatorService(); + } } diff --git a/spring-drools/src/main/resources/TAXI_FARE_RULE.drl b/spring-drools/src/main/resources/TAXI_FARE_RULE.drl index 56335c08b3..97f40ddae2 100644 --- a/spring-drools/src/main/resources/TAXI_FARE_RULE.drl +++ b/spring-drools/src/main/resources/TAXI_FARE_RULE.drl @@ -7,7 +7,7 @@ dialect "mvel" rule "Calculate Taxi Fare - Scenario 1" when - taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile < 10); + taxiRideInstance:TaxiRide(isNightSurcharge == false && distanceInMile < 10); then rideFare.setNightSurcharge(0); rideFare.setRideFare(70); @@ -15,7 +15,7 @@ end rule "Calculate Taxi Fare - Scenario 2" when - taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile < 10); + taxiRideInstance:TaxiRide(isNightSurcharge == true && distanceInMile < 10); then rideFare.setNightSurcharge(30); rideFare.setRideFare(70); @@ -24,7 +24,7 @@ end rule "Calculate Taxi Fare - Scenario 3" when - taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile >= 10 && distanceInMile < 100); + taxiRideInstance:TaxiRide(isNightSurcharge == false && distanceInMile >= 10 && distanceInMile < 100); then rideFare.setNightSurcharge(0); rideFare.setRideFare(70+(2*taxiRideInstance.getDistanceInMile())); @@ -33,7 +33,7 @@ end rule "Calculate Taxi Fare - Scenario 4" when - taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile >= 10 && distanceInMile < 100); + taxiRideInstance:TaxiRide(isNightSurcharge == true && distanceInMile >= 10 && distanceInMile < 100); then rideFare.setNightSurcharge(30+taxiRideInstance.getDistanceInMile()); rideFare.setRideFare(70+(2*taxiRideInstance.getDistanceInMile())); @@ -42,7 +42,7 @@ end rule "Calculate Taxi Fare - Scenario 5" when - taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile >= 100); + taxiRideInstance:TaxiRide(isNightSurcharge == false && distanceInMile >= 100); then rideFare.setNightSurcharge(0); rideFare.setRideFare(70+(1.5*taxiRideInstance.getDistanceInMile())); @@ -50,7 +50,7 @@ end rule "Calculate Taxi Fare - Scenario 6" when - taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile >= 100); + taxiRideInstance:TaxiRide(isNightSurcharge == true && distanceInMile >= 100); then rideFare.setNightSurcharge(30+taxiRideInstance.getDistanceInMile()); rideFare.setRideFare(70+(1.5*taxiRideInstance.getDistanceInMile())); diff --git a/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java b/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java index 97bd44316f..bb28dd2293 100644 --- a/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java +++ b/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java @@ -17,78 +17,72 @@ import static org.junit.Assert.assertNotNull; public class TaxiFareCalculatorServiceIntegrationTest { @Autowired - private TaxiFareCalculatorService taxiFareCalculatorService; + TaxiFareCalculatorService taxiFareCalculatorService; @Test - public void testCalculateFareScenario1() { + public void whenNightSurchargeFalseAndDistanceLessThan10_thenFixFareWithoutNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(false); + taxiRide.setIsNightSurcharge(false); taxiRide.setDistanceInMile(9L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - - assertNotNull(totalCharge); - assertEquals(Long.valueOf(70), totalCharge); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(70), totalCharge); } - + @Test - public void testCalculateFareScenario2() { + public void whenNightSurchargeTrueAndDistanceLessThan10_thenFixFareWithNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(true); + taxiRide.setIsNightSurcharge(true); taxiRide.setDistanceInMile(5L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - - assertNotNull(totalCharge); - assertEquals(Long.valueOf(100), totalCharge); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(100), totalCharge); } @Test - public void testCalculateFareScenario3() { + public void whenNightSurchargeFalseAndDistanceLessThan100_thenDoubleFareWithoutNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(false); + taxiRide.setIsNightSurcharge(false); taxiRide.setDistanceInMile(50L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - - assertNotNull(totalCharge); - assertEquals(Long.valueOf(170), totalCharge); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(170), totalCharge); } - + @Test - public void testCalculateFareScenario4() { + public void whenNightSurchargeTrueAndDistanceLessThan100_thenDoubleFareWithNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(true); + taxiRide.setIsNightSurcharge(true); taxiRide.setDistanceInMile(50L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - - assertNotNull(totalCharge); - assertEquals(Long.valueOf(250), totalCharge); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(250), totalCharge); } - + @Test - public void testCalculateFareScenario5() { + public void whenNightSurchargeFalseAndDistanceGreaterThan100_thenExtraPercentFareWithoutNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(false); + taxiRide.setIsNightSurcharge(false); taxiRide.setDistanceInMile(100L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - - assertNotNull(totalCharge); - assertEquals(Long.valueOf(220), totalCharge); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(220), totalCharge); } - + @Test - public void testCalculateFareScenario6() { + public void whenNightSurchargeTrueAndDistanceGreaterThan100_thenExtraPercentFareWithNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(true); + taxiRide.setIsNightSurcharge(true); taxiRide.setDistanceInMile(100L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - - assertNotNull(totalCharge); - assertEquals(Long.valueOf(350), totalCharge); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(350), totalCharge); } } From 5d7e0bc531780252235aff42c252363b2038125b Mon Sep 17 00:00:00 2001 From: Mohammed Sarfaraz Khan Date: Tue, 4 Jul 2017 11:07:03 +0530 Subject: [PATCH 2/4] Review Comment Incorporation --- .../spring/drools/app/ApplicationRunner.java | 6 +-- .../baeldung/spring/drools/model/Fare.java | 3 +- .../spring/drools/model/TaxiRide.java | 12 ++--- .../service/TaxiFareCalculatorService.java | 6 ++- .../drools/service/TaxiFareConfiguration.java | 11 ++-- .../src/main/resources/TAXI_FARE_RULE.drl | 12 ++--- ...iFareCalculatorServiceIntegrationTest.java | 51 +++++++++---------- 7 files changed, 48 insertions(+), 53 deletions(-) diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/app/ApplicationRunner.java b/spring-drools/src/main/java/com/baeldung/spring/drools/app/ApplicationRunner.java index ce4b49451a..007dd64ed7 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/app/ApplicationRunner.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/app/ApplicationRunner.java @@ -12,13 +12,13 @@ public class ApplicationRunner { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(TaxiFareConfiguration.class); - TaxiFareCalculatorService orderService = context.getBean(TaxiFareCalculatorService.class); + TaxiFareCalculatorService taxiFareCalculatorService = (TaxiFareCalculatorService) context.getBean(TaxiFareCalculatorService.class); TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(true); + taxiRide.setIsNightSurcharge(true); taxiRide.setDistanceInMile(190L); Fare rideFare = new Fare(); - orderService.calculateFare(taxiRide, rideFare); + taxiFareCalculatorService.calculateFare(taxiRide, rideFare); } } diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java b/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java index 86044b281a..677efb9bb2 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java @@ -4,7 +4,7 @@ public class Fare { private Long nightSurcharge; private Long rideFare; - + public Fare() { nightSurcharge = 0L; rideFare = 0L; @@ -29,4 +29,5 @@ public class Fare { public Long getTotalFare() { return nightSurcharge + rideFare; } + } diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/model/TaxiRide.java b/spring-drools/src/main/java/com/baeldung/spring/drools/model/TaxiRide.java index 6dc08bbb60..8dc0f373b1 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/model/TaxiRide.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/model/TaxiRide.java @@ -1,16 +1,16 @@ package com.baeldung.spring.drools.model; public class TaxiRide { - - private Boolean bNightSurcharge; + + private Boolean isNightSurcharge; private Long distanceInMile; - public Boolean getbNightSurcharge() { - return bNightSurcharge; + public Boolean getIsNightSurcharge() { + return isNightSurcharge; } - public void setbNightSurcharge(Boolean bNightSurcharge) { - this.bNightSurcharge = bNightSurcharge; + public void setIsNightSurcharge(Boolean isNightSurcharge) { + this.isNightSurcharge = isNightSurcharge; } public Long getDistanceInMile() { diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareCalculatorService.java b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareCalculatorService.java index 0407eff5d9..c2c5b399df 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareCalculatorService.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareCalculatorService.java @@ -3,17 +3,19 @@ package com.baeldung.spring.drools.service; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; import com.baeldung.spring.drools.model.Fare; import com.baeldung.spring.drools.model.TaxiRide; +@Service public class TaxiFareCalculatorService { @Autowired - private KieContainer kieContainer; + private KieContainer kContainer; public Long calculateFare(TaxiRide taxiRide, Fare rideFare) { - KieSession kieSession = kieContainer.newKieSession(); + KieSession kieSession = kContainer.newKieSession(); kieSession.setGlobal("rideFare", rideFare); kieSession.insert(taxiRide); kieSession.fireAllRules(); diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java index 8da1d4b992..e7b614694e 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java @@ -5,15 +5,16 @@ import org.kie.api.builder.KieBuilder; import org.kie.api.builder.KieFileSystem; import org.kie.api.builder.KieModule; import org.kie.api.runtime.KieContainer; -import org.kie.api.runtime.KieSession; import org.kie.internal.io.ResourceFactory; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration +@ComponentScan("com.baeldung.spring.drools.service") public class TaxiFareConfiguration { - private static final String drlFile = "TAXI_FARE_RULE.drl"; + public static final String drlFile = "TAXI_FARE_RULE.drl"; @Bean public KieContainer kieContainer() { @@ -26,10 +27,6 @@ public class TaxiFareConfiguration { KieModule kieModule = kieBuilder.getKieModule(); return kieServices.newKieContainer(kieModule.getReleaseId()); - } - - @Bean - public TaxiFareCalculatorService taxiFareCalculatorService() { - return new TaxiFareCalculatorService(); + } } diff --git a/spring-drools/src/main/resources/TAXI_FARE_RULE.drl b/spring-drools/src/main/resources/TAXI_FARE_RULE.drl index 56335c08b3..97f40ddae2 100644 --- a/spring-drools/src/main/resources/TAXI_FARE_RULE.drl +++ b/spring-drools/src/main/resources/TAXI_FARE_RULE.drl @@ -7,7 +7,7 @@ dialect "mvel" rule "Calculate Taxi Fare - Scenario 1" when - taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile < 10); + taxiRideInstance:TaxiRide(isNightSurcharge == false && distanceInMile < 10); then rideFare.setNightSurcharge(0); rideFare.setRideFare(70); @@ -15,7 +15,7 @@ end rule "Calculate Taxi Fare - Scenario 2" when - taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile < 10); + taxiRideInstance:TaxiRide(isNightSurcharge == true && distanceInMile < 10); then rideFare.setNightSurcharge(30); rideFare.setRideFare(70); @@ -24,7 +24,7 @@ end rule "Calculate Taxi Fare - Scenario 3" when - taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile >= 10 && distanceInMile < 100); + taxiRideInstance:TaxiRide(isNightSurcharge == false && distanceInMile >= 10 && distanceInMile < 100); then rideFare.setNightSurcharge(0); rideFare.setRideFare(70+(2*taxiRideInstance.getDistanceInMile())); @@ -33,7 +33,7 @@ end rule "Calculate Taxi Fare - Scenario 4" when - taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile >= 10 && distanceInMile < 100); + taxiRideInstance:TaxiRide(isNightSurcharge == true && distanceInMile >= 10 && distanceInMile < 100); then rideFare.setNightSurcharge(30+taxiRideInstance.getDistanceInMile()); rideFare.setRideFare(70+(2*taxiRideInstance.getDistanceInMile())); @@ -42,7 +42,7 @@ end rule "Calculate Taxi Fare - Scenario 5" when - taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile >= 100); + taxiRideInstance:TaxiRide(isNightSurcharge == false && distanceInMile >= 100); then rideFare.setNightSurcharge(0); rideFare.setRideFare(70+(1.5*taxiRideInstance.getDistanceInMile())); @@ -50,7 +50,7 @@ end rule "Calculate Taxi Fare - Scenario 6" when - taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile >= 100); + taxiRideInstance:TaxiRide(isNightSurcharge == true && distanceInMile >= 100); then rideFare.setNightSurcharge(30+taxiRideInstance.getDistanceInMile()); rideFare.setRideFare(70+(1.5*taxiRideInstance.getDistanceInMile())); diff --git a/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java b/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java index 97bd44316f..f74efe3f77 100644 --- a/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java +++ b/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java @@ -1,92 +1,87 @@ package com.baeldung.spring.drools.service; -import com.baeldung.spring.drools.model.Fare; -import com.baeldung.spring.drools.model.TaxiRide; -import org.junit.Assert; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import com.baeldung.spring.drools.model.Fare; +import com.baeldung.spring.drools.model.TaxiRide; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TaxiFareConfiguration.class) public class TaxiFareCalculatorServiceIntegrationTest { @Autowired - private TaxiFareCalculatorService taxiFareCalculatorService; + TaxiFareCalculatorService taxiFareCalculatorService; @Test - public void testCalculateFareScenario1() { + public void whenNightSurchargeFalseAndDistanceLessThan10_thenFixFareWithoutNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(false); + taxiRide.setIsNightSurcharge(false); taxiRide.setDistanceInMile(9L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - assertNotNull(totalCharge); assertEquals(Long.valueOf(70), totalCharge); } - + @Test - public void testCalculateFareScenario2() { + public void whenNightSurchargeTrueAndDistanceLessThan10_thenFixFareWithNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(true); + taxiRide.setIsNightSurcharge(true); taxiRide.setDistanceInMile(5L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - assertNotNull(totalCharge); assertEquals(Long.valueOf(100), totalCharge); } @Test - public void testCalculateFareScenario3() { + public void whenNightSurchargeFalseAndDistanceLessThan100_thenDoubleFareWithoutNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(false); + taxiRide.setIsNightSurcharge(false); taxiRide.setDistanceInMile(50L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - assertNotNull(totalCharge); assertEquals(Long.valueOf(170), totalCharge); } - + @Test - public void testCalculateFareScenario4() { + public void whenNightSurchargeTrueAndDistanceLessThan100_thenDoubleFareWithNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(true); + taxiRide.setIsNightSurcharge(true); taxiRide.setDistanceInMile(50L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - assertNotNull(totalCharge); assertEquals(Long.valueOf(250), totalCharge); } - + @Test - public void testCalculateFareScenario5() { + public void whenNightSurchargeFalseAndDistanceGreaterThan100_thenExtraPercentFareWithoutNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(false); + taxiRide.setIsNightSurcharge(false); taxiRide.setDistanceInMile(100L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - assertNotNull(totalCharge); assertEquals(Long.valueOf(220), totalCharge); } - + @Test - public void testCalculateFareScenario6() { + public void whenNightSurchargeTrueAndDistanceGreaterThan100_thenExtraPercentFareWithNightSurcharge() { TaxiRide taxiRide = new TaxiRide(); - taxiRide.setbNightSurcharge(true); + taxiRide.setIsNightSurcharge(true); taxiRide.setDistanceInMile(100L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - assertNotNull(totalCharge); assertEquals(Long.valueOf(350), totalCharge); } From 19439943b76eb608aee5dfa04319f72e7f304f52 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Wed, 5 Jul 2017 12:25:40 +0100 Subject: [PATCH 3/4] BAEL-804 - minor code refactors --- spring-cloud/spring-cloud-bootstrap/gateway/pom.xml | 2 +- .../service/TaxiFareCalculatorServiceIntegrationTest.java | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml index 84dc2a6ca9..aa1cec9c39 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml @@ -4,7 +4,7 @@ 4.0.0 gateway - 1.0.0-SNAPSHOT + 1.0.0-SNAPSHOTcd parent-boot-4 diff --git a/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java b/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java index f74efe3f77..8ecd0d59a8 100644 --- a/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java +++ b/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceIntegrationTest.java @@ -18,7 +18,7 @@ import com.baeldung.spring.drools.model.TaxiRide; public class TaxiFareCalculatorServiceIntegrationTest { @Autowired - TaxiFareCalculatorService taxiFareCalculatorService; + private TaxiFareCalculatorService taxiFareCalculatorService; @Test public void whenNightSurchargeFalseAndDistanceLessThan10_thenFixFareWithoutNightSurcharge() { @@ -27,6 +27,7 @@ public class TaxiFareCalculatorServiceIntegrationTest { taxiRide.setDistanceInMile(9L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + assertNotNull(totalCharge); assertEquals(Long.valueOf(70), totalCharge); } @@ -38,6 +39,7 @@ public class TaxiFareCalculatorServiceIntegrationTest { taxiRide.setDistanceInMile(5L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + assertNotNull(totalCharge); assertEquals(Long.valueOf(100), totalCharge); } @@ -49,6 +51,7 @@ public class TaxiFareCalculatorServiceIntegrationTest { taxiRide.setDistanceInMile(50L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + assertNotNull(totalCharge); assertEquals(Long.valueOf(170), totalCharge); } @@ -60,6 +63,7 @@ public class TaxiFareCalculatorServiceIntegrationTest { taxiRide.setDistanceInMile(50L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + assertNotNull(totalCharge); assertEquals(Long.valueOf(250), totalCharge); } @@ -71,6 +75,7 @@ public class TaxiFareCalculatorServiceIntegrationTest { taxiRide.setDistanceInMile(100L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + assertNotNull(totalCharge); assertEquals(Long.valueOf(220), totalCharge); } @@ -82,6 +87,7 @@ public class TaxiFareCalculatorServiceIntegrationTest { taxiRide.setDistanceInMile(100L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + assertNotNull(totalCharge); assertEquals(Long.valueOf(350), totalCharge); } From 59216419e5d6384509f4a1320362697f5519e4ed Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Wed, 5 Jul 2017 13:15:46 +0100 Subject: [PATCH 4/4] BAEL-804 - fixing wrong pom.xml --- spring-cloud/spring-cloud-bootstrap/gateway/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml index aa1cec9c39..84dc2a6ca9 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml @@ -4,7 +4,7 @@ 4.0.0 gateway - 1.0.0-SNAPSHOTcd + 1.0.0-SNAPSHOT parent-boot-4