diff --git a/pom.xml b/pom.xml index a2c527f0a..994ff5019 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,7 @@ rules-camel-cbr rules-interview rules-interview-container + rules-interview-dtable soap-addressing soap-attachment soap-binding-rpc diff --git a/rules-interview-container/Readme.md b/rules-interview-container/Readme.md index 97e0a8970..3cc6d2270 100644 --- a/rules-interview-container/Readme.md +++ b/rules-interview-container/Readme.md @@ -1,16 +1,18 @@ Introduction ============ -This quickstart demonstrates the usage of a rules service which performs an age check. -The drl resource is specified using annotations within the InterviewRules interface. -The name of the service in the drl is specified using a Mapping annotation containing an MVEL expression. +This quickstart demonstrates the usage of a rules service which performs an age check. +The input is the incoming message content (an "Applicant"). +The output is a boolean, and mapped using the implicit "globals" Map. + +This rules-interview-container quickstart differs from the rules-interview quickstart. +A /META-INF/kmodule.xml is used, which is referenced by both the element in switchyard.xml, and the KIE/Drools container. +The rules-interview quickstart, on the other hand, manually lists the required resource. This quickstart also demonstrates how a domain property can be made available as a global variable inside your DRL. See references to ${user.name} (system property) mapping to userName (domain property) mapping to userName (global variable) inside switchyard.xml. Then look at Interview.drl and how the userName global variable is used in the DRL. -This rules-interview-container quickstart differs from the rules-interview quickstart. -A /META-INF/kmodule.xml is used, which is referenced by both the element in switchyard.xml, and the KIE/Drools container. -The rules-interview quickstart, on the other hand, manually lists the required resource. +This quickstart can be executed using either a java interface or a web service interface. ![Rules Interview Container Quickstart](https://github.com/jboss-switchyard/quickstarts/raw/master/rules-interview-container/rules-interview-container.jpg) @@ -21,24 +23,25 @@ Maven Running the quickstart ====================== - -JBoss AS 7 ----------- 1. Build the quickstart: mvn clean install 2. Run the test: + (Using java interface:) mvn -Dtest=RulesInterviewTest test + (Using web service interface:) + mvn -Dtest=WebServiceTest test + Expected Output: ================ (userName is a placeholder in this Readme.) ``` -Running org.switchyard.quickstarts.rules.interview.RulesInterviewTest -********** Twenty is a valid applicant, userName. ********** -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.131 sec` +Running org.switchyard.quickstarts.rules.interview.RulesInterviewTest +**** Twenty is an old enough applicant, userName. **** +**** Sixteen is too young of an applicant, userName. **** ``` ## Further Reading diff --git a/rules-interview-container/pom.xml b/rules-interview-container/pom.xml index 6094d590d..2b8ec2521 100644 --- a/rules-interview-container/pom.xml +++ b/rules-interview-container/pom.xml @@ -1,73 +1,95 @@ - - - 4.0.0 - - org.switchyard.quickstarts - switchyard-quickstart-parent - 1.1.0-SNAPSHOT - ../pom.xml - - switchyard-quickstart-rules-interview-container - Quickstart : Rules Interview Container - jar - - - org.switchyard - switchyard-api - - - org.switchyard - switchyard-plugin - - - org.switchyard - switchyard-test - test - - - org.switchyard.components - switchyard-component-test-mixin-cdi - test - - - org.switchyard.components - switchyard-component-rules - - - - ${project.artifactId} - - - maven-compiler-plugin - - 1.6 - 1.6 - - - - org.switchyard - switchyard-plugin - - - - configure - - - - - - + + 4.0.0 + + org.switchyard.quickstarts + switchyard-quickstart-parent + 1.1.0-SNAPSHOT + + switchyard-quickstart-rules-interview-container + Quickstart : Rules Interview (Container) + + + org.switchyard + switchyard-api + + + org.switchyard + switchyard-plugin + + + org.switchyard.components + switchyard-component-rules + + + org.switchyard + switchyard-test + test + + + org.switchyard.components + switchyard-component-test-mixin-cdi + test + + + org.switchyard.components + switchyard-component-test-mixin-http + test + + + org.switchyard + switchyard-transform + + + org.switchyard + switchyard-validate + + + org.switchyard.components + switchyard-component-soap + + + + + jboss-public-repository + JBoss Public Maven Repository + http://repository.jboss.org/nexus/content/groups/public + + + + + jboss-public-repository + JBoss Public Maven Repository + http://repository.jboss.org/nexus/content/groups/public + + + + ${project.artifactId} + + + maven-compiler-plugin + + 1.6 + 1.6 + + + + org.switchyard + switchyard-plugin + + + + configure + + + + org.switchyard.transform.config.model.TransformSwitchYardScanner + + + + + + + diff --git a/rules-interview-container/rules-interview-container.jpg b/rules-interview-container/rules-interview-container.jpg index 675aeef59..729b8266f 100644 Binary files a/rules-interview-container/rules-interview-container.jpg and b/rules-interview-container/rules-interview-container.jpg differ diff --git a/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java b/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java index 816c777e2..2c55862bd 100644 --- a/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java +++ b/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java @@ -20,7 +20,10 @@ public class Applicant { private String name; private int age; - private boolean valid; + + public Applicant() { + this("", 0); + } public Applicant(String name, int age) { setName(name); @@ -43,12 +46,4 @@ public void setAge(int age) { this.age = age; } - public boolean isValid() { - return valid; - } - - public void setValid(boolean valid) { - this.valid = valid; - } - } diff --git a/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java b/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java index 56d4c9cf3..d07257181 100644 --- a/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java +++ b/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java @@ -18,6 +18,6 @@ */ public interface Interview { - public void verify(Applicant applicant); + public boolean verify(Applicant applicant); } diff --git a/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Transformers.java b/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Transformers.java new file mode 100644 index 000000000..9a7e033c9 --- /dev/null +++ b/rules-interview-container/src/main/java/org/switchyard/quickstarts/rules/interview/Transformers.java @@ -0,0 +1,68 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.switchyard.quickstarts.rules.interview; + +import java.io.StringReader; + +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.stream.StreamSource; + +import org.switchyard.annotations.Transformer; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +/** + * @author David Ward <dward@jboss.org> © 2013 Red Hat Inc. + */ +public class Transformers { + + @Transformer(from = "{urn:switchyard-quickstart:rules-interview-container:0.1.0}verify") + public Applicant transformVerifyToApplicant(Element e) { + String name = getElementValue(e, "name"); + int age = Integer.valueOf(getElementValue(e, "age")).intValue(); + return new Applicant(name, age); + } + + @Transformer(to = "{urn:switchyard-quickstart:rules-interview-container:0.1.0}verifyResponse") + public Element transformBooleanToVerifyResponse(boolean b) { + String xml = new StringBuilder() + .append("") + .append( "").append(b).append("") + .append("") + .toString(); + return toElement(xml); + } + + private String getElementValue(Element parent, String elementName) { + String value = null; + NodeList nodes = parent.getElementsByTagName(elementName); + if (nodes.getLength() > 0) { + value = nodes.item(0).getChildNodes().item(0).getNodeValue(); + } + return value; + } + + private Element toElement(String xml) { + DOMResult dom = new DOMResult(); + try { + TransformerFactory.newInstance().newTransformer().transform(new StreamSource(new StringReader(xml)), dom); + } catch (Exception e) { + e.printStackTrace(); + } + return ((Document)dom.getNode()).getDocumentElement(); + } + +} diff --git a/rules-interview-container/src/main/resources/META-INF/Interview.wsdl b/rules-interview-container/src/main/resources/META-INF/Interview.wsdl new file mode 100644 index 000000000..f18142b1c --- /dev/null +++ b/rules-interview-container/src/main/resources/META-INF/Interview.wsdl @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rules-interview-container/src/main/resources/META-INF/switchyard.xml b/rules-interview-container/src/main/resources/META-INF/switchyard.xml index 8d49dbca8..87a2bb37f 100644 --- a/rules-interview-container/src/main/resources/META-INF/switchyard.xml +++ b/rules-interview-container/src/main/resources/META-INF/switchyard.xml @@ -1,9 +1,17 @@ - + + + + + META-INF/Interview.wsdl + :18001 + rules-interview-container + + @@ -14,6 +22,12 @@ + + + + + + @@ -22,6 +36,10 @@ + + + + diff --git a/rules-interview-container/src/main/resources/org/switchyard/quickstarts/rules/interview/Interview.drl b/rules-interview-container/src/main/resources/org/switchyard/quickstarts/rules/interview/Interview.drl index 49c6c81d8..af4bbf97d 100644 --- a/rules-interview-container/src/main/resources/org/switchyard/quickstarts/rules/interview/Interview.drl +++ b/rules-interview-container/src/main/resources/org/switchyard/quickstarts/rules/interview/Interview.drl @@ -14,19 +14,20 @@ package org.switchyard.quickstarts.rules.interview global java.lang.String userName +global java.util.Map globals rule "Is of valid age" when $a : Applicant( age > 17 ) then - $a.setValid( true ); - System.out.println("********** " + $a.getName() + " is a valid applicant, " + userName + ". **********"); + globals.put("Result", Boolean.TRUE); + System.out.println("**** " + $a.getName() + " is an old enough applicant, " + userName + ". ****"); end rule "Is not of valid age" when $a : Applicant( age < 18 ) then - $a.setValid( false ); - System.err.println("********** " + $a.getName() + " is NOT a valid applicant, " + userName + ". **********"); + globals.put("Result", Boolean.FALSE); + System.err.println("**** " + $a.getName() + " is too young of an applicant, " + userName + ". ****"); end diff --git a/rules-interview-container/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java b/rules-interview-container/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java index 099b41106..5f36cd847 100644 --- a/rules-interview-container/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java +++ b/rules-interview-container/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java @@ -13,6 +13,7 @@ */ package org.switchyard.quickstarts.rules.interview; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.switchyard.test.Invoker; @@ -31,8 +32,9 @@ public class RulesInterviewTest { private Invoker verify; @Test - public void testInterviewRules() { - verify.sendInOnly(new Applicant("Twenty", 20)); + public void testRulesInterview() { + Assert.assertEquals(Boolean.TRUE, verify.sendInOut(new Applicant("Twenty", 20)).getContent()); + Assert.assertEquals(Boolean.FALSE, verify.sendInOut(new Applicant("Sixteen", 16)).getContent()); } } diff --git a/rules-interview-container/src/test/java/org/switchyard/quickstarts/rules/interview/WebServiceTest.java b/rules-interview-container/src/test/java/org/switchyard/quickstarts/rules/interview/WebServiceTest.java new file mode 100644 index 000000000..d019d5c27 --- /dev/null +++ b/rules-interview-container/src/test/java/org/switchyard/quickstarts/rules/interview/WebServiceTest.java @@ -0,0 +1,44 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.switchyard.quickstarts.rules.interview; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.switchyard.component.test.mixins.cdi.CDIMixIn; +import org.switchyard.component.test.mixins.http.HTTPMixIn; +import org.switchyard.test.SwitchYardRunner; +import org.switchyard.test.SwitchYardTestCaseConfig; +import org.switchyard.transform.config.model.TransformSwitchYardScanner; + +/** + * @author David Ward <dward@jboss.org> © 2013 Red Hat Inc. + */ +@RunWith(SwitchYardRunner.class) +@SwitchYardTestCaseConfig( + config = SwitchYardTestCaseConfig.SWITCHYARD_XML, + scanners = TransformSwitchYardScanner.class, + mixins = {CDIMixIn.class, HTTPMixIn.class}) +public class WebServiceTest { + + private HTTPMixIn httpMixIn; + + @Test + public void testWebService() { + httpMixIn.postResourceAndTestXML("http://localhost:18001/rules-interview-container/Interview", + "/xml/soap-request-pass.xml", "/xml/soap-response-pass.xml"); + httpMixIn.postResourceAndTestXML("http://localhost:18001/rules-interview-container/Interview", + "/xml/soap-request-fail.xml", "/xml/soap-response-fail.xml"); + } + +} diff --git a/rules-interview-container/src/test/resources/xml/soap-request-fail.xml b/rules-interview-container/src/test/resources/xml/soap-request-fail.xml new file mode 100644 index 000000000..82c5a5c7a --- /dev/null +++ b/rules-interview-container/src/test/resources/xml/soap-request-fail.xml @@ -0,0 +1,11 @@ + + + + + + 16 + Sixteen + + + + diff --git a/rules-interview-container/src/test/resources/xml/soap-request-pass.xml b/rules-interview-container/src/test/resources/xml/soap-request-pass.xml new file mode 100644 index 000000000..413622074 --- /dev/null +++ b/rules-interview-container/src/test/resources/xml/soap-request-pass.xml @@ -0,0 +1,11 @@ + + + + + + 20 + Twenty + + + + diff --git a/rules-interview-container/src/test/resources/xml/soap-response-fail.xml b/rules-interview-container/src/test/resources/xml/soap-response-fail.xml new file mode 100644 index 000000000..c236ba021 --- /dev/null +++ b/rules-interview-container/src/test/resources/xml/soap-response-fail.xml @@ -0,0 +1,8 @@ + + + + + false + + + diff --git a/rules-interview-container/src/test/resources/xml/soap-response-pass.xml b/rules-interview-container/src/test/resources/xml/soap-response-pass.xml new file mode 100644 index 000000000..44a47b67f --- /dev/null +++ b/rules-interview-container/src/test/resources/xml/soap-response-pass.xml @@ -0,0 +1,8 @@ + + + + + true + + + diff --git a/rules-interview-dtable/Readme.md b/rules-interview-dtable/Readme.md new file mode 100644 index 000000000..8e0777c18 --- /dev/null +++ b/rules-interview-dtable/Readme.md @@ -0,0 +1,38 @@ +Introduction +============ +This quickstart demonstrates the usage of a rules service which performs an age check. +The rules are specified using a DTABLE resource (and XLS resourceDetail) in the manifest. +The input is the incoming message content (an "Applicant"). +The output is a boolean, and mapped using the implicit "globals" Map. + +This quickstart also demonstrates how a domain property can be made available as a global variable inside your DRL. +See references to ${user.name} (system property) mapping to userName (domain property) mapping to userName (global variable) inside switchyard.xml. +Then look at Interview.drl and how the userName global variable is used in the DRL. + +This quickstart can be executed using either a java interface or a web service interface. + +![Rules Interview Quickstart](https://github.com/jboss-switchyard/quickstarts/raw/master/rules-interview-dtable/rules-interview-dtable.jpg) + + +Preqrequisites +============== +Maven + +Running the quickstart +====================== +1. Build the quickstart: + + mvn clean install + +2. Run the test: + + (Using java interface:) + mvn -Dtest=RulesInterviewTest test + + (Using web service interface:) + mvn -Dtest=WebServiceTest test + +## Further Reading + +1. [Configuration Documentation](https://docs.jboss.org/author/display/SWITCHYARD/Configuration) +2. [Rules Service Documentation](https://docs.jboss.org/author/display/SWITCHYARD/Rules) diff --git a/rules-interview-dtable/pom.xml b/rules-interview-dtable/pom.xml new file mode 100644 index 000000000..7fa72ab8e --- /dev/null +++ b/rules-interview-dtable/pom.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + + org.switchyard.quickstarts + switchyard-quickstart-parent + 1.1.0-SNAPSHOT + + switchyard-quickstart-rules-interview-dtable + Quickstart : Rules Interview (Decision Table) + + + org.switchyard + switchyard-api + + + org.switchyard + switchyard-plugin + + + org.switchyard.components + switchyard-component-rules + + + org.switchyard + switchyard-test + test + + + org.switchyard.components + switchyard-component-test-mixin-cdi + test + + + org.switchyard.components + switchyard-component-test-mixin-http + test + + + org.switchyard + switchyard-transform + + + org.switchyard + switchyard-validate + + + org.switchyard.components + switchyard-component-soap + + + + + jboss-public-repository + JBoss Public Maven Repository + http://repository.jboss.org/nexus/content/groups/public + + + + + jboss-public-repository + JBoss Public Maven Repository + http://repository.jboss.org/nexus/content/groups/public + + + + ${project.artifactId} + + + maven-compiler-plugin + + 1.6 + 1.6 + + + + org.switchyard + switchyard-plugin + + + + configure + + + + org.switchyard.transform.config.model.TransformSwitchYardScanner + + + + + + + + diff --git a/rules-interview-dtable/rules-interview-dtable.jpg b/rules-interview-dtable/rules-interview-dtable.jpg new file mode 100644 index 000000000..729b8266f Binary files /dev/null and b/rules-interview-dtable/rules-interview-dtable.jpg differ diff --git a/rules-interview-dtable/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java b/rules-interview-dtable/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java new file mode 100644 index 000000000..2c55862bd --- /dev/null +++ b/rules-interview-dtable/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java @@ -0,0 +1,49 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.switchyard.quickstarts.rules.interview; + +/** + * @author David Ward <dward@jboss.org> © 2012 Red Hat Inc. + */ +public class Applicant { + + private String name; + private int age; + + public Applicant() { + this("", 0); + } + + public Applicant(String name, int age) { + setName(name); + setAge(age); + } + + 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; + } + +} diff --git a/rules-interview-dtable/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java b/rules-interview-dtable/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java new file mode 100644 index 000000000..d07257181 --- /dev/null +++ b/rules-interview-dtable/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java @@ -0,0 +1,23 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.switchyard.quickstarts.rules.interview; + +/** + * @author David Ward <dward@jboss.org> © 2012 Red Hat Inc. + */ +public interface Interview { + + public boolean verify(Applicant applicant); + +} diff --git a/rules-interview-dtable/src/main/java/org/switchyard/quickstarts/rules/interview/Transformers.java b/rules-interview-dtable/src/main/java/org/switchyard/quickstarts/rules/interview/Transformers.java new file mode 100644 index 000000000..d8eaf4925 --- /dev/null +++ b/rules-interview-dtable/src/main/java/org/switchyard/quickstarts/rules/interview/Transformers.java @@ -0,0 +1,68 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.switchyard.quickstarts.rules.interview; + +import java.io.StringReader; + +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.stream.StreamSource; + +import org.switchyard.annotations.Transformer; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +/** + * @author David Ward <dward@jboss.org> © 2013 Red Hat Inc. + */ +public class Transformers { + + @Transformer(from = "{urn:switchyard-quickstart:rules-interview-dtable:0.1.0}verify") + public Applicant transformVerifyToApplicant(Element e) { + String name = getElementValue(e, "name"); + int age = Integer.valueOf(getElementValue(e, "age")).intValue(); + return new Applicant(name, age); + } + + @Transformer(to = "{urn:switchyard-quickstart:rules-interview-dtable:0.1.0}verifyResponse") + public Element transformBooleanToVerifyResponse(boolean b) { + String xml = new StringBuilder() + .append("") + .append( "").append(b).append("") + .append("") + .toString(); + return toElement(xml); + } + + private String getElementValue(Element parent, String elementName) { + String value = null; + NodeList nodes = parent.getElementsByTagName(elementName); + if (nodes.getLength() > 0) { + value = nodes.item(0).getChildNodes().item(0).getNodeValue(); + } + return value; + } + + private Element toElement(String xml) { + DOMResult dom = new DOMResult(); + try { + TransformerFactory.newInstance().newTransformer().transform(new StreamSource(new StringReader(xml)), dom); + } catch (Exception e) { + e.printStackTrace(); + } + return ((Document)dom.getNode()).getDocumentElement(); + } + +} diff --git a/rules-interview-dtable/src/main/resources/META-INF/Interview.wsdl b/rules-interview-dtable/src/main/resources/META-INF/Interview.wsdl new file mode 100644 index 000000000..4f0f1a025 --- /dev/null +++ b/rules-interview-dtable/src/main/resources/META-INF/Interview.wsdl @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rules-interview-dtable/src/main/resources/META-INF/Interview.xls b/rules-interview-dtable/src/main/resources/META-INF/Interview.xls new file mode 100644 index 000000000..68fddc4e8 Binary files /dev/null and b/rules-interview-dtable/src/main/resources/META-INF/Interview.xls differ diff --git a/rules-interview-dtable/src/main/resources/META-INF/beans.xml b/rules-interview-dtable/src/main/resources/META-INF/beans.xml new file mode 100644 index 000000000..e69de29bb diff --git a/rules-interview-dtable/src/main/resources/META-INF/forge.xml b/rules-interview-dtable/src/main/resources/META-INF/forge.xml new file mode 100644 index 000000000..ba28ab345 --- /dev/null +++ b/rules-interview-dtable/src/main/resources/META-INF/forge.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/rules-interview-dtable/src/main/resources/META-INF/switchyard.xml b/rules-interview-dtable/src/main/resources/META-INF/switchyard.xml new file mode 100644 index 000000000..5e9296269 --- /dev/null +++ b/rules-interview-dtable/src/main/resources/META-INF/switchyard.xml @@ -0,0 +1,52 @@ + + + + + + + + + + META-INF/Interview.wsdl + :18001 + rules-interview-dtable + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rules-interview-dtable/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java b/rules-interview-dtable/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java new file mode 100644 index 000000000..5f36cd847 --- /dev/null +++ b/rules-interview-dtable/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.switchyard.quickstarts.rules.interview; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.switchyard.test.Invoker; +import org.switchyard.test.ServiceOperation; +import org.switchyard.test.SwitchYardRunner; +import org.switchyard.test.SwitchYardTestCaseConfig; + +/** + * @author David Ward <dward@jboss.org> © 2012 Red Hat Inc. + */ +@RunWith(SwitchYardRunner.class) +@SwitchYardTestCaseConfig(config = SwitchYardTestCaseConfig.SWITCHYARD_XML) +public class RulesInterviewTest { + + @ServiceOperation("Interview.verify") + private Invoker verify; + + @Test + public void testRulesInterview() { + Assert.assertEquals(Boolean.TRUE, verify.sendInOut(new Applicant("Twenty", 20)).getContent()); + Assert.assertEquals(Boolean.FALSE, verify.sendInOut(new Applicant("Sixteen", 16)).getContent()); + } + +} diff --git a/rules-interview-dtable/src/test/java/org/switchyard/quickstarts/rules/interview/WebServiceTest.java b/rules-interview-dtable/src/test/java/org/switchyard/quickstarts/rules/interview/WebServiceTest.java new file mode 100644 index 000000000..7e80bc3b6 --- /dev/null +++ b/rules-interview-dtable/src/test/java/org/switchyard/quickstarts/rules/interview/WebServiceTest.java @@ -0,0 +1,44 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.switchyard.quickstarts.rules.interview; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.switchyard.component.test.mixins.cdi.CDIMixIn; +import org.switchyard.component.test.mixins.http.HTTPMixIn; +import org.switchyard.test.SwitchYardRunner; +import org.switchyard.test.SwitchYardTestCaseConfig; +import org.switchyard.transform.config.model.TransformSwitchYardScanner; + +/** + * @author David Ward <dward@jboss.org> © 2013 Red Hat Inc. + */ +@RunWith(SwitchYardRunner.class) +@SwitchYardTestCaseConfig( + config = SwitchYardTestCaseConfig.SWITCHYARD_XML, + scanners = TransformSwitchYardScanner.class, + mixins = {CDIMixIn.class, HTTPMixIn.class}) +public class WebServiceTest { + + private HTTPMixIn httpMixIn; + + @Test + public void testWebService() { + httpMixIn.postResourceAndTestXML("http://localhost:18001/rules-interview-dtable/Interview", + "/xml/soap-request-pass.xml", "/xml/soap-response-pass.xml"); + httpMixIn.postResourceAndTestXML("http://localhost:18001/rules-interview-dtable/Interview", + "/xml/soap-request-fail.xml", "/xml/soap-response-fail.xml"); + } + +} diff --git a/rules-interview-dtable/src/test/resources/META-INF/beans.xml b/rules-interview-dtable/src/test/resources/META-INF/beans.xml new file mode 100644 index 000000000..e69de29bb diff --git a/rules-interview-dtable/src/test/resources/xml/soap-request-fail.xml b/rules-interview-dtable/src/test/resources/xml/soap-request-fail.xml new file mode 100644 index 000000000..c7f3ea170 --- /dev/null +++ b/rules-interview-dtable/src/test/resources/xml/soap-request-fail.xml @@ -0,0 +1,11 @@ + + + + + + 16 + Sixteen + + + + diff --git a/rules-interview-dtable/src/test/resources/xml/soap-request-pass.xml b/rules-interview-dtable/src/test/resources/xml/soap-request-pass.xml new file mode 100644 index 000000000..912667cf0 --- /dev/null +++ b/rules-interview-dtable/src/test/resources/xml/soap-request-pass.xml @@ -0,0 +1,11 @@ + + + + + + 20 + Twenty + + + + diff --git a/rules-interview-dtable/src/test/resources/xml/soap-response-fail.xml b/rules-interview-dtable/src/test/resources/xml/soap-response-fail.xml new file mode 100644 index 000000000..879f5ee00 --- /dev/null +++ b/rules-interview-dtable/src/test/resources/xml/soap-response-fail.xml @@ -0,0 +1,8 @@ + + + + + false + + + diff --git a/rules-interview-dtable/src/test/resources/xml/soap-response-pass.xml b/rules-interview-dtable/src/test/resources/xml/soap-response-pass.xml new file mode 100644 index 000000000..4981ca24e --- /dev/null +++ b/rules-interview-dtable/src/test/resources/xml/soap-response-pass.xml @@ -0,0 +1,8 @@ + + + + + true + + + diff --git a/rules-interview/Readme.md b/rules-interview/Readme.md index 7bb51799d..8bf8eeaae 100644 --- a/rules-interview/Readme.md +++ b/rules-interview/Readme.md @@ -1,13 +1,16 @@ Introduction ============ This quickstart demonstrates the usage of a rules service which performs an age check. -The drl resource is specified using annotations within the InterviewRules interface. -The name of the service in the drl is specified using a Mapping annotation containing an MVEL expression. +The rules are specified using a DRL resource in the manifest. +The input is the incoming message content (an "Applicant"). +The output is a boolean, and mapped using the implicit "globals" Map. This quickstart also demonstrates how a domain property can be made available as a global variable inside your DRL. See references to ${user.name} (system property) mapping to userName (domain property) mapping to userName (global variable) inside switchyard.xml. Then look at Interview.drl and how the userName global variable is used in the DRL. +This quickstart can be executed using either a java interface or a web service interface. + ![Rules Interview Quickstart](https://github.com/jboss-switchyard/quickstarts/raw/master/rules-interview/rules-interview.jpg) @@ -17,24 +20,25 @@ Maven Running the quickstart ====================== - -JBoss AS 7 ----------- 1. Build the quickstart: mvn clean install 2. Run the test: + (Using java interface:) mvn -Dtest=RulesInterviewTest test + (Using web service interface:) + mvn -Dtest=WebServiceTest test + Expected Output: ================ (userName is a placeholder in this Readme.) ``` Running org.switchyard.quickstarts.rules.interview.RulesInterviewTest -********** Twenty is a valid applicant, userName. ********** -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.131 sec` +**** Twenty is an old enough applicant, userName. **** +**** Sixteen is too young of an applicant, userName. **** ``` ## Further Reading diff --git a/rules-interview/pom.xml b/rules-interview/pom.xml index 48e0a1fa9..56d7405f1 100644 --- a/rules-interview/pom.xml +++ b/rules-interview/pom.xml @@ -1,73 +1,95 @@ - - - 4.0.0 - - org.switchyard.quickstarts - switchyard-quickstart-parent - 1.1.0-SNAPSHOT - ../pom.xml - - switchyard-quickstart-rules-interview - Quickstart : Rules Interview - jar - - - org.switchyard - switchyard-api - - - org.switchyard - switchyard-plugin - - - org.switchyard - switchyard-test - test - - - org.switchyard.components - switchyard-component-test-mixin-cdi - test - - - org.switchyard.components - switchyard-component-rules - - - - ${project.artifactId} - - - maven-compiler-plugin - - 1.6 - 1.6 - - - - org.switchyard - switchyard-plugin - - - - configure - - - - - - + + 4.0.0 + + org.switchyard.quickstarts + switchyard-quickstart-parent + 1.1.0-SNAPSHOT + + switchyard-quickstart-rules-interview + Quickstart : Rules Interview + + + org.switchyard + switchyard-api + + + org.switchyard + switchyard-plugin + + + org.switchyard.components + switchyard-component-rules + + + org.switchyard + switchyard-test + test + + + org.switchyard.components + switchyard-component-test-mixin-cdi + test + + + org.switchyard.components + switchyard-component-test-mixin-http + test + + + org.switchyard + switchyard-transform + + + org.switchyard + switchyard-validate + + + org.switchyard.components + switchyard-component-soap + + + + + jboss-public-repository + JBoss Public Maven Repository + http://repository.jboss.org/nexus/content/groups/public + + + + + jboss-public-repository + JBoss Public Maven Repository + http://repository.jboss.org/nexus/content/groups/public + + + + ${project.artifactId} + + + maven-compiler-plugin + + 1.6 + 1.6 + + + + org.switchyard + switchyard-plugin + + + + configure + + + + org.switchyard.transform.config.model.TransformSwitchYardScanner + + + + + + + diff --git a/rules-interview/rules-interview.jpg b/rules-interview/rules-interview.jpg index 675aeef59..729b8266f 100644 Binary files a/rules-interview/rules-interview.jpg and b/rules-interview/rules-interview.jpg differ diff --git a/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java b/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java index 816c777e2..2c55862bd 100644 --- a/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java +++ b/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Applicant.java @@ -20,7 +20,10 @@ public class Applicant { private String name; private int age; - private boolean valid; + + public Applicant() { + this("", 0); + } public Applicant(String name, int age) { setName(name); @@ -43,12 +46,4 @@ public void setAge(int age) { this.age = age; } - public boolean isValid() { - return valid; - } - - public void setValid(boolean valid) { - this.valid = valid; - } - } diff --git a/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java b/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java index 56d4c9cf3..d07257181 100644 --- a/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java +++ b/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Interview.java @@ -18,6 +18,6 @@ */ public interface Interview { - public void verify(Applicant applicant); + public boolean verify(Applicant applicant); } diff --git a/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Transformers.java b/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Transformers.java new file mode 100644 index 000000000..17d8bb16b --- /dev/null +++ b/rules-interview/src/main/java/org/switchyard/quickstarts/rules/interview/Transformers.java @@ -0,0 +1,68 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.switchyard.quickstarts.rules.interview; + +import java.io.StringReader; + +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.stream.StreamSource; + +import org.switchyard.annotations.Transformer; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +/** + * @author David Ward <dward@jboss.org> © 2013 Red Hat Inc. + */ +public class Transformers { + + @Transformer(from = "{urn:switchyard-quickstart:rules-interview:0.1.0}verify") + public Applicant transformVerifyToApplicant(Element e) { + String name = getElementValue(e, "name"); + int age = Integer.valueOf(getElementValue(e, "age")).intValue(); + return new Applicant(name, age); + } + + @Transformer(to = "{urn:switchyard-quickstart:rules-interview:0.1.0}verifyResponse") + public Element transformBooleanToVerifyResponse(boolean b) { + String xml = new StringBuilder() + .append("") + .append( "").append(b).append("") + .append("") + .toString(); + return toElement(xml); + } + + private String getElementValue(Element parent, String elementName) { + String value = null; + NodeList nodes = parent.getElementsByTagName(elementName); + if (nodes.getLength() > 0) { + value = nodes.item(0).getChildNodes().item(0).getNodeValue(); + } + return value; + } + + private Element toElement(String xml) { + DOMResult dom = new DOMResult(); + try { + TransformerFactory.newInstance().newTransformer().transform(new StreamSource(new StringReader(xml)), dom); + } catch (Exception e) { + e.printStackTrace(); + } + return ((Document)dom.getNode()).getDocumentElement(); + } + +} diff --git a/rules-interview/src/main/resources/org/switchyard/quickstarts/rules/interview/Interview.drl b/rules-interview/src/main/resources/META-INF/Interview.drl similarity index 72% rename from rules-interview/src/main/resources/org/switchyard/quickstarts/rules/interview/Interview.drl rename to rules-interview/src/main/resources/META-INF/Interview.drl index 49c6c81d8..af4bbf97d 100644 --- a/rules-interview/src/main/resources/org/switchyard/quickstarts/rules/interview/Interview.drl +++ b/rules-interview/src/main/resources/META-INF/Interview.drl @@ -14,19 +14,20 @@ package org.switchyard.quickstarts.rules.interview global java.lang.String userName +global java.util.Map globals rule "Is of valid age" when $a : Applicant( age > 17 ) then - $a.setValid( true ); - System.out.println("********** " + $a.getName() + " is a valid applicant, " + userName + ". **********"); + globals.put("Result", Boolean.TRUE); + System.out.println("**** " + $a.getName() + " is an old enough applicant, " + userName + ". ****"); end rule "Is not of valid age" when $a : Applicant( age < 18 ) then - $a.setValid( false ); - System.err.println("********** " + $a.getName() + " is NOT a valid applicant, " + userName + ". **********"); + globals.put("Result", Boolean.FALSE); + System.err.println("**** " + $a.getName() + " is too young of an applicant, " + userName + ". ****"); end diff --git a/rules-interview/src/main/resources/META-INF/Interview.wsdl b/rules-interview/src/main/resources/META-INF/Interview.wsdl new file mode 100644 index 000000000..6c6152899 --- /dev/null +++ b/rules-interview/src/main/resources/META-INF/Interview.wsdl @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rules-interview/src/main/resources/META-INF/switchyard.xml b/rules-interview/src/main/resources/META-INF/switchyard.xml index 480f75a7f..dc1755869 100644 --- a/rules-interview/src/main/resources/META-INF/switchyard.xml +++ b/rules-interview/src/main/resources/META-INF/switchyard.xml @@ -1,14 +1,22 @@ - + + + + + META-INF/Interview.wsdl + :18001 + rules-interview + + - + @@ -16,6 +24,12 @@ + + + + + + @@ -24,6 +38,10 @@ + + + + diff --git a/rules-interview/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java b/rules-interview/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java index 099b41106..5f36cd847 100644 --- a/rules-interview/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java +++ b/rules-interview/src/test/java/org/switchyard/quickstarts/rules/interview/RulesInterviewTest.java @@ -13,6 +13,7 @@ */ package org.switchyard.quickstarts.rules.interview; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.switchyard.test.Invoker; @@ -31,8 +32,9 @@ public class RulesInterviewTest { private Invoker verify; @Test - public void testInterviewRules() { - verify.sendInOnly(new Applicant("Twenty", 20)); + public void testRulesInterview() { + Assert.assertEquals(Boolean.TRUE, verify.sendInOut(new Applicant("Twenty", 20)).getContent()); + Assert.assertEquals(Boolean.FALSE, verify.sendInOut(new Applicant("Sixteen", 16)).getContent()); } } diff --git a/rules-interview/src/test/java/org/switchyard/quickstarts/rules/interview/WebServiceTest.java b/rules-interview/src/test/java/org/switchyard/quickstarts/rules/interview/WebServiceTest.java new file mode 100644 index 000000000..4d4d319e7 --- /dev/null +++ b/rules-interview/src/test/java/org/switchyard/quickstarts/rules/interview/WebServiceTest.java @@ -0,0 +1,44 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.switchyard.quickstarts.rules.interview; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.switchyard.component.test.mixins.cdi.CDIMixIn; +import org.switchyard.component.test.mixins.http.HTTPMixIn; +import org.switchyard.test.SwitchYardRunner; +import org.switchyard.test.SwitchYardTestCaseConfig; +import org.switchyard.transform.config.model.TransformSwitchYardScanner; + +/** + * @author David Ward <dward@jboss.org> © 2013 Red Hat Inc. + */ +@RunWith(SwitchYardRunner.class) +@SwitchYardTestCaseConfig( + config = SwitchYardTestCaseConfig.SWITCHYARD_XML, + scanners = TransformSwitchYardScanner.class, + mixins = {CDIMixIn.class, HTTPMixIn.class}) +public class WebServiceTest { + + private HTTPMixIn httpMixIn; + + @Test + public void testWebService() { + httpMixIn.postResourceAndTestXML("http://localhost:18001/rules-interview/Interview", + "/xml/soap-request-pass.xml", "/xml/soap-response-pass.xml"); + httpMixIn.postResourceAndTestXML("http://localhost:18001/rules-interview/Interview", + "/xml/soap-request-fail.xml", "/xml/soap-response-fail.xml"); + } + +} diff --git a/rules-interview/src/test/resources/xml/soap-request-fail.xml b/rules-interview/src/test/resources/xml/soap-request-fail.xml new file mode 100644 index 000000000..058febfe8 --- /dev/null +++ b/rules-interview/src/test/resources/xml/soap-request-fail.xml @@ -0,0 +1,11 @@ + + + + + + 16 + Sixteen + + + + \ No newline at end of file diff --git a/rules-interview/src/test/resources/xml/soap-request-pass.xml b/rules-interview/src/test/resources/xml/soap-request-pass.xml new file mode 100644 index 000000000..11a8b4abd --- /dev/null +++ b/rules-interview/src/test/resources/xml/soap-request-pass.xml @@ -0,0 +1,11 @@ + + + + + + 20 + Twenty + + + + \ No newline at end of file diff --git a/rules-interview/src/test/resources/xml/soap-response-fail.xml b/rules-interview/src/test/resources/xml/soap-response-fail.xml new file mode 100644 index 000000000..71a491aed --- /dev/null +++ b/rules-interview/src/test/resources/xml/soap-response-fail.xml @@ -0,0 +1,8 @@ + + + + + false + + + \ No newline at end of file diff --git a/rules-interview/src/test/resources/xml/soap-response-pass.xml b/rules-interview/src/test/resources/xml/soap-response-pass.xml new file mode 100644 index 000000000..0b07091db --- /dev/null +++ b/rules-interview/src/test/resources/xml/soap-response-pass.xml @@ -0,0 +1,8 @@ + + + + + true + + + \ No newline at end of file