Merge "add CompareModelsRequest.java"
[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
32 import com.openpojo.reflection.PojoField;
33
34 public class HasAnnotationPropertyWithValueMatcher<T extends PojoField> extends TypeSafeDiagnosingMatcher<T> {
35         private final String attribute;
36         private final Matcher<?> annotationMatcher;
37         private final Class<? extends Annotation> annotationClass;
38         public HasAnnotationPropertyWithValueMatcher(Class<? extends Annotation> clazz, String attribute, final Matcher<?> annotationMatcher) {
39                 this.attribute = attribute;
40                 this.annotationMatcher = annotationMatcher;
41                 this.annotationClass = clazz;
42         }
43
44         @Override
45         protected boolean matchesSafely(T obj, final Description mismatchDescription) {
46                 final PojoField temp = (PojoField)obj;
47                 final Method method;
48                 try {
49                         Annotation a = temp.getAnnotation(this.annotationClass);
50                         if (a == null) {
51                                 mismatchDescription.appendText("does not have annotation ").appendText(this.annotationClass.getSimpleName());
52                                 return false;
53                         }
54                         method = a.getClass().getMethod(attribute);
55                         final Object result = method.invoke(a);
56                         if (!this.annotationMatcher.matches(result)) {
57                                 this.annotationMatcher.describeMismatch(result, mismatchDescription);
58                                 return false;
59                         }
60                 } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
61                         mismatchDescription.appendText("does not have property ").appendText(attribute);
62                         return false;
63                 }
64                 return true;
65         }
66
67         @Override
68         public void describeTo(final Description description) {
69                 // Intentionally left blank.
70         }
71
72         public static <T extends PojoField> Matcher<T> hasAnnotationPropertyWithValue(Class<? extends Annotation> clazz, String attribute, final Matcher<?> annotationMatcher) {
73                 return new HasAnnotationPropertyWithValueMatcher<T>(clazz, attribute, annotationMatcher);
74         }
75 }