diff --git a/spring-drools/pom.xml b/spring-drools/pom.xml index 8dba1271d7..de2ccd882b 100644 --- a/spring-drools/pom.xml +++ b/spring-drools/pom.xml @@ -16,7 +16,7 @@ 1.1.1.RELEASE 4.0-alpha6 - 7.0.0.CR1 + 7.0.0.Final 3.13 @@ -86,7 +86,7 @@ org.kie kie-spring - 6.4.0.Final + 7.0.0.Final org.springframework diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/Application.java b/spring-drools/src/main/java/com/baeldung/spring/drools/Application.java deleted file mode 100644 index 8f5b915f88..0000000000 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/Application.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.spring.drools; - - -import com.baeldung.spring.drools.model.Applicant; -import com.baeldung.spring.drools.model.Product; -import com.baeldung.spring.drools.model.SuggestedRole; -import com.baeldung.spring.drools.service.ApplicantService; -import com.baeldung.spring.drools.service.ProductService; -import org.kie.api.KieServices; -import org.kie.api.runtime.KieContainer; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Import; - -@SpringBootApplication -@Import(DroolConfiguration.class) -public class Application { - - public static void main(String[] args) { - ApplicationContext ctx = SpringApplication.run(Application.class, args); - ApplicantService applicantService=(ApplicantService)ctx.getBean("applicantService"); - applicantService.suggestARoleForApplicant(new Applicant("Baljeet",37,1200000.0,8),new SuggestedRole()); - - ProductService productService=(ProductService)ctx.getBean("productService"); - Product returnedProduct=productService.applyLabelToProduct(new Product("Microwave","Book")); - } - -} diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/DroolConfiguration.java b/spring-drools/src/main/java/com/baeldung/spring/drools/DroolConfiguration.java deleted file mode 100644 index 7a5e833ed5..0000000000 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/DroolConfiguration.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.baeldung.spring.drools; - -import com.baeldung.spring.drools.service.ApplicantService; -import com.baeldung.spring.drools.service.ProductService; -import org.kie.api.KieBase; -import org.kie.api.KieServices; -import org.kie.api.builder.*; -import org.kie.api.runtime.KieContainer; -import org.kie.api.runtime.KieSession; -import org.kie.internal.io.ResourceFactory; -import org.kie.spring.KModuleBeanFactoryPostProcessor; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.ResourcePatternResolver; - -import java.io.IOException; - -@Configuration -public class DroolConfiguration { - - private static final String RULES_PATH = "com/baeldung/spring/drools/rules/"; - - @Bean - public KieFileSystem kieFileSystem() throws IOException { - KieFileSystem kieFileSystem = getKieServices().newKieFileSystem(); - for (Resource file : getRuleFiles()) { - kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8")); - } - return kieFileSystem; - } - - private Resource[] getRuleFiles() throws IOException { - ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); - return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*"); - } - - @Bean - public KieContainer kieContainer() throws IOException { - final KieRepository kieRepository = getKieServices().getRepository(); - - kieRepository.addKieModule(new KieModule() { - public ReleaseId getReleaseId() { - return kieRepository.getDefaultReleaseId(); - } - }); - - KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem()); - kieBuilder.buildAll(); - - return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId()); - } - - private KieServices getKieServices() { - return KieServices.Factory.get(); - } - - @Bean - public KieBase kieBase() throws IOException { - return kieContainer().getKieBase(); - } - - @Bean - public KieSession kieSession() throws IOException { - return kieContainer().newKieSession(); - } - - @Bean - public ApplicantService getApplicantService(){ - return new ApplicantService(); - } - - @Bean - public ProductService getProductService(KieContainer kieContainer){ - return new ProductService(); - } - - @Bean - public KModuleBeanFactoryPostProcessor kiePostProcessor() { - return new KModuleBeanFactoryPostProcessor(); - } -} \ No newline at end of file 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 new file mode 100644 index 0000000000..e8f926cc6f --- /dev/null +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/app/ApplicationRunner.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.drools.app; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.spring.drools.model.TaxiRide; +import com.baeldung.spring.drools.model.Fare; +import com.baeldung.spring.drools.service.TaxiFareCalculatorService; +import com.baeldung.spring.drools.service.TaxiFareConfiguration; + +public class ApplicationRunner { + + public static void main(String[] args) { + ApplicationContext context = new AnnotationConfigApplicationContext(TaxiFareConfiguration.class); + TaxiFareCalculatorService orderService = (TaxiFareCalculatorService) context.getBean(TaxiFareCalculatorService.class); + + TaxiRide taxiRide = new TaxiRide(); + taxiRide.setbNightSurcharge(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/Applicant.java b/spring-drools/src/main/java/com/baeldung/spring/drools/model/Applicant.java deleted file mode 100644 index f28d0fcab7..0000000000 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/model/Applicant.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.spring.drools.model; - -public class Applicant { - - private String name; - private int age; - private double currentSalary; - private int experienceInYears; - - public Applicant(String name, int age, Double currentSalary, int experienceInYears) { - this.name = name; - this.age = age; - this.currentSalary = currentSalary; - this.experienceInYears = experienceInYears; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public Double getCurrentSalary() { - return currentSalary; - } - - public void setCurrentSalary(Double currentSalary) { - this.currentSalary = currentSalary; - } - - public int getExperienceInYears() { - return experienceInYears; - } - - public void setExperienceInYears(int experienceInYears) { - this.experienceInYears = experienceInYears; - } -} 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 new file mode 100644 index 0000000000..a67156f167 --- /dev/null +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java @@ -0,0 +1,39 @@ +package com.baeldung.spring.drools.model; + +public class Fare { + + private Long nightSurcharge; + private Long rideFare; + private Long totalFare; + + public Fare() { + nightSurcharge = 0L; + rideFare = 0L; + totalFare = 0L; + } + + public Long getNightSurcharge() { + return nightSurcharge; + } + + public void setNightSurcharge(Long nightSurcharge) { + this.nightSurcharge = nightSurcharge; + } + + public Long getRideFare() { + return rideFare; + } + + public void setRideFare(Long rideFare) { + this.rideFare = rideFare; + } + + public Long getTotalFare() { + return nightSurcharge + rideFare; + } + + public void setTotalFare(Long totalFare) { + this.totalFare = totalFare; + } + +} diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/model/Product.java b/spring-drools/src/main/java/com/baeldung/spring/drools/model/Product.java deleted file mode 100644 index a861df5834..0000000000 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/model/Product.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.spring.drools.model; - -public class Product { - private String name; - private String type; - - private String label; - - public Product(String name, String type) { - this.name = name; - this.type = type; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - -} diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/model/SuggestedRole.java b/spring-drools/src/main/java/com/baeldung/spring/drools/model/SuggestedRole.java deleted file mode 100644 index 1ace5aa4ae..0000000000 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/model/SuggestedRole.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.spring.drools.model; - -public class SuggestedRole { - - private String role; - - public String getRole() { - return role; - } - - public void setRole(String role) { - this.role = role; - } -} 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 new file mode 100644 index 0000000000..6dc08bbb60 --- /dev/null +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/model/TaxiRide.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.drools.model; + +public class TaxiRide { + + private Boolean bNightSurcharge; + private Long distanceInMile; + + public Boolean getbNightSurcharge() { + return bNightSurcharge; + } + + public void setbNightSurcharge(Boolean bNightSurcharge) { + this.bNightSurcharge = bNightSurcharge; + } + + public Long getDistanceInMile() { + return distanceInMile; + } + + public void setDistanceInMile(Long distanceInMile) { + this.distanceInMile = distanceInMile; + } + +} diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/service/ApplicantService.java b/spring-drools/src/main/java/com/baeldung/spring/drools/service/ApplicantService.java deleted file mode 100644 index f78ffe668b..0000000000 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/service/ApplicantService.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.baeldung.spring.drools.service; - -import com.baeldung.spring.drools.model.Applicant; -import com.baeldung.spring.drools.model.SuggestedRole; -import org.kie.api.runtime.KieContainer; -import org.kie.api.runtime.KieSession; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class ApplicantService { - - @Autowired - private KieContainer kieContainer; - - public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole){ - KieSession kieSession = kieContainer.newKieSession(); - kieSession.insert(applicant); - kieSession.setGlobal("suggestedRole",suggestedRole); - kieSession.fireAllRules(); - System.out.println(suggestedRole.getRole()); - return suggestedRole; - - } -} diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/service/ProductService.java b/spring-drools/src/main/java/com/baeldung/spring/drools/service/ProductService.java deleted file mode 100644 index 21ca9c9730..0000000000 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/service/ProductService.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.spring.drools.service; - -import com.baeldung.spring.drools.model.Product; -import org.kie.api.runtime.KieContainer; -import org.kie.api.runtime.KieSession; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class ProductService { - - @Autowired - private KieContainer kieContainer; - - public Product applyLabelToProduct(Product product){ - KieSession kieSession = kieContainer.newKieSession(); - kieSession.insert(product); - kieSession.fireAllRules(); - System.out.println(product.getLabel()); - return product; - - } - -} 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 new file mode 100644 index 0000000000..4279643339 --- /dev/null +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareCalculatorService.java @@ -0,0 +1,22 @@ +package com.baeldung.spring.drools.service; + +import org.kie.api.runtime.KieSession; +import org.springframework.beans.factory.annotation.Autowired; + +import com.baeldung.spring.drools.model.Fare; +import com.baeldung.spring.drools.model.TaxiRide; + +public class TaxiFareCalculatorService { + + @Autowired + private KieSession kieSession; + + public Long calculateFare(TaxiRide taxiRide, Fare rideFare) { + kieSession.setGlobal("rideFare", rideFare); + kieSession.insert(taxiRide); + kieSession.fireAllRules(); + kieSession.dispose(); + System.out.println("!! RIDE FARE !! " + rideFare.getTotalFare()); + return rideFare.getTotalFare(); + } +} 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 new file mode 100644 index 0000000000..69c910f84d --- /dev/null +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java @@ -0,0 +1,39 @@ +package com.baeldung.spring.drools.service; + +import org.kie.api.KieServices; +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.Configuration; + +@Configuration +public class TaxiFareConfiguration { + + public static final String drlFile = "TAXI_FARE_RULE.drl"; + + @Bean + public KieSession kieSession() { + KieServices kieServices = KieServices.Factory.get(); + + KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); + kieFileSystem.write(ResourceFactory.newClassPathResource(drlFile)); + KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem); + kieBuilder.buildAll(); + KieModule kieModule = kieBuilder.getKieModule(); + + KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId()); + KieSession ksession = kContainer.newKieSession(); + + return ksession; + } + + @Bean + public TaxiFareCalculatorService taxiFareCalculatorService() { + TaxiFareCalculatorService taxiFareCalculatorService = new TaxiFareCalculatorService(); + return taxiFareCalculatorService; + } +} diff --git a/spring-drools/src/main/resources/TAXI_FARE_RULE.drl b/spring-drools/src/main/resources/TAXI_FARE_RULE.drl new file mode 100644 index 0000000000..56335c08b3 --- /dev/null +++ b/spring-drools/src/main/resources/TAXI_FARE_RULE.drl @@ -0,0 +1,57 @@ +import com.baeldung.spring.drools.model.TaxiRide; +import com.baeldung.spring.drools.model.Fare; +import java.util.*; + +global com.baeldung.spring.drools.model.Fare rideFare; +dialect "mvel" + +rule "Calculate Taxi Fare - Scenario 1" + when + taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile < 10); + then + rideFare.setNightSurcharge(0); + rideFare.setRideFare(70); +end + +rule "Calculate Taxi Fare - Scenario 2" + when + taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile < 10); + then + rideFare.setNightSurcharge(30); + rideFare.setRideFare(70); +end + + +rule "Calculate Taxi Fare - Scenario 3" + when + taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile >= 10 && distanceInMile < 100); + then + rideFare.setNightSurcharge(0); + rideFare.setRideFare(70+(2*taxiRideInstance.getDistanceInMile())); +end + + +rule "Calculate Taxi Fare - Scenario 4" + when + taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile >= 10 && distanceInMile < 100); + then + rideFare.setNightSurcharge(30+taxiRideInstance.getDistanceInMile()); + rideFare.setRideFare(70+(2*taxiRideInstance.getDistanceInMile())); +end + + +rule "Calculate Taxi Fare - Scenario 5" + when + taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile >= 100); + then + rideFare.setNightSurcharge(0); + rideFare.setRideFare(70+(1.5*taxiRideInstance.getDistanceInMile())); +end + +rule "Calculate Taxi Fare - Scenario 6" + when + taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile >= 100); + then + rideFare.setNightSurcharge(30+taxiRideInstance.getDistanceInMile()); + rideFare.setRideFare(70+(1.5*taxiRideInstance.getDistanceInMile())); +end diff --git a/spring-drools/src/main/resources/com/baeldung/spring/drools/rules/Product_rules.xls b/spring-drools/src/main/resources/com/baeldung/spring/drools/rules/Product_rules.xls deleted file mode 100644 index 24caaf2470..0000000000 Binary files a/spring-drools/src/main/resources/com/baeldung/spring/drools/rules/Product_rules.xls and /dev/null differ diff --git a/spring-drools/src/main/resources/com/baeldung/spring/drools/rules/SuggestApplicant.drl b/spring-drools/src/main/resources/com/baeldung/spring/drools/rules/SuggestApplicant.drl deleted file mode 100644 index baaf29d0b4..0000000000 --- a/spring-drools/src/main/resources/com/baeldung/spring/drools/rules/SuggestApplicant.drl +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.spring.drools.rules; - -import com.baeldung.spring.drools.model.Applicant; - -global com.baeldung.spring.drools.model.SuggestedRole suggestedRole; - -dialect "mvel" - -rule "Suggest Manager Role" - when - Applicant(experienceInYears > 10) - Applicant(currentSalary > 1000000 && currentSalary <= 2500000) - then - suggestedRole.setRole("Manager"); -end - -rule "Suggest Senior developer Role" - when - Applicant(experienceInYears > 5 && experienceInYears <= 10) - Applicant(currentSalary > 500000 && currentSalary <= 1500000) - then - suggestedRole.setRole("Senior developer"); -end - -rule "Suggest Developer Role" - when - Applicant(experienceInYears > 0 && experienceInYears <= 5) - Applicant(currentSalary > 200000 && currentSalary <= 1000000) - then - suggestedRole.setRole("Developer"); -end diff --git a/spring-drools/src/test/java/com/baeldung/spring/drools/service/ApplicantServiceIntegrationTest.java b/spring-drools/src/test/java/com/baeldung/spring/drools/service/ApplicantServiceIntegrationTest.java deleted file mode 100644 index d7e7634c00..0000000000 --- a/spring-drools/src/test/java/com/baeldung/spring/drools/service/ApplicantServiceIntegrationTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.baeldung.spring.drools.service; - -import com.baeldung.spring.drools.Application; -import com.baeldung.spring.drools.DroolConfiguration; -import com.baeldung.spring.drools.model.Applicant; -import com.baeldung.spring.drools.model.SuggestedRole; -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 junit.framework.Assert.assertNull; -import static junit.framework.TestCase.assertEquals; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = {DroolConfiguration.class}) -public class ApplicantServiceIntegrationTest { - - @Autowired - ApplicantService applicantService; - - @Test - public void whenCriteriaMatching_ThenSuggestManagerRole(){ - Applicant applicant=new Applicant("Davis",37,1600000.0,11); - SuggestedRole suggestedRole=new SuggestedRole(); - applicantService.suggestARoleForApplicant(applicant,suggestedRole); - assertEquals("Manager",suggestedRole.getRole()); - } - - @Test - public void whenCriteriaMatching_ThenSuggestSeniorDeveloperRole(){ - Applicant applicant=new Applicant("John",37,1200000.0,8); - SuggestedRole suggestedRole=new SuggestedRole(); - applicantService.suggestARoleForApplicant(applicant,suggestedRole); - assertEquals("Senior developer",suggestedRole.getRole()); - } - @Test - public void whenCriteriaMatching_ThenSuggestDeveloperRole(){ - Applicant applicant=new Applicant("Davis",37,800000.0,3); - SuggestedRole suggestedRole=new SuggestedRole(); - applicantService.suggestARoleForApplicant(applicant,suggestedRole); - assertEquals("Developer",suggestedRole.getRole()); - } - @Test - public void whenCriteriaNotMatching_ThenNoRole(){ - Applicant applicant=new Applicant("John",37,1200000.0,5); - SuggestedRole suggestedRole=new SuggestedRole(); - applicantService.suggestARoleForApplicant(applicant,suggestedRole); - assertNull(suggestedRole.getRole()); - } -} diff --git a/spring-drools/src/test/java/com/baeldung/spring/drools/service/ProductServiceIntegrationTest.java b/spring-drools/src/test/java/com/baeldung/spring/drools/service/ProductServiceIntegrationTest.java deleted file mode 100644 index 7a442e48fd..0000000000 --- a/spring-drools/src/test/java/com/baeldung/spring/drools/service/ProductServiceIntegrationTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.spring.drools.service; - -import com.baeldung.spring.drools.Application; -import com.baeldung.spring.drools.DroolConfiguration; -import com.baeldung.spring.drools.model.Product; -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 junit.framework.TestCase.assertEquals; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = {DroolConfiguration.class}) -public class ProductServiceIntegrationTest { - - @Autowired - ProductService productService; - - @Test - public void whenProductTypeElectronic_ThenLabelBarcode(){ - Product product=new Product("Microwave","Electronic"); - product=productService.applyLabelToProduct(product); - assertEquals("BarCode",product.getLabel()); - } - - @Test - public void whenProductTypeBook_ThenLabelIsbn(){ - Product product=new Product("AutoBiography","Book"); - product=productService.applyLabelToProduct(product); - assertEquals("ISBN",product.getLabel()); - } -} diff --git a/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceTest.java b/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceTest.java new file mode 100644 index 0000000000..b8e105a785 --- /dev/null +++ b/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceTest.java @@ -0,0 +1,95 @@ +package com.baeldung.spring.drools.service; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.spring.drools.model.TaxiRide; +import com.baeldung.spring.drools.model.Fare; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class) +public class TaxiFareCalculatorServiceTest { + + ApplicationContext context = null; + TaxiFareCalculatorService taxiFareCalculatorService = null; + + @Before + public void init() { + context = new AnnotationConfigApplicationContext(TaxiFareConfiguration.class); + taxiFareCalculatorService = (TaxiFareCalculatorService) context.getBean(TaxiFareCalculatorService.class); + } + + @Test + public void testCalculateFareScenario1() { + TaxiRide taxiRide = new TaxiRide(); + taxiRide.setbNightSurcharge(false); + taxiRide.setDistanceInMile(9L); + Fare rideFare = new Fare(); + Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(70), totalCharge); + } + + @Test + public void testCalculateFareScenario2() { + TaxiRide taxiRide = new TaxiRide(); + taxiRide.setbNightSurcharge(true); + taxiRide.setDistanceInMile(5L); + Fare rideFare = new Fare(); + Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(100), totalCharge); + } + + @Test + public void testCalculateFareScenario3() { + TaxiRide taxiRide = new TaxiRide(); + taxiRide.setbNightSurcharge(false); + taxiRide.setDistanceInMile(50L); + Fare rideFare = new Fare(); + Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(170), totalCharge); + } + + @Test + public void testCalculateFareScenario4() { + TaxiRide taxiRide = new TaxiRide(); + taxiRide.setbNightSurcharge(true); + taxiRide.setDistanceInMile(50L); + Fare rideFare = new Fare(); + Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(250), totalCharge); + } + + @Test + public void testCalculateFareScenario5() { + TaxiRide taxiRide = new TaxiRide(); + taxiRide.setbNightSurcharge(false); + taxiRide.setDistanceInMile(100L); + Fare rideFare = new Fare(); + Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(220), totalCharge); + } + + @Test + public void testCalculateFareScenario6() { + TaxiRide taxiRide = new TaxiRide(); + taxiRide.setbNightSurcharge(true); + taxiRide.setDistanceInMile(100L); + Fare rideFare = new Fare(); + Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); + Assert.assertNotNull(totalCharge); + Assert.assertEquals(Long.valueOf(350), totalCharge); + } + +}