From db675a54e8e7bb3553e059d237774b4b737488f1 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Wed, 21 Mar 2018 14:16:56 -0400 Subject: added custom validators for use with openpojo Change-Id: I7c9fdeb6bccddb1b7ec6bf845351731c3043aafa Issue-ID: SO-510 Signed-off-by: Benjamin, Max (mb388a) --- adapters/mso-catalog-db-adapter/pom.xml | 8 +- adapters/mso-tenant-adapter/pom.xml | 6 - adapters/mso-vnf-adapter/pom.xml | 344 +++++---- asdc-controller/pom.xml | 6 - bpmn/MSOInfrastructureBPMN/pom.xml | 773 ++++++++++----------- bpmn/MSORESTClient/pom.xml | 82 +-- common/pom.xml | 6 - .../openpojo/rules/CustomSetterMustExistRule.java | 58 ++ .../openpojo/rules/EqualsAndHashCodeTester.java | 103 +++ .../mso/openpojo/rules/HasAnnotationMatcher.java | 69 ++ .../HasAnnotationPropertyWithValueMatcher.java | 75 ++ .../openpojo/rules/HasEqualsAndHashCodeRule.java | 81 +++ .../mso/openpojo/rules/HasToStringRule.java | 72 ++ .../mso/openpojo/rules/ToStringTester.java | 55 ++ pom.xml | 25 +- 15 files changed, 1114 insertions(+), 649 deletions(-) create mode 100644 common/src/main/java/org/openecomp/mso/openpojo/rules/CustomSetterMustExistRule.java create mode 100644 common/src/main/java/org/openecomp/mso/openpojo/rules/EqualsAndHashCodeTester.java create mode 100644 common/src/main/java/org/openecomp/mso/openpojo/rules/HasAnnotationMatcher.java create mode 100644 common/src/main/java/org/openecomp/mso/openpojo/rules/HasAnnotationPropertyWithValueMatcher.java create mode 100644 common/src/main/java/org/openecomp/mso/openpojo/rules/HasEqualsAndHashCodeRule.java create mode 100644 common/src/main/java/org/openecomp/mso/openpojo/rules/HasToStringRule.java create mode 100644 common/src/main/java/org/openecomp/mso/openpojo/rules/ToStringTester.java diff --git a/adapters/mso-catalog-db-adapter/pom.xml b/adapters/mso-catalog-db-adapter/pom.xml index 67b1f3124e..67d81dbb49 100644 --- a/adapters/mso-catalog-db-adapter/pom.xml +++ b/adapters/mso-catalog-db-adapter/pom.xml @@ -45,7 +45,7 @@ jaxws-maven-plugin - [2.3,) + [2.3.1,) wsgen @@ -86,12 +86,6 @@ 1.0.4 test - - org.hamcrest - hamcrest-all - 1.3 - test - javax.servlet javax.servlet-api diff --git a/adapters/mso-tenant-adapter/pom.xml b/adapters/mso-tenant-adapter/pom.xml index 6604e7e664..0b15b0dd6f 100644 --- a/adapters/mso-tenant-adapter/pom.xml +++ b/adapters/mso-tenant-adapter/pom.xml @@ -151,11 +151,5 @@ status-control ${project.version} - - org.mockito - mockito-all - 1.10.19 - test - diff --git a/adapters/mso-vnf-adapter/pom.xml b/adapters/mso-vnf-adapter/pom.xml index 643f42f2ba..c4eb83c879 100644 --- a/adapters/mso-vnf-adapter/pom.xml +++ b/adapters/mso-vnf-adapter/pom.xml @@ -1,175 +1,169 @@ - - 4.0.0 - - org.onap.so - adapters - 1.2.0-SNAPSHOT - - org.onap.so.adapters - mso-vnf-adapter - war - mso-vnf-adapter - Web Service and REST endpoint for VNF operations - - - ${project.artifactId}-${project.version} - - - maven-war-plugin - 2.4 - - WebContent - false - true - - - - org.jvnet.jax-ws-commons - jaxws-maven-plugin - 2.3 - - - Synch - - wsgen - - - true - org.openecomp.mso.adapters.vnf.MsoVnfAdapterImpl - true - true - - - - Asynch - - wsgen - - - true - org.openecomp.mso.adapters.vnf.MsoVnfAdapterAsyncImpl - true - true - - - - - - org.onap.so.adapters - mso-adapter-utils - ${project.version} - - - com.sun.xml.ws - jaxws-tools - 2.2.7 - - - - - - - - - org.eclipse.m2e - lifecycle-mapping - 1.0.0 - - - - - - - org.jvnet.jax-ws-commons - - - jaxws-maven-plugin - - - [2.3,) - - - wsgen - - - - - - - - - - - - - - - - org.onap.so.adapters - mso-adapter-utils - ${project.version} - - - org.onap.so.adapters - mso-adapters-rest-interface - ${project.version} - - - org.onap.so.adapters - mso-vnf-adapter-async-client - ${project.version} - - - javax - javaee-web-api - 6.0 - provided - - - org.jboss.spec.javax.ejb - jboss-ejb-api_3.2_spec - 1.0.0.Final - provided - - - org.jboss.ejb3 - jboss-ejb3-ext-api - 2.2.0.Final - provided - - - org.onap.so - status-control - ${project.version} - - - org.onap.so - aria-client - 1.2.0 - - - org.jmockit - jmockit - 1.8 - test - - - junit - junit - 4.12 - test - - - - - - - - - - - - - + + 4.0.0 + + org.onap.so + adapters + 1.2.0-SNAPSHOT + + org.onap.so.adapters + mso-vnf-adapter + war + mso-vnf-adapter + Web Service and REST endpoint for VNF operations + + + ${project.artifactId}-${project.version} + + + maven-war-plugin + 2.4 + + WebContent + false + true + + + + org.codehaus.mojo + jaxws-maven-plugin + 2.4.1 + + + Synch + + wsgen + + + true + org.openecomp.mso.adapters.vnf.MsoVnfAdapterImpl + true + true + + + + Asynch + + wsgen + + + true + org.openecomp.mso.adapters.vnf.MsoVnfAdapterAsyncImpl + true + true + + + + + + org.onap.so.adapters + mso-adapter-utils + ${project.version} + + + com.sun.xml.ws + jaxws-tools + 2.2.7 + + + + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + + org.codehaus.mojo + + + jaxws-maven-plugin + + + [2.4.1,) + + + wsgen + + + + + + + + + + + + + + + + org.onap.so.adapters + mso-adapter-utils + ${project.version} + + + org.onap.so.adapters + mso-adapters-rest-interface + ${project.version} + + + org.onap.so.adapters + mso-vnf-adapter-async-client + ${project.version} + + + javax + javaee-web-api + 6.0 + provided + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.2_spec + 1.0.0.Final + provided + + + org.jboss.ejb3 + jboss-ejb3-ext-api + 2.2.0.Final + provided + + + org.onap.so + status-control + ${project.version} + + + org.onap.so + aria-client + 1.2.0 + + + org.jmockit + jmockit + 1.8 + test + + + + + + + + + + + + + diff --git a/asdc-controller/pom.xml b/asdc-controller/pom.xml index a06a16ddd8..ff21372240 100644 --- a/asdc-controller/pom.xml +++ b/asdc-controller/pom.xml @@ -28,12 +28,6 @@ - - org.mockito - mockito-all - 1.10.19 - test - org.jmockit jmockit diff --git a/bpmn/MSOInfrastructureBPMN/pom.xml b/bpmn/MSOInfrastructureBPMN/pom.xml index f7cb61a512..0329f2883e 100644 --- a/bpmn/MSOInfrastructureBPMN/pom.xml +++ b/bpmn/MSOInfrastructureBPMN/pom.xml @@ -1,390 +1,383 @@ - - - org.onap.so - bpmn - 1.2.0-SNAPSHOT - - 4.0.0 - MSOInfrastructureBPMN - war - - - - - org.apache.maven.plugins - maven-war-plugin - 2.3 - - false - - - - org.apache.maven.plugins - maven-jar-plugin - 2.6 - - - default-jar - package - - jar - - - - org/openecomp/mso/bpmn/infrastructure/MSOInfrastructureApplication.class - - - - - - - - org.codehaus.mojo - build-helper-maven-plugin - 3.0.0 - - - - attach-artifact - - package - - - - ${project.build.directory}/${project.artifactId}-${project.version}.jar - jar - - - - - - - - org.apache.cxf - cxf-codegen-plugin - 2.5.2 - - - org.apache.maven.plugins - maven-eclipse-plugin - 2.8 - - - org.eclipse.jdt.groovy.core.groovyNature - - - **/*.groovy - - - - - maven-antrun-plugin - - - compile - compile - - - - - - - - - - - - - - run - - - - test-compile - test-compile - - - - - - - - - - - - - - run - - - - - - - org.apache.maven.plugins - maven-dependency-plugin - - - maven-failsafe-plugin - 2.6 - - - - integration-test - verify - - - - - - - - - - org.eclipse.m2e - lifecycle-mapping - 1.0.0 - - - - - - - org.apache.maven.plugins - - - maven-antrun-plugin - - - [1.3,) - - - run - - - - - - - - - - - - - - - - - - org.camunda.bpm - camunda-engine - compile - - - org.slf4j - slf4j-api - - - - - - - org.camunda.bpm - camunda-engine-cdi - - - com.google.guava - guava - 22.0 - - - - org.camunda.bpm.extension - camunda-bpm-assert - 1.2 - test - - - - - org.camunda.spin - camunda-spin-dataformat-all - compile - - - camunda-spin-dataformat-all - org.camunda.spin - compile - - - org.camunda.bpm - camunda-engine-plugin-spin - compile - - - - org.camunda.bpm - camunda-engine-plugin-connect - compile - - - - - org.webjars - bootstrap - 2.3.2 - - - - com.h2database - h2 - test - - - - com.fasterxml.uuid - java-uuid-generator - - - org.codehaus.groovy - groovy-all - - - org.apache.commons - commons-lang3 - 3.4 - - - org.onap.so - MSOCoreBPMN - ${project.version} - - - org.onap.so - MSOCommonBPMN - ${project.version} - - - org.onap.so - MSOCommonBPMN - ${project.version} - tests - test - - - - org.onap.so - MSOCoreBPMN - ${project.version} - tests - test - - - javax.ws.rs - javax.ws.rs-api - 2.0 - - - - org.springframework - spring-web - 4.3.2.RELEASE - - - org.onap.so - MSORESTClient - ${project.version} - - - - javax.servlet - javax.servlet-api - 3.0.1 - provided - - - - - org.camunda.bpm - camunda-engine-spring - - - org.springframework - spring-beans - 4.3.2.RELEASE - - - - org.camunda.bpm.workbench - camunda-workbench-dist-embeddable - 1.0.0-alpha8 - test - - - - org.onap.msb.java-sdk - msb-java-sdk - 1.0.0 - - - com.eclipsesource.jaxrs - jersey-all - - - - - - - org.eclipse.jetty - jetty-util - 9.4.8.v20171121 - - - - org.hibernate - hibernate-validator - 5.4.2.Final - - - - org.springframework - spring-test - 4.3.14.RELEASE - - - - com.googlecode.libphonenumber - libphonenumber - 8.9.1 - - - - junit - junit - 4.12 - test - - - - - - + + + org.onap.so + bpmn + 1.2.0-SNAPSHOT + + 4.0.0 + MSOInfrastructureBPMN + war + + + + + org.apache.maven.plugins + maven-war-plugin + 2.3 + + false + + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + default-jar + package + + jar + + + + org/openecomp/mso/bpmn/infrastructure/MSOInfrastructureApplication.class + + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.0.0 + + + + attach-artifact + + package + + + + ${project.build.directory}/${project.artifactId}-${project.version}.jar + jar + + + + + + + + org.apache.cxf + cxf-codegen-plugin + 2.5.2 + + + org.apache.maven.plugins + maven-eclipse-plugin + 2.8 + + + org.eclipse.jdt.groovy.core.groovyNature + + + **/*.groovy + + + + + maven-antrun-plugin + + + compile + compile + + + + + + + + + + + + + + run + + + + test-compile + test-compile + + + + + + + + + + + + + + run + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + maven-failsafe-plugin + 2.6 + + + + integration-test + verify + + + + + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + + org.apache.maven.plugins + + + maven-antrun-plugin + + + [1.3,) + + + run + + + + + + + + + + + + + + + + + + org.camunda.bpm + camunda-engine + compile + + + org.slf4j + slf4j-api + + + + + + + org.camunda.bpm + camunda-engine-cdi + + + com.google.guava + guava + 22.0 + + + + org.camunda.bpm.extension + camunda-bpm-assert + 1.2 + test + + + + + org.camunda.spin + camunda-spin-dataformat-all + compile + + + camunda-spin-dataformat-all + org.camunda.spin + compile + + + org.camunda.bpm + camunda-engine-plugin-spin + compile + + + + org.camunda.bpm + camunda-engine-plugin-connect + compile + + + + + org.webjars + bootstrap + 2.3.2 + + + + com.h2database + h2 + test + + + + com.fasterxml.uuid + java-uuid-generator + + + org.codehaus.groovy + groovy-all + + + org.apache.commons + commons-lang3 + 3.4 + + + org.onap.so + MSOCoreBPMN + ${project.version} + + + org.onap.so + MSOCommonBPMN + ${project.version} + + + org.onap.so + MSOCommonBPMN + ${project.version} + tests + test + + + + org.onap.so + MSOCoreBPMN + ${project.version} + tests + test + + + javax.ws.rs + javax.ws.rs-api + 2.0 + + + + org.springframework + spring-web + 4.3.2.RELEASE + + + org.onap.so + MSORESTClient + ${project.version} + + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + + + org.camunda.bpm + camunda-engine-spring + + + org.springframework + spring-beans + 4.3.2.RELEASE + + + + org.camunda.bpm.workbench + camunda-workbench-dist-embeddable + 1.0.0-alpha8 + test + + + + org.onap.msb.java-sdk + msb-java-sdk + 1.0.0 + + + com.eclipsesource.jaxrs + jersey-all + + + + + + + org.eclipse.jetty + jetty-util + 9.4.8.v20171121 + + + + org.hibernate + hibernate-validator + 5.4.2.Final + + + + org.springframework + spring-test + 4.3.14.RELEASE + + + + com.googlecode.libphonenumber + libphonenumber + 8.9.1 + + + + + + diff --git a/bpmn/MSORESTClient/pom.xml b/bpmn/MSORESTClient/pom.xml index 9c5ce1487e..6f2f007e5f 100644 --- a/bpmn/MSORESTClient/pom.xml +++ b/bpmn/MSORESTClient/pom.xml @@ -1,45 +1,37 @@ - - - 4.0.0 - - - org.onap.so - bpmn - 1.2.0-SNAPSHOT - - - MSORESTClient - - jar - - MSO REST Client API - - - - org.apache.httpcomponents - httpmime - 4.5 - - - - org.mockito - mockito-all - 1.10.19 - test - - - - org.onap.so - common - ${project.version} - - - - MSORESTClient - - - - - - + + + 4.0.0 + + + org.onap.so + bpmn + 1.2.0-SNAPSHOT + + + MSORESTClient + + jar + + MSO REST Client API + + + + org.apache.httpcomponents + httpmime + 4.5 + + + org.onap.so + common + ${project.version} + + + + MSORESTClient + + + + + + diff --git a/common/pom.xml b/common/pom.xml index aeef257d48..adc102e150 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -52,12 +52,6 @@ 3.1.0 provided - - org.hamcrest - hamcrest-library - 1.3 - test - org.jboss.spec.javax.ejb jboss-ejb-api_3.2_spec diff --git a/common/src/main/java/org/openecomp/mso/openpojo/rules/CustomSetterMustExistRule.java b/common/src/main/java/org/openecomp/mso/openpojo/rules/CustomSetterMustExistRule.java new file mode 100644 index 0000000000..fa24662d69 --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/openpojo/rules/CustomSetterMustExistRule.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.openpojo.rules; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.anything; +import static org.hamcrest.CoreMatchers.not; + +import org.hamcrest.Matcher; + +import com.openpojo.reflection.PojoClass; +import com.openpojo.reflection.PojoField; +import com.openpojo.validation.affirm.Affirm; +import com.openpojo.validation.rule.Rule; + +public class CustomSetterMustExistRule implements Rule { + + private Matcher[] excludeMatchers = new Matcher[]{not(anything())}; + private Matcher[] includeMatchers = new Matcher[]{anything()}; + public CustomSetterMustExistRule() { + } + @Override + public void evaluate(final PojoClass pojoClass) { + for (PojoField fieldEntry : pojoClass.getPojoFields()) { + if (!anyOf(excludeMatchers).matches(fieldEntry) && anyOf(includeMatchers).matches(fieldEntry) && !fieldEntry.isFinal() && !fieldEntry.hasSetter()) { + Affirm.fail(String.format("[%s] is missing a setter", fieldEntry)); + } + } + } + public CustomSetterMustExistRule exclude(Matcher... excludeMatchers) { + this.excludeMatchers = excludeMatchers; + return this; + } + + public CustomSetterMustExistRule include(Matcher... includeMatchers) { + this.includeMatchers = includeMatchers; + return this; + } + +} diff --git a/common/src/main/java/org/openecomp/mso/openpojo/rules/EqualsAndHashCodeTester.java b/common/src/main/java/org/openecomp/mso/openpojo/rules/EqualsAndHashCodeTester.java new file mode 100644 index 0000000000..f4192e6cc8 --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/openpojo/rules/EqualsAndHashCodeTester.java @@ -0,0 +1,103 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.openpojo.rules; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.anything; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.persistence.Id; + +import org.hamcrest.Matcher; + +import com.openpojo.business.annotation.BusinessKey; +import com.openpojo.random.RandomFactory; +import com.openpojo.reflection.PojoClass; +import com.openpojo.reflection.PojoField; +import com.openpojo.validation.affirm.Affirm; +import com.openpojo.validation.test.Tester; +import com.openpojo.validation.utils.ValidationHelper; + +public class EqualsAndHashCodeTester implements Tester { + + + private final Matcher m; + public EqualsAndHashCodeTester() { + m = anything(); + } + + public EqualsAndHashCodeTester(Matcher m) { + this.m = m; + } + @Override + public void run(PojoClass pojoClass) { + Class clazz = pojoClass.getClazz(); + if (anyOf(m).matches(clazz)) { + final Object classInstanceOne = ValidationHelper.getBasicInstance(pojoClass); + final Object classInstanceTwo = ValidationHelper.getBasicInstance(pojoClass); + Set identityFields = hasIdOrBusinessKey(pojoClass); + List otherFields = new ArrayList<>(pojoClass.getPojoFields()); + otherFields.removeAll(identityFields); + + for (PojoField field : identityFields) { + final Object value = RandomFactory.getRandomValue(field); + + field.invokeSetter(classInstanceOne, value); + field.invokeSetter(classInstanceTwo, value); + } + + for (PojoField field : otherFields) { + if (field.hasSetter()) { + final Object valueOne = RandomFactory.getRandomValue(field); + final Object valueTwo = RandomFactory.getRandomValue(field); + + field.invokeSetter(classInstanceOne, valueOne); + field.invokeSetter(classInstanceTwo, valueTwo); + } + } + + Affirm.affirmTrue("Equals test failed for [" + classInstanceOne.getClass().getName() + "]", classInstanceOne.equals(classInstanceTwo)); + + Affirm.affirmTrue("Equals test failed for [" + classInstanceOne.getClass().getName() + "]", classInstanceOne.equals( + classInstanceOne)); + + Affirm.affirmTrue("HashCode test failed for [" + classInstanceOne.getClass().getName() + "]", classInstanceOne.hashCode() == classInstanceTwo.hashCode()); + + Affirm.affirmFalse("Expected false for comparison of two unlike objects", classInstanceOne.equals("test")); + } + } + + + private Set hasIdOrBusinessKey(PojoClass pojoClass) { + final Set fields = new HashSet<>(); + + fields.addAll(pojoClass.getPojoFieldsAnnotatedWith(BusinessKey.class)); + fields.addAll(pojoClass.getPojoFieldsAnnotatedWith(Id.class)); + + return fields; + + } + +} diff --git a/common/src/main/java/org/openecomp/mso/openpojo/rules/HasAnnotationMatcher.java b/common/src/main/java/org/openecomp/mso/openpojo/rules/HasAnnotationMatcher.java new file mode 100644 index 0000000000..fdfb9695e7 --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/openpojo/rules/HasAnnotationMatcher.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.openpojo.rules; + +import static org.hamcrest.CoreMatchers.anything; + +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +public class HasAnnotationMatcher extends TypeSafeDiagnosingMatcher { + private final Class annotationType; + private final Matcher annotationMatcher; + + public HasAnnotationMatcher(final Class annotationType, final Matcher annotationMatcher) { + this.annotationType = annotationType; + this.annotationMatcher = annotationMatcher; + } + + @Override + protected boolean matchesSafely(final AnnotatedElement item, final Description mismatchDescription) { + final T annotation = item.getAnnotation(this.annotationType); + if (annotation == null) { + mismatchDescription.appendText("does not have annotation ").appendText(this.annotationType.getName()); + return false; + } + + if (!this.annotationMatcher.matches(annotation)) { + this.annotationMatcher.describeMismatch(annotation, mismatchDescription); + return false; + } + + return true; + } + + @Override + public void describeTo(final Description description) { + // Intentionally left blank. + } + + public static Matcher hasAnnotation(final Class annotationType) { + return hasAnnotation(annotationType, anything("")); + } + + public static Matcher hasAnnotation(final Class annotationType, final Matcher annotationMatcher) { + return new HasAnnotationMatcher(annotationType, annotationMatcher); + } +} diff --git a/common/src/main/java/org/openecomp/mso/openpojo/rules/HasAnnotationPropertyWithValueMatcher.java b/common/src/main/java/org/openecomp/mso/openpojo/rules/HasAnnotationPropertyWithValueMatcher.java new file mode 100644 index 0000000000..f0c1b18df2 --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/openpojo/rules/HasAnnotationPropertyWithValueMatcher.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.openpojo.rules; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +import com.openpojo.reflection.PojoField; + +public class HasAnnotationPropertyWithValueMatcher extends TypeSafeDiagnosingMatcher { + private final String attribute; + private final Matcher annotationMatcher; + private final Class annotationClass; + public HasAnnotationPropertyWithValueMatcher(Class clazz, String attribute, final Matcher annotationMatcher) { + this.attribute = attribute; + this.annotationMatcher = annotationMatcher; + this.annotationClass = clazz; + } + + @Override + protected boolean matchesSafely(T obj, final Description mismatchDescription) { + final PojoField temp = (PojoField)obj; + final Method method; + try { + Annotation a = temp.getAnnotation(this.annotationClass); + if (a == null) { + mismatchDescription.appendText("does not have annotation ").appendText(this.annotationClass.getSimpleName()); + return false; + } + method = a.getClass().getMethod(attribute); + final Object result = method.invoke(a); + if (!this.annotationMatcher.matches(result)) { + this.annotationMatcher.describeMismatch(result, mismatchDescription); + return false; + } + } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + mismatchDescription.appendText("does not have property ").appendText(attribute); + return false; + } + return true; + } + + @Override + public void describeTo(final Description description) { + // Intentionally left blank. + } + + public static Matcher hasAnnotationPropertyWithValue(Class clazz, String attribute, final Matcher annotationMatcher) { + return new HasAnnotationPropertyWithValueMatcher(clazz, attribute, annotationMatcher); + } +} diff --git a/common/src/main/java/org/openecomp/mso/openpojo/rules/HasEqualsAndHashCodeRule.java b/common/src/main/java/org/openecomp/mso/openpojo/rules/HasEqualsAndHashCodeRule.java new file mode 100644 index 0000000000..4ef560721f --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/openpojo/rules/HasEqualsAndHashCodeRule.java @@ -0,0 +1,81 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.openpojo.rules; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.anything; + +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; + +import org.hamcrest.Matcher; + +import com.openpojo.reflection.PojoClass; +import com.openpojo.validation.affirm.Affirm; +import com.openpojo.validation.rule.Rule; + +/** + * This rule ensures that classes have overriden the default equals and hashCode methods from Object + */ +public class HasEqualsAndHashCodeRule implements Rule { + + private final Matcher m; + public HasEqualsAndHashCodeRule() { + m = anything(); + } + + public HasEqualsAndHashCodeRule(Matcher m) { + this.m = m; + } + @Override + public void evaluate(PojoClass pojoClass) { + Class clazz = pojoClass.getClazz(); + if (anyOf(m).matches(clazz)) { + boolean hasEquals = false; + boolean hasHashCode = false; + final String name = clazz.getSimpleName(); + final Method[] methods; + if (clazz.getSuperclass().equals(Object.class)) { + methods = clazz.getDeclaredMethods(); + } else { + methods = clazz.getMethods(); + } + for (Method method : methods) { + Parameter[] parameters = method.getParameters(); + if ("equals".equals(method.getName()) && boolean.class.equals(method.getReturnType()) && parameters.length == 1 && Object.class.equals(parameters[0].getType())) { + hasEquals = true; + } else if ("hashCode".equals(method.getName()) && int.class.equals(method.getReturnType())) { + hasHashCode = true; + } + } + + if (!hasEquals) { + Affirm.fail(String.format( + "[%s] does not override equals", name)); + } + if (!hasHashCode) { + Affirm.fail(String.format( + "[%s] does not override hashCode", name)); + } + } + } + +} diff --git a/common/src/main/java/org/openecomp/mso/openpojo/rules/HasToStringRule.java b/common/src/main/java/org/openecomp/mso/openpojo/rules/HasToStringRule.java new file mode 100644 index 0000000000..f866650d66 --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/openpojo/rules/HasToStringRule.java @@ -0,0 +1,72 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.openpojo.rules; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.anything; + +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; + +import org.hamcrest.Matcher; + +import com.openpojo.reflection.PojoClass; +import com.openpojo.validation.affirm.Affirm; +import com.openpojo.validation.rule.Rule; + +public class HasToStringRule implements Rule { + + private final Matcher m; + public HasToStringRule() { + m = anything(); + } + + public HasToStringRule(Matcher m) { + this.m = m; + } + @Override + public void evaluate(PojoClass pojoClass) { + Class clazz = pojoClass.getClazz(); + if (anyOf(m).matches(clazz)) { + boolean hasToString = false; + final String name = clazz.getSimpleName(); + final Method[] methods; + if (clazz.getSuperclass().equals(Object.class)) { + methods = clazz.getDeclaredMethods(); + } else { + methods = clazz.getMethods(); + } + for (Method method : methods) { + Parameter[] parameters = method.getParameters(); + if ("toString".equals(method.getName()) && String.class.equals(method.getReturnType()) && parameters.length == 0) { + hasToString = true; + break; + } + } + + if (!hasToString) { + Affirm.fail(String.format( + "[%s] does not override toString", name)); + } + } + } + +} diff --git a/common/src/main/java/org/openecomp/mso/openpojo/rules/ToStringTester.java b/common/src/main/java/org/openecomp/mso/openpojo/rules/ToStringTester.java new file mode 100644 index 0000000000..bd582d45dc --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/openpojo/rules/ToStringTester.java @@ -0,0 +1,55 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.openpojo.rules; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.anything; + +import org.hamcrest.Matcher; + +import com.openpojo.reflection.PojoClass; +import com.openpojo.validation.affirm.Affirm; +import com.openpojo.validation.test.Tester; +import com.openpojo.validation.utils.ValidationHelper; + +public class ToStringTester implements Tester { + + private final Matcher m; + public ToStringTester() { + m = anything(); + } + + public ToStringTester(Matcher m) { + this.m = m; + } + + @Override + public void run(PojoClass pojoClass) { + Class clazz = pojoClass.getClazz(); + if (anyOf(m).matches(clazz)) { + final Object classInstance = ValidationHelper.getBasicInstance(pojoClass); + + Affirm.affirmFalse("Found default toString output", classInstance.toString().matches(Object.class.getName() + "@" + "\\w+")); + } + + } + +} diff --git a/pom.xml b/pom.xml index 0ca48368b2..8242e585ea 100644 --- a/pom.xml +++ b/pom.xml @@ -436,13 +436,22 @@ resteasy-jackson2-provider ${resteasy.version} - + + org.hamcrest + hamcrest-core + 1.3 + + + org.hamcrest + hamcrest-all + 1.3 + + junit junit 4.12 test - org.mockito mockito-all @@ -473,12 +482,6 @@ 1.6.2 test - - org.hamcrest - hamcrest-all - 1.3 - test - com.github.tomakehurst wiremock @@ -564,12 +567,6 @@ 2.5 compile - - org.hamcrest - hamcrest-core - 1.3 - test - log4j log4j -- cgit 1.2.3-korg