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;
14 import org.junit.Test;
15 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
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.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23 public class GeneratedTypesStringTest {
26 public void constantGenerationTest() {
27 final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-string-demo.yang");
29 assertNotNull(context);
30 final BindingGenerator bindingGen = new BindingGeneratorImpl();
31 final List<Type> genTypes = bindingGen.generateTypes(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 pType;
55 if (con.getType() instanceof ParameterizedType) {
56 pType = (ParameterizedType) con.getType();
62 if (pType.getName().equals("List")) {
63 constantRegExListTypeContainer = true;
64 types = pType.getActualTypeArguments();
69 if (types.length == 1) {
70 constantRegExListTypeOneGeneric = true;
75 if (types[0].getName().equals("String")) {
76 constantRegExListTypeGeneric = true;
81 if (con.getValue() instanceof List) {
82 constantRegExListValueOK = true;
87 for (Object obj : (List<?>) con.getValue()) {
88 if (!(obj 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);