commit
79d9649c41
|
@ -12,13 +12,12 @@ 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,4 +29,5 @@ public class Fare {
|
|||
public Long getTotalFare() {
|
||||
return nightSurcharge + rideFare;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()));
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
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)
|
||||
|
@ -20,9 +21,9 @@ public class TaxiFareCalculatorServiceIntegrationTest {
|
|||
private 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);
|
||||
|
@ -30,11 +31,11 @@ public class TaxiFareCalculatorServiceIntegrationTest {
|
|||
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);
|
||||
|
@ -44,9 +45,9 @@ public class TaxiFareCalculatorServiceIntegrationTest {
|
|||
}
|
||||
|
||||
@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);
|
||||
|
@ -54,11 +55,11 @@ public class TaxiFareCalculatorServiceIntegrationTest {
|
|||
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);
|
||||
|
@ -66,11 +67,11 @@ public class TaxiFareCalculatorServiceIntegrationTest {
|
|||
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);
|
||||
|
@ -78,11 +79,11 @@ public class TaxiFareCalculatorServiceIntegrationTest {
|
|||
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);
|
||||
|
|
Loading…
Reference in New Issue