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.assertTrue;
12 import java.util.List;
14 import java.util.Map.Entry;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.binding.model.api.Constant;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
18 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
19 import org.opendaylight.mdsal.binding.model.api.Type;
20 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
22 public class GeneratedTypesStringTest {
24 public void constantGenerationTest() {
25 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
26 "/simple-string-demo.yang"));
28 boolean typedefStringFound = false;
29 boolean constantRegExListFound = false;
30 boolean constantRegExListTypeGeneric = false;
31 boolean constantRegExListTypeContainer = false;
32 boolean noStringInReqExListFound = false;
33 boolean constantRegExListValueOK = false;
34 boolean constantRegExListTypeOneGeneric = false;
35 for (final Type type : genTypes) {
36 if (type instanceof GeneratedTransferObject) {
37 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
39 if (genTO.getName().equals("TypedefString")) {
40 typedefStringFound = true;
42 List<Constant> constants = genTO.getConstantDefinitions();
43 for (Constant con : constants) {
44 if (con.getName().equals("PATTERN_CONSTANTS")) {
45 constantRegExListFound = true;
49 ParameterizedType paramType;
50 if (con.getType() instanceof ParameterizedType) {
51 paramType = (ParameterizedType) con.getType();
57 if (paramType.getName().equals("List")) {
58 constantRegExListTypeContainer = true;
59 types = paramType.getActualTypeArguments();
64 if (types.length == 1) {
65 constantRegExListTypeOneGeneric = true;
70 if (types[0].getName().equals("String")) {
71 constantRegExListTypeGeneric = true;
76 if (con.getValue() instanceof Map) {
77 constantRegExListValueOK = true;
82 for (Entry<?, ?> e : ((Map<?, ?>) con.getValue()).entrySet()) {
83 if (!(e.getKey() instanceof String) || !(e.getKey() instanceof String)) {
84 noStringInReqExListFound = true;
95 assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
96 assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
97 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
98 assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
99 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
100 assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
101 assertTrue("In list found other type than String", !noStringInReqExListFound);