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