diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eec1f90 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +.idea/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + 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 [yyyy] [name of copyright owner] + + 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3bd3422 --- /dev/null +++ b/README.md @@ -0,0 +1,90 @@ +# ApiUnit + +ApiUnit is a free and simple library for checking REST API guidelines of your Java code. +It is based on [ArchUnit](https://www.archunit.org/) and uses whose deliverd Java code structure. +ApiUnit's focus is to automatically check compliance with guidelines in projects that are developed with Spring. The analysis takes into account common Spring annotations. + +## An Example + +### Add the Maven Central dependency to your project + +##### Maven ToDo: + +``` + + com.tngtech.archunit + archunit + 1.1.0 + test + +``` + +### Create a test + +#### Check a couple of Guidelines/Rules + +```java +import de.viadee.apiunit.ApiUnitTest; +import de.viadee.apiunit.ApiUnitTestResultObject; +import de.viadee.apiunit.rules.EnumerationValuesMustBeCapitalizedNamesWithUnderscores; +import de.viadee.apiunit.rules.HttpGetMethodsMustNotHaveRequestBody; +import de.viadee.apiunit.rules.HttpPatchMethodsMustHaveRequestBody; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class MyApiGuidelineTest { + + @Test + public void checkApiTests() { + ApiUnitTest myTest = new ApiUnitTest("de.viadee.project"); + myTest.addRule(new EnumerationValuesMustBeCapitalizedNamesWithUnderscores()); + myTest.addRule(new HttpGetMethodsMustNotHaveRequestBody()); + myTest.addRule(new HttpPatchMethodsMustHaveRequestBody()); + myTest.addRule(...); + ApiUnitTestResultObject testResult = myTest.check(); + System.out.println(testResult); + assertEquals(0, testResult.getViolationCounter()); + } +} +``` + +Currently, the following rules are available for consideration: +- HttpPutMethodsMustHaveRequestBody +- HttpPostMethodsMustHaveRequestBody +- HttpPatchMethodsMustHaveRequestBody +- HttpGetMethodsMustNotHaveRequestBody +- HttpDeleteMethodsMustNotHaveRequestBody +- EnumerationValuesMustBeCapitalizedNamesWithUnderscores +- EnumerationTypesMustBeUpperCamelCase +- CollectionIdsMustMatchExpression +- CollectionIdsMustBeValidCIdentifier + +These rules are based on Google´s [API design guide](https://cloud.google.com/apis/design). + +#### Check a single of Guideline/Rule + +```java +import de.viadee.apiunit.ApiUnitTestResultObject; +import de.viadee.apiunit.rules.CollectionIdsMustBeValidCIdentifier; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class MyApiGuidelineTest { + + @Test + public void collectionIds_Must_Be_Valid_C_Identifier() { + ApiUnitTestResultObject testResult = new CollectionIdsMustBeValidCIdentifier().check("de.viadee.project"); + assertEquals(0, testResult.getViolationCounter()); + } +} +``` + +## License + +ApiUnit is published under the Apache License 2.0, see http://www.apache.org/licenses/LICENSE-2.0 for details. + +It redistributes some third party libraries: + +* ArchUnit (https://www.archunit.org/), under Apache License 2.0 \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a1c4514 --- /dev/null +++ b/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.0.0 + + + + de.viadee + apiunit + 0.1 + + + 17 + 17 + UTF-8 + + + + + com.tngtech.archunit + archunit + 1.0.1 + compile + + + + org.springframework.boot + spring-boot-starter-web + + + org.junit.jupiter + junit-jupiter + test + + + \ No newline at end of file diff --git a/src/main/java/de/viadee/apiunit/ApiRule.java b/src/main/java/de/viadee/apiunit/ApiRule.java new file mode 100644 index 0000000..ca6fe9d --- /dev/null +++ b/src/main/java/de/viadee/apiunit/ApiRule.java @@ -0,0 +1,13 @@ +package de.viadee.apiunit; + +import com.tngtech.archunit.core.domain.JavaClasses; + +public interface ApiRule { + + ApiUnitTestResultObject check(JavaClasses importedClasses); + + ApiUnitTestResultObject check(String packagePath); + + @Override + String toString(); +} diff --git a/src/main/java/de/viadee/apiunit/ApiRuleCheck.java b/src/main/java/de/viadee/apiunit/ApiRuleCheck.java new file mode 100644 index 0000000..b934da5 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/ApiRuleCheck.java @@ -0,0 +1,19 @@ +package de.viadee.apiunit; + +public class ApiRuleCheck { + + ApiRule rule; + String location; + String description; + + public ApiRuleCheck(ApiRule rule, String location, String description) { + this.rule = rule; + this.location = location; + this.description = description; + } + + @Override + public String toString() { + return "ApiRuleCheck{ rule='" + rule + '\'' + ", location='" + location + '\'' + ", description='" + description + "'}"; + } +} diff --git a/src/main/java/de/viadee/apiunit/ApiUnitTest.java b/src/main/java/de/viadee/apiunit/ApiUnitTest.java new file mode 100644 index 0000000..46062f6 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/ApiUnitTest.java @@ -0,0 +1,36 @@ +package de.viadee.apiunit; + +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.importer.ClassFileImporter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +public class ApiUnitTest { + + Collection packages = new ArrayList<>(); + List rules = new ArrayList<>(); + + public ApiUnitTest(String[] packages) { + this.packages = new ArrayList<>(Arrays.asList(packages)); + } + + public ApiUnitTest(String packagePath) { + packages.add(packagePath); + } + + public void addRule(ApiRule rule){ + this.rules.add(rule); + } + + public ApiUnitTestResultObject check(){ + JavaClasses importedClasses = new ClassFileImporter().importPackages(packages); + ApiUnitTestResultObject result = new ApiUnitTestResultObject(); + for (ApiRule rule : rules) { + result.concat(rule.check(importedClasses)); + } + return result; + } +} diff --git a/src/main/java/de/viadee/apiunit/ApiUnitTestResultObject.java b/src/main/java/de/viadee/apiunit/ApiUnitTestResultObject.java new file mode 100644 index 0000000..c48e294 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/ApiUnitTestResultObject.java @@ -0,0 +1,78 @@ +package de.viadee.apiunit; + +import java.util.ArrayList; +import java.util.List; + +public class ApiUnitTestResultObject { + List violations = new ArrayList<>(); + List compliances = new ArrayList<>(); + + public ApiUnitTestResultObject() { + + } + + public ApiUnitTestResultObject(List violations, List compliances) { + this.violations = violations; + this.compliances = compliances; + } + + public int getViolationCounter() { + return violations.size(); + } + + public int getComplianceCounter() { + return compliances.size(); + } + + public void addViolations(List violations) { + this.violations.addAll(violations); + } + + public void addCompliances(List compliances) { + this.compliances.addAll(compliances); + } + + public List getViolations() { + return violations; + } + + public List getCompliances() { + return compliances; + } + + public void concat(ApiUnitTestResultObject tmp) { + this.addViolations(tmp.getViolations()); + this.addCompliances(tmp.getCompliances()); + } + + @Override + public String toString() { + return "ApiUnitTestResultObject{ \r\n \tviolations (n="+getViolationCounter()+")= [\r\n" + buildViolationsString() + " \t] \r\n \tcompliances (n="+getComplianceCounter()+")= [\r\n" + buildCompliancesString() + " \t] \r\n}"; + } + + private String buildViolationsString(){ + StringBuilder violationsString = new StringBuilder(); + for (int i = 0; i < violations.size(); i++) { + violationsString.append("\t \t"); + violationsString.append(violations.get(i).toString()); + if(i != (violations.size()-1)){ + violationsString.append(","); + } + violationsString.append("\r\n"); + } + return violationsString.toString(); + } + + private String buildCompliancesString(){ + StringBuilder compliancesString = new StringBuilder(); + for (int i = 0; i < compliances.size(); i++) { + compliancesString.append("\t \t"); + compliancesString.append(compliances.get(i).toString()); + if(i != (compliances.size()-1)){ + compliancesString.append(","); + } + compliancesString.append("\r\n"); + } + return compliancesString.toString(); + } +} diff --git a/src/main/java/de/viadee/apiunit/rules/CollectionIdsMustBeLowerCamelCase.java b/src/main/java/de/viadee/apiunit/rules/CollectionIdsMustBeLowerCamelCase.java new file mode 100644 index 0000000..09113eb --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/CollectionIdsMustBeLowerCamelCase.java @@ -0,0 +1,11 @@ +package de.viadee.apiunit.rules; + +public class CollectionIdsMustBeLowerCamelCase extends CollectionIdsMustMatchExpression { + + public CollectionIdsMustBeLowerCamelCase() { + super("([a-z]+[A-Z]?)*"); + this.sucessMsg = " is lower camel case"; + this.failMsg = " is not lower camel case"; + } + +} diff --git a/src/main/java/de/viadee/apiunit/rules/CollectionIdsMustBeValidCIdentifier.java b/src/main/java/de/viadee/apiunit/rules/CollectionIdsMustBeValidCIdentifier.java new file mode 100644 index 0000000..5bbb3e9 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/CollectionIdsMustBeValidCIdentifier.java @@ -0,0 +1,10 @@ +package de.viadee.apiunit.rules; + +public class CollectionIdsMustBeValidCIdentifier extends CollectionIdsMustMatchExpression { + public CollectionIdsMustBeValidCIdentifier() { + super("[_a-zA-Z][_a-zA-Z0-9]*"); + this.sucessMsg = " is valid c identifier"; + this.failMsg = " is no valid c identifier"; + } + +} diff --git a/src/main/java/de/viadee/apiunit/rules/CollectionIdsMustMatchExpression.java b/src/main/java/de/viadee/apiunit/rules/CollectionIdsMustMatchExpression.java new file mode 100644 index 0000000..2143074 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/CollectionIdsMustMatchExpression.java @@ -0,0 +1,57 @@ +package de.viadee.apiunit.rules; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaClasses; +import de.viadee.apiunit.ApiRule; +import de.viadee.apiunit.ApiRuleCheck; +import de.viadee.apiunit.ApiUnitTest; +import de.viadee.apiunit.ApiUnitTestResultObject; + +import java.util.ArrayList; +import java.util.List; + +public class CollectionIdsMustMatchExpression implements ApiRule { + private final String regEx; + String sucessMsg; + String failMsg; + private final List violations = new ArrayList<>(); + private final List compliances = new ArrayList<>(); + + public CollectionIdsMustMatchExpression(String regEx) { + this.regEx = regEx; + sucessMsg = " does match " + this.regEx; + failMsg = " does not match " + this.regEx; + } + + @Override + public ApiUnitTestResultObject check(JavaClasses importedClasses) { + for (JavaClass importedClass : importedClasses) { + checkMethodsForValidIdentifier(importedClass); + } + return new ApiUnitTestResultObject(violations, compliances); + } + + public ApiUnitTestResultObject check(String packagePath) { + ApiUnitTest myTest = new ApiUnitTest(packagePath); + myTest.addRule(this); + return myTest.check(); + } + + private void checkMethodsForValidIdentifier(JavaClass javaClass) { + javaClass.getAllMethods().forEach(javaMethod -> { + if (Utils.isHttpMethod(javaMethod)) { + String collectionId = Utils.getCollectionId(javaMethod); + if (collectionId.matches(this.regEx)) { + this.compliances.add(new ApiRuleCheck(this, javaMethod.getFullName(), collectionId + sucessMsg)); + } else { + this.violations.add(new ApiRuleCheck(this, javaMethod.getFullName(), collectionId + failMsg)); + } + } + }); + } + + @Override + public String toString(){ + return this.getClass().getSimpleName(); + } +} diff --git a/src/main/java/de/viadee/apiunit/rules/EnumerationTypesMustBeUpperCamelCase.java b/src/main/java/de/viadee/apiunit/rules/EnumerationTypesMustBeUpperCamelCase.java new file mode 100644 index 0000000..9bb3d51 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/EnumerationTypesMustBeUpperCamelCase.java @@ -0,0 +1,45 @@ +package de.viadee.apiunit.rules; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaClasses; +import de.viadee.apiunit.ApiRule; +import de.viadee.apiunit.ApiRuleCheck; +import de.viadee.apiunit.ApiUnitTest; +import de.viadee.apiunit.ApiUnitTestResultObject; + +import java.util.ArrayList; +import java.util.List; + +public class EnumerationTypesMustBeUpperCamelCase implements ApiRule { + private final List violations = new ArrayList<>(); + private final List compliances = new ArrayList<>(); + + @Override + public ApiUnitTestResultObject check(JavaClasses importedClasses) { + for (JavaClass importedClass : importedClasses) { + if (importedClass.isEnum()) { + if (valueIsUpperCamelCase(importedClass.getSimpleName())) { + this.compliances.add(new ApiRuleCheck(this, importedClass.getFullName(), importedClass.getName() + " is Upper Camel Case")); + }else{ + this.violations.add(new ApiRuleCheck(this, importedClass.getFullName(), importedClass.getName() + " is not Upper Camel Case")); + } + } + } + return new ApiUnitTestResultObject(violations, compliances); + } + + public ApiUnitTestResultObject check(String packagePath) { + ApiUnitTest myTest = new ApiUnitTest(packagePath); + myTest.addRule(this); + return myTest.check(); + } + + private boolean valueIsUpperCamelCase(String value) { + return value.matches("([A-Z][a-z]+)+"); + } + + @Override + public String toString(){ + return this.getClass().getSimpleName(); + } +} diff --git a/src/main/java/de/viadee/apiunit/rules/EnumerationValuesMustBeCapitalizedNamesWithUnderscores.java b/src/main/java/de/viadee/apiunit/rules/EnumerationValuesMustBeCapitalizedNamesWithUnderscores.java new file mode 100644 index 0000000..821b979 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/EnumerationValuesMustBeCapitalizedNamesWithUnderscores.java @@ -0,0 +1,48 @@ +package de.viadee.apiunit.rules; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.domain.JavaEnumConstant; +import de.viadee.apiunit.ApiRule; +import de.viadee.apiunit.ApiRuleCheck; +import de.viadee.apiunit.ApiUnitTest; +import de.viadee.apiunit.ApiUnitTestResultObject; + +import java.util.ArrayList; +import java.util.List; + +public class EnumerationValuesMustBeCapitalizedNamesWithUnderscores implements ApiRule { + private final List violations = new ArrayList<>(); + private final List compliances = new ArrayList<>(); + + @Override + public ApiUnitTestResultObject check(JavaClasses importedClasses) { + for (JavaClass importedClass : importedClasses) { + if (importedClass.isEnum()) { + for (JavaEnumConstant enumConstant : importedClass.getEnumConstants()) { + if (!valueIsCapitalizedNamesWithUnderscores(enumConstant.name())) { + this.violations.add(new ApiRuleCheck(this, importedClass.getFullName(), enumConstant.name() + " is not capitalized Name with Underscores")); + }else{ + this.compliances.add(new ApiRuleCheck(this, importedClass.getFullName(), enumConstant.name() + " is capitalized Name with Underscores")); + } + } + } + } + return new ApiUnitTestResultObject(violations, compliances); + } + + public ApiUnitTestResultObject check(String packagePath) { + ApiUnitTest myTest = new ApiUnitTest(packagePath); + myTest.addRule(this); + return myTest.check(); + } + + private boolean valueIsCapitalizedNamesWithUnderscores(String value) { + return value.matches("[A-Z][A-Z_]*"); + } + + @Override + public String toString(){ + return this.getClass().getSimpleName(); + } +} diff --git a/src/main/java/de/viadee/apiunit/rules/HttpDeleteMethodsMustNotHaveRequestBody.java b/src/main/java/de/viadee/apiunit/rules/HttpDeleteMethodsMustNotHaveRequestBody.java new file mode 100644 index 0000000..95ce7f6 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/HttpDeleteMethodsMustNotHaveRequestBody.java @@ -0,0 +1,63 @@ +package de.viadee.apiunit.rules; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.domain.JavaMethod; +import com.tngtech.archunit.core.domain.JavaParameter; +import de.viadee.apiunit.ApiRule; +import de.viadee.apiunit.ApiRuleCheck; +import de.viadee.apiunit.ApiUnitTest; +import de.viadee.apiunit.ApiUnitTestResultObject; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class HttpDeleteMethodsMustNotHaveRequestBody implements ApiRule { + private final List violations = new ArrayList<>(); + private final List compliances = new ArrayList<>(); + + @Override + public ApiUnitTestResultObject check(JavaClasses importedClasses) { + for (JavaClass importedClass : importedClasses) { + checkMethodsForDeleteMethodsForRequestBody(importedClass); + } + return new ApiUnitTestResultObject(violations, compliances); + } + + public ApiUnitTestResultObject check(String packagePath) { + ApiUnitTest myTest = new ApiUnitTest(packagePath); + myTest.addRule(this); + return myTest.check(); + } + + private void checkMethodsForDeleteMethodsForRequestBody(JavaClass javaClass) { + javaClass.getAllMethods().forEach(javaMethod -> { + if (Utils.isDeleteMethod(javaMethod)) { + checkForNotExistingRequestBody(javaMethod); + } + }); + } + + private void checkForNotExistingRequestBody(JavaMethod method) { + boolean hasExistingRequestBody = false; + List methodParameters = method.getParameters(); + for (JavaParameter methodParameter : methodParameters) { + Optional optionalRequestBody = methodParameter.tryGetAnnotationOfType(RequestBody.class); + if (optionalRequestBody.isPresent()) { + hasExistingRequestBody = true; + } + } + if (hasExistingRequestBody) { + this.violations.add(new ApiRuleCheck(this, method.getFullName(), "method has a Request Body")); + } else { + this.compliances.add(new ApiRuleCheck(this, method.getFullName(), "method has no Request Body")); + } + } + + @Override + public String toString(){ + return this.getClass().getSimpleName(); + } +} diff --git a/src/main/java/de/viadee/apiunit/rules/HttpGetMethodsMustNotHaveRequestBody.java b/src/main/java/de/viadee/apiunit/rules/HttpGetMethodsMustNotHaveRequestBody.java new file mode 100644 index 0000000..9df2e61 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/HttpGetMethodsMustNotHaveRequestBody.java @@ -0,0 +1,63 @@ +package de.viadee.apiunit.rules; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.domain.JavaMethod; +import com.tngtech.archunit.core.domain.JavaParameter; +import de.viadee.apiunit.ApiRule; +import de.viadee.apiunit.ApiRuleCheck; +import de.viadee.apiunit.ApiUnitTest; +import de.viadee.apiunit.ApiUnitTestResultObject; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class HttpGetMethodsMustNotHaveRequestBody implements ApiRule { + private final List violations = new ArrayList<>(); + private final List compliances = new ArrayList<>(); + + @Override + public ApiUnitTestResultObject check(JavaClasses importedClasses) { + for (JavaClass importedClass : importedClasses) { + checkMethodsForGetMethodsForRequestBody(importedClass); + } + return new ApiUnitTestResultObject(violations, compliances); + } + + public ApiUnitTestResultObject check(String packagePath) { + ApiUnitTest myTest = new ApiUnitTest(packagePath); + myTest.addRule(this); + return myTest.check(); + } + + private void checkMethodsForGetMethodsForRequestBody(JavaClass javaClass) { + javaClass.getAllMethods().forEach(javaMethod -> { + if (Utils.isGetMethod(javaMethod)) { + checkForNotExistingRequestBody(javaMethod); + } + }); + } + + private void checkForNotExistingRequestBody(JavaMethod method) { + boolean hasExistingRequestBody = false; + List methodParameters = method.getParameters(); + for (JavaParameter methodParameter : methodParameters) { + Optional optionalRequestBody = methodParameter.tryGetAnnotationOfType(RequestBody.class); + if (optionalRequestBody.isPresent()) { + hasExistingRequestBody = true; + } + } + if (hasExistingRequestBody) { + this.violations.add(new ApiRuleCheck(this, method.getFullName(), "method has a Request Body")); + } else { + this.compliances.add(new ApiRuleCheck(this, method.getFullName(), "method has no Request Body")); + } + } + + @Override + public String toString(){ + return this.getClass().getSimpleName(); + } +} diff --git a/src/main/java/de/viadee/apiunit/rules/HttpPatchMethodsMustHaveRequestBody.java b/src/main/java/de/viadee/apiunit/rules/HttpPatchMethodsMustHaveRequestBody.java new file mode 100644 index 0000000..993955b --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/HttpPatchMethodsMustHaveRequestBody.java @@ -0,0 +1,65 @@ +package de.viadee.apiunit.rules; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.domain.JavaMethod; +import com.tngtech.archunit.core.domain.JavaParameter; +import de.viadee.apiunit.ApiRule; +import de.viadee.apiunit.ApiRuleCheck; +import de.viadee.apiunit.ApiUnitTest; +import de.viadee.apiunit.ApiUnitTestResultObject; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class HttpPatchMethodsMustHaveRequestBody implements ApiRule { + private final List violations = new ArrayList<>(); + private final List compliances = new ArrayList<>(); + + @Override + public ApiUnitTestResultObject check(JavaClasses importedClasses) { + for (JavaClass importedClass : importedClasses) { + checkMethodsForPatchMethodsForRequestBody(importedClass); + } + return new ApiUnitTestResultObject(violations, compliances); + } + + public ApiUnitTestResultObject check(String packagePath) { + ApiUnitTest myTest = new ApiUnitTest(packagePath); + myTest.addRule(this); + return myTest.check(); + } + + private void checkMethodsForPatchMethodsForRequestBody(JavaClass javaClass) { + javaClass.getAllMethods().forEach(javaMethod -> { + if (Utils.isPatchMethod(javaMethod)) { + checkForExistingRequestBody(javaMethod); + } + }); + } + + private void checkForExistingRequestBody(JavaMethod method) { + boolean hasExistingAndRequiredRequestBody = false; + List methodParameters = method.getParameters(); + for (JavaParameter methodParameter : methodParameters) { + Optional optionalRequestBody = methodParameter.tryGetAnnotationOfType(RequestBody.class); + if (optionalRequestBody.isPresent()) { + if (optionalRequestBody.get().required()) { + hasExistingAndRequiredRequestBody = true; + } + } + } + if (!hasExistingAndRequiredRequestBody) { + this.violations.add(new ApiRuleCheck(this, method.getFullName(), "method has no Request Body or the Request Body is set to be not required")); + } else { + this.compliances.add(new ApiRuleCheck(this, method.getFullName(), "method has a required Request Body")); + } + } + + @Override + public String toString(){ + return this.getClass().getSimpleName(); + } +} diff --git a/src/main/java/de/viadee/apiunit/rules/HttpPostMethodsMustHaveRequestBody.java b/src/main/java/de/viadee/apiunit/rules/HttpPostMethodsMustHaveRequestBody.java new file mode 100644 index 0000000..0234020 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/HttpPostMethodsMustHaveRequestBody.java @@ -0,0 +1,65 @@ +package de.viadee.apiunit.rules; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.domain.JavaMethod; +import com.tngtech.archunit.core.domain.JavaParameter; +import de.viadee.apiunit.ApiRule; +import de.viadee.apiunit.ApiRuleCheck; +import de.viadee.apiunit.ApiUnitTest; +import de.viadee.apiunit.ApiUnitTestResultObject; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class HttpPostMethodsMustHaveRequestBody implements ApiRule { + private final List violations = new ArrayList<>(); + private final List compliances = new ArrayList<>(); + + @Override + public ApiUnitTestResultObject check(JavaClasses importedClasses) { + for (JavaClass importedClass : importedClasses) { + checkMethodsForPostMethodsForRequestBody(importedClass); + } + return new ApiUnitTestResultObject(violations, compliances); + } + + public ApiUnitTestResultObject check(String packagePath) { + ApiUnitTest myTest = new ApiUnitTest(packagePath); + myTest.addRule(this); + return myTest.check(); + } + + private void checkMethodsForPostMethodsForRequestBody(JavaClass javaClass) { + javaClass.getAllMethods().forEach(javaMethod -> { + if (Utils.isPostMethod(javaMethod)) { + checkForExistingRequestBody(javaMethod); + } + }); + } + + private void checkForExistingRequestBody(JavaMethod method) { + boolean hasExistingAndRequiredRequestBody = false; + List methodParameters = method.getParameters(); + for (JavaParameter methodParameter : methodParameters) { + Optional optionalRequestBody = methodParameter.tryGetAnnotationOfType(RequestBody.class); + if (optionalRequestBody.isPresent()) { + if (optionalRequestBody.get().required()) { + hasExistingAndRequiredRequestBody = true; + } + } + } + if (!hasExistingAndRequiredRequestBody) { + this.violations.add(new ApiRuleCheck(this, method.getFullName(), "method has no Request Body or the Request Body is set to be not required")); + } else { + this.compliances.add(new ApiRuleCheck(this, method.getFullName(), "method has a required Request Body")); + } + } + + @Override + public String toString(){ + return this.getClass().getSimpleName(); + } +} diff --git a/src/main/java/de/viadee/apiunit/rules/HttpPutMethodsMustHaveRequestBody.java b/src/main/java/de/viadee/apiunit/rules/HttpPutMethodsMustHaveRequestBody.java new file mode 100644 index 0000000..17e15a1 --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/HttpPutMethodsMustHaveRequestBody.java @@ -0,0 +1,65 @@ +package de.viadee.apiunit.rules; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.domain.JavaMethod; +import com.tngtech.archunit.core.domain.JavaParameter; +import de.viadee.apiunit.ApiRule; +import de.viadee.apiunit.ApiRuleCheck; +import de.viadee.apiunit.ApiUnitTest; +import de.viadee.apiunit.ApiUnitTestResultObject; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class HttpPutMethodsMustHaveRequestBody implements ApiRule { + private final List violations = new ArrayList<>(); + private final List compliances = new ArrayList<>(); + + @Override + public ApiUnitTestResultObject check(JavaClasses importedClasses) { + for (JavaClass importedClass : importedClasses) { + checkMethodsForPutMethodsForRequestBody(importedClass); + } + return new ApiUnitTestResultObject(violations, compliances); + } + + public ApiUnitTestResultObject check(String packagePath) { + ApiUnitTest myTest = new ApiUnitTest(packagePath); + myTest.addRule(this); + return myTest.check(); + } + + private void checkMethodsForPutMethodsForRequestBody(JavaClass javaClass) { + javaClass.getAllMethods().forEach(javaMethod -> { + if (Utils.isPutMethod(javaMethod)) { + checkForExistingRequestBody(javaMethod); + } + }); + } + + private void checkForExistingRequestBody(JavaMethod method) { + boolean hasExistingAndRequiredRequestBody = false; + List methodParameters = method.getParameters(); + for (JavaParameter methodParameter : methodParameters) { + Optional optionalRequestBody = methodParameter.tryGetAnnotationOfType(RequestBody.class); + if (optionalRequestBody.isPresent()) { + if (optionalRequestBody.get().required()) { + hasExistingAndRequiredRequestBody = true; + } + } + } + if (!hasExistingAndRequiredRequestBody) { + this.violations.add(new ApiRuleCheck(this, method.getFullName(), "method has no Request Body or the Request Body is set to be not required")); + } else { + this.compliances.add(new ApiRuleCheck(this, method.getFullName(), "method has a required Request Body")); + } + } + + @Override + public String toString(){ + return this.getClass().getSimpleName(); + } +} diff --git a/src/main/java/de/viadee/apiunit/rules/Utils.java b/src/main/java/de/viadee/apiunit/rules/Utils.java new file mode 100644 index 0000000..988820e --- /dev/null +++ b/src/main/java/de/viadee/apiunit/rules/Utils.java @@ -0,0 +1,206 @@ +package de.viadee.apiunit.rules; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaMethod; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +class Utils { + + public static boolean isPatchMethod(JavaMethod method) { + if (method.isAnnotatedWith(PatchMapping.class)) { + return true; + } else { + return checkRequestMappingForMethodType(method, RequestMethod.PATCH); + } + } + + public static boolean isPutMethod(JavaMethod method) { + if (method.isAnnotatedWith(PutMapping.class)) { + return true; + } else { + return checkRequestMappingForMethodType(method, RequestMethod.PUT); + } + } + + public static boolean isPostMethod(JavaMethod method) { + if (method.isAnnotatedWith(PostMapping.class)) { + return true; + } else { + return checkRequestMappingForMethodType(method, RequestMethod.POST); + } + } + + public static boolean isDeleteMethod(JavaMethod method) { + if (method.isAnnotatedWith(DeleteMapping.class)) { + return true; + } else { + return checkRequestMappingForMethodType(method, RequestMethod.DELETE); + } + } + + public static boolean isGetMethod(JavaMethod method) { + if (method.isAnnotatedWith(GetMapping.class)) { + return true; + } else { + return checkRequestMappingForMethodType(method, RequestMethod.GET); + } + } + + private static boolean checkRequestMappingForMethodType(JavaMethod method, RequestMethod methodType){ + Optional optionalRequestMapping = method.tryGetAnnotationOfType(RequestMapping.class); + if (optionalRequestMapping.isPresent()) { + for (RequestMethod requestMethod : optionalRequestMapping.get().method()) { + if (requestMethod == methodType) { + return true; + } + } + } + return false; + } + + public static boolean isHttpMethod(JavaMethod method) { + if (method.isAnnotatedWith(RequestMapping.class)) { + return true; + } else if (method.isAnnotatedWith(GetMapping.class)) { + return true; + } else if (method.isAnnotatedWith(PostMapping.class)) { + return true; + } else if (method.isAnnotatedWith(PutMapping.class)) { + return true; + } else if (method.isAnnotatedWith(DeleteMapping.class)) { + return true; + } else if (method.isAnnotatedWith(PatchMapping.class)) { + return true; + } + return false; + } + + public static String getCollectionId(JavaMethod method) { + String[] tmp = new String[]{""}; + if (method.isAnnotatedWith(RequestMapping.class)) { + tmp = getURLFromRequestMappingMethod(method); + } else if (method.isAnnotatedWith(GetMapping.class)) { + tmp = getURLFromGetMappingMethod(method); + } else if (method.isAnnotatedWith(PostMapping.class)) { + tmp = getURLFromPostMappingMethod(method); + } else if (method.isAnnotatedWith(PutMapping.class)) { + tmp = getURLFromPutMappingMethod(method); + } else if (method.isAnnotatedWith(DeleteMapping.class)) { + tmp = getURLFromDeleteMappingMethod(method); + } else if (method.isAnnotatedWith(PatchMapping.class)) { + tmp = getURLFromPatchMappingMethod(method); + } + for (String s : tmp) { + String[] splittedURL = s.split("/"); + for (int y = splittedURL.length - 1; y > 0; y--) { + if (!isPathParam(splittedURL[y])) { + return splittedURL[y]; + } + } + } + return ""; + } + + private static String[] getURLFromRequestMappingMethod(JavaMethod method) { + Optional opt = method.tryGetAnnotationOfType(RequestMapping.class); + if (opt.isPresent()) { + String[] methodURLs = getValuesOrPaths(opt.get().value(), opt.get().path()); + return concatClassUrlWithMethodUrl(method, methodURLs); + } + return new String[]{""}; + } + + private static String[] getURLFromGetMappingMethod(JavaMethod method) { + Optional opt = method.tryGetAnnotationOfType(GetMapping.class); + if (opt.isPresent()) { + String[] methodURLs = getValuesOrPaths(opt.get().value(), opt.get().path()); + return concatClassUrlWithMethodUrl(method, methodURLs); + } + return new String[]{""}; + } + + + private static String[] getURLFromPostMappingMethod(JavaMethod method) { + Optional opt = method.tryGetAnnotationOfType(PostMapping.class); + if (opt.isPresent()) { + String[] methodURLs = getValuesOrPaths(opt.get().value(), opt.get().path()); + return concatClassUrlWithMethodUrl(method, methodURLs); + } + return new String[]{""}; + } + + private static String[] getURLFromPutMappingMethod(JavaMethod method) { + Optional opt = method.tryGetAnnotationOfType(PutMapping.class); + if (opt.isPresent()) { + String[] methodURLs = getValuesOrPaths(opt.get().value(), opt.get().path()); + return concatClassUrlWithMethodUrl(method, methodURLs); + } + return new String[]{""}; + } + + private static String[] getURLFromDeleteMappingMethod(JavaMethod method) { + Optional opt = method.tryGetAnnotationOfType(DeleteMapping.class); + if (opt.isPresent()) { + String[] methodURLs = getValuesOrPaths(opt.get().value(), opt.get().path()); + return concatClassUrlWithMethodUrl(method, methodURLs); + } + return new String[]{""}; + } + + private static String[] getURLFromPatchMappingMethod(JavaMethod method) { + Optional opt = method.tryGetAnnotationOfType(PatchMapping.class); + if (opt.isPresent()) { + String[] methodURLs = getValuesOrPaths(opt.get().value(), opt.get().path()); + return concatClassUrlWithMethodUrl(method, methodURLs); + } + return new String[]{""}; + } + + private static String[] concatClassUrlWithMethodUrl(JavaMethod method, String[] methodURLs) { + String[] preFixURLs = new String[0]; + JavaClass owner = method.getOwner(); + Optional optionalRequestMapping = owner.tryGetAnnotationOfType(RequestMapping.class); + if (optionalRequestMapping.isPresent()) { + preFixURLs = getValuesOrPaths(optionalRequestMapping.get().value(), optionalRequestMapping.get().path()); + } + List urls = new ArrayList<>(); + if (preFixURLs.length > 0) { + if(methodURLs != null){ + for (String methodURL : methodURLs) { + StringBuilder methodURLBuilder = new StringBuilder(methodURL); + for (String preFixURL : preFixURLs) { + if(methodURLBuilder.charAt(0) != '/'){ + methodURLBuilder.insert(0, '/'); + } + urls.add(preFixURL + methodURLBuilder); + } + } + String[] results = new String[urls.size()]; + results = urls.toArray(results); + return results; + }else{ + return preFixURLs; + } + } else { + return methodURLs; + } + } + + private static String[] getValuesOrPaths(String[] value, String[] path) { + if (value != null && value.length > 0) { + return value; + } else if (path != null && path.length > 0) { + return path; + } else { + return null; + } + } + + private static boolean isPathParam(String s) { + return s.matches("\\{[A-Za-z0-9_$]*}"); + } +} diff --git a/src/test/java/de/viadee/apiunit/ExampleUsages.java b/src/test/java/de/viadee/apiunit/ExampleUsages.java new file mode 100644 index 0000000..5ecb0c9 --- /dev/null +++ b/src/test/java/de/viadee/apiunit/ExampleUsages.java @@ -0,0 +1,101 @@ +package de.viadee.apiunit; +import de.viadee.apiunit.rules.*; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ExampleUsages { + + @Test + public void checkHttpGetMethodsMustNotHaveRequestBody() { + ApiUnitTestResultObject testResult = new HttpGetMethodsMustNotHaveRequestBody().check("de.viadee.apiunit.demo"); + System.out.println(testResult); + assertEquals(5, testResult.getComplianceCounter()); + assertEquals(1, testResult.getViolationCounter()); + } + + @Test + public void checkHttpPostMethodsMustHaveRequestBody() { + ApiUnitTestResultObject testResult = new HttpPostMethodsMustHaveRequestBody().check("de.viadee.apiunit.demo"); + System.out.println(testResult); + assertEquals(1, testResult.getComplianceCounter()); + assertEquals(2, testResult.getViolationCounter()); + } + + @Test + public void checkHttpPutMethodsMustHaveRequestBody() { + ApiUnitTestResultObject testResult = new HttpPutMethodsMustHaveRequestBody().check("de.viadee.apiunit.demo"); + System.out.println(testResult); + assertEquals(1, testResult.getComplianceCounter()); + assertEquals(2, testResult.getViolationCounter()); + } + + @Test + public void checkHttpPatchMethodsMustHaveRequestBody() { + ApiUnitTestResultObject testResult = new HttpPatchMethodsMustHaveRequestBody().check("de.viadee.apiunit.demo"); + System.out.println(testResult); + assertEquals(1, testResult.getComplianceCounter()); + assertEquals(2, testResult.getViolationCounter()); + } + + @Test + public void checkHttpDeleteMethodsMustNotHaveRequestBody() { + ApiUnitTestResultObject testResult = new HttpDeleteMethodsMustNotHaveRequestBody().check("de.viadee.apiunit.demo"); + System.out.println(testResult); + assertEquals(1, testResult.getComplianceCounter()); + assertEquals(2, testResult.getViolationCounter()); + } + + @Test + public void checkCollectionIdsMustBeValidCIdentifier() { + ApiUnitTestResultObject testResult = new CollectionIdsMustBeValidCIdentifier().check("de.viadee.apiunit.demo"); + System.out.println(testResult); + assertEquals(12, testResult.getComplianceCounter()); + assertEquals(6, testResult.getViolationCounter()); + } + + @Test + public void checkCollectionIdsMustBeLowerCamelCase() { + ApiUnitTestResultObject testResult = new CollectionIdsMustBeLowerCamelCase().check("de.viadee.apiunit.demo"); + System.out.println(testResult); + assertEquals(12, testResult.getComplianceCounter()); + assertEquals(6, testResult.getViolationCounter()); + } + + @Test + public void checkCollectionIdsMustMatchExpression() { + ApiUnitTestResultObject testResult = new CollectionIdsMustMatchExpression("([a-z]+-)*[a-z]+").check("de.viadee.apiunit.demo"); + System.out.println(testResult); + assertEquals(12, testResult.getComplianceCounter()); + assertEquals(6, testResult.getViolationCounter()); + } + + @Test + public void checkEnumerationValuesMustBeCapitalizedNamesWithUnderscores() { + ApiUnitTestResultObject testResult = new EnumerationValuesMustBeCapitalizedNamesWithUnderscores().check("de.viadee.apiunit.demo"); + System.out.println(testResult); + assertEquals(3, testResult.getComplianceCounter()); + assertEquals(1, testResult.getViolationCounter()); + } + + @Test + public void checkEnumerationTypesMustBeUpperCamelCase() { + ApiUnitTestResultObject testResult = new EnumerationTypesMustBeUpperCamelCase().check("de.viadee.apiunit.demo"); + System.out.println(testResult); + assertEquals(1, testResult.getComplianceCounter()); + assertEquals(0, testResult.getViolationCounter()); + } + + @Test + public void checkMultipleRules() { + ApiUnitTest myTest = new ApiUnitTest("de.viadee.apiunit.demo"); + myTest.addRule(new CollectionIdsMustBeValidCIdentifier()); + myTest.addRule(new EnumerationValuesMustBeCapitalizedNamesWithUnderscores()); + myTest.addRule(new HttpGetMethodsMustNotHaveRequestBody()); + myTest.addRule(new HttpPatchMethodsMustHaveRequestBody()); + ApiUnitTestResultObject testResult = myTest.check(); + System.out.println(testResult); + //assertEquals(0, testResult.getViolationCounter()); + assertEquals(10, testResult.getViolationCounter()); + } +} diff --git a/src/test/java/de/viadee/apiunit/demo/Book.java b/src/test/java/de/viadee/apiunit/demo/Book.java new file mode 100644 index 0000000..cfd4e43 --- /dev/null +++ b/src/test/java/de/viadee/apiunit/demo/Book.java @@ -0,0 +1,64 @@ +package de.viadee.apiunit.demo; + +import java.util.Objects; + +public class Book { + + private Long bookId; + private String author; + private String title; + private Genre genre; + private Shelf shelf; + + public Long getBookId() { + return bookId; + } + + public void setBookId(Long bookId) { + this.bookId = bookId; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Genre getGenre() { + return genre; + } + + public void setGenre(Genre genre) { + this.genre = genre; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof Book)) + return false; + Book book = (Book) o; + return Objects.equals(this.bookId, book.getBookId()); + } + + @Override + public int hashCode() { + return Objects.hash(bookId); + } + + @Override + public String toString() { + return "Book{" + "id=" + this.getBookId() + "; title= " + this.getTitle() + "; author= " + this.getAuthor() + '}'; + } +} diff --git a/src/test/java/de/viadee/apiunit/demo/BookController.java b/src/test/java/de/viadee/apiunit/demo/BookController.java new file mode 100644 index 0000000..17f4e0c --- /dev/null +++ b/src/test/java/de/viadee/apiunit/demo/BookController.java @@ -0,0 +1,40 @@ +package de.viadee.apiunit.demo; + +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/shelves/{shelfId}/book`s") +class BookController { + + @GetMapping() + public List getAllBooks(@PathVariable Long shelfId, @RequestBody FilterObject filterObject) { + return null; + } + + @GetMapping("{bookId}") + public Book getBook(@PathVariable Long shelfId, @PathVariable Long bookId) { + return null; + } + + @PatchMapping("/{bookId}") + public Book patchBook(@PathVariable Long shelfId, @PathVariable Long bookId, @RequestBody(required = false) RequestBodyBook newBook) { + return null; + } + + @PutMapping("/{bookId}") + public Book putBook(@PathVariable Long shelfId, @PathVariable Long bookId, @RequestBody(required = false) RequestBodyBook newBook) { + return null; + } + + @PostMapping + public Book postBook(@RequestBody(required = false) RequestBodyBook newBook) { + return null; + } + + @DeleteMapping("/{bookId}") + public Book deleteBook(@PathVariable Long shelfId, @PathVariable Long bookId, @RequestBody(required = false) RequestBodyBook newBook) { + return null; + } +} \ No newline at end of file diff --git a/src/test/java/de/viadee/apiunit/demo/Dvd.java b/src/test/java/de/viadee/apiunit/demo/Dvd.java new file mode 100644 index 0000000..1740204 --- /dev/null +++ b/src/test/java/de/viadee/apiunit/demo/Dvd.java @@ -0,0 +1,55 @@ +package de.viadee.apiunit.demo; + +import java.util.Objects; + +public class Dvd { + + private Long dvdId; + private String title; + private String director; + private Shelf shelf; + + public Long getDvdId() { + return dvdId; + } + + public void setDvdId(Long dvdId) { + this.dvdId = dvdId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDirector() { + return director; + } + + public void setDirector(String director) { + this.director = director; + } + + @Override + public String toString() { + return "Dvd{" + "id=" + this.getDvdId() + "; title= " + this.getTitle() + "; director= " + this.getDirector() + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof Dvd)) + return false; + Dvd dvd = (Dvd) o; + return Objects.equals(this.dvdId, dvd.getDvdId()); + } + + @Override + public int hashCode() { + return Objects.hash(dvdId); + } +} diff --git a/src/test/java/de/viadee/apiunit/demo/DvdController.java b/src/test/java/de/viadee/apiunit/demo/DvdController.java new file mode 100644 index 0000000..9c2fbef --- /dev/null +++ b/src/test/java/de/viadee/apiunit/demo/DvdController.java @@ -0,0 +1,40 @@ +package de.viadee.apiunit.demo; + +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/shelves/{shelfId}/dvd") +class DvdController{ + + @RequestMapping(method = RequestMethod.GET) + public List getAllDvds(@PathVariable Long shelfId) { + return null; + } + + @GetMapping("/{dvdId}") + public Dvd getDvd(@PathVariable Long shelfId, @PathVariable Long dvdId) { + return null; + } + + @PatchMapping("/{dvdId}") + public Dvd patchDvd(@PathVariable Long shelfId, @PathVariable Long dvdId, @RequestParam String title, @RequestParam String director) { + return null; + } + + @PutMapping("/{dvdId}") + public Dvd putDvd(@PathVariable Long shelfId, @PathVariable Long dvdId, @RequestParam String title, @RequestParam String director) { + return null; + } + + @PostMapping + public Dvd postDvd(@RequestParam String title, @RequestParam String director, @RequestParam Long shelfId) { + return null; + } + + @DeleteMapping("/{dvdId}") + public Dvd deleteDvd(@PathVariable Long shelfId, @PathVariable Long dvdId) { + return null; + } +} \ No newline at end of file diff --git a/src/test/java/de/viadee/apiunit/demo/FilterObject.java b/src/test/java/de/viadee/apiunit/demo/FilterObject.java new file mode 100644 index 0000000..50bdf8e --- /dev/null +++ b/src/test/java/de/viadee/apiunit/demo/FilterObject.java @@ -0,0 +1,4 @@ +package de.viadee.apiunit.demo; + +public class FilterObject { +} diff --git a/src/test/java/de/viadee/apiunit/demo/Genre.java b/src/test/java/de/viadee/apiunit/demo/Genre.java new file mode 100644 index 0000000..2a76416 --- /dev/null +++ b/src/test/java/de/viadee/apiunit/demo/Genre.java @@ -0,0 +1,8 @@ +package de.viadee.apiunit.demo; + +public enum Genre { + THRILLER, + ACTION_ADVENTURE, + DRAMA, + Romance; +} diff --git a/src/test/java/de/viadee/apiunit/demo/RequestBodyBook.java b/src/test/java/de/viadee/apiunit/demo/RequestBodyBook.java new file mode 100644 index 0000000..5ffe9d1 --- /dev/null +++ b/src/test/java/de/viadee/apiunit/demo/RequestBodyBook.java @@ -0,0 +1,64 @@ +package de.viadee.apiunit.demo; + +import java.util.Objects; + +public class RequestBodyBook { + + private String author; + + private String title; + + private Genre genre; + + public RequestBodyBook(String author, String title, Genre genre) { + this.author = author; + this.title = title; + this.genre = genre; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Genre getGenre() { + return genre; + } + + public void setGenre(Genre genre) { + this.genre = genre; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof RequestBodyBook)) + return false; + RequestBodyBook book = (RequestBodyBook) o; + return Objects.equals(this.author, book.getAuthor()) && Objects.equals(this.title, book.getTitle()) && Objects.equals(this.getGenre(), book.getGenre()); + } + + @Override + public int hashCode() { + return Objects.hash(title,author,genre); + } + + @Override + public String toString() { + return "RequestBodyBook{title= " + this.getTitle() + "; author= " + this.getAuthor() + '}'; + } + + +} diff --git a/src/test/java/de/viadee/apiunit/demo/Shelf.java b/src/test/java/de/viadee/apiunit/demo/Shelf.java new file mode 100644 index 0000000..9050557 --- /dev/null +++ b/src/test/java/de/viadee/apiunit/demo/Shelf.java @@ -0,0 +1,70 @@ +package de.viadee.apiunit.demo; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +public class Shelf { + + private Long shelfId; + private Set books = new HashSet<>(); + private Set dvds = new HashSet<>(); + + public Shelf() { + } + + public Shelf(Long shelfId, Set books, Set dvds) { + this.shelfId = shelfId; + this.books = books; + this.dvds = dvds; + } + + public Shelf(Long shelfId) { + this.shelfId = shelfId; + } + + public Long getShelfId() { + return shelfId; + } + + public void setShelfId(Long shelfId) { + this.shelfId = shelfId; + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + + public Set getDvds() { + return dvds; + } + + public void setDvds(Set dvds) { + this.dvds = dvds; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof Book)) + return false; + Shelf shelf = (Shelf) o; + return Objects.equals(this.shelfId, shelf.getShelfId()); + } + + @Override + public int hashCode() { + return Objects.hash(shelfId); + } + + @Override + public String toString() { + return "Shelf{" + "id=" + this.getShelfId() + "; books= " + this.getBooks() + "; dvds= " + this.getDvds() + '}'; + } + +} diff --git a/src/test/java/de/viadee/apiunit/demo/ShelfController.java b/src/test/java/de/viadee/apiunit/demo/ShelfController.java new file mode 100644 index 0000000..976f23f --- /dev/null +++ b/src/test/java/de/viadee/apiunit/demo/ShelfController.java @@ -0,0 +1,41 @@ +package de.viadee.apiunit.demo; + +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Set; + +@RestController +@RequestMapping("/shelves") +class ShelfController { + + @GetMapping() + List getAllShelves() { + return null; + } + + @GetMapping("/{shelfId}") + Shelf getShelf(@PathVariable Long shelfId) { + return null; + } + + @PatchMapping("/{shelfId}") + public Shelf patchShelf(Long shelfId, @RequestBody Set books) { + return null; + } + + @PutMapping("/{shelfId}") + public Shelf putShelf(Long shelfId, @RequestBody Set books) { + return null; + } + + @PostMapping + public Shelf postShelf(@RequestBody Shelf shelf) { + return null; + } + + @DeleteMapping("/{shelfId}") + public Shelf deleteShelf(@RequestBody Shelf shelf) { + return null; + } +} \ No newline at end of file