diff --git a/sources/examples/muContext-play-with-filters/pom.xml b/sources/examples/muContext-play-with-filters/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..57f795fbe8cde196684e500b0a00bb7b9a4b823b --- /dev/null +++ b/sources/examples/muContext-play-with-filters/pom.xml @@ -0,0 +1,147 @@ +<!-- +This file is part of the INCOME platform. + +The INCOME platform is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The INCOME platform is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the INCOME platform. If not, see <http://www.gnu.org/licenses/>. + +Initial developer(s): Denis Conan +Contributor(s): +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>muContext</groupId> + <artifactId>muContext-play-with-filters</artifactId> + <name>The module for playing with filters of muContext</name> + <packaging>jar</packaging> + <version>0.15.3</version> + + <description> + Since the design of JavaScript muContext filters may be + non-trivial, this module is for you to play with filters before + inserting them in applications' modules. + </description> + + <repositories> + <repository> + <id>muDEBS-release</id> + <name>muDEBS's release repository</name> + <url>https://fusionforge.int-evry.fr/www/mudebs/maven-repository/release/</url> + </repository> + <repository> + <id>muContext-release</id> + <name>muContext's release repository</name> + <url>https://fusionforge.int-evry.fr/www/mucontext/maven-repository/release/</url> + </repository> + <repository> + <id>qocim-release</id> + <name>qocim's release repository</name> + <url>https://fusionforge.int-evry.fr/www/qocim/maven-repository/release/</url> + </repository> + <repository> + <id>qocim-snapshot</id> + <name>qocim's snapshotrelease repository</name> + <url>https://fusionforge.int-evry.fr/www/qocim/maven-repository/snapshot/</url> + </repository> + </repositories> + + <dependencies> + <dependency> + <groupId>muContext.data-model</groupId> + <artifactId>muContext-data-model-context</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>muContext.data-model</groupId> + <artifactId>muContext-data-model-contract</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>muContext.data-model</groupId> + <artifactId>muContext-data-model-multiscalability</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>muContext.data-model</groupId> + <artifactId>muContext-data-model-recommendation</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.10</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <!-- surefire configuration --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <excludes> + <exclude> + **/Util*.java + </exclude> + </excludes> + </configuration> + </plugin> + <!-- jar package generation configuration --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>jar</goal> + <goal>test-jar</goal> + </goals> + </execution> + </executions> + <configuration> + <archive> + <index>true</index> + <manifest> + <addClasspath>true</addClasspath> + </manifest> + <manifestEntries> + <mode>alpha</mode> + <url>${project.url}</url> + <key>value</key> + <Implementation-Vendor>Institut Mines Télécom, Télécom SudParis</Implementation-Vendor> + <Implementation-Version>${parent-version}</Implementation-Version> + <Implementation-Title>${project.name}</Implementation-Title> + </manifestEntries> + </archive> + </configuration> + </plugin> + <!-- javadoc generation configuration: add overview --> + <plugin> + <artifactId>maven-javadoc-plugin</artifactId> + <configuration> + <overview>${basedir}/src/main/javadoc/overview.html</overview> + </configuration> + </plugin> + </plugins> + </build> + + <reporting> + </reporting> +</project> diff --git a/sources/examples/muContext-play-with-filters/src/main/javadoc/overview.html b/sources/examples/muContext-play-with-filters/src/main/javadoc/overview.html new file mode 100644 index 0000000000000000000000000000000000000000..6cf055dff41fdb071802cf7dada851e4bdb2d3ba --- /dev/null +++ b/sources/examples/muContext-play-with-filters/src/main/javadoc/overview.html @@ -0,0 +1,33 @@ +<!-- +This file is part of the INCOME platform. + +The INCOME platform is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The INCOME platform is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the INCOME platform. If not, see <http://www.gnu.org/licenses/>. + +Initial developer(s): Denis Conan +Contributor(s): +--> + +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> + +<html> +<head> +</head> +<body> + +Since the design of <tt>JavaScript</tt> muContext filters may be +non-trivial, this module is for you to play with filters before +inserting them in applications' modules. + +</body> +</html> diff --git a/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MEDistUrLocation.java b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MEDistUrLocation.java new file mode 100644 index 0000000000000000000000000000000000000000..10172f5038c3bd0e404ed403b1b9ca812edeb0c3 --- /dev/null +++ b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MEDistUrLocation.java @@ -0,0 +1,142 @@ +/** +This file is part of the muContext middleware. + +Copyright (C) 2014-2017 Télécom SudParis + +The INCOME platform is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The INCOME platform is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the INCOME platform. If not, see <http://www.gnu.org/licenses/>. + +Initial developer(s): Denis Conan +Contributor(s): + */ + +package mucontext.playwithfilters; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Vector; + +import javax.script.ScriptException; +import javax.xml.bind.JAXBException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; + +import mucontext.datamodel.context.ContextDataModelFacade; +import mucontext.datamodel.context.ContextEntity; +import mucontext.datamodel.context.ContextObservable; +import mucontext.datamodel.context.ContextObservation; +import mucontext.datamodel.context.ContextReport; +import mucontext.datamodel.context.EntityRelationType; +import mucontext.datamodel.multiscalability.Dimension; +import mucontext.datamodel.multiscalability.EventDistributionRange; +import mucontext.datamodel.multiscalability.Measure; +import mucontext.datamodel.multiscalability.MultiscalabilityDataModelFacade; +import mucontext.datamodel.multiscalability.MultiscalabilityReport; +import mucontext.datamodel.multiscalability.ProjectionValue; +import mucontext.datamodel.multiscalability.Scale; +import mucontext.datamodel.multiscalability.ScaleSet; +import mucontext.datamodel.multiscalability.Viewpoint; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + +public class TestBe4MEDistUrLocation { + /** + * the subscription and advertisement filter. + */ + private static String filterToTest = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('//observable/name', doc, XPathConstants.NODE);" + + "if (n1 == null) return false;" + + "if (!n1.getTextContent().equalsIgnoreCase('presencebusrfidtag')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n2 = xpath.evaluate('//entity/name', n1, XPathConstants.NODE);" + + "if (n2 == null) return null;" + + "if (!n2.getTextContent().startsWith('rfidtagbus')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n3 = xpath.evaluate('//scale/name', doc, XPathConstants.NODE);" + + "if (n3 == null) return false;" + + "if (!n3.getTextContent().equalsIgnoreCase('Universe encompassing all the worlds')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n4 = xpath.evaluate('//measure/value', n3, XPathConstants.NODE);" + + "if (n4 == null) return false;" + + "if (!n4.getTextContent().equalsIgnoreCase('UNIVERSE')) return false;" + "return true;}"; + + @Test + public void testWithUniverse() throws URISyntaxException, JAXBException, ScriptException, + ParserConfigurationException, SAXException, IOException, XPathExpressionException { + ContextDataModelFacade facade = new ContextDataModelFacade("facade"); + String uriPath = "mucontext://localhost:3000"; + ContextEntity userEntity = facade.createContextEntity("Leon", new URI(uriPath + "/user/Leon")); + ContextEntity busEntity = facade.createContextEntity("rfidtagbus367", new URI(uriPath + + "/rfidtag/bus367")); + EntityRelationType inbustype = facade.createEntityRelationType("inbustype"); + Vector<ContextEntity> entities = new Vector<ContextEntity>(); + entities.add(userEntity); + entities.add(busEntity); + facade.createBinaryEntityRelation("inbus", inbustype, entities); + MultiscalabilityDataModelFacade mfacade = new MultiscalabilityDataModelFacade("facade"); + Viewpoint viewpoint = mfacade.CreateViewpoint("Distributed" + " Event-Based Systems (DEBS)"); + Dimension dimension = mfacade.createDimension("Event Distribution Range (EDR)", viewpoint); + Measure<?> measure = mfacade.createMeasure("EDR measure (EDRM)", EventDistributionRange.UNIVERSE); + ScaleSet scaleset = mfacade.createScaleSet("EDR" + " in EDRM scaleset", dimension, measure); + Scale scale = mfacade.createScale("Universe encompassing all the worlds", scaleset); + ProjectionValue projectionvalue = mfacade.createProjectionValue("pv", scale); + MultiscalabilityReport mr = mfacade.createMultiscalabilityReport("mreport"); + mfacade.addProjectionValue(mr, projectionvalue); + ContextObservable observable = facade.createContextObservable("presencebusrfidtag", new URI(uriPath + + "/presence/bus367/rfidtag"), busEntity); + ContextObservation<?> observation1 = facade.createContextObservation("o", new Integer(1), observable); + ContextReport report = facade.createContextReport("report"); + facade.addMultiscalabilityReport(report, mr); + facade.addContextObservation(report, observation1); + String serialised = facade.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest, serialised)); + } + + @Test + public void testWithWorld() throws URISyntaxException, JAXBException, ScriptException, + ParserConfigurationException, SAXException, IOException, XPathExpressionException { + ContextDataModelFacade facade = new ContextDataModelFacade("facade"); + String uriPath = "mucontext://localhost:3000"; + ContextEntity userEntity = facade.createContextEntity("Leon", new URI(uriPath + "/user/Leon")); + ContextEntity busEntity = facade.createContextEntity("rfidtagbus367", new URI(uriPath + + "/rfidtag/bus367")); + EntityRelationType inbustype = facade.createEntityRelationType("inbustype"); + Vector<ContextEntity> entities = new Vector<ContextEntity>(); + entities.add(userEntity); + entities.add(busEntity); + facade.createBinaryEntityRelation("inbus", inbustype, entities); + MultiscalabilityDataModelFacade mfacade = new MultiscalabilityDataModelFacade("facade"); + Viewpoint viewpoint = mfacade.CreateViewpoint("Distributed" + " Event-Based Systems (DEBS)"); + Dimension dimension = mfacade.createDimension("Event Distribution Range (EDR)", viewpoint); + Measure<?> measure = mfacade.createMeasure("EDR measure (EDRM)", EventDistributionRange.WORLD); + ScaleSet scaleset = mfacade.createScaleSet("EDR" + " in EDRM scaleset", dimension, measure); + Scale scale = mfacade.createScale("Local DEBS world", scaleset); + ProjectionValue projectionvalue = mfacade.createProjectionValue("pv", scale); + MultiscalabilityReport mr = mfacade.createMultiscalabilityReport("mreport"); + mfacade.addProjectionValue(mr, projectionvalue); + ContextObservable observable = facade.createContextObservable("presencebusrfidtag", new URI(uriPath + + "/presence/bus367/rfidtag"), busEntity); + ContextObservation<?> observation1 = facade.createContextObservation("o", new Integer(1), observable); + ContextReport report = facade.createContextReport("report"); + facade.addMultiscalabilityReport(report, mr); + facade.addContextObservation(report, observation1); + String serialised = facade.serialiseToXML(report); + Assert.assertFalse(Util.evaluate(filterToTest, serialised)); + } +} diff --git a/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MEIncident.java b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MEIncident.java new file mode 100644 index 0000000000000000000000000000000000000000..7e81ba5e0f2c578561fe04343d068c8c1e8d462e --- /dev/null +++ b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MEIncident.java @@ -0,0 +1,100 @@ +/** +This file is part of the muContext middleware. + +Copyright (C) 2014-2017 Télécom SudParis + +The INCOME platform is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The INCOME platform is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the INCOME platform. If not, see <http://www.gnu.org/licenses/>. + +Initial developer(s): Denis Conan +Contributor(s): + */ + +package mucontext.playwithfilters; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +import javax.script.ScriptException; +import javax.xml.bind.JAXBException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; + +import mucontext.datamodel.context.ContextDataModelFacade; +import mucontext.datamodel.context.ContextEntity; +import mucontext.datamodel.context.ContextObservable; +import mucontext.datamodel.context.ContextObservation; +import mucontext.datamodel.context.ContextReport; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + +public class TestBe4MEIncident { + /** + * the filter to test. + */ + private String filterToTest = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('/contextReport', doc, XPathConstants.NODE);" + + "if (n1 == null) return false;" + + "var n2 = xpath.evaluate('//observable/name', n1, XPathConstants.NODE);" + + "if (!n2.getTextContent().equalsIgnoreCase('incident')) return false;" + + "var n3 = xpath.evaluate('//observation/value', n1, XPathConstants.NODE);" + + "if (n3 == null) return false;" + + "return n3.getTextContent().equalsIgnoreCase('ongoing');" + + "}"; + + @Test + public void testOngoingIncident() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + ContextDataModelFacade facade = new ContextDataModelFacade("facade"); + String uriPath = "mucontext://localhost:3005"; + String segment = "Caubiere-IUT"; + ContextEntity entity = facade.createContextEntity(segment, new URI( + uriPath + "/bus/" + segment)); + ContextObservable observable = facade.createContextObservable( + "incident", new URI(uriPath + "/bus/" + segment + "/incident"), + entity); + ContextObservation<?> observation1 = facade.createContextObservation( + "o", "ongoing", observable); + ContextReport report = facade.createContextReport("report"); + facade.addContextObservation(report, observation1); + String serialised = facade.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest, serialised)); + } + + @Test + public void testNoIncident() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + ContextDataModelFacade facade = new ContextDataModelFacade("facade"); + String uriPath = "mucontext://localhost:3005"; + String segment = "Caubiere-IUT"; + ContextEntity entity = facade.createContextEntity(segment, new URI( + uriPath + "/bus/" + segment)); + ContextObservable observable = facade.createContextObservable( + "incident", new URI(uriPath + "/bus/" + segment + "/incident"), + entity); + ContextObservation<?> observation1 = facade.createContextObservation( + "o", "none", observable); + ContextReport report = facade.createContextReport("report"); + facade.addContextObservation(report, observation1); + String serialised = facade.serialiseToXML(report); + Assert.assertFalse(Util.evaluate(filterToTest, serialised)); + } +} diff --git a/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MEItinerary.java b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MEItinerary.java new file mode 100644 index 0000000000000000000000000000000000000000..8c2ca4541f10283cc020292ffa3d0152a9ec85f8 --- /dev/null +++ b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MEItinerary.java @@ -0,0 +1,159 @@ +/** +This file is part of the muContext middleware. + +Copyright (C) 2014-2017 Télécom SudParis + +The INCOME platform is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The INCOME platform is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the INCOME platform. If not, see <http://www.gnu.org/licenses/>. + +Initial developer(s): Denis Conan +Contributor(s): + */ + +package mucontext.playwithfilters; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +import javax.script.ScriptException; +import javax.xml.bind.JAXBException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; + +import mucontext.datamodel.context.ContextDataModelFacade; +import mucontext.datamodel.context.ContextEntity; +import mucontext.datamodel.context.ContextObservable; +import mucontext.datamodel.context.ContextObservation; +import mucontext.datamodel.context.ContextReport; +import mucontext.datamodel.feedback.Feedback; +import mucontext.datamodel.feedback.FeedbackDataModelFacade; +import mucontext.datamodel.feedback.FeedbackReport; +import mucontext.datamodel.recommendation.Cause; +import mucontext.datamodel.recommendation.Proposition; +import mucontext.datamodel.recommendation.Recommendation; +import mucontext.datamodel.recommendation.RecommendationDataModelFacade; +import mucontext.datamodel.recommendation.RecommendationReport; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + +public class TestBe4MEItinerary { + /** + * the first filter to test. + */ + private String filterToTest1 = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('/contextReport', doc, XPathConstants.NODE);" + + "if (n1 == null) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n2 = xpath.evaluate('//observable/name', doc, XPathConstants.NODE);" + + "return n2.getTextContent().equalsIgnoreCase('incident');}"; + + /** + * the second filter to test. + */ + private String filterToTest2 = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('/feedbackReport', doc, XPathConstants.NODE);" + + "return (n1 != null);}"; + + /** + * the third filter to test. + */ + private String filterToTest3 = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('/recommendationReport', doc, XPathConstants.NODE);" + + "return (n1 != null);}"; + + @Test + public void testFirstFilter() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + ContextDataModelFacade facade = new ContextDataModelFacade("facade"); + String uriPath = "mucontext://localhost:3005"; + String segment = "Caubiere-IUT"; + ContextEntity entity = facade.createContextEntity(segment, new URI( + uriPath + "/bus/" + segment)); + ContextObservable observable = facade.createContextObservable( + "incident", new URI(uriPath + "/bus/" + segment + "/incident"), + entity); + ContextObservation<?> observation1 = facade.createContextObservation( + "o", "ongoing", observable); + ContextReport report = facade.createContextReport("report"); + facade.addContextObservation(report, observation1); + String serialised = facade.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest1, serialised)); + } + + @Test + public void testSecondFilter() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + FeedbackDataModelFacade facadeFeedback = new FeedbackDataModelFacade( + "facadeFeedBack"); + RecommendationDataModelFacade facadeRecommendation = new RecommendationDataModelFacade( + "facade"); + ContextDataModelFacade facadeContext = new ContextDataModelFacade( + "facadeContext"); + ContextEntity entity = facadeContext.createContextEntity("Sophie", + new URI("mucontext://localhost:3000/user/Sophie")); + Proposition<?> proposition = facadeRecommendation.createProposition( + "Itinerary", new Integer(1)); + Feedback feedback = facadeFeedback.createFeedback("itineraryFeedback", + proposition, entity); + FeedbackReport report = facadeFeedback + .createFeedbackReport("feedbackReport"); + facadeFeedback.addFeedback(report, feedback); + String serialised = facadeFeedback.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest2, serialised)); + } + + @Test + public void testThirdFilter() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + ContextDataModelFacade facadeC = new ContextDataModelFacade("facade"); + String uriPath = "mucontext://localhost:3005"; + String segment = "Caubiere-IUT"; + ContextEntity entity = facadeC.createContextEntity(segment, new URI( + uriPath + "/bus/" + segment)); + ContextObservable observable = facadeC.createContextObservable( + "incident", new URI(uriPath + "/bus/" + segment + "/incident"), + entity); + ContextObservation<?> observation1 = facadeC.createContextObservation( + "o", "ongoing", observable); + ContextReport creport = facadeC.createContextReport("report"); + facadeC.addContextObservation(creport, observation1); + RecommendationDataModelFacade facade = new RecommendationDataModelFacade( + "facadeRecommendation"); + Cause cause = facade.createCause("incident", creport); + Recommendation recommendation = facade + .createRecommendation("alternativeItineraries"); + facade.addCause(recommendation, cause); + facade.addProposition(recommendation, + facade.createProposition("Itinerary", new Integer(1))); + RecommendationReport report = facade + .createRecommendationReport("recommendationReport"); + facade.addRecommendation(report, recommendation); + String serialised = facade.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest3, serialised)); + } +} diff --git a/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MELocateAndRecommendU.java b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MELocateAndRecommendU.java new file mode 100644 index 0000000000000000000000000000000000000000..2f19b8ed8f6129ddced99564588cfc11e6879e69 --- /dev/null +++ b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MELocateAndRecommendU.java @@ -0,0 +1,180 @@ +/** +This file is part of the muContext middleware. + +Copyright (C) 2014-2017 Télécom SudParis + +The INCOME platform is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The INCOME platform is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the INCOME platform. If not, see <http://www.gnu.org/licenses/>. + +Initial developer(s): Denis Conan +Contributor(s): + */ + +package mucontext.playwithfilters; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Vector; + +import javax.script.ScriptException; +import javax.xml.bind.JAXBException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; + +import mucontext.datamodel.context.ContextDataModelFacade; +import mucontext.datamodel.context.ContextEntity; +import mucontext.datamodel.context.ContextObservable; +import mucontext.datamodel.context.ContextObservation; +import mucontext.datamodel.context.ContextReport; +import mucontext.datamodel.context.EntityRelationType; +import mucontext.datamodel.feedback.Feedback; +import mucontext.datamodel.feedback.FeedbackDataModelFacade; +import mucontext.datamodel.feedback.FeedbackReport; +import mucontext.datamodel.recommendation.Cause; +import mucontext.datamodel.recommendation.Proposition; +import mucontext.datamodel.recommendation.Recommendation; +import mucontext.datamodel.recommendation.RecommendationDataModelFacade; +import mucontext.datamodel.recommendation.RecommendationReport; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + +public class TestBe4MELocateAndRecommendU { + /** + * the first filter to test. + */ + private String filterToTest1 = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('//observable/name', doc, XPathConstants.NODE);" + + "if (n1 == null) return false;" + + "if (!n1.getTextContent().equalsIgnoreCase('locationItinerary')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n2 = xpath.evaluate('//relation/name', n1, XPathConstants.NODE);" + + "if (n2 == null) return false;" + + "if (!n2.getTextContent().equalsIgnoreCase('locatedat')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n3 = xpath.evaluate('//entity', n2, XPathConstants.NODESET);" + + "if (n3 == null) return false;" + + "for (var i = 0; i < n3.getLength(); i++) {" + + " if(n3.item(i).getTextContent().contains('user')) {return true;}" + + "}" + "return false;" + "}"; + /** + * the second filter to test. + */ + private String filterToTest2 = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('/recommendationReport', doc, XPathConstants.NODE);" + + "return (n1 != null);}"; + /** + * the third filter to test. + */ + private String filterToTest3 = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('/feedbackReport', doc, XPathConstants.NODE);" + + "return (n1 != null);}"; + + @Test + public void testFirstFilter() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + String uriPath = "mucontext://localhost:3001"; + String userName = "Chantal"; + String busLine = "2"; + String busNumber = "327"; + ContextDataModelFacade facade = new ContextDataModelFacade("facade"); + ContextEntity userEntity = facade.createContextEntity(userName, + new URI(uriPath + "/user/" + userName)); + ContextEntity busLineEntity = facade.createContextEntity(busLine, + new URI(uriPath + "/bus/line/" + busNumber + + "/UniversitéPaulSabatier")); + EntityRelationType inbustype = facade + .createEntityRelationType("locatedattype"); + Vector<ContextEntity> entities = new Vector<ContextEntity>(); + entities.add(userEntity); + entities.add(busLineEntity); + facade.createBinaryEntityRelation("locatedat", inbustype, entities); + ContextObservable observable = facade.createContextObservable( + "locationItinerary", new URI(uriPath + "/location/busline/" + + busNumber + "/UniversitéPaulSabatier"), + busLineEntity); + ContextReport report = null; + report = facade.createContextReport("report"); + ContextObservation<?> toPublish = facade.createContextObservation("o", + "Pastourelles", observable); + facade.addContextObservation(report, toPublish); + String serialised = facade.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest1, serialised)); + } + + @Test + public void testSecondFilter() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + ContextDataModelFacade facadeC = new ContextDataModelFacade("facade"); + String uriPath = "mucontext://localhost:3005"; + String segment = "Caubiere-IUT"; + ContextEntity entity = facadeC.createContextEntity(segment, + new URI(uriPath + "/bus/" + segment)); + ContextObservable observable = facadeC.createContextObservable( + "incident", new URI(uriPath + "/bus/" + segment + "/incident"), + entity); + ContextObservation<?> observation1 = facadeC + .createContextObservation("o", "ongoing", observable); + ContextReport creport = facadeC.createContextReport("report"); + facadeC.addContextObservation(creport, observation1); + RecommendationDataModelFacade facade = new RecommendationDataModelFacade( + "facadeRecommendation"); + Cause cause = facade.createCause("incident", creport); + Recommendation recommendation = facade + .createRecommendation("alternativeItineraries"); + facade.addCause(recommendation, cause); + facade.addProposition(recommendation, + facade.createProposition("Itinerary", new Integer(1))); + RecommendationReport report = facade + .createRecommendationReport("recommendationReport"); + facade.addRecommendation(report, recommendation); + String serialised = facade.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest2, serialised)); + } + + @Test + public void testThirdFilter() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + FeedbackDataModelFacade facadeFeedback = new FeedbackDataModelFacade( + "facadeFeedBack"); + RecommendationDataModelFacade facadeRecommendation = new RecommendationDataModelFacade( + "facade"); + ContextDataModelFacade facadeContext = new ContextDataModelFacade( + "facadeContext"); + ContextEntity entity = facadeContext.createContextEntity("Sophie", + new URI("mucontext://localhost:3000/user/Sophie")); + Proposition<?> proposition = facadeRecommendation + .createProposition("Itinerary", new Integer(1)); + Feedback feedback = facadeFeedback.createFeedback("itineraryFeedback", + proposition, entity); + FeedbackReport report = facadeFeedback + .createFeedbackReport("feedbackReport"); + facadeFeedback.addFeedback(report, feedback); + String serialised = facadeFeedback.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest3, serialised)); + } +} diff --git a/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MERFIDTag.java b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MERFIDTag.java new file mode 100644 index 0000000000000000000000000000000000000000..bf3314ab0e023fe6467aa63c5d2908890c0a0d9f --- /dev/null +++ b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4MERFIDTag.java @@ -0,0 +1,164 @@ +/** +This file is part of the muContext middleware. + +Copyright (C) 2014-2017 Télécom SudParis + +The INCOME platform is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The INCOME platform is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the INCOME platform. If not, see <http://www.gnu.org/licenses/>. + +Initial developer(s): Denis Conan +Contributor(s): + */ + +package mucontext.playwithfilters; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Vector; + +import javax.script.ScriptException; +import javax.xml.bind.JAXBException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; + +import mucontext.datamodel.context.ContextDataModelFacade; +import mucontext.datamodel.context.ContextEntity; +import mucontext.datamodel.context.ContextObservable; +import mucontext.datamodel.context.ContextObservation; +import mucontext.datamodel.context.ContextReport; +import mucontext.datamodel.context.EntityRelationType; +import mucontext.datamodel.multiscalability.Dimension; +import mucontext.datamodel.multiscalability.EventDistributionRange; +import mucontext.datamodel.multiscalability.Measure; +import mucontext.datamodel.multiscalability.MultiscalabilityDataModelFacade; +import mucontext.datamodel.multiscalability.MultiscalabilityReport; +import mucontext.datamodel.multiscalability.ProjectionValue; +import mucontext.datamodel.multiscalability.Scale; +import mucontext.datamodel.multiscalability.ScaleSet; +import mucontext.datamodel.multiscalability.Viewpoint; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + +public class TestBe4MERFIDTag { + /** + * the filter to test. + */ + private static String filterToTest = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('//observable/name', doc, XPathConstants.NODE);" + + "if (!n1.getTextContent().equalsIgnoreCase('presencebusrfidtag')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n2 = xpath.evaluate('//entity/name', n1, XPathConstants.NODE);" + + "if (!n2.getTextContent().startsWith('rfidtagbus')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n3 = xpath.evaluate('//scale/name', doc, XPathConstants.NODE);" + + "if (!n3.getTextContent().equalsIgnoreCase('Local DEBS world')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n4 = xpath.evaluate('//measure/value', n3, XPathConstants.NODE);" + + "if (!n4.getTextContent().equalsIgnoreCase('WORLD')) return false;" + + "return true;}"; + + @Test + public void testWithUniverse() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + ContextDataModelFacade facade = new ContextDataModelFacade("facade"); + String uriPath = "mucontext://localhost:3000"; + ContextEntity userEntity = facade.createContextEntity("Leon", + new URI(uriPath + "/user/Leon")); + ContextEntity busEntity = facade.createContextEntity("rfidtagbus367", + new URI(uriPath + "/rfidtag/bus367")); + EntityRelationType inbustype = facade + .createEntityRelationType("inbustype"); + Vector<ContextEntity> entities = new Vector<ContextEntity>(); + entities.add(userEntity); + entities.add(busEntity); + facade.createBinaryEntityRelation("inbus", inbustype, entities); + MultiscalabilityDataModelFacade mfacade = new MultiscalabilityDataModelFacade( + "facade"); + Viewpoint viewpoint = mfacade + .CreateViewpoint("Distributed" + " Event-Based Systems (DEBS)"); + Dimension dimension = mfacade + .createDimension("Event Distribution Range (EDR)", viewpoint); + Measure<?> measure = mfacade.createMeasure("EDR measure (EDRM)", + EventDistributionRange.UNIVERSE); + ScaleSet scaleset = mfacade.createScaleSet("EDR" + " in EDRM scaleset", + dimension, measure); + Scale scale = mfacade + .createScale("Universe encompassing all the worlds", scaleset); + ProjectionValue projectionvalue = mfacade.createProjectionValue("pv", + scale); + MultiscalabilityReport mr = mfacade + .createMultiscalabilityReport("mreport"); + mfacade.addProjectionValue(mr, projectionvalue); + ContextObservable observable = facade.createContextObservable( + "presencebusrfidtag", + new URI(uriPath + "/presence/bus367/rfidtag"), busEntity); + ContextObservation<?> observation1 = facade + .createContextObservation("o", new Integer(1), observable); + ContextReport report = facade.createContextReport("report"); + facade.addMultiscalabilityReport(report, mr); + facade.addContextObservation(report, observation1); + String serialised = facade.serialiseToXML(report); + Assert.assertFalse(Util.evaluate(filterToTest, serialised)); + } + + @Test + public void testWithWorld() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + ContextDataModelFacade facade = new ContextDataModelFacade("facade"); + String uriPath = "mucontext://localhost:3000"; + ContextEntity userEntity = facade.createContextEntity("Leon", + new URI(uriPath + "/user/Leon")); + ContextEntity busEntity = facade.createContextEntity("rfidtagbus367", + new URI(uriPath + "/rfidtag/bus367")); + EntityRelationType inbustype = facade + .createEntityRelationType("inbustype"); + Vector<ContextEntity> entities = new Vector<ContextEntity>(); + entities.add(userEntity); + entities.add(busEntity); + facade.createBinaryEntityRelation("inbus", inbustype, entities); + MultiscalabilityDataModelFacade mfacade = new MultiscalabilityDataModelFacade( + "facade"); + Viewpoint viewpoint = mfacade + .CreateViewpoint("Distributed" + " Event-Based Systems (DEBS)"); + Dimension dimension = mfacade + .createDimension("Event Distribution Range (EDR)", viewpoint); + Measure<?> measure = mfacade.createMeasure("EDR measure (EDRM)", + EventDistributionRange.WORLD); + ScaleSet scaleset = mfacade.createScaleSet("EDR" + " in EDRM scaleset", + dimension, measure); + Scale scale = mfacade.createScale("Local DEBS world", scaleset); + ProjectionValue projectionvalue = mfacade.createProjectionValue("pv", + scale); + MultiscalabilityReport mr = mfacade + .createMultiscalabilityReport("mreport"); + mfacade.addProjectionValue(mr, projectionvalue); + ContextObservable observable = facade.createContextObservable( + "presencebusrfidtag", + new URI(uriPath + "/presence/bus367/rfidtag"), busEntity); + ContextObservation<?> observation1 = facade + .createContextObservation("o", new Integer(1), observable); + ContextReport report = facade.createContextReport("report"); + facade.addMultiscalabilityReport(report, mr); + facade.addContextObservation(report, observation1); + String serialised = facade.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest, serialised)); + } +} diff --git a/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4METagUrLocation.java b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4METagUrLocation.java new file mode 100644 index 0000000000000000000000000000000000000000..dd7a09fb8e17e56ed591d5a3df3b7c38fcf72d77 --- /dev/null +++ b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/TestBe4METagUrLocation.java @@ -0,0 +1,137 @@ +/** +This file is part of the muContext middleware. + +Copyright (C) 2014-2017 Télécom SudParis + +The INCOME platform is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The INCOME platform is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the INCOME platform. If not, see <http://www.gnu.org/licenses/>. + +Initial developer(s): Denis Conan +Contributor(s): + */ + +package mucontext.playwithfilters; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Vector; + +import javax.script.ScriptException; +import javax.xml.bind.JAXBException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; + +import mucontext.datamodel.context.ContextDataModelFacade; +import mucontext.datamodel.context.ContextEntity; +import mucontext.datamodel.context.ContextObservable; +import mucontext.datamodel.context.ContextObservation; +import mucontext.datamodel.context.ContextReport; +import mucontext.datamodel.context.EntityRelationType; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + +public class TestBe4METagUrLocation { + /** + * the first filter to test. + */ + private String filterToTest1 = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('//observable/name', doc, XPathConstants.NODE);" + + "if (n1 == null) return false;" + + "if (!n1.getTextContent().equalsIgnoreCase('presencebusrfidtag')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n2 = xpath.evaluate('//entity/name', n1, XPathConstants.NODE);" + + "if (n2 == null) return false;" + + "if (!n2.getTextContent().startsWith('rfidtagbus')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n3 = xpath.evaluate('//relation/name', n2, XPathConstants.NODE);" + + "if (n3 == null) return false;" + + "return n3.getTextContent().equalsIgnoreCase('inbus');}"; + /** + * the second filter to test. + */ + private String filterToTest2 = "function evaluate(doc) {" + + "load(\"nashorn:mozilla_compat.js\");" + + "importPackage(javax.xml.xpath);" + + "var xpath = XPathFactory.newInstance().newXPath();" + + "var n1 = xpath.evaluate('//observable/name', doc, XPathConstants.NODE);" + + "if (!n1.getTextContent().equalsIgnoreCase('locationItinerary')) return false;" + + "xpath = XPathFactory.newInstance().newXPath();" + + "var n2 = xpath.evaluate('//relation/name', n1, XPathConstants.NODE);" + + "return n2.getTextContent().equalsIgnoreCase('locatedat');}"; + + @Test + public void testFirstFilter() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + ContextDataModelFacade facade = new ContextDataModelFacade("facade"); + String uriPath = "mucontext://localhost:3000"; + ContextEntity userEntity = facade.createContextEntity("Leon", + new URI(uriPath + "/user/Leon")); + ContextEntity busEntity = facade.createContextEntity("rfidtagbus367", + new URI(uriPath + "/rfidtag/bus367")); + EntityRelationType inbustype = facade + .createEntityRelationType("inbustype"); + Vector<ContextEntity> entities = new Vector<ContextEntity>(); + entities.add(userEntity); + entities.add(busEntity); + facade.createBinaryEntityRelation("inbus", inbustype, entities); + ContextObservable observable = facade.createContextObservable( + "presencebusrfidtag", + new URI(uriPath + "/presence/bus367/rfidtag"), busEntity); + ContextObservation<?> observation1 = facade + .createContextObservation("o", new Integer(1), observable); + ContextReport report = facade.createContextReport("report"); + facade.addContextObservation(report, observation1); + String serialised = facade.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest1, serialised)); + } + + @Test + public void testSecondFilter() throws URISyntaxException, JAXBException, + ScriptException, ParserConfigurationException, SAXException, + IOException, XPathExpressionException { + String uriPath = "mucontext://localhost:3001"; + String userName = "Chantal"; + String busLine = "2"; + String busNumber = "327"; + ContextDataModelFacade facade = new ContextDataModelFacade("facade"); + ContextEntity userEntity = facade.createContextEntity(userName, + new URI(uriPath + "/user/" + userName)); + ContextEntity busLineEntity = facade.createContextEntity(busLine, + new URI(uriPath + "/bus/line/" + busNumber + + "/UniversitéPaulSabatier")); + EntityRelationType inbustype = facade + .createEntityRelationType("locatedattype"); + Vector<ContextEntity> entities = new Vector<ContextEntity>(); + entities.add(userEntity); + entities.add(busLineEntity); + facade.createBinaryEntityRelation("locatedat", inbustype, entities); + ContextObservable observable = facade.createContextObservable( + "locationItinerary", new URI(uriPath + "/location/busline/" + + busNumber + "/UniversitéPaulSabatier"), + busLineEntity); + ContextReport report = null; + report = facade.createContextReport("report"); + ContextObservation<?> toPublish = facade.createContextObservation("o", + "Pastourelles", observable); + facade.addContextObservation(report, toPublish); + String serialised = facade.serialiseToXML(report); + Assert.assertTrue(Util.evaluate(filterToTest2, serialised)); + } +} diff --git a/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/Util.java b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/Util.java new file mode 100644 index 0000000000000000000000000000000000000000..fd82c86b7625ad56d06226522e15769396693b51 --- /dev/null +++ b/sources/examples/muContext-play-with-filters/src/test/java/mucontext/playwithfilters/Util.java @@ -0,0 +1,58 @@ +/** +This file is part of the muContext middleware. + +Copyright (C) 2014-2015 Télécom SudParis + +The INCOME platform is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The INCOME platform is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the INCOME platform. If not, see <http://www.gnu.org/licenses/>. + +Initial developer(s): Denis Conan +Contributor(s): + */ + +package mucontext.playwithfilters; + +import java.io.IOException; +import java.io.StringReader; + +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import mudebs.common.filters.JavaScriptRoutingFilter; + +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class Util { + static boolean evaluate(final String filterToTest, final String serialised) throws ScriptException, + ParserConfigurationException, SAXException, IOException { + ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); + ScriptEngine engine = scriptEngineManager.getEngineByName("JavaScript"); + engine.eval(filterToTest); + Invocable inv = (Invocable) engine; + JavaScriptRoutingFilter mf = inv.getInterface(JavaScriptRoutingFilter.class); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputSource is = new InputSource(); + is.setCharacterStream(new StringReader(serialised)); + Document doc = builder.parse(is); + + return mf.evaluate(doc); + } +}