Skip to content

Commit bfc6d48

Browse files
sunilmogadatiEugen
authored and
Eugen
committed
BAEL-802: Drools using rules from Excel Files (eugenp#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
1 parent 0267760 commit bfc6d48

File tree

6 files changed

+203
-2
lines changed

6 files changed

+203
-2
lines changed

drools/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<properties>
1818
<http-component-version>4.4.6</http-component-version>
19-
<drools-version>7.0.0.CR1</drools-version>
19+
<drools-version>7.1.0.Beta2</drools-version>
2020
<apache-poi-version>3.13</apache-poi-version>
2121
</properties>
2222

drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.baeldung.drools.config;
22

3+
import org.drools.decisiontable.DecisionTableProviderImpl;
34
import org.kie.api.KieServices;
45
import org.kie.api.builder.*;
56
import org.kie.api.io.KieResources;
7+
import org.kie.api.io.Resource;
68
import org.kie.api.runtime.KieContainer;
79
import org.kie.api.runtime.KieSession;
10+
import org.kie.internal.builder.DecisionTableConfiguration;
11+
import org.kie.internal.builder.DecisionTableInputType;
12+
import org.kie.internal.builder.KnowledgeBuilderFactory;
813
import org.kie.internal.io.ResourceFactory;
914
import java.io.IOException;
1015
import java.util.Arrays;
@@ -64,4 +69,39 @@ public KieSession getKieSession(){
6469

6570
}
6671

67-
}
72+
public KieSession getKieSession(Resource dt) {
73+
KieFileSystem kieFileSystem = kieServices.newKieFileSystem()
74+
.write(dt);
75+
76+
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem)
77+
.buildAll();
78+
79+
KieRepository kieRepository = kieServices.getRepository();
80+
81+
ReleaseId krDefaultReleaseId = kieRepository.getDefaultReleaseId();
82+
83+
KieContainer kieContainer = kieServices.newKieContainer(krDefaultReleaseId);
84+
85+
KieSession ksession = kieContainer.newKieSession();
86+
87+
return ksession;
88+
}
89+
90+
/*
91+
* Can be used for debugging
92+
* Input excelFile example: com/baeldung/drools/rules/Discount.xls
93+
*/
94+
public String getDrlFromExcel(String excelFile) {
95+
DecisionTableConfiguration configuration = KnowledgeBuilderFactory.newDecisionTableConfiguration();
96+
configuration.setInputType(DecisionTableInputType.XLS);
97+
98+
Resource dt = ResourceFactory.newClassPathResource(excelFile, getClass());
99+
100+
DecisionTableProviderImpl decisionTableProvider = new DecisionTableProviderImpl();
101+
102+
String drl = decisionTableProvider.loadFromResource(dt, null);
103+
104+
return drl;
105+
}
106+
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.drools.model;
2+
3+
public class Customer {
4+
5+
private CustomerType type;
6+
7+
private int years;
8+
9+
private int discount;
10+
11+
public Customer(CustomerType type, int numOfYears) {
12+
super();
13+
this.type = type;
14+
this.years = numOfYears;
15+
}
16+
17+
public CustomerType getType() {
18+
return type;
19+
}
20+
21+
public void setType(CustomerType type) {
22+
this.type = type;
23+
}
24+
25+
public int getYears() {
26+
return years;
27+
}
28+
29+
public void setYears(int years) {
30+
this.years = years;
31+
}
32+
33+
public int getDiscount() {
34+
return discount;
35+
}
36+
37+
public void setDiscount(int discount) {
38+
this.discount = discount;
39+
}
40+
41+
public enum CustomerType {
42+
INDIVIDUAL, BUSINESS;
43+
}
44+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.drools.service;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.kie.api.io.Resource;
8+
import org.kie.api.runtime.KieSession;
9+
import org.kie.internal.io.ResourceFactory;
10+
11+
import com.baeldung.drools.config.DroolsBeanFactory;
12+
import com.baeldung.drools.model.Customer;
13+
import com.baeldung.drools.model.Customer.CustomerType;
14+
15+
public class DiscountExcelIntegrationTest {
16+
17+
private KieSession kSession;
18+
19+
@Before
20+
public void setup() {
21+
Resource resource = ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Discount.xls", getClass());
22+
kSession = new DroolsBeanFactory().getKieSession(resource);
23+
}
24+
25+
@Test
26+
public void giveIndvidualLongStanding_whenFireRule_thenCorrectDiscount() throws Exception {
27+
Customer customer = new Customer(CustomerType.INDIVIDUAL, 5);
28+
kSession.insert(customer);
29+
30+
kSession.fireAllRules();
31+
32+
assertEquals(customer.getDiscount(), 15);
33+
}
34+
35+
@Test
36+
public void giveIndvidualRecent_whenFireRule_thenCorrectDiscount() throws Exception {
37+
38+
Customer customer = new Customer(CustomerType.INDIVIDUAL, 1);
39+
kSession.insert(customer);
40+
41+
kSession.fireAllRules();
42+
43+
assertEquals(customer.getDiscount(), 5);
44+
}
45+
46+
@Test
47+
public void giveBusinessAny_whenFireRule_thenCorrectDiscount() throws Exception {
48+
Customer customer = new Customer(CustomerType.BUSINESS, 0);
49+
kSession.insert(customer);
50+
51+
kSession.fireAllRules();
52+
53+
assertEquals(customer.getDiscount(), 20);
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.baeldung.web;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Collections;
6+
7+
import org.baeldung.multipleauthproviders.MultipleAuthProvidersApplication;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
13+
import org.springframework.boot.test.web.client.TestRestTemplate;
14+
import org.springframework.http.ResponseEntity;
15+
import org.springframework.test.context.junit4.SpringRunner;
16+
17+
@RunWith(SpringRunner.class)
18+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = MultipleAuthProvidersApplication.class)
19+
public class MultipleAuthProvidersApplicationTests {
20+
@Autowired
21+
private TestRestTemplate restTemplate;
22+
23+
@Test
24+
public void givenMemUsers_whenGetPingWithValidUser_thenOk() {
25+
ResponseEntity<String> result = makeRestCallToGetPing("memuser", "pass");
26+
27+
assertThat(result.getStatusCodeValue()).isEqualTo(200);
28+
assertThat(result.getBody()).isEqualTo("OK");
29+
}
30+
31+
@Test
32+
public void givenExternalUsers_whenGetPingWithValidUser_thenOK() {
33+
ResponseEntity<String> result = makeRestCallToGetPing("externaluser", "pass");
34+
35+
assertThat(result.getStatusCodeValue()).isEqualTo(200);
36+
assertThat(result.getBody()).isEqualTo("OK");
37+
}
38+
39+
@Test
40+
public void givenAuthProviders_whenGetPingWithNoCred_then401() {
41+
ResponseEntity<String> result = makeRestCallToGetPing();
42+
43+
assertThat(result.getStatusCodeValue()).isEqualTo(401);
44+
}
45+
46+
@Test
47+
public void givenAuthProviders_whenGetPingWithBadCred_then401() {
48+
ResponseEntity<String> result = makeRestCallToGetPing("user", "bad_password");
49+
50+
assertThat(result.getStatusCodeValue()).isEqualTo(401);
51+
}
52+
53+
private ResponseEntity<String> makeRestCallToGetPing(String username, String password) {
54+
return restTemplate.withBasicAuth(username, password)
55+
.getForEntity("/api/ping", String.class, Collections.emptyMap());
56+
}
57+
58+
private ResponseEntity<String> makeRestCallToGetPing() {
59+
return restTemplate.getForEntity("/api/ping", String.class, Collections.emptyMap());
60+
}
61+
}

0 commit comments

Comments
 (0)