diff --git a/drools/pom.xml b/drools/pom.xml index b352044fb8..29231f150c 100644 --- a/drools/pom.xml +++ b/drools/pom.xml @@ -16,7 +16,7 @@ 4.4.6 - 7.0.0.CR1 + 7.1.0.Beta2 3.13 diff --git a/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java b/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java index 5f0d619813..e8841b05e2 100644 --- a/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java +++ b/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java @@ -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 { } -} \ No newline at end of file + 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; + } + +} diff --git a/drools/src/main/java/com/baeldung/drools/model/Customer.java b/drools/src/main/java/com/baeldung/drools/model/Customer.java new file mode 100644 index 0000000000..e900612b55 --- /dev/null +++ b/drools/src/main/java/com/baeldung/drools/model/Customer.java @@ -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; + } +} diff --git a/drools/src/main/resources/com/baeldung/drools/rules/Discount.xls b/drools/src/main/resources/com/baeldung/drools/rules/Discount.xls new file mode 100644 index 0000000000..198cc8dcda Binary files /dev/null and b/drools/src/main/resources/com/baeldung/drools/rules/Discount.xls differ diff --git a/drools/src/test/java/com/baeldung/drools/service/DiscountExcelIntegrationTest.java b/drools/src/test/java/com/baeldung/drools/service/DiscountExcelIntegrationTest.java new file mode 100644 index 0000000000..c3c4ebd4f8 --- /dev/null +++ b/drools/src/test/java/com/baeldung/drools/service/DiscountExcelIntegrationTest.java @@ -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); + } + +} diff --git a/spring-security-mvc-boot/src/test/java/org/baeldung/web/MultipleAuthProvidersApplicationTests.java b/spring-security-mvc-boot/src/test/java/org/baeldung/web/MultipleAuthProvidersApplicationTests.java new file mode 100644 index 0000000000..0d70cd64e4 --- /dev/null +++ b/spring-security-mvc-boot/src/test/java/org/baeldung/web/MultipleAuthProvidersApplicationTests.java @@ -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 result = makeRestCallToGetPing("memuser", "pass"); + + assertThat(result.getStatusCodeValue()).isEqualTo(200); + assertThat(result.getBody()).isEqualTo("OK"); + } + + @Test + public void givenExternalUsers_whenGetPingWithValidUser_thenOK() { + ResponseEntity result = makeRestCallToGetPing("externaluser", "pass"); + + assertThat(result.getStatusCodeValue()).isEqualTo(200); + assertThat(result.getBody()).isEqualTo("OK"); + } + + @Test + public void givenAuthProviders_whenGetPingWithNoCred_then401() { + ResponseEntity result = makeRestCallToGetPing(); + + assertThat(result.getStatusCodeValue()).isEqualTo(401); + } + + @Test + public void givenAuthProviders_whenGetPingWithBadCred_then401() { + ResponseEntity result = makeRestCallToGetPing("user", "bad_password"); + + assertThat(result.getStatusCodeValue()).isEqualTo(401); + } + + private ResponseEntity makeRestCallToGetPing(String username, String password) { + return restTemplate.withBasicAuth(username, password) + .getForEntity("/api/ping", String.class, Collections.emptyMap()); + } + + private ResponseEntity makeRestCallToGetPing() { + return restTemplate.getForEntity("/api/ping", String.class, Collections.emptyMap()); + } +}