2 * Copyright (c) 2014 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.yangtools.sal.binding.generator.impl;
10 import static org.junit.Assert.*;
13 import java.util.ArrayList;
14 import java.util.List;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
20 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29 public class GeneratedTypesStringTest {
31 private final static List<File> testModels = new ArrayList<File>();
34 public static void loadTestResources() {
35 final File listModelFile = new File(GeneratedTypesStringTest.class.getResource("/simple-string-demo.yang")
37 testModels.add(listModelFile);
41 public void constantGenerationTest() {
42 final YangModelParser parser = new YangParserImpl();
43 final Set<Module> modules = parser.parseYangModels(testModels);
44 final SchemaContext context = parser.resolveSchemaContext(modules);
46 assertNotNull(context);
47 final BindingGenerator bindingGen = new BindingGeneratorImpl();
48 final List<Type> genTypes = bindingGen.generateTypes(context);
50 boolean typedefStringFound = false;
51 boolean constantRegExListFound = false;
52 boolean constantRegExListTypeGeneric = false;
53 boolean constantRegExListTypeContainer = false;
54 boolean noStringInReqExListFound = false;
55 boolean constantRegExListValueOK = false;
56 boolean constantRegExListTypeOneGeneric = false;
57 for (final Type type : genTypes) {
58 if (type instanceof GeneratedTransferObject) {
59 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
61 if (genTO.getName().equals("TypedefString")) {
62 typedefStringFound = true;
64 List<Constant> constants = genTO.getConstantDefinitions();
65 for (Constant con : constants) {
66 if (con.getName().equals("PATTERN_CONSTANTS")) {
67 constantRegExListFound = true;
70 ParameterizedType pType;
71 if (con.getType() instanceof ParameterizedType) {
72 pType = (ParameterizedType) con.getType();
77 if (pType.getName().equals("List")) {
78 constantRegExListTypeContainer = true;
79 types = pType.getActualTypeArguments();
83 if (types.length == 1) {
84 constantRegExListTypeOneGeneric = true;
88 if (types[0].getName().equals("String")) {
89 constantRegExListTypeGeneric = true;
93 if (con.getValue() instanceof List) {
94 constantRegExListValueOK = true;
98 for (Object obj : (List<?>) con.getValue()) {
99 if (!(obj instanceof String)) {
100 noStringInReqExListFound = true;
111 assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
112 assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
113 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
114 assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
115 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
116 assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
117 assertTrue("In list found other type than String", !noStringInReqExListFound);