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.generator.api.BindingGenerator;
18 import org.opendaylight.mdsal.binding.model.api.Constant;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25 public class GeneratedTypesStringTest {
28 public void constantGenerationTest() {
29 final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-string-demo.yang");
31 assertNotNull(context);
32 final BindingGenerator bindingGen = new BindingGeneratorImpl();
33 final List<Type> genTypes = bindingGen.generateTypes(context);
35 boolean typedefStringFound = false;
36 boolean constantRegExListFound = false;
37 boolean constantRegExListTypeGeneric = false;
38 boolean constantRegExListTypeContainer = false;
39 boolean noStringInReqExListFound = false;
40 boolean constantRegExListValueOK = false;
41 boolean constantRegExListTypeOneGeneric = false;
42 for (final Type type : genTypes) {
43 if (type instanceof GeneratedTransferObject) {
44 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
46 if (genTO.getName().equals("TypedefString")) {
47 typedefStringFound = true;
49 List<Constant> constants = genTO.getConstantDefinitions();
50 for (Constant con : constants) {
51 if (con.getName().equals("PATTERN_CONSTANTS")) {
52 constantRegExListFound = true;
56 ParameterizedType pType;
57 if (con.getType() instanceof ParameterizedType) {
58 pType = (ParameterizedType) con.getType();
64 if (pType.getName().equals("List")) {
65 constantRegExListTypeContainer = true;
66 types = pType.getActualTypeArguments();
71 if (types.length == 1) {
72 constantRegExListTypeOneGeneric = true;
77 if (types[0].getName().equals("String")) {
78 constantRegExListTypeGeneric = true;
83 if (con.getValue() instanceof Map) {
84 constantRegExListValueOK = true;
89 for (Entry<?, ?> e : ((Map<?, ?>) con.getValue()).entrySet()) {
90 if (!(e.getKey() instanceof String) || !(e.getKey() instanceof String)) {
91 noStringInReqExListFound = true;
102 assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
103 assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
104 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
105 assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
106 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
107 assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
108 assertTrue("In list found other type than String", !noStringInReqExListFound);