Add missing copyright headers
[yangtools.git] / code-generator / binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / SupportTestUtil.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.sal.binding.generator.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.util.List;
14
15 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
16 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
17 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
18 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
19 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
20 import org.opendaylight.yangtools.sal.binding.model.api.Type;
21
22 public class SupportTestUtil {
23
24     public static void containsMethods(final GeneratedType genType, final NameTypePattern... searchedSignsWhat) {
25         final List<MethodSignature> searchedSignsIn = genType.getMethodDefinitions();
26         containsMethods(searchedSignsIn, searchedSignsWhat);
27     }
28
29     public static void containsMethods(final List<MethodSignature> searchedSignsIn,
30             final NameTypePattern... searchedSignsWhat) {
31         if (searchedSignsIn == null) {
32             throw new IllegalArgumentException("List of method signatures in which should be searched can't be null");
33         }
34         if (searchedSignsWhat == null) {
35             throw new IllegalArgumentException("Array of method signatures which should be searched can't be null");
36         }
37
38         for (NameTypePattern searchedSignWhat : searchedSignsWhat) {
39             boolean nameMatchFound = false;
40             String typeNameFound = "";
41             for (MethodSignature searchedSignIn : searchedSignsIn) {
42                 if (searchedSignWhat.getName().equals(searchedSignIn.getName())) {
43                     nameMatchFound = true;
44                     typeNameFound = resolveFullNameOfReturnType(searchedSignIn.getReturnType());
45                     if (searchedSignWhat.getType().equals(typeNameFound)) {
46                         break;
47                     }
48                 }
49             }
50             assertTrue("Method " + searchedSignWhat.getName() + " wasn't found.", nameMatchFound);
51             assertEquals("Return type in method " + searchedSignWhat.getName() + " doesn't match expected type ",
52                     searchedSignWhat.getType(), typeNameFound);
53
54         }
55     }
56
57     public static void containsAttributes(final GeneratedTransferObject genTO, boolean equal, boolean hash,
58             boolean toString, final NameTypePattern... searchedSignsWhat) {
59         List<GeneratedProperty> searchedPropertiesIn = genTO.getProperties();
60         containsAttributes(searchedPropertiesIn, "", searchedSignsWhat);
61         if (equal) {
62             searchedPropertiesIn = genTO.getEqualsIdentifiers();
63             containsAttributes(searchedPropertiesIn, "equal", searchedSignsWhat);
64         }
65         if (hash) {
66             searchedPropertiesIn = genTO.getHashCodeIdentifiers();
67             containsAttributes(searchedPropertiesIn, "hash", searchedSignsWhat);
68         }
69         if (toString) {
70             searchedPropertiesIn = genTO.getToStringIdentifiers();
71             containsAttributes(searchedPropertiesIn, "toString", searchedSignsWhat);
72         }
73
74     }
75
76     public static void containsAttributes(final List<GeneratedProperty> searchedPropertiesIn, final String listType,
77             final NameTypePattern... searchedPropertiesWhat) {
78
79         for (NameTypePattern searchedPropertyWhat : searchedPropertiesWhat) {
80             boolean nameMatchFound = false;
81             String typeNameFound = "";
82             for (GeneratedProperty searchedPropertyIn : searchedPropertiesIn) {
83                 if (searchedPropertyWhat.getName().equals(searchedPropertyIn.getName())) {
84                     nameMatchFound = true;
85                     typeNameFound = resolveFullNameOfReturnType(searchedPropertyIn.getReturnType());
86                     if (searchedPropertyWhat.getType().equals(typeNameFound)) {
87                         break;
88                     }
89                 }
90             }
91             assertTrue("Property " + searchedPropertyWhat.getName() + " wasn't found in " + listType
92                     + " property list.", nameMatchFound);
93             assertEquals("The type of property " + searchedPropertyWhat.getName() + " in " + listType
94                     + " property list doesn't match expected type.", searchedPropertyWhat.getType(), typeNameFound);
95
96         }
97     }
98
99     public static String resolveFullNameOfReturnType(final Type type) {
100         final StringBuilder nameBuilder = new StringBuilder();
101         if (type instanceof ParameterizedType) {
102             nameBuilder.append(type.getName() + "<");
103             ParameterizedType parametrizedTypes = (ParameterizedType) type;
104             for (Type parametrizedType : parametrizedTypes.getActualTypeArguments()) {
105                 nameBuilder.append(parametrizedType.getName() + ",");
106             }
107             if (nameBuilder.charAt(nameBuilder.length() - 1) == ',') {
108                 nameBuilder.deleteCharAt(nameBuilder.length() - 1);
109             }
110             nameBuilder.append(">");
111         } else {
112             nameBuilder.append(type.getName());
113         }
114         return nameBuilder.toString();
115     }
116
117     public static void containsInterface(String interfaceNameSearched, GeneratedType genType) {
118         List<Type> caseCImplements = genType.getImplements();
119         boolean interfaceFound = false;
120         for (Type caseCImplement : caseCImplements) {
121             String interfaceName = resolveFullNameOfReturnType(caseCImplement);
122             if (interfaceName.equals(interfaceNameSearched)) {
123                 interfaceFound = true;
124                 break;
125             }
126         }
127         assertTrue("Generated type " + genType.getName() + " doesn't implement interface " + interfaceNameSearched,
128                 interfaceFound);
129     }
130
131 }