Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / openpojo / rules / HasAnnotationPropertyWithValueMatcher.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.so.openpojo.rules;
24
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;
34
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;
40
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;
46     }
47
48     @Override
49     protected boolean matchesSafely(T obj, final Description mismatchDescription) {
50         final PojoField temp = (PojoField) obj;
51         final Method method;
52         try {
53             Annotation a = temp.getAnnotation(this.annotationClass);
54             if (a == null) {
55                 mismatchDescription.appendText("does not have annotation ")
56                         .appendText(this.annotationClass.getSimpleName());
57                 return false;
58             }
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);
63                 return false;
64             }
65         } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
66                 | InvocationTargetException e) {
67             mismatchDescription.appendText("does not have property ").appendText(attribute);
68             logger.debug("Error occured", e);
69             return false;
70         }
71         return true;
72     }
73
74     @Override
75     public void describeTo(final Description description) {
76         // Intentionally left blank.
77     }
78
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);
82     }
83 }