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