Cleaned up Java Binding code from YANG Tools
[mdsal.git] / binding / mdsal-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.io.File;
14 import java.io.IOException;
15 import java.net.URI;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
19 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
21 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
22 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
26 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
27
28 public class SupportTestUtil {
29
30     public static SchemaContext resolveSchemaContextFromFiles(final URI... yangFiles) throws IOException {
31         final YangContextParser parser = new YangParserImpl();
32
33         final List<File> inputFiles = new ArrayList<File>();
34         for (int i = 0; i < yangFiles.length; ++i) {
35             inputFiles.add(new File(yangFiles[i]));
36         }
37
38         return parser.parseFiles(inputFiles);
39     }
40
41     public static void containsMethods(final GeneratedType genType, final NameTypePattern... searchedSignsWhat) {
42         final List<MethodSignature> searchedSignsIn = genType.getMethodDefinitions();
43         containsMethods(searchedSignsIn, searchedSignsWhat);
44     }
45
46     public static void containsMethods(final List<MethodSignature> searchedSignsIn,
47             final NameTypePattern... searchedSignsWhat) {
48         if (searchedSignsIn == null) {
49             throw new IllegalArgumentException("List of method signatures in which should be searched can't be null");
50         }
51         if (searchedSignsWhat == null) {
52             throw new IllegalArgumentException("Array of method signatures which should be searched can't be null");
53         }
54
55         for (NameTypePattern searchedSignWhat : searchedSignsWhat) {
56             boolean nameMatchFound = false;
57             String typeNameFound = "";
58             for (MethodSignature searchedSignIn : searchedSignsIn) {
59                 if (searchedSignWhat.getName().equals(searchedSignIn.getName())) {
60                     nameMatchFound = true;
61                     typeNameFound = resolveFullNameOfReturnType(searchedSignIn.getReturnType());
62                     if (searchedSignWhat.getType().equals(typeNameFound)) {
63                         break;
64                     }
65                 }
66             }
67             assertTrue("Method " + searchedSignWhat.getName() + " wasn't found.", nameMatchFound);
68             assertEquals("Return type in method " + searchedSignWhat.getName() + " doesn't match expected type ",
69                     searchedSignWhat.getType(), typeNameFound);
70
71         }
72     }
73
74     public static void containsAttributes(final GeneratedTransferObject genTO, boolean equal, boolean hash,
75             boolean toString, final NameTypePattern... searchedSignsWhat) {
76         List<GeneratedProperty> searchedPropertiesIn = genTO.getProperties();
77         containsAttributes(searchedPropertiesIn, "", searchedSignsWhat);
78         if (equal) {
79             searchedPropertiesIn = genTO.getEqualsIdentifiers();
80             containsAttributes(searchedPropertiesIn, "equal", searchedSignsWhat);
81         }
82         if (hash) {
83             searchedPropertiesIn = genTO.getHashCodeIdentifiers();
84             containsAttributes(searchedPropertiesIn, "hash", searchedSignsWhat);
85         }
86         if (toString) {
87             searchedPropertiesIn = genTO.getToStringIdentifiers();
88             containsAttributes(searchedPropertiesIn, "toString", searchedSignsWhat);
89         }
90
91     }
92
93     public static void containsAttributes(final List<GeneratedProperty> searchedPropertiesIn, final String listType,
94             final NameTypePattern... searchedPropertiesWhat) {
95
96         for (NameTypePattern searchedPropertyWhat : searchedPropertiesWhat) {
97             boolean nameMatchFound = false;
98             String typeNameFound = "";
99             for (GeneratedProperty searchedPropertyIn : searchedPropertiesIn) {
100                 if (searchedPropertyWhat.getName().equals(searchedPropertyIn.getName())) {
101                     nameMatchFound = true;
102                     typeNameFound = resolveFullNameOfReturnType(searchedPropertyIn.getReturnType());
103                     if (searchedPropertyWhat.getType().equals(typeNameFound)) {
104                         break;
105                     }
106                 }
107             }
108             assertTrue("Property " + searchedPropertyWhat.getName() + " wasn't found in " + listType
109                     + " property list.", nameMatchFound);
110             assertEquals("The type of property " + searchedPropertyWhat.getName() + " in " + listType
111                     + " property list doesn't match expected type.", searchedPropertyWhat.getType(), typeNameFound);
112
113         }
114     }
115
116     public static String resolveFullNameOfReturnType(final Type type) {
117         final StringBuilder nameBuilder = new StringBuilder();
118         if (type instanceof ParameterizedType) {
119             nameBuilder.append(type.getName() + "<");
120             ParameterizedType parametrizedTypes = (ParameterizedType) type;
121             for (Type parametrizedType : parametrizedTypes.getActualTypeArguments()) {
122                 nameBuilder.append(parametrizedType.getName() + ",");
123             }
124             if (nameBuilder.charAt(nameBuilder.length() - 1) == ',') {
125                 nameBuilder.deleteCharAt(nameBuilder.length() - 1);
126             }
127             nameBuilder.append(">");
128         } else {
129             nameBuilder.append(type.getName());
130         }
131         return nameBuilder.toString();
132     }
133
134     public static void containsInterface(String interfaceNameSearched, GeneratedType genType) {
135         List<Type> caseCImplements = genType.getImplements();
136         boolean interfaceFound = false;
137         for (Type caseCImplement : caseCImplements) {
138             String interfaceName = resolveFullNameOfReturnType(caseCImplement);
139             if (interfaceName.equals(interfaceNameSearched)) {
140                 interfaceFound = true;
141                 break;
142             }
143         }
144         assertTrue("Generated type " + genType.getName() + " doesn't implement interface " + interfaceNameSearched,
145                 interfaceFound);
146     }
147
148 }