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); } }