2 * Copyright (c) 2016 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.mdsal.binding.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import java.util.List;
14 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
15 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
16 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
17 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
18 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
19 import org.opendaylight.mdsal.binding.model.api.Type;
21 public class SupportTestUtil {
23 public static void containsMethods(final GeneratedType genType, final NameTypePattern... searchedSignsWhat) {
24 final List<MethodSignature> searchedSignsIn = genType.getMethodDefinitions();
25 containsMethods(searchedSignsIn, searchedSignsWhat);
28 public static void containsMethods(final List<MethodSignature> searchedSignsIn,
29 final NameTypePattern... searchedSignsWhat) {
30 if (searchedSignsIn == null) {
31 throw new IllegalArgumentException("List of method signatures in which should be searched can't be null");
33 if (searchedSignsWhat == null) {
34 throw new IllegalArgumentException("Array of method signatures which should be searched can't be null");
37 for (NameTypePattern searchedSignWhat : searchedSignsWhat) {
38 boolean nameMatchFound = false;
39 String typeNameFound = "";
40 for (MethodSignature searchedSignIn : searchedSignsIn) {
41 if (searchedSignWhat.getName().equals(searchedSignIn.getName())) {
42 nameMatchFound = true;
43 typeNameFound = resolveFullNameOfReturnType(searchedSignIn.getReturnType());
44 if (searchedSignWhat.getType().equals(typeNameFound)) {
49 assertTrue("Method " + searchedSignWhat.getName() + " wasn't found.", nameMatchFound);
50 assertEquals("Return type in method " + searchedSignWhat.getName() + " doesn't match expected type ",
51 searchedSignWhat.getType(), typeNameFound);
56 public static void containsAttributes(final GeneratedTransferObject genTO, boolean equal, boolean hash,
57 boolean toString, final NameTypePattern... searchedSignsWhat) {
58 List<GeneratedProperty> searchedPropertiesIn = genTO.getProperties();
59 containsAttributes(searchedPropertiesIn, "", searchedSignsWhat);
61 searchedPropertiesIn = genTO.getEqualsIdentifiers();
62 containsAttributes(searchedPropertiesIn, "equal", searchedSignsWhat);
65 searchedPropertiesIn = genTO.getHashCodeIdentifiers();
66 containsAttributes(searchedPropertiesIn, "hash", searchedSignsWhat);
69 searchedPropertiesIn = genTO.getToStringIdentifiers();
70 containsAttributes(searchedPropertiesIn, "toString", searchedSignsWhat);
75 public static void containsAttributes(final List<GeneratedProperty> searchedPropertiesIn, final String listType,
76 final NameTypePattern... searchedPropertiesWhat) {
78 for (NameTypePattern searchedPropertyWhat : searchedPropertiesWhat) {
79 boolean nameMatchFound = false;
80 String typeNameFound = "";
81 for (GeneratedProperty searchedPropertyIn : searchedPropertiesIn) {
82 if (searchedPropertyWhat.getName().equals(searchedPropertyIn.getName())) {
83 nameMatchFound = true;
84 typeNameFound = resolveFullNameOfReturnType(searchedPropertyIn.getReturnType());
85 if (searchedPropertyWhat.getType().equals(typeNameFound)) {
90 assertTrue("Property " + searchedPropertyWhat.getName() + " wasn't found in " + listType
91 + " property list.", nameMatchFound);
92 assertEquals("The type of property " + searchedPropertyWhat.getName() + " in " + listType
93 + " property list doesn't match expected type.", searchedPropertyWhat.getType(), typeNameFound);
98 public static String resolveFullNameOfReturnType(final Type type) {
99 final StringBuilder nameBuilder = new StringBuilder();
100 if (type instanceof ParameterizedType) {
101 nameBuilder.append(type.getName()).append('<');
102 ParameterizedType parametrizedTypes = (ParameterizedType) type;
103 for (Type parametrizedType : parametrizedTypes.getActualTypeArguments()) {
104 nameBuilder.append(parametrizedType.getName()).append(',');
106 if (nameBuilder.charAt(nameBuilder.length() - 1) == ',') {
107 nameBuilder.deleteCharAt(nameBuilder.length() - 1);
109 nameBuilder.append(">");
111 nameBuilder.append(type.getName());
113 return nameBuilder.toString();
116 public static void containsInterface(String interfaceNameSearched, GeneratedType genType) {
117 List<Type> caseCImplements = genType.getImplements();
118 boolean interfaceFound = false;
119 for (Type caseCImplement : caseCImplements) {
120 String interfaceName = resolveFullNameOfReturnType(caseCImplement);
121 if (interfaceName.equals(interfaceNameSearched)) {
122 interfaceFound = true;
126 assertTrue("Generated type " + genType.getName() + " doesn't implement interface " + interfaceNameSearched,