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