BAEL-802: Drools using rules from Excel Files (#2115)

* Add NDC and JBoss Logging to the demo application

* NDC for Log4j, Log4j2 and JBoss Logging

* Simplify NDC example by making it a single operation instead of two

* Make NDC example as RestController, Use JBoss Logging only as a logging bridge

* Fix merge conflicts in pull request - log-mdc pom.xml updated

* BAEL-445 Update to Spring security SpEL example

* BAEL-445: Change tabs to spaces in the updated code

* BAEL-245: Add Enum Serialization exmaple

* BAEL-245: Remove the folder jackson/src/test/java/com/baeldung/jackson/dtos/withEnum as the example is not used anymore

* Add more enum serialization examples to align with previous example and prevent build fail

* BAEL-611: Minor formatting changes

* BAEL-611: Update Test case method names

* BAEL-611 Add JAX-WS client and JUnit Test

* BAEL-245: Issue 1753. Fix the typo - change from writeNumber() to writeString()

* BAEL-741: Spring Security Multiple Authentication Providers

* BAEL-741: Spring Security Multiple Authentication Providers

* Remove unnecessary change in pom.xml

* BAEL-802: Drools Excel example
This commit is contained in:
Sunil Mogadati 2017-06-20 14:34:58 -06:00 committed by Eugen
parent 0267760688
commit bfc6d480c8
6 changed files with 203 additions and 2 deletions

View File

@ -16,7 +16,7 @@
<properties>
<http-component-version>4.4.6</http-component-version>
<drools-version>7.0.0.CR1</drools-version>
<drools-version>7.1.0.Beta2</drools-version>
<apache-poi-version>3.13</apache-poi-version>
</properties>

View File

@ -1,10 +1,15 @@
package com.baeldung.drools.config;
import org.drools.decisiontable.DecisionTableProviderImpl;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.io.KieResources;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.builder.DecisionTableConfiguration;
import org.kie.internal.builder.DecisionTableInputType;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import java.io.IOException;
import java.util.Arrays;
@ -64,4 +69,39 @@ public class DroolsBeanFactory {
}
}
public KieSession getKieSession(Resource dt) {
KieFileSystem kieFileSystem = kieServices.newKieFileSystem()
.write(dt);
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem)
.buildAll();
KieRepository kieRepository = kieServices.getRepository();
ReleaseId krDefaultReleaseId = kieRepository.getDefaultReleaseId();
KieContainer kieContainer = kieServices.newKieContainer(krDefaultReleaseId);
KieSession ksession = kieContainer.newKieSession();
return ksession;
}
/*
* Can be used for debugging
* Input excelFile example: com/baeldung/drools/rules/Discount.xls
*/
public String getDrlFromExcel(String excelFile) {
DecisionTableConfiguration configuration = KnowledgeBuilderFactory.newDecisionTableConfiguration();
configuration.setInputType(DecisionTableInputType.XLS);
Resource dt = ResourceFactory.newClassPathResource(excelFile, getClass());
DecisionTableProviderImpl decisionTableProvider = new DecisionTableProviderImpl();
String drl = decisionTableProvider.loadFromResource(dt, null);
return drl;
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.drools.model;
public class Customer {
private CustomerType type;
private int years;
private int discount;
public Customer(CustomerType type, int numOfYears) {
super();
this.type = type;
this.years = numOfYears;
}
public CustomerType getType() {
return type;
}
public void setType(CustomerType type) {
this.type = type;
}
public int getYears() {
return years;
}
public void setYears(int years) {
this.years = years;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public enum CustomerType {
INDIVIDUAL, BUSINESS;
}
}

View File

@ -0,0 +1,56 @@
package com.baeldung.drools.service;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import com.baeldung.drools.config.DroolsBeanFactory;
import com.baeldung.drools.model.Customer;
import com.baeldung.drools.model.Customer.CustomerType;
public class DiscountExcelIntegrationTest {
private KieSession kSession;
@Before
public void setup() {
Resource resource = ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Discount.xls", getClass());
kSession = new DroolsBeanFactory().getKieSession(resource);
}
@Test
public void giveIndvidualLongStanding_whenFireRule_thenCorrectDiscount() throws Exception {
Customer customer = new Customer(CustomerType.INDIVIDUAL, 5);
kSession.insert(customer);
kSession.fireAllRules();
assertEquals(customer.getDiscount(), 15);
}
@Test
public void giveIndvidualRecent_whenFireRule_thenCorrectDiscount() throws Exception {
Customer customer = new Customer(CustomerType.INDIVIDUAL, 1);
kSession.insert(customer);
kSession.fireAllRules();
assertEquals(customer.getDiscount(), 5);
}
@Test
public void giveBusinessAny_whenFireRule_thenCorrectDiscount() throws Exception {
Customer customer = new Customer(CustomerType.BUSINESS, 0);
kSession.insert(customer);
kSession.fireAllRules();
assertEquals(customer.getDiscount(), 20);
}
}

View File

@ -0,0 +1,61 @@
package org.baeldung.web;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collections;
import org.baeldung.multipleauthproviders.MultipleAuthProvidersApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = MultipleAuthProvidersApplication.class)
public class MultipleAuthProvidersApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void givenMemUsers_whenGetPingWithValidUser_thenOk() {
ResponseEntity<String> result = makeRestCallToGetPing("memuser", "pass");
assertThat(result.getStatusCodeValue()).isEqualTo(200);
assertThat(result.getBody()).isEqualTo("OK");
}
@Test
public void givenExternalUsers_whenGetPingWithValidUser_thenOK() {
ResponseEntity<String> result = makeRestCallToGetPing("externaluser", "pass");
assertThat(result.getStatusCodeValue()).isEqualTo(200);
assertThat(result.getBody()).isEqualTo("OK");
}
@Test
public void givenAuthProviders_whenGetPingWithNoCred_then401() {
ResponseEntity<String> result = makeRestCallToGetPing();
assertThat(result.getStatusCodeValue()).isEqualTo(401);
}
@Test
public void givenAuthProviders_whenGetPingWithBadCred_then401() {
ResponseEntity<String> result = makeRestCallToGetPing("user", "bad_password");
assertThat(result.getStatusCodeValue()).isEqualTo(401);
}
private ResponseEntity<String> makeRestCallToGetPing(String username, String password) {
return restTemplate.withBasicAuth(username, password)
.getForEntity("/api/ping", String.class, Collections.emptyMap());
}
private ResponseEntity<String> makeRestCallToGetPing() {
return restTemplate.getForEntity("/api/ping", String.class, Collections.emptyMap());
}
}