2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.openpojo.rules;
23 import java.lang.annotation.Annotation;
24 import java.lang.reflect.Field;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
28 import org.hamcrest.Description;
29 import org.hamcrest.Matcher;
30 import org.hamcrest.TypeSafeDiagnosingMatcher;
31 import org.onap.so.logger.MsoLogger;
33 import com.openpojo.reflection.PojoField;
35 public class HasAnnotationPropertyWithValueMatcher<T extends PojoField> extends TypeSafeDiagnosingMatcher<T> {
36 private MsoLogger logger=MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL, HasAnnotationPropertyWithValueMatcher.class);
37 private final String attribute;
38 private final Matcher<?> annotationMatcher;
39 private final Class<? extends Annotation> annotationClass;
40 public HasAnnotationPropertyWithValueMatcher(Class<? extends Annotation> clazz, String attribute, final Matcher<?> annotationMatcher) {
41 this.attribute = attribute;
42 this.annotationMatcher = annotationMatcher;
43 this.annotationClass = clazz;
47 protected boolean matchesSafely(T obj, final Description mismatchDescription) {
48 final PojoField temp = (PojoField)obj;
51 Annotation a = temp.getAnnotation(this.annotationClass);
53 mismatchDescription.appendText("does not have annotation ").appendText(this.annotationClass.getSimpleName());
56 method = a.getClass().getMethod(attribute);
57 final Object result = method.invoke(a);
58 if (!this.annotationMatcher.matches(result)) {
59 this.annotationMatcher.describeMismatch(result, mismatchDescription);
62 } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
63 mismatchDescription.appendText("does not have property ").appendText(attribute);
64 logger.debug("Error occured", e);
71 public void describeTo(final Description description) {
72 // Intentionally left blank.
75 public static <T extends PojoField> Matcher<T> hasAnnotationPropertyWithValue(Class<? extends Annotation> clazz, String attribute, final Matcher<?> annotationMatcher) {
76 return new HasAnnotationPropertyWithValueMatcher<T>(clazz, attribute, annotationMatcher);