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.assertNotNull;
11 import static org.junit.Assert.assertTrue;
13 import java.util.List;
15 import java.util.Map.Entry;
16 import org.junit.Test;
17 import org.opendaylight.mdsal.binding.model.api.Constant;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
20 import org.opendaylight.mdsal.binding.model.api.Type;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 public class GeneratedTypesStringTest {
27 public void constantGenerationTest() {
28 final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-string-demo.yang");
30 assertNotNull(context);
31 final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
33 boolean typedefStringFound = false;
34 boolean constantRegExListFound = false;
35 boolean constantRegExListTypeGeneric = false;
36 boolean constantRegExListTypeContainer = false;
37 boolean noStringInReqExListFound = false;
38 boolean constantRegExListValueOK = false;
39 boolean constantRegExListTypeOneGeneric = false;
40 for (final Type type : genTypes) {
41 if (type instanceof GeneratedTransferObject) {
42 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
44 if (genTO.getName().equals("TypedefString")) {
45 typedefStringFound = true;
47 List<Constant> constants = genTO.getConstantDefinitions();
48 for (Constant con : constants) {
49 if (con.getName().equals("PATTERN_CONSTANTS")) {
50 constantRegExListFound = true;
54 ParameterizedType paramType;
55 if (con.getType() instanceof ParameterizedType) {
56 paramType = (ParameterizedType) con.getType();
62 if (paramType.getName().equals("List")) {
63 constantRegExListTypeContainer = true;
64 types = paramType.getActualTypeArguments();
69 if (types.length == 1) {
70 constantRegExListTypeOneGeneric = true;
75 if (types[0].getName().equals("String")) {
76 constantRegExListTypeGeneric = true;
81 if (con.getValue() instanceof Map) {
82 constantRegExListValueOK = true;
87 for (Entry<?, ?> e : ((Map<?, ?>) con.getValue()).entrySet()) {
88 if (!(e.getKey() instanceof String) || !(e.getKey() instanceof String)) {
89 noStringInReqExListFound = true;
100 assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
101 assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
102 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
103 assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
104 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
105 assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
106 assertTrue("In list found other type than String", !noStringInReqExListFound);