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 final class SupportTestUtil {
22 private SupportTestUtil() {
26 public static void containsMethods(final GeneratedType genType, final NameTypePattern... searchedSignsWhat) {
27 final List<MethodSignature> searchedSignsIn = genType.getMethodDefinitions();
28 containsMethods(searchedSignsIn, searchedSignsWhat);
31 public static void containsMethods(final List<MethodSignature> searchedSignsIn,
32 final NameTypePattern... searchedSignsWhat) {
33 if (searchedSignsIn == null) {
34 throw new IllegalArgumentException("List of method signatures in which should be searched can't be null");
36 if (searchedSignsWhat == null) {
37 throw new IllegalArgumentException("Array of method signatures which should be searched can't be null");
40 for (NameTypePattern searchedSignWhat : searchedSignsWhat) {
41 boolean nameMatchFound = false;
42 String typeNameFound = "";
43 for (MethodSignature searchedSignIn : searchedSignsIn) {
44 if (searchedSignWhat.getName().equals(searchedSignIn.getName())) {
45 nameMatchFound = true;
46 typeNameFound = resolveFullNameOfReturnType(searchedSignIn.getReturnType());
47 if (searchedSignWhat.getType().equals(typeNameFound)) {
52 assertTrue("Method " + searchedSignWhat.getName() + " wasn't found.", nameMatchFound);
53 assertEquals("Return type in method " + searchedSignWhat.getName() + " doesn't match expected type ",
54 searchedSignWhat.getType(), typeNameFound);
59 public static void containsAttributes(final GeneratedTransferObject genTO, final boolean equal, final boolean hash,
60 final boolean toString, final NameTypePattern... searchedSignsWhat) {
61 List<GeneratedProperty> searchedPropertiesIn = genTO.getProperties();
62 containsAttributes(searchedPropertiesIn, "", searchedSignsWhat);
64 searchedPropertiesIn = genTO.getEqualsIdentifiers();
65 containsAttributes(searchedPropertiesIn, "equal", searchedSignsWhat);
68 searchedPropertiesIn = genTO.getHashCodeIdentifiers();
69 containsAttributes(searchedPropertiesIn, "hash", searchedSignsWhat);
72 searchedPropertiesIn = genTO.getToStringIdentifiers();
73 containsAttributes(searchedPropertiesIn, "toString", searchedSignsWhat);
78 public static void containsAttributes(final List<GeneratedProperty> searchedPropertiesIn, final String listType,
79 final NameTypePattern... searchedPropertiesWhat) {
81 for (NameTypePattern searchedPropertyWhat : searchedPropertiesWhat) {
82 boolean nameMatchFound = false;
83 String typeNameFound = "";
84 for (GeneratedProperty searchedPropertyIn : searchedPropertiesIn) {
85 if (searchedPropertyWhat.getName().equals(searchedPropertyIn.getName())) {
86 nameMatchFound = true;
87 typeNameFound = resolveFullNameOfReturnType(searchedPropertyIn.getReturnType());
88 if (searchedPropertyWhat.getType().equals(typeNameFound)) {
93 assertTrue("Property " + searchedPropertyWhat.getName() + " wasn't found in " + listType
94 + " property list.", nameMatchFound);
95 assertEquals("The type of property " + searchedPropertyWhat.getName() + " in " + listType
96 + " property list doesn't match expected type.", searchedPropertyWhat.getType(), typeNameFound);
101 public static String resolveFullNameOfReturnType(final Type type) {
102 final StringBuilder nameBuilder = new StringBuilder();
103 if (type instanceof ParameterizedType) {
104 nameBuilder.append(type.getName()).append('<');
105 ParameterizedType parametrizedTypes = (ParameterizedType) type;
106 for (Type parametrizedType : parametrizedTypes.getActualTypeArguments()) {
107 nameBuilder.append(parametrizedType.getName()).append(',');
109 if (nameBuilder.charAt(nameBuilder.length() - 1) == ',') {
110 nameBuilder.deleteCharAt(nameBuilder.length() - 1);
112 nameBuilder.append(">");
114 nameBuilder.append(type.getName());
116 return nameBuilder.toString();
119 public static void containsInterface(final String interfaceNameSearched, final GeneratedType genType) {
120 List<Type> caseCImplements = genType.getImplements();
121 boolean interfaceFound = false;
122 for (Type caseCImplement : caseCImplements) {
123 String interfaceName = resolveFullNameOfReturnType(caseCImplement);
124 if (interfaceName.equals(interfaceNameSearched)) {
125 interfaceFound = true;
129 assertTrue("Generated type " + genType.getName() + " doesn't implement interface " + interfaceNameSearched,