2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.sal.binding.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
15 import java.util.ArrayList;
16 import java.util.List;
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;
30 public class SupportTestUtil {
32 public static SchemaContext resolveSchemaContextFromFiles(final URI... yangFiles) {
33 final YangModelParser parser = new YangParserImpl();
35 final List<File> inputFiles = new ArrayList<File>();
36 for (int i = 0; i < yangFiles.length; ++i) {
37 inputFiles.add(new File(yangFiles[i]));
40 final Set<Module> modules = parser.parseYangModels(inputFiles);
41 return parser.resolveSchemaContext(modules);
44 public static void containsMethods(final GeneratedType genType, final NameTypePattern... searchedSignsWhat) {
45 final List<MethodSignature> searchedSignsIn = genType.getMethodDefinitions();
46 containsMethods(searchedSignsIn, searchedSignsWhat);
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");
54 if (searchedSignsWhat == null) {
55 throw new IllegalArgumentException("Array of method signatures which should be searched can't be null");
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)) {
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);
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);
82 searchedPropertiesIn = genTO.getEqualsIdentifiers();
83 containsAttributes(searchedPropertiesIn, "equal", searchedSignsWhat);
86 searchedPropertiesIn = genTO.getHashCodeIdentifiers();
87 containsAttributes(searchedPropertiesIn, "hash", searchedSignsWhat);
90 searchedPropertiesIn = genTO.getToStringIdentifiers();
91 containsAttributes(searchedPropertiesIn, "toString", searchedSignsWhat);
96 public static void containsAttributes(final List<GeneratedProperty> searchedPropertiesIn, final String listType,
97 final NameTypePattern... searchedPropertiesWhat) {
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)) {
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);
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() + ",");
127 if (nameBuilder.charAt(nameBuilder.length() - 1) == ',') {
128 nameBuilder.deleteCharAt(nameBuilder.length() - 1);
130 nameBuilder.append(">");
132 nameBuilder.append(type.getName());
134 return nameBuilder.toString();
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;
147 assertTrue("Generated type " + genType.getName() + " doesn't implement interface " + interfaceNameSearched,