Review Comment Incorporation

This commit is contained in:
Mohammed Sarfaraz Khan 2017-07-04 11:07:03 +05:30
parent db2bb252de
commit a0d82c25e3
7 changed files with 54 additions and 58 deletions

View File

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

View File

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

View File

@ -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() {

View File

@ -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();

View File

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

View File

@ -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()));

View File

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