Review Comment Incorporation

This commit is contained in:
Mohammed Sarfaraz Khan 2017-07-04 11:07:03 +05:30 committed by mokhan
parent db2bb252de
commit 5d7e0bc531
7 changed files with 48 additions and 53 deletions

View File

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

View File

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

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

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