Merge "Reorder modifiers"
[so.git] / common / src / main / java / org / openecomp / mso / openpojo / rules / HasAnnotationPropertyWithValueMatcher.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.openpojo.rules;
22
23 import java.lang.annotation.Annotation;
24 import java.lang.reflect.Field;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27
28 import org.hamcrest.Description;
29 import org.hamcrest.Matcher;
30 import org.hamcrest.TypeSafeDiagnosingMatcher;
31 import org.openecomp.mso.logger.MsoLogger;
32
33 import com.openpojo.reflection.PojoField;
34
35 public class HasAnnotationPropertyWithValueMatcher<T extends PojoField> extends TypeSafeDiagnosingMatcher<T> {
36         private MsoLogger logger=MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
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;
44         }
45
46         @Override
47         protected boolean matchesSafely(T obj, final Description mismatchDescription) {
48                 final PojoField temp = (PojoField)obj;
49                 final Method method;
50                 try {
51                         Annotation a = temp.getAnnotation(this.annotationClass);
52                         if (a == null) {
53                                 mismatchDescription.appendText("does not have annotation ").appendText(this.annotationClass.getSimpleName());
54                                 return false;
55                         }
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);
60                                 return false;
61                         }
62                 } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
63                         mismatchDescription.appendText("does not have property ").appendText(attribute);
64                         logger.debug("Error occured", e);
65                         return false;
66                 }
67                 return true;
68         }
69
70         @Override
71         public void describeTo(final Description description) {
72                 // Intentionally left blank.
73         }
74
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);
77         }
78 }