diff --git a/CHANGELOG.md b/CHANGELOG.md index 88a9f8c..743055d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.2.8 (29/07/2016) +- Added request routing feature + # 0.2.7 (24/07/2016) - Added 'Called Times' feature sets - More Bug fixes and fixing failing tests diff --git a/README.md b/README.md index 1f1a421..fadd76e 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ WSSimulator is an open source library that easily allows for you to simulate ext **Getting Started** * Java 8 -* The dependency from JCenter or the Standalone [distro](https://github.com/CognitiveJ/wssimulator/releases/download/0.2.7/wssimulator-0.2.7.zip "Download Standalone Version") +* The dependency from JCenter or the Standalone [distro](https://github.com/CognitiveJ/wssimulator/releases/download/0.2.8/wssimulator-0.2.8.zip "Download Standalone Version") To simulate web service calls, you first need to describe the simulation. This process is very easy as Simulations are created in a YAML format and you don't need to 'simulate' much to get start as the only required field for you to define is _path_ and WSSimulator will default the other options. @@ -32,7 +32,8 @@ httpMethod: post successResponseCode:201 badRequestResponseCode: 500 response: ok -requestStructure: +request: + requestStructure: @@ -53,7 +54,7 @@ There are 2 ways to use WSSimulator; as a standalone application or as an embedd WSSimulator is packaged here and supports been executed on both *nix & windows systems; * For *nix systems - - Unizip the ws-simulator-0.2.7.zip file to a local directory + - Unizip the ws-simulator-0.2.8.zip file to a local directory - Then Call: ```shell @@ -61,7 +62,7 @@ WSSimulator is packaged here and supports been executed on both *nix & windows s ``` * For Windows - - Unizip the ws-simulator-0.2.7.zip file to a local directory + - Unizip the ws-simulator-0.2.8.zip file to a local directory - Then Call: ```shell ./wssimulator.bat @@ -95,7 +96,7 @@ repositories { } dependencies { - compile "cognitivej:wssimulator:0.2.7" + compile "cognitivej:wssimulator:0.2.8" ... } @@ -106,7 +107,7 @@ repositories { cognitivej wssimulator - 0.2.7 + 0.2.8 pom ``` @@ -155,3 +156,18 @@ resilience: 1 resilienceFailureCode: 501 ``` +### Request based routing +WSimulator allows for content within the request payload to determine what specification is called allowing for + +```yaml +path: /publish +consumes: application/xml +httpMethod: post +successResponseCode:201 +badRequestResponseCode: 500 +response: ok +request: + filterType: contains + filter: Hello World +``` +_Filters by any request containing the phrase 'Hello World'_ \ No newline at end of file diff --git a/build.gradle b/build.gradle index a6ac3a5..b1f6587 100644 --- a/build.gradle +++ b/build.gradle @@ -53,4 +53,5 @@ jar { test.onlyIf { // run tests locally until we get the circleci port issue sorted. Boolean.getBoolean('run.tests') -} \ No newline at end of file +} + diff --git a/src/main/java/wssimulator/RouteRequestFilterType.java b/src/main/java/wssimulator/RouteRequestFilterType.java new file mode 100644 index 0000000..f95a7d1 --- /dev/null +++ b/src/main/java/wssimulator/RouteRequestFilterType.java @@ -0,0 +1,214 @@ +/* + * Apache License + * Version 2.0, January 2004 + * http://www.apache.org/licenses/ + * + * TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + * + * 1. Definitions. + * + * "License" shall mean the terms and conditions for use, reproduction, + * and distribution as defined by Sections 1 through 9 of this document. + * + * "Licensor" shall mean the copyright owner or entity authorized by + * the copyright owner that is granting the License. + * + * "Legal Entity" shall mean the union of the acting entity and all + * other entities that control, are controlled by, or are under common + * control with that entity. For the purposes of this definition, + * "control" means (i) the power, direct or indirect, to cause the + * direction or management of such entity, whether by contract or + * otherwise, or (ii) ownership of fifty percent (50%) or more of the + * outstanding shares, or (iii) beneficial ownership of such entity. + * + * "You" (or "Your") shall mean an individual or Legal Entity + * exercising permissions granted by this License. + * + * "Source" form shall mean the preferred form for making modifications, + * including but not limited to software source code, documentation + * source, and configuration files. + * + * "Object" form shall mean any form resulting from mechanical + * transformation or translation of a Source form, including but + * not limited to compiled object code, generated documentation, + * and conversions to other media types. + * + * "Work" shall mean the work of authorship, whether in Source or + * Object form, made available under the License, as indicated by a + * copyright notice that is included in or attached to the work + * (an example is provided in the Appendix below). + * + * "Derivative Works" shall mean any work, whether in Source or Object + * form, that is based on (or derived from) the Work and for which the + * editorial revisions, annotations, elaborations, or other modifications + * represent, as a whole, an original work of authorship. For the purposes + * of this License, Derivative Works shall not include works that remain + * separable from, or merely link (or bind by name) to the interfaces of, + * the Work and Derivative Works thereof. + * + * "Contribution" shall mean any work of authorship, including + * the original version of the Work and any modifications or additions + * to that Work or Derivative Works thereof, that is intentionally + * submitted to Licensor for inclusion in the Work by the copyright owner + * or by an individual or Legal Entity authorized to submit on behalf of + * the copyright owner. For the purposes of this definition, "submitted" + * means any form of electronic, verbal, or written communication sent + * to the Licensor or its representatives, including but not limited to + * communication on electronic mailing lists, source code control systems, + * and issue tracking systems that are managed by, or on behalf of, the + * Licensor for the purpose of discussing and improving the Work, but + * excluding communication that is conspicuously marked or otherwise + * designated in writing by the copyright owner as "Not a Contribution." + * + * "Contributor" shall mean Licensor and any individual or Legal Entity + * on behalf of whom a Contribution has been received by Licensor and + * subsequently incorporated within the Work. + * + * 2. Grant of Copyright License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * copyright license to reproduce, prepare Derivative Works of, + * publicly display, publicly perform, sublicense, and distribute the + * Work and such Derivative Works in Source or Object form. + * + * 3. Grant of Patent License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * (except as stated in this section) patent license to make, have made, + * use, offer to sell, sell, import, and otherwise transfer the Work, + * where such license applies only to those patent claims licensable + * by such Contributor that are necessarily infringed by their + * Contribution(s) alone or by combination of their Contribution(s) + * with the Work to which such Contribution(s) was submitted. If You + * institute patent litigation against any entity (including a + * cross-claim or counterclaim in a lawsuit) alleging that the Work + * or a Contribution incorporated within the Work constitutes direct + * or contributory patent infringement, then any patent licenses + * granted to You under this License for that Work shall terminate + * as of the date such litigation is filed. + * + * 4. Redistribution. You may reproduce and distribute copies of the + * Work or Derivative Works thereof in any medium, with or without + * modifications, and in Source or Object form, provided that You + * meet the following conditions: + * + * (a) You must give any other recipients of the Work or + * Derivative Works a copy of this License; and + * + * (b) You must cause any modified files to carry prominent notices + * stating that You changed the files; and + * + * (c) You must retain, in the Source form of any Derivative Works + * that You distribute, all copyright, patent, trademark, and + * attribution notices from the Source form of the Work, + * excluding those notices that do not pertain to any part of + * the Derivative Works; and + * + * (d) If the Work includes a "NOTICE" text file as part of its + * distribution, then any Derivative Works that You distribute must + * include a readable copy of the attribution notices contained + * within such NOTICE file, excluding those notices that do not + * pertain to any part of the Derivative Works, in at least one + * of the following places: within a NOTICE text file distributed + * as part of the Derivative Works; within the Source form or + * documentation, if provided along with the Derivative Works; or, + * within a display generated by the Derivative Works, if and + * wherever such third-party notices normally appear. The contents + * of the NOTICE file are for informational purposes only and + * do not modify the License. You may add Your own attribution + * notices within Derivative Works that You distribute, alongside + * or as an addendum to the NOTICE text from the Work, provided + * that such additional attribution notices cannot be construed + * as modifying the License. + * + * You may add Your own copyright statement to Your modifications and + * may provide additional or different license terms and conditions + * for use, reproduction, or distribution of Your modifications, or + * for any such Derivative Works as a whole, provided Your use, + * reproduction, and distribution of the Work otherwise complies with + * the conditions stated in this License. + * + * 5. Submission of Contributions. Unless You explicitly state otherwise, + * any Contribution intentionally submitted for inclusion in the Work + * by You to the Licensor shall be under the terms and conditions of + * this License, without any additional terms or conditions. + * Notwithstanding the above, nothing herein shall supersede or modify + * the terms of any separate license agreement you may have executed + * with Licensor regarding such Contributions. + * + * 6. Trademarks. This License does not grant permission to use the trade + * names, trademarks, service marks, or product names of the Licensor, + * except as required for reasonable and customary use in describing the + * origin of the Work and reproducing the content of the NOTICE file. + * + * 7. Disclaimer of Warranty. Unless required by applicable law or + * agreed to in writing, Licensor provides the Work (and each + * Contributor provides its Contributions) on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied, including, without limitation, any warranties or conditions + * of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + * PARTICULAR PURPOSE. You are solely responsible for determining the + * appropriateness of using or redistributing the Work and assume any + * risks associated with Your exercise of permissions under this License. + * + * 8. Limitation of Liability. In no event and under no legal theory, + * whether in tort (including negligence), contract, or otherwise, + * unless required by applicable law (such as deliberate and grossly + * negligent acts) or agreed to in writing, shall any Contributor be + * liable to You for damages, including any direct, indirect, special, + * incidental, or consequential damages of any character arising as a + * result of this License or out of the use or inability to use the + * Work (including but not limited to damages for loss of goodwill, + * work stoppage, computer failure or malfunction, or any and all + * other commercial damages or losses), even if such Contributor + * has been advised of the possibility of such damages. + * + * 9. Accepting Warranty or Additional Liability. While redistributing + * the Work or Derivative Works thereof, You may choose to offer, + * and charge a fee for, acceptance of support, warranty, indemnity, + * or other liability obligations and/or rights consistent with this + * License. However, in accepting such obligations, You may act only + * on Your own behalf and on Your sole responsibility, not on behalf + * of any other Contributor, and only if You agree to indemnify, + * defend, and hold each Contributor harmless for any liability + * incurred by, or claims asserted against, such Contributor by reason + * of your accepting any such warranty or additional liability. + * + * END OF TERMS AND CONDITIONS + * + * APPENDIX: How to apply the Apache License to your work. + * + * To apply the Apache License to your work, attach the following + * boilerplate notice, with the fields enclosed by brackets "[]" + * replaced with your own identifying information. (Don't include + * the brackets!) The text should be enclosed in the appropriate + * comment syntax for the file format. We also recommend that a + * file or class name and description of purpose be included on the + * same "printed page" as the copyright notice for easier + * identification within third-party archives. + * + * Copyright 2016 Ian Kelly + * + * 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 wssimulator; + +/** + * + * The Route Filter options + */ +public enum RouteRequestFilterType { + none, contains +} diff --git a/src/main/java/wssimulator/WSSimulation.java b/src/main/java/wssimulator/WSSimulation.java index 74118c6..73119d6 100644 --- a/src/main/java/wssimulator/WSSimulation.java +++ b/src/main/java/wssimulator/WSSimulation.java @@ -236,10 +236,11 @@ public class WSSimulation { */ public String response; + /** - * Validate the structure of the request. + * Holds request logic for validation/filtering */ - public String requestStructure; + public WSSimulatorRequest request = new WSSimulatorRequest(); /** * sets the successful response code to send back to the server @@ -264,7 +265,7 @@ public class WSSimulation { * Context object for this simulation */ @JsonIgnore - public final WSSimulationContext wsSimulationContext=new WSSimulationContext(); + public final WSSimulationContext wsSimulationContext = new WSSimulationContext(); @Override public String toString() { diff --git a/src/main/java/wssimulator/WSSimulator.java b/src/main/java/wssimulator/WSSimulator.java index 07f63fc..da6f19b 100644 --- a/src/main/java/wssimulator/WSSimulator.java +++ b/src/main/java/wssimulator/WSSimulator.java @@ -211,6 +211,7 @@ import java.io.File; import java.util.Collection; +import java.util.concurrent.TimeUnit; /** * Base simulator for web services. @@ -307,6 +308,7 @@ public static int loadedSimulationCount() { /** * Holds the times a simulation has been called. + * * @param simulationId - the simulation id * @return the number of times a service has been called. */ @@ -316,11 +318,12 @@ public static int calledCount(int simulationId) { /** * Return the ID of a simulation path based on its logical path - * @param path the path that the simulation + * + * @param path the path that the simulation + * @param httpMethod the httpmethod of the specification * @return the id or -1 if not loaded. */ - public static int findSimulationIdByPath(@NotNull String path) - { - return wsSimulatorServiceManager.findSimulationIdByPath(path); + public static int findSimulationId(@NotNull String path, @NotNull HttpMethod httpMethod) { + return wsSimulatorServiceManager.findSimulationIdByPath(path, httpMethod); } } diff --git a/src/main/java/wssimulator/WSSimulatorRequest.java b/src/main/java/wssimulator/WSSimulatorRequest.java new file mode 100644 index 0000000..1d3b494 --- /dev/null +++ b/src/main/java/wssimulator/WSSimulatorRequest.java @@ -0,0 +1,237 @@ +/* + * Apache License + * Version 2.0, January 2004 + * http://www.apache.org/licenses/ + * + * TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + * + * 1. Definitions. + * + * "License" shall mean the terms and conditions for use, reproduction, + * and distribution as defined by Sections 1 through 9 of this document. + * + * "Licensor" shall mean the copyright owner or entity authorized by + * the copyright owner that is granting the License. + * + * "Legal Entity" shall mean the union of the acting entity and all + * other entities that control, are controlled by, or are under common + * control with that entity. For the purposes of this definition, + * "control" means (i) the power, direct or indirect, to cause the + * direction or management of such entity, whether by contract or + * otherwise, or (ii) ownership of fifty percent (50%) or more of the + * outstanding shares, or (iii) beneficial ownership of such entity. + * + * "You" (or "Your") shall mean an individual or Legal Entity + * exercising permissions granted by this License. + * + * "Source" form shall mean the preferred form for making modifications, + * including but not limited to software source code, documentation + * source, and configuration files. + * + * "Object" form shall mean any form resulting from mechanical + * transformation or translation of a Source form, including but + * not limited to compiled object code, generated documentation, + * and conversions to other media types. + * + * "Work" shall mean the work of authorship, whether in Source or + * Object form, made available under the License, as indicated by a + * copyright notice that is included in or attached to the work + * (an example is provided in the Appendix below). + * + * "Derivative Works" shall mean any work, whether in Source or Object + * form, that is based on (or derived from) the Work and for which the + * editorial revisions, annotations, elaborations, or other modifications + * represent, as a whole, an original work of authorship. For the purposes + * of this License, Derivative Works shall not include works that remain + * separable from, or merely link (or bind by name) to the interfaces of, + * the Work and Derivative Works thereof. + * + * "Contribution" shall mean any work of authorship, including + * the original version of the Work and any modifications or additions + * to that Work or Derivative Works thereof, that is intentionally + * submitted to Licensor for inclusion in the Work by the copyright owner + * or by an individual or Legal Entity authorized to submit on behalf of + * the copyright owner. For the purposes of this definition, "submitted" + * means any form of electronic, verbal, or written communication sent + * to the Licensor or its representatives, including but not limited to + * communication on electronic mailing lists, source code control systems, + * and issue tracking systems that are managed by, or on behalf of, the + * Licensor for the purpose of discussing and improving the Work, but + * excluding communication that is conspicuously marked or otherwise + * designated in writing by the copyright owner as "Not a Contribution." + * + * "Contributor" shall mean Licensor and any individual or Legal Entity + * on behalf of whom a Contribution has been received by Licensor and + * subsequently incorporated within the Work. + * + * 2. Grant of Copyright License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * copyright license to reproduce, prepare Derivative Works of, + * publicly display, publicly perform, sublicense, and distribute the + * Work and such Derivative Works in Source or Object form. + * + * 3. Grant of Patent License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * (except as stated in this section) patent license to make, have made, + * use, offer to sell, sell, import, and otherwise transfer the Work, + * where such license applies only to those patent claims licensable + * by such Contributor that are necessarily infringed by their + * Contribution(s) alone or by combination of their Contribution(s) + * with the Work to which such Contribution(s) was submitted. If You + * institute patent litigation against any entity (including a + * cross-claim or counterclaim in a lawsuit) alleging that the Work + * or a Contribution incorporated within the Work constitutes direct + * or contributory patent infringement, then any patent licenses + * granted to You under this License for that Work shall terminate + * as of the date such litigation is filed. + * + * 4. Redistribution. You may reproduce and distribute copies of the + * Work or Derivative Works thereof in any medium, with or without + * modifications, and in Source or Object form, provided that You + * meet the following conditions: + * + * (a) You must give any other recipients of the Work or + * Derivative Works a copy of this License; and + * + * (b) You must cause any modified files to carry prominent notices + * stating that You changed the files; and + * + * (c) You must retain, in the Source form of any Derivative Works + * that You distribute, all copyright, patent, trademark, and + * attribution notices from the Source form of the Work, + * excluding those notices that do not pertain to any part of + * the Derivative Works; and + * + * (d) If the Work includes a "NOTICE" text file as part of its + * distribution, then any Derivative Works that You distribute must + * include a readable copy of the attribution notices contained + * within such NOTICE file, excluding those notices that do not + * pertain to any part of the Derivative Works, in at least one + * of the following places: within a NOTICE text file distributed + * as part of the Derivative Works; within the Source form or + * documentation, if provided along with the Derivative Works; or, + * within a display generated by the Derivative Works, if and + * wherever such third-party notices normally appear. The contents + * of the NOTICE file are for informational purposes only and + * do not modify the License. You may add Your own attribution + * notices within Derivative Works that You distribute, alongside + * or as an addendum to the NOTICE text from the Work, provided + * that such additional attribution notices cannot be construed + * as modifying the License. + * + * You may add Your own copyright statement to Your modifications and + * may provide additional or different license terms and conditions + * for use, reproduction, or distribution of Your modifications, or + * for any such Derivative Works as a whole, provided Your use, + * reproduction, and distribution of the Work otherwise complies with + * the conditions stated in this License. + * + * 5. Submission of Contributions. Unless You explicitly state otherwise, + * any Contribution intentionally submitted for inclusion in the Work + * by You to the Licensor shall be under the terms and conditions of + * this License, without any additional terms or conditions. + * Notwithstanding the above, nothing herein shall supersede or modify + * the terms of any separate license agreement you may have executed + * with Licensor regarding such Contributions. + * + * 6. Trademarks. This License does not grant permission to use the trade + * names, trademarks, service marks, or product names of the Licensor, + * except as required for reasonable and customary use in describing the + * origin of the Work and reproducing the content of the NOTICE file. + * + * 7. Disclaimer of Warranty. Unless required by applicable law or + * agreed to in writing, Licensor provides the Work (and each + * Contributor provides its Contributions) on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied, including, without limitation, any warranties or conditions + * of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + * PARTICULAR PURPOSE. You are solely responsible for determining the + * appropriateness of using or redistributing the Work and assume any + * risks associated with Your exercise of permissions under this License. + * + * 8. Limitation of Liability. In no event and under no legal theory, + * whether in tort (including negligence), contract, or otherwise, + * unless required by applicable law (such as deliberate and grossly + * negligent acts) or agreed to in writing, shall any Contributor be + * liable to You for damages, including any direct, indirect, special, + * incidental, or consequential damages of any character arising as a + * result of this License or out of the use or inability to use the + * Work (including but not limited to damages for loss of goodwill, + * work stoppage, computer failure or malfunction, or any and all + * other commercial damages or losses), even if such Contributor + * has been advised of the possibility of such damages. + * + * 9. Accepting Warranty or Additional Liability. While redistributing + * the Work or Derivative Works thereof, You may choose to offer, + * and charge a fee for, acceptance of support, warranty, indemnity, + * or other liability obligations and/or rights consistent with this + * License. However, in accepting such obligations, You may act only + * on Your own behalf and on Your sole responsibility, not on behalf + * of any other Contributor, and only if You agree to indemnify, + * defend, and hold each Contributor harmless for any liability + * incurred by, or claims asserted against, such Contributor by reason + * of your accepting any such warranty or additional liability. + * + * END OF TERMS AND CONDITIONS + * + * APPENDIX: How to apply the Apache License to your work. + * + * To apply the Apache License to your work, attach the following + * boilerplate notice, with the fields enclosed by brackets "[]" + * replaced with your own identifying information. (Don't include + * the brackets!) The text should be enclosed in the appropriate + * comment syntax for the file format. We also recommend that a + * file or class name and description of purpose be included on the + * same "printed page" as the copyright notice for easier + * identification within third-party archives. + * + * Copyright 2016 Ian Kelly + * + * 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 wssimulator; + +/** + * Holds the request validation and routing logic. + */ +public class WSSimulatorRequest { + + /** + * Type of route filter to apply to request + */ + public RouteRequestFilterType filterType = RouteRequestFilterType.none; + + /** + * Filter content + */ + public String filter = ""; + + /** + * Validate the structure of the request. + */ + public String requestStructure = ""; + + + @Override + public String toString() { + return "WSSimulatorRequest{" + + "requestStructure='" + requestStructure + '\'' + + ", filter='" + filter + '\'' + + ", filterType=" + filterType + + '}'; + } +} diff --git a/src/main/java/wssimulator/WSSimulatorServiceManager.java b/src/main/java/wssimulator/WSSimulatorServiceManager.java index 0105359..4d8a62b 100644 --- a/src/main/java/wssimulator/WSSimulatorServiceManager.java +++ b/src/main/java/wssimulator/WSSimulatorServiceManager.java @@ -226,6 +226,7 @@ */ public class WSSimulatorServiceManager { private int counter = 0; + private Map handlers = new HashMap<>(); private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(WSSimulatorServiceManager.class); @@ -286,44 +287,53 @@ public int add(@NotNull WSSimulation simulation) { * @param simulation the simulator simulation to setup. */ private int setupRoute(@NotNull WSSimulation simulation) { - BaseHandler handler = handler(simulation); + BaseHandler handler = lookupHandler(simulation); validSimulations.put(++counter, simulation); - switch (simulation.httpMethod) { - case get: - get(simulation.path, handler::processRequest); - LOG.info("GET {} now listening", simulation.path); - break; - case post: - post(simulation.path, handler::processRequest); - LOG.info("POST {} now listening", simulation.path); - break; - case put: - put(simulation.path, handler::processRequest); - LOG.info("PUT {} now listening", simulation.path); - break; - case patch: - patch(simulation.path, handler::processRequest); - LOG.info("PATCH {} now listening", simulation.path); - break; - case delete: - delete(simulation.path, handler::processRequest); - LOG.info("DELETE {} now listening", simulation.path); - break; - case head: - head(simulation.path, handler::processRequest); - LOG.info("HEAD {} now listening", simulation.path); - break; + if (handler.routeCount() == 1) { + switch (simulation.httpMethod) { + case get: + get(simulation.path, handler::processRequest); + LOG.info("GET {} now listening", simulation.path); + break; + case post: + post(simulation.path, handler::processRequest); + LOG.info("POST {} now listening", simulation.path); + break; + case put: + put(simulation.path, handler::processRequest); + LOG.info("PUT {} now listening", simulation.path); + break; + case patch: + patch(simulation.path, handler::processRequest); + LOG.info("PATCH {} now listening", simulation.path); + break; + case delete: + delete(simulation.path, handler::processRequest); + LOG.info("DELETE {} now listening", simulation.path); + break; + case head: + head(simulation.path, handler::processRequest); + LOG.info("HEAD {} now listening", simulation.path); + break; + } } return counter; } - private BaseHandler handler(@NotNull WSSimulation wsSimulation) { - if ("application/xml".equals(wsSimulation.consumes)) - return new XMLHandler(wsSimulation); - else if ("application/json".equals(wsSimulation.consumes)) - return new JSONHandler(wsSimulation); - - return new GenericHandler(wsSimulation); + private BaseHandler lookupHandler(@NotNull WSSimulation wsSimulation) { + BaseHandler baseHandler = handlers.get(wsSimulation.path + "::" + wsSimulation.httpMethod); + if (baseHandler == null) { + if ("application/xml".equals(wsSimulation.consumes)) + baseHandler = new XMLHandler(wsSimulation); + else if ("application/json".equals(wsSimulation.consumes)) + baseHandler = new JSONHandler(wsSimulation); + else + baseHandler = new GenericHandler(wsSimulation); + handlers.put(wsSimulation.path + "::" + wsSimulation.httpMethod, baseHandler); + } else { + baseHandler.addRoute(wsSimulation); + } + return baseHandler; } /** @@ -370,10 +380,10 @@ public int calledCounter(int simulationId) { return wsSimulation.wsSimulationContext.callCount; } - public int findSimulationIdByPath(@NotNull String path) { + public int findSimulationIdByPath(@NotNull String path, @NotNull HttpMethod httpMethod) { return validSimulations.entrySet() .stream() - .filter(e-> e.getValue().path.equalsIgnoreCase(path)) + .filter(e -> e.getValue().path.equalsIgnoreCase(path) && e.getValue().httpMethod.equals(httpMethod)) .findFirst() .map(Map.Entry::getKey) .orElse(-1); diff --git a/src/main/java/wssimulator/handler/BaseHandler.java b/src/main/java/wssimulator/handler/BaseHandler.java index b3137c5..c229d89 100644 --- a/src/main/java/wssimulator/handler/BaseHandler.java +++ b/src/main/java/wssimulator/handler/BaseHandler.java @@ -211,11 +211,11 @@ import org.jetbrains.annotations.Nullable; import spark.Request; import spark.Response; +import wssimulator.RouteRequestFilterType; import wssimulator.WSSimulation; import wssimulator.WSSimulator; -import java.util.Map; -import java.util.Random; +import java.util.*; import java.util.stream.Collectors; @@ -228,35 +228,56 @@ public abstract class BaseHandler { private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(WSSimulator.class); - @NotNull - private final WSSimulation wsSimulation; + + private Map filterTypes = new HashMap() {{ + put(RouteRequestFilterType.none, new NoneRouteRequestFilterer()); + put(RouteRequestFilterType.contains, new ContainsRouteRequestFilterer()); + }}; + + private final List wsSimulations = new ArrayList<>(); private final Random random = new Random(); - public BaseHandler(@NotNull WSSimulation wsSimulation) { - this.wsSimulation = wsSimulation; + public BaseHandler(@NotNull WSSimulation wsSimulations) { + this.wsSimulations.add(wsSimulations); } @NotNull public final Object processRequest(@NotNull Request request, @NotNull Response response) { final Map params = buildParameterValues(request); LOG.info("request body:{}", request.body()); - wsSimulation.wsSimulationContext.callCount++; - if (!validate(wsSimulation, request.body())) { - LOG.info("Validation failed for request, returning status code:{}", wsSimulation.badRequestResponseCode); - response.status(wsSimulation.badRequestResponseCode); - return ""; + //find the correct simulation to use + WSSimulation wsSimulation = loadSimulation(request); + if (wsSimulation != null) { + wsSimulation.wsSimulationContext.callCount++; + if (!validate(wsSimulation, request.body())) { + LOG.info("Validation failed for request, returning status code:{}", wsSimulation.badRequestResponseCode); + response.status(wsSimulation.badRequestResponseCode); + return ""; + } + if (resilienceCheck(wsSimulation)) { + LOG.info("Resilience Failed, returning status code:{}", wsSimulation.resilienceFailureCode); + response.status(wsSimulation.resilienceFailureCode); + return ""; + } + if (StringUtils.isNotEmpty(wsSimulation.response)) + return new StrSubstitutor(params).replace(wsSimulation.response); } - if (resilienceCheck(wsSimulation)) { - LOG.info("Resilience Failed, returning status code:{}", wsSimulation.resilienceFailureCode); - response.status(wsSimulation.resilienceFailureCode); - return ""; - } - if (StringUtils.isNotEmpty(wsSimulation.response)) - return new StrSubstitutor(params).replace(wsSimulation.response); return ""; } + @Nullable + private WSSimulation loadSimulation(@Nullable Request request) { + if (wsSimulations.size() == 1) + return wsSimulations.get(0); + + for (WSSimulation wsSimulation : wsSimulations) { + if (filterTypes.get(wsSimulation.request.filterType).filter(wsSimulation, request)) + return wsSimulation; + } + return null; + } + private boolean resilienceCheck(@NotNull WSSimulation wsSimulation) { return wsSimulation.resilience < 1 && wsSimulation.resilience - random.nextDouble() <= 0; } @@ -273,4 +294,22 @@ private Map buildParameterValues(@NotNull Request request) { request::params)); return params; } + + /** + * Return the route count that this handler manages + * + * @return + */ + public int routeCount() { + return wsSimulations.size(); + } + + /** + * Adds a route to this handler + * + * @param simulation a simulation that will be added. + */ + public void addRoute(@NotNull WSSimulation simulation) { + wsSimulations.add(simulation); + } } diff --git a/src/main/java/wssimulator/handler/ContainsRouteRequestFilterer.java b/src/main/java/wssimulator/handler/ContainsRouteRequestFilterer.java new file mode 100644 index 0000000..e913158 --- /dev/null +++ b/src/main/java/wssimulator/handler/ContainsRouteRequestFilterer.java @@ -0,0 +1,222 @@ +/* + * Apache License + * Version 2.0, January 2004 + * http://www.apache.org/licenses/ + * + * TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + * + * 1. Definitions. + * + * "License" shall mean the terms and conditions for use, reproduction, + * and distribution as defined by Sections 1 through 9 of this document. + * + * "Licensor" shall mean the copyright owner or entity authorized by + * the copyright owner that is granting the License. + * + * "Legal Entity" shall mean the union of the acting entity and all + * other entities that control, are controlled by, or are under common + * control with that entity. For the purposes of this definition, + * "control" means (i) the power, direct or indirect, to cause the + * direction or management of such entity, whether by contract or + * otherwise, or (ii) ownership of fifty percent (50%) or more of the + * outstanding shares, or (iii) beneficial ownership of such entity. + * + * "You" (or "Your") shall mean an individual or Legal Entity + * exercising permissions granted by this License. + * + * "Source" form shall mean the preferred form for making modifications, + * including but not limited to software source code, documentation + * source, and configuration files. + * + * "Object" form shall mean any form resulting from mechanical + * transformation or translation of a Source form, including but + * not limited to compiled object code, generated documentation, + * and conversions to other media types. + * + * "Work" shall mean the work of authorship, whether in Source or + * Object form, made available under the License, as indicated by a + * copyright notice that is included in or attached to the work + * (an example is provided in the Appendix below). + * + * "Derivative Works" shall mean any work, whether in Source or Object + * form, that is based on (or derived from) the Work and for which the + * editorial revisions, annotations, elaborations, or other modifications + * represent, as a whole, an original work of authorship. For the purposes + * of this License, Derivative Works shall not include works that remain + * separable from, or merely link (or bind by name) to the interfaces of, + * the Work and Derivative Works thereof. + * + * "Contribution" shall mean any work of authorship, including + * the original version of the Work and any modifications or additions + * to that Work or Derivative Works thereof, that is intentionally + * submitted to Licensor for inclusion in the Work by the copyright owner + * or by an individual or Legal Entity authorized to submit on behalf of + * the copyright owner. For the purposes of this definition, "submitted" + * means any form of electronic, verbal, or written communication sent + * to the Licensor or its representatives, including but not limited to + * communication on electronic mailing lists, source code control systems, + * and issue tracking systems that are managed by, or on behalf of, the + * Licensor for the purpose of discussing and improving the Work, but + * excluding communication that is conspicuously marked or otherwise + * designated in writing by the copyright owner as "Not a Contribution." + * + * "Contributor" shall mean Licensor and any individual or Legal Entity + * on behalf of whom a Contribution has been received by Licensor and + * subsequently incorporated within the Work. + * + * 2. Grant of Copyright License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * copyright license to reproduce, prepare Derivative Works of, + * publicly display, publicly perform, sublicense, and distribute the + * Work and such Derivative Works in Source or Object form. + * + * 3. Grant of Patent License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * (except as stated in this section) patent license to make, have made, + * use, offer to sell, sell, import, and otherwise transfer the Work, + * where such license applies only to those patent claims licensable + * by such Contributor that are necessarily infringed by their + * Contribution(s) alone or by combination of their Contribution(s) + * with the Work to which such Contribution(s) was submitted. If You + * institute patent litigation against any entity (including a + * cross-claim or counterclaim in a lawsuit) alleging that the Work + * or a Contribution incorporated within the Work constitutes direct + * or contributory patent infringement, then any patent licenses + * granted to You under this License for that Work shall terminate + * as of the date such litigation is filed. + * + * 4. Redistribution. You may reproduce and distribute copies of the + * Work or Derivative Works thereof in any medium, with or without + * modifications, and in Source or Object form, provided that You + * meet the following conditions: + * + * (a) You must give any other recipients of the Work or + * Derivative Works a copy of this License; and + * + * (b) You must cause any modified files to carry prominent notices + * stating that You changed the files; and + * + * (c) You must retain, in the Source form of any Derivative Works + * that You distribute, all copyright, patent, trademark, and + * attribution notices from the Source form of the Work, + * excluding those notices that do not pertain to any part of + * the Derivative Works; and + * + * (d) If the Work includes a "NOTICE" text file as part of its + * distribution, then any Derivative Works that You distribute must + * include a readable copy of the attribution notices contained + * within such NOTICE file, excluding those notices that do not + * pertain to any part of the Derivative Works, in at least one + * of the following places: within a NOTICE text file distributed + * as part of the Derivative Works; within the Source form or + * documentation, if provided along with the Derivative Works; or, + * within a display generated by the Derivative Works, if and + * wherever such third-party notices normally appear. The contents + * of the NOTICE file are for informational purposes only and + * do not modify the License. You may add Your own attribution + * notices within Derivative Works that You distribute, alongside + * or as an addendum to the NOTICE text from the Work, provided + * that such additional attribution notices cannot be construed + * as modifying the License. + * + * You may add Your own copyright statement to Your modifications and + * may provide additional or different license terms and conditions + * for use, reproduction, or distribution of Your modifications, or + * for any such Derivative Works as a whole, provided Your use, + * reproduction, and distribution of the Work otherwise complies with + * the conditions stated in this License. + * + * 5. Submission of Contributions. Unless You explicitly state otherwise, + * any Contribution intentionally submitted for inclusion in the Work + * by You to the Licensor shall be under the terms and conditions of + * this License, without any additional terms or conditions. + * Notwithstanding the above, nothing herein shall supersede or modify + * the terms of any separate license agreement you may have executed + * with Licensor regarding such Contributions. + * + * 6. Trademarks. This License does not grant permission to use the trade + * names, trademarks, service marks, or product names of the Licensor, + * except as required for reasonable and customary use in describing the + * origin of the Work and reproducing the content of the NOTICE file. + * + * 7. Disclaimer of Warranty. Unless required by applicable law or + * agreed to in writing, Licensor provides the Work (and each + * Contributor provides its Contributions) on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied, including, without limitation, any warranties or conditions + * of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + * PARTICULAR PURPOSE. You are solely responsible for determining the + * appropriateness of using or redistributing the Work and assume any + * risks associated with Your exercise of permissions under this License. + * + * 8. Limitation of Liability. In no event and under no legal theory, + * whether in tort (including negligence), contract, or otherwise, + * unless required by applicable law (such as deliberate and grossly + * negligent acts) or agreed to in writing, shall any Contributor be + * liable to You for damages, including any direct, indirect, special, + * incidental, or consequential damages of any character arising as a + * result of this License or out of the use or inability to use the + * Work (including but not limited to damages for loss of goodwill, + * work stoppage, computer failure or malfunction, or any and all + * other commercial damages or losses), even if such Contributor + * has been advised of the possibility of such damages. + * + * 9. Accepting Warranty or Additional Liability. While redistributing + * the Work or Derivative Works thereof, You may choose to offer, + * and charge a fee for, acceptance of support, warranty, indemnity, + * or other liability obligations and/or rights consistent with this + * License. However, in accepting such obligations, You may act only + * on Your own behalf and on Your sole responsibility, not on behalf + * of any other Contributor, and only if You agree to indemnify, + * defend, and hold each Contributor harmless for any liability + * incurred by, or claims asserted against, such Contributor by reason + * of your accepting any such warranty or additional liability. + * + * END OF TERMS AND CONDITIONS + * + * APPENDIX: How to apply the Apache License to your work. + * + * To apply the Apache License to your work, attach the following + * boilerplate notice, with the fields enclosed by brackets "[]" + * replaced with your own identifying information. (Don't include + * the brackets!) The text should be enclosed in the appropriate + * comment syntax for the file format. We also recommend that a + * file or class name and description of purpose be included on the + * same "printed page" as the copyright notice for easier + * identification within third-party archives. + * + * Copyright 2016 Ian Kelly + * + * 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 wssimulator.handler; + +import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; +import spark.Request; +import wssimulator.WSSimulation; + +/** + * + * + */ +public class ContainsRouteRequestFilterer implements RouteRequestFilterer { + @Override + public boolean filter(@NotNull WSSimulation wsSimulation, @NotNull Request request) { + return StringUtils.isNotEmpty(request.body()) && request.body().contains(wsSimulation.request.filter); + } +} diff --git a/src/main/java/wssimulator/handler/GenericHandler.java b/src/main/java/wssimulator/handler/GenericHandler.java index d042ebc..77dd56c 100644 --- a/src/main/java/wssimulator/handler/GenericHandler.java +++ b/src/main/java/wssimulator/handler/GenericHandler.java @@ -210,17 +210,19 @@ import org.jetbrains.annotations.Nullable; import wssimulator.WSSimulation; +import java.util.List; + /** * Handles all other media types */ public class GenericHandler extends BaseHandler { - public GenericHandler(@NotNull WSSimulation WSSimulation) { - super(WSSimulation); + public GenericHandler(@NotNull WSSimulation wsSimulations) { + super(wsSimulations); } @Override - protected boolean validate(@NotNull WSSimulation WSSimulation, @Nullable String body) { + protected boolean validate(@NotNull WSSimulation wsSimulation, @Nullable String body) { return true; } } diff --git a/src/main/java/wssimulator/handler/JSONHandler.java b/src/main/java/wssimulator/handler/JSONHandler.java index 6e07fa0..a0922e6 100644 --- a/src/main/java/wssimulator/handler/JSONHandler.java +++ b/src/main/java/wssimulator/handler/JSONHandler.java @@ -224,8 +224,8 @@ public JSONHandler(@NotNull WSSimulation WSSimulation) { @Override protected boolean validate(@NotNull WSSimulation simulatorSimulation, @Nullable String body) { - if (isNotEmpty(simulatorSimulation.requestStructure) && isNotEmpty(body)) - return validateJSON(simulatorSimulation.requestStructure, body); + if (isNotEmpty(simulatorSimulation.request.requestStructure) && isNotEmpty(body)) + return validateJSON(simulatorSimulation.request.requestStructure, body); return true; } diff --git a/src/main/java/wssimulator/handler/NoneRouteRequestFilterer.java b/src/main/java/wssimulator/handler/NoneRouteRequestFilterer.java new file mode 100644 index 0000000..32e8851 --- /dev/null +++ b/src/main/java/wssimulator/handler/NoneRouteRequestFilterer.java @@ -0,0 +1,220 @@ +/* + * Apache License + * Version 2.0, January 2004 + * http://www.apache.org/licenses/ + * + * TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + * + * 1. Definitions. + * + * "License" shall mean the terms and conditions for use, reproduction, + * and distribution as defined by Sections 1 through 9 of this document. + * + * "Licensor" shall mean the copyright owner or entity authorized by + * the copyright owner that is granting the License. + * + * "Legal Entity" shall mean the union of the acting entity and all + * other entities that control, are controlled by, or are under common + * control with that entity. For the purposes of this definition, + * "control" means (i) the power, direct or indirect, to cause the + * direction or management of such entity, whether by contract or + * otherwise, or (ii) ownership of fifty percent (50%) or more of the + * outstanding shares, or (iii) beneficial ownership of such entity. + * + * "You" (or "Your") shall mean an individual or Legal Entity + * exercising permissions granted by this License. + * + * "Source" form shall mean the preferred form for making modifications, + * including but not limited to software source code, documentation + * source, and configuration files. + * + * "Object" form shall mean any form resulting from mechanical + * transformation or translation of a Source form, including but + * not limited to compiled object code, generated documentation, + * and conversions to other media types. + * + * "Work" shall mean the work of authorship, whether in Source or + * Object form, made available under the License, as indicated by a + * copyright notice that is included in or attached to the work + * (an example is provided in the Appendix below). + * + * "Derivative Works" shall mean any work, whether in Source or Object + * form, that is based on (or derived from) the Work and for which the + * editorial revisions, annotations, elaborations, or other modifications + * represent, as a whole, an original work of authorship. For the purposes + * of this License, Derivative Works shall not include works that remain + * separable from, or merely link (or bind by name) to the interfaces of, + * the Work and Derivative Works thereof. + * + * "Contribution" shall mean any work of authorship, including + * the original version of the Work and any modifications or additions + * to that Work or Derivative Works thereof, that is intentionally + * submitted to Licensor for inclusion in the Work by the copyright owner + * or by an individual or Legal Entity authorized to submit on behalf of + * the copyright owner. For the purposes of this definition, "submitted" + * means any form of electronic, verbal, or written communication sent + * to the Licensor or its representatives, including but not limited to + * communication on electronic mailing lists, source code control systems, + * and issue tracking systems that are managed by, or on behalf of, the + * Licensor for the purpose of discussing and improving the Work, but + * excluding communication that is conspicuously marked or otherwise + * designated in writing by the copyright owner as "Not a Contribution." + * + * "Contributor" shall mean Licensor and any individual or Legal Entity + * on behalf of whom a Contribution has been received by Licensor and + * subsequently incorporated within the Work. + * + * 2. Grant of Copyright License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * copyright license to reproduce, prepare Derivative Works of, + * publicly display, publicly perform, sublicense, and distribute the + * Work and such Derivative Works in Source or Object form. + * + * 3. Grant of Patent License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * (except as stated in this section) patent license to make, have made, + * use, offer to sell, sell, import, and otherwise transfer the Work, + * where such license applies only to those patent claims licensable + * by such Contributor that are necessarily infringed by their + * Contribution(s) alone or by combination of their Contribution(s) + * with the Work to which such Contribution(s) was submitted. If You + * institute patent litigation against any entity (including a + * cross-claim or counterclaim in a lawsuit) alleging that the Work + * or a Contribution incorporated within the Work constitutes direct + * or contributory patent infringement, then any patent licenses + * granted to You under this License for that Work shall terminate + * as of the date such litigation is filed. + * + * 4. Redistribution. You may reproduce and distribute copies of the + * Work or Derivative Works thereof in any medium, with or without + * modifications, and in Source or Object form, provided that You + * meet the following conditions: + * + * (a) You must give any other recipients of the Work or + * Derivative Works a copy of this License; and + * + * (b) You must cause any modified files to carry prominent notices + * stating that You changed the files; and + * + * (c) You must retain, in the Source form of any Derivative Works + * that You distribute, all copyright, patent, trademark, and + * attribution notices from the Source form of the Work, + * excluding those notices that do not pertain to any part of + * the Derivative Works; and + * + * (d) If the Work includes a "NOTICE" text file as part of its + * distribution, then any Derivative Works that You distribute must + * include a readable copy of the attribution notices contained + * within such NOTICE file, excluding those notices that do not + * pertain to any part of the Derivative Works, in at least one + * of the following places: within a NOTICE text file distributed + * as part of the Derivative Works; within the Source form or + * documentation, if provided along with the Derivative Works; or, + * within a display generated by the Derivative Works, if and + * wherever such third-party notices normally appear. The contents + * of the NOTICE file are for informational purposes only and + * do not modify the License. You may add Your own attribution + * notices within Derivative Works that You distribute, alongside + * or as an addendum to the NOTICE text from the Work, provided + * that such additional attribution notices cannot be construed + * as modifying the License. + * + * You may add Your own copyright statement to Your modifications and + * may provide additional or different license terms and conditions + * for use, reproduction, or distribution of Your modifications, or + * for any such Derivative Works as a whole, provided Your use, + * reproduction, and distribution of the Work otherwise complies with + * the conditions stated in this License. + * + * 5. Submission of Contributions. Unless You explicitly state otherwise, + * any Contribution intentionally submitted for inclusion in the Work + * by You to the Licensor shall be under the terms and conditions of + * this License, without any additional terms or conditions. + * Notwithstanding the above, nothing herein shall supersede or modify + * the terms of any separate license agreement you may have executed + * with Licensor regarding such Contributions. + * + * 6. Trademarks. This License does not grant permission to use the trade + * names, trademarks, service marks, or product names of the Licensor, + * except as required for reasonable and customary use in describing the + * origin of the Work and reproducing the content of the NOTICE file. + * + * 7. Disclaimer of Warranty. Unless required by applicable law or + * agreed to in writing, Licensor provides the Work (and each + * Contributor provides its Contributions) on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied, including, without limitation, any warranties or conditions + * of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + * PARTICULAR PURPOSE. You are solely responsible for determining the + * appropriateness of using or redistributing the Work and assume any + * risks associated with Your exercise of permissions under this License. + * + * 8. Limitation of Liability. In no event and under no legal theory, + * whether in tort (including negligence), contract, or otherwise, + * unless required by applicable law (such as deliberate and grossly + * negligent acts) or agreed to in writing, shall any Contributor be + * liable to You for damages, including any direct, indirect, special, + * incidental, or consequential damages of any character arising as a + * result of this License or out of the use or inability to use the + * Work (including but not limited to damages for loss of goodwill, + * work stoppage, computer failure or malfunction, or any and all + * other commercial damages or losses), even if such Contributor + * has been advised of the possibility of such damages. + * + * 9. Accepting Warranty or Additional Liability. While redistributing + * the Work or Derivative Works thereof, You may choose to offer, + * and charge a fee for, acceptance of support, warranty, indemnity, + * or other liability obligations and/or rights consistent with this + * License. However, in accepting such obligations, You may act only + * on Your own behalf and on Your sole responsibility, not on behalf + * of any other Contributor, and only if You agree to indemnify, + * defend, and hold each Contributor harmless for any liability + * incurred by, or claims asserted against, such Contributor by reason + * of your accepting any such warranty or additional liability. + * + * END OF TERMS AND CONDITIONS + * + * APPENDIX: How to apply the Apache License to your work. + * + * To apply the Apache License to your work, attach the following + * boilerplate notice, with the fields enclosed by brackets "[]" + * replaced with your own identifying information. (Don't include + * the brackets!) The text should be enclosed in the appropriate + * comment syntax for the file format. We also recommend that a + * file or class name and description of purpose be included on the + * same "printed page" as the copyright notice for easier + * identification within third-party archives. + * + * Copyright 2016 Ian Kelly + * + * 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 wssimulator.handler; + +import org.jetbrains.annotations.NotNull; +import spark.Request; +import wssimulator.WSSimulation; + +/** + * Created by Ian on 29/07/2016. + */ +public class NoneRouteRequestFilterer implements RouteRequestFilterer { + @Override + public boolean filter(@NotNull WSSimulation wsSimulation, Request request) { + return true; + } +} diff --git a/src/main/java/wssimulator/handler/RouteRequestFilterer.java b/src/main/java/wssimulator/handler/RouteRequestFilterer.java new file mode 100644 index 0000000..277a5f3 --- /dev/null +++ b/src/main/java/wssimulator/handler/RouteRequestFilterer.java @@ -0,0 +1,218 @@ +/* + * Apache License + * Version 2.0, January 2004 + * http://www.apache.org/licenses/ + * + * TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + * + * 1. Definitions. + * + * "License" shall mean the terms and conditions for use, reproduction, + * and distribution as defined by Sections 1 through 9 of this document. + * + * "Licensor" shall mean the copyright owner or entity authorized by + * the copyright owner that is granting the License. + * + * "Legal Entity" shall mean the union of the acting entity and all + * other entities that control, are controlled by, or are under common + * control with that entity. For the purposes of this definition, + * "control" means (i) the power, direct or indirect, to cause the + * direction or management of such entity, whether by contract or + * otherwise, or (ii) ownership of fifty percent (50%) or more of the + * outstanding shares, or (iii) beneficial ownership of such entity. + * + * "You" (or "Your") shall mean an individual or Legal Entity + * exercising permissions granted by this License. + * + * "Source" form shall mean the preferred form for making modifications, + * including but not limited to software source code, documentation + * source, and configuration files. + * + * "Object" form shall mean any form resulting from mechanical + * transformation or translation of a Source form, including but + * not limited to compiled object code, generated documentation, + * and conversions to other media types. + * + * "Work" shall mean the work of authorship, whether in Source or + * Object form, made available under the License, as indicated by a + * copyright notice that is included in or attached to the work + * (an example is provided in the Appendix below). + * + * "Derivative Works" shall mean any work, whether in Source or Object + * form, that is based on (or derived from) the Work and for which the + * editorial revisions, annotations, elaborations, or other modifications + * represent, as a whole, an original work of authorship. For the purposes + * of this License, Derivative Works shall not include works that remain + * separable from, or merely link (or bind by name) to the interfaces of, + * the Work and Derivative Works thereof. + * + * "Contribution" shall mean any work of authorship, including + * the original version of the Work and any modifications or additions + * to that Work or Derivative Works thereof, that is intentionally + * submitted to Licensor for inclusion in the Work by the copyright owner + * or by an individual or Legal Entity authorized to submit on behalf of + * the copyright owner. For the purposes of this definition, "submitted" + * means any form of electronic, verbal, or written communication sent + * to the Licensor or its representatives, including but not limited to + * communication on electronic mailing lists, source code control systems, + * and issue tracking systems that are managed by, or on behalf of, the + * Licensor for the purpose of discussing and improving the Work, but + * excluding communication that is conspicuously marked or otherwise + * designated in writing by the copyright owner as "Not a Contribution." + * + * "Contributor" shall mean Licensor and any individual or Legal Entity + * on behalf of whom a Contribution has been received by Licensor and + * subsequently incorporated within the Work. + * + * 2. Grant of Copyright License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * copyright license to reproduce, prepare Derivative Works of, + * publicly display, publicly perform, sublicense, and distribute the + * Work and such Derivative Works in Source or Object form. + * + * 3. Grant of Patent License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * (except as stated in this section) patent license to make, have made, + * use, offer to sell, sell, import, and otherwise transfer the Work, + * where such license applies only to those patent claims licensable + * by such Contributor that are necessarily infringed by their + * Contribution(s) alone or by combination of their Contribution(s) + * with the Work to which such Contribution(s) was submitted. If You + * institute patent litigation against any entity (including a + * cross-claim or counterclaim in a lawsuit) alleging that the Work + * or a Contribution incorporated within the Work constitutes direct + * or contributory patent infringement, then any patent licenses + * granted to You under this License for that Work shall terminate + * as of the date such litigation is filed. + * + * 4. Redistribution. You may reproduce and distribute copies of the + * Work or Derivative Works thereof in any medium, with or without + * modifications, and in Source or Object form, provided that You + * meet the following conditions: + * + * (a) You must give any other recipients of the Work or + * Derivative Works a copy of this License; and + * + * (b) You must cause any modified files to carry prominent notices + * stating that You changed the files; and + * + * (c) You must retain, in the Source form of any Derivative Works + * that You distribute, all copyright, patent, trademark, and + * attribution notices from the Source form of the Work, + * excluding those notices that do not pertain to any part of + * the Derivative Works; and + * + * (d) If the Work includes a "NOTICE" text file as part of its + * distribution, then any Derivative Works that You distribute must + * include a readable copy of the attribution notices contained + * within such NOTICE file, excluding those notices that do not + * pertain to any part of the Derivative Works, in at least one + * of the following places: within a NOTICE text file distributed + * as part of the Derivative Works; within the Source form or + * documentation, if provided along with the Derivative Works; or, + * within a display generated by the Derivative Works, if and + * wherever such third-party notices normally appear. The contents + * of the NOTICE file are for informational purposes only and + * do not modify the License. You may add Your own attribution + * notices within Derivative Works that You distribute, alongside + * or as an addendum to the NOTICE text from the Work, provided + * that such additional attribution notices cannot be construed + * as modifying the License. + * + * You may add Your own copyright statement to Your modifications and + * may provide additional or different license terms and conditions + * for use, reproduction, or distribution of Your modifications, or + * for any such Derivative Works as a whole, provided Your use, + * reproduction, and distribution of the Work otherwise complies with + * the conditions stated in this License. + * + * 5. Submission of Contributions. Unless You explicitly state otherwise, + * any Contribution intentionally submitted for inclusion in the Work + * by You to the Licensor shall be under the terms and conditions of + * this License, without any additional terms or conditions. + * Notwithstanding the above, nothing herein shall supersede or modify + * the terms of any separate license agreement you may have executed + * with Licensor regarding such Contributions. + * + * 6. Trademarks. This License does not grant permission to use the trade + * names, trademarks, service marks, or product names of the Licensor, + * except as required for reasonable and customary use in describing the + * origin of the Work and reproducing the content of the NOTICE file. + * + * 7. Disclaimer of Warranty. Unless required by applicable law or + * agreed to in writing, Licensor provides the Work (and each + * Contributor provides its Contributions) on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied, including, without limitation, any warranties or conditions + * of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + * PARTICULAR PURPOSE. You are solely responsible for determining the + * appropriateness of using or redistributing the Work and assume any + * risks associated with Your exercise of permissions under this License. + * + * 8. Limitation of Liability. In no event and under no legal theory, + * whether in tort (including negligence), contract, or otherwise, + * unless required by applicable law (such as deliberate and grossly + * negligent acts) or agreed to in writing, shall any Contributor be + * liable to You for damages, including any direct, indirect, special, + * incidental, or consequential damages of any character arising as a + * result of this License or out of the use or inability to use the + * Work (including but not limited to damages for loss of goodwill, + * work stoppage, computer failure or malfunction, or any and all + * other commercial damages or losses), even if such Contributor + * has been advised of the possibility of such damages. + * + * 9. Accepting Warranty or Additional Liability. While redistributing + * the Work or Derivative Works thereof, You may choose to offer, + * and charge a fee for, acceptance of support, warranty, indemnity, + * or other liability obligations and/or rights consistent with this + * License. However, in accepting such obligations, You may act only + * on Your own behalf and on Your sole responsibility, not on behalf + * of any other Contributor, and only if You agree to indemnify, + * defend, and hold each Contributor harmless for any liability + * incurred by, or claims asserted against, such Contributor by reason + * of your accepting any such warranty or additional liability. + * + * END OF TERMS AND CONDITIONS + * + * APPENDIX: How to apply the Apache License to your work. + * + * To apply the Apache License to your work, attach the following + * boilerplate notice, with the fields enclosed by brackets "[]" + * replaced with your own identifying information. (Don't include + * the brackets!) The text should be enclosed in the appropriate + * comment syntax for the file format. We also recommend that a + * file or class name and description of purpose be included on the + * same "printed page" as the copyright notice for easier + * identification within third-party archives. + * + * Copyright 2016 Ian Kelly + * + * 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 wssimulator.handler; + +import org.jetbrains.annotations.NotNull; +import spark.Request; +import wssimulator.WSSimulation; + +/** + * + */ +public interface RouteRequestFilterer { + + boolean filter(@NotNull WSSimulation wsSimulation, @NotNull Request request); +} diff --git a/src/main/java/wssimulator/handler/XMLHandler.java b/src/main/java/wssimulator/handler/XMLHandler.java index 46d3fa3..d8327ca 100644 --- a/src/main/java/wssimulator/handler/XMLHandler.java +++ b/src/main/java/wssimulator/handler/XMLHandler.java @@ -224,8 +224,8 @@ public XMLHandler(@NotNull WSSimulation WSSimulation) { @Override protected boolean validate(@NotNull WSSimulation WSSimulation, @Nullable String body) { - if (isNotEmpty(WSSimulation.requestStructure) && isNotEmpty(body)) - return validateXml(WSSimulation.requestStructure, body); + if (isNotEmpty(WSSimulation.request.requestStructure) && isNotEmpty(body)) + return validateXml(WSSimulation.request.requestStructure, body); return true; } diff --git a/src/main/resources/example.yml b/src/main/resources/example.yml index 6f68e77..1e98028 100644 --- a/src/main/resources/example.yml +++ b/src/main/resources/example.yml @@ -1,4 +1,5 @@ -path: /hello +path: /payment +request: action: allocate consumes: application/xml httpMethod: post response: I expect this as the response diff --git a/src/main/resources/wssimulator-version.txt b/src/main/resources/wssimulator-version.txt index 967b33f..08456a4 100644 --- a/src/main/resources/wssimulator-version.txt +++ b/src/main/resources/wssimulator-version.txt @@ -1 +1 @@ -0.2.7 \ No newline at end of file +0.2.8 \ No newline at end of file diff --git a/src/test/groovy/wssimulator/CalledCounterTestSpecification.groovy b/src/test/groovy/wssimulator/CalledCounterTestSpecification.groovy index 5c7b381..6b21af7 100644 --- a/src/test/groovy/wssimulator/CalledCounterTestSpecification.groovy +++ b/src/test/groovy/wssimulator/CalledCounterTestSpecification.groovy @@ -237,7 +237,7 @@ class CalledCounterTestSpecification extends Specification { when: WSSimulator.setPort(port) int addedSimulationId = WSSimulator.addSimulation(new File(getClass().getResource("/simple.yml").toURI())) - int foundSimulationId = WSSimulator.findSimulationIdByPath("/hello") + int foundSimulationId = WSSimulator.findSimulationId("/hello",HttpMethod.get) then: addedSimulationId == foundSimulationId cleanup: @@ -250,7 +250,7 @@ class CalledCounterTestSpecification extends Specification { when: WSSimulator.setPort(port) int addedSimulationId = WSSimulator.addSimulation(new File(getClass().getResource("/simple.yml").toURI())) - int foundSimulationId = WSSimulator.findSimulationIdByPath("/notfound") + int foundSimulationId = WSSimulator.findSimulationId("/notfound",HttpMethod.get) then: addedSimulationId != foundSimulationId cleanup: diff --git a/src/test/groovy/wssimulator/HappyPathTestSpecification.groovy b/src/test/groovy/wssimulator/HappyPathTestSpecification.groovy index 46988a9..bff196f 100644 --- a/src/test/groovy/wssimulator/HappyPathTestSpecification.groovy +++ b/src/test/groovy/wssimulator/HappyPathTestSpecification.groovy @@ -207,6 +207,8 @@ package wssimulator import spock.lang.Specification +import java.util.concurrent.TimeUnit + import static io.restassured.RestAssured.given import static org.hamcrest.core.IsEqual.equalTo @@ -280,11 +282,14 @@ class HappyPathTestSpecification extends Specification { WSSimulator.addSimulationOnClasspath("/simple3.yml") then: WSSimulator.loadedSimulationCount() == 2 + println "rest assured - opening" + TestUtils.waitFor(10, TimeUnit.SECONDS) given().port(port).get("/hello/tester").then().assertThat() .statusCode(200).and().body(equalTo("Hello World tester")) given().port(port).get("/test").then().assertThat() .statusCode(200) cleanup: + println "last yaml - shutting down" WSSimulator.shutdown() } diff --git a/src/test/groovy/wssimulator/RouteFilterSimulationSpecification.groovy b/src/test/groovy/wssimulator/RouteFilterSimulationSpecification.groovy new file mode 100644 index 0000000..109e56f --- /dev/null +++ b/src/test/groovy/wssimulator/RouteFilterSimulationSpecification.groovy @@ -0,0 +1,248 @@ +/* + * Apache License + * Version 2.0, January 2004 + * http://www.apache.org/licenses/ + * + * TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + * + * 1. Definitions. + * + * "License" shall mean the terms and conditions for use, reproduction, + * and distribution as defined by Sections 1 through 9 of this document. + * + * "Licensor" shall mean the copyright owner or entity authorized by + * the copyright owner that is granting the License. + * + * "Legal Entity" shall mean the union of the acting entity and all + * other entities that control, are controlled by, or are under common + * control with that entity. For the purposes of this definition, + * "control" means (i) the power, direct or indirect, to cause the + * direction or management of such entity, whether by contract or + * otherwise, or (ii) ownership of fifty percent (50%) or more of the + * outstanding shares, or (iii) beneficial ownership of such entity. + * + * "You" (or "Your") shall mean an individual or Legal Entity + * exercising permissions granted by this License. + * + * "Source" form shall mean the preferred form for making modifications, + * including but not limited to software source code, documentation + * source, and configuration files. + * + * "Object" form shall mean any form resulting from mechanical + * transformation or translation of a Source form, including but + * not limited to compiled object code, generated documentation, + * and conversions to other media types. + * + * "Work" shall mean the work of authorship, whether in Source or + * Object form, made available under the License, as indicated by a + * copyright notice that is included in or attached to the work + * (an example is provided in the Appendix below). + * + * "Derivative Works" shall mean any work, whether in Source or Object + * form, that is based on (or derived from) the Work and for which the + * editorial revisions, annotations, elaborations, or other modifications + * represent, as a whole, an original work of authorship. For the purposes + * of this License, Derivative Works shall not include works that remain + * separable from, or merely link (or bind by name) to the interfaces of, + * the Work and Derivative Works thereof. + * + * "Contribution" shall mean any work of authorship, including + * the original version of the Work and any modifications or additions + * to that Work or Derivative Works thereof, that is intentionally + * submitted to Licensor for inclusion in the Work by the copyright owner + * or by an individual or Legal Entity authorized to submit on behalf of + * the copyright owner. For the purposes of this definition, "submitted" + * means any form of electronic, verbal, or written communication sent + * to the Licensor or its representatives, including but not limited to + * communication on electronic mailing lists, source code control systems, + * and issue tracking systems that are managed by, or on behalf of, the + * Licensor for the purpose of discussing and improving the Work, but + * excluding communication that is conspicuously marked or otherwise + * designated in writing by the copyright owner as "Not a Contribution." + * + * "Contributor" shall mean Licensor and any individual or Legal Entity + * on behalf of whom a Contribution has been received by Licensor and + * subsequently incorporated within the Work. + * + * 2. Grant of Copyright License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * copyright license to reproduce, prepare Derivative Works of, + * publicly display, publicly perform, sublicense, and distribute the + * Work and such Derivative Works in Source or Object form. + * + * 3. Grant of Patent License. Subject to the terms and conditions of + * this License, each Contributor hereby grants to You a perpetual, + * worldwide, non-exclusive, no-charge, royalty-free, irrevocable + * (except as stated in this section) patent license to make, have made, + * use, offer to sell, sell, import, and otherwise transfer the Work, + * where such license applies only to those patent claims licensable + * by such Contributor that are necessarily infringed by their + * Contribution(s) alone or by combination of their Contribution(s) + * with the Work to which such Contribution(s) was submitted. If You + * institute patent litigation against any entity (including a + * cross-claim or counterclaim in a lawsuit) alleging that the Work + * or a Contribution incorporated within the Work constitutes direct + * or contributory patent infringement, then any patent licenses + * granted to You under this License for that Work shall terminate + * as of the date such litigation is filed. + * + * 4. Redistribution. You may reproduce and distribute copies of the + * Work or Derivative Works thereof in any medium, with or without + * modifications, and in Source or Object form, provided that You + * meet the following conditions: + * + * (a) You must give any other recipients of the Work or + * Derivative Works a copy of this License; and + * + * (b) You must cause any modified files to carry prominent notices + * stating that You changed the files; and + * + * (c) You must retain, in the Source form of any Derivative Works + * that You distribute, all copyright, patent, trademark, and + * attribution notices from the Source form of the Work, + * excluding those notices that do not pertain to any part of + * the Derivative Works; and + * + * (d) If the Work includes a "NOTICE" text file as part of its + * distribution, then any Derivative Works that You distribute must + * include a readable copy of the attribution notices contained + * within such NOTICE file, excluding those notices that do not + * pertain to any part of the Derivative Works, in at least one + * of the following places: within a NOTICE text file distributed + * as part of the Derivative Works; within the Source form or + * documentation, if provided along with the Derivative Works; or, + * within a display generated by the Derivative Works, if and + * wherever such third-party notices normally appear. The contents + * of the NOTICE file are for informational purposes only and + * do not modify the License. You may add Your own attribution + * notices within Derivative Works that You distribute, alongside + * or as an addendum to the NOTICE text from the Work, provided + * that such additional attribution notices cannot be construed + * as modifying the License. + * + * You may add Your own copyright statement to Your modifications and + * may provide additional or different license terms and conditions + * for use, reproduction, or distribution of Your modifications, or + * for any such Derivative Works as a whole, provided Your use, + * reproduction, and distribution of the Work otherwise complies with + * the conditions stated in this License. + * + * 5. Submission of Contributions. Unless You explicitly state otherwise, + * any Contribution intentionally submitted for inclusion in the Work + * by You to the Licensor shall be under the terms and conditions of + * this License, without any additional terms or conditions. + * Notwithstanding the above, nothing herein shall supersede or modify + * the terms of any separate license agreement you may have executed + * with Licensor regarding such Contributions. + * + * 6. Trademarks. This License does not grant permission to use the trade + * names, trademarks, service marks, or product names of the Licensor, + * except as required for reasonable and customary use in describing the + * origin of the Work and reproducing the content of the NOTICE file. + * + * 7. Disclaimer of Warranty. Unless required by applicable law or + * agreed to in writing, Licensor provides the Work (and each + * Contributor provides its Contributions) on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied, including, without limitation, any warranties or conditions + * of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + * PARTICULAR PURPOSE. You are solely responsible for determining the + * appropriateness of using or redistributing the Work and assume any + * risks associated with Your exercise of permissions under this License. + * + * 8. Limitation of Liability. In no event and under no legal theory, + * whether in tort (including negligence), contract, or otherwise, + * unless required by applicable law (such as deliberate and grossly + * negligent acts) or agreed to in writing, shall any Contributor be + * liable to You for damages, including any direct, indirect, special, + * incidental, or consequential damages of any character arising as a + * result of this License or out of the use or inability to use the + * Work (including but not limited to damages for loss of goodwill, + * work stoppage, computer failure or malfunction, or any and all + * other commercial damages or losses), even if such Contributor + * has been advised of the possibility of such damages. + * + * 9. Accepting Warranty or Additional Liability. While redistributing + * the Work or Derivative Works thereof, You may choose to offer, + * and charge a fee for, acceptance of support, warranty, indemnity, + * or other liability obligations and/or rights consistent with this + * License. However, in accepting such obligations, You may act only + * on Your own behalf and on Your sole responsibility, not on behalf + * of any other Contributor, and only if You agree to indemnify, + * defend, and hold each Contributor harmless for any liability + * incurred by, or claims asserted against, such Contributor by reason + * of your accepting any such warranty or additional liability. + * + * END OF TERMS AND CONDITIONS + * + * APPENDIX: How to apply the Apache License to your work. + * + * To apply the Apache License to your work, attach the following + * boilerplate notice, with the fields enclosed by brackets "[]" + * replaced with your own identifying information. (Don't include + * the brackets!) The text should be enclosed in the appropriate + * comment syntax for the file format. We also recommend that a + * file or class name and description of purpose be included on the + * same "printed page" as the copyright notice for easier + * identification within third-party archives. + * + * Copyright 2016 Ian Kelly + * + * 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 wssimulator + +import io.restassured.http.ContentType +import spock.lang.Specification + +import static io.restassured.RestAssured.given +import static org.hamcrest.core.IsEqual.equalTo + +/** + * Tests reading all yaml files for XML files. + */ +class RouteFilterSimulationSpecification extends Specification { + + def "Validate the route filter feature"() { + setup: + int port = TestUtils.randomPort() + when: + WSSimulator.setPort(port) + WSSimulator.addSimulation(new File(getClass().getResource("/route/route1.yml").toURI())) + WSSimulator.addSimulation(new File(getClass().getResource("/route/route2.yml").toURI())) + WSSimulator.addSimulation(new File(getClass().getResource("/route/route3.yml").toURI())) + then: + given().port(port) + .contentType(ContentType.XML) + .body("This is just random test with Action1 contained within it") + .post("/publish").then().assertThat() + .statusCode(200).and().body(equalTo("FilteredByAction1")) + given().port(port) + .contentType(ContentType.XML) + .body("This is just random test with Action2 contained within it") + .post("/publish").then().assertThat() + .statusCode(200).and().body(equalTo("FilteredByAction2")) + given().port(port) + .contentType(ContentType.XML) + .body("This is just random test with Action3 contained within it") + .post("/publish").then().assertThat() + .statusCode(200).and().body(equalTo("FilteredByAction3")) + + cleanup: + WSSimulator.shutdown() + } + +} diff --git a/src/test/groovy/wssimulator/TestUtils.groovy b/src/test/groovy/wssimulator/TestUtils.groovy index 40db54a..e123eee 100644 --- a/src/test/groovy/wssimulator/TestUtils.groovy +++ b/src/test/groovy/wssimulator/TestUtils.groovy @@ -205,6 +205,8 @@ package wssimulator +import java.util.concurrent.TimeUnit + /** * */ @@ -212,6 +214,10 @@ class TestUtils { static Random rand = new Random() static int randomPort() { - rand.nextInt(500) + 55000 + rand.nextInt(5000) + 55000 + } + + static void waitFor(long duration, TimeUnit unit) { + Thread.sleep(unit.toMillis(duration)) } } diff --git a/src/test/resources/json/json1.yml b/src/test/resources/json/json1.yml index 8c9bccc..133919e 100644 --- a/src/test/resources/json/json1.yml +++ b/src/test/resources/json/json1.yml @@ -2,7 +2,9 @@ path: /publish consumes: application/json httpMethod: post response: ok -requestStructure: | +request: + requestStructure: | + { "$schema": "http://json-schema.org/draft-04/schema#", "title": "/etc/fstab", diff --git a/src/test/resources/route/route1.yml b/src/test/resources/route/route1.yml new file mode 100644 index 0000000..3581b49 --- /dev/null +++ b/src/test/resources/route/route1.yml @@ -0,0 +1,6 @@ +path: /publish +httpMethod: post +request: + filterType: contains + filter: Action1 +response: FilteredByAction1 diff --git a/src/test/resources/route/route2.yml b/src/test/resources/route/route2.yml new file mode 100644 index 0000000..e3571a8 --- /dev/null +++ b/src/test/resources/route/route2.yml @@ -0,0 +1,6 @@ +path: /publish +httpMethod: post +request: + filterType: contains + filter: Action2 +response: FilteredByAction2 diff --git a/src/test/resources/route/route3.yml b/src/test/resources/route/route3.yml new file mode 100644 index 0000000..ae893a5 --- /dev/null +++ b/src/test/resources/route/route3.yml @@ -0,0 +1,6 @@ +path: /publish +httpMethod: post +request: + filterType: contains + filter: Action3 +response: FilteredByAction3 diff --git a/src/test/resources/xml/xmlValidationExample.yml b/src/test/resources/xml/xmlValidationExample.yml index 8fab85a..f3914a7 100644 --- a/src/test/resources/xml/xmlValidationExample.yml +++ b/src/test/resources/xml/xmlValidationExample.yml @@ -4,14 +4,15 @@ httpMethod: post successResponseCode: 201 badRequestResponseCode: 500 response: ok -requestStructure: - - - - - - - - - +request: + requestStructure: + + + + + + + + +