1 package org.opendaylight.controller.sal.binding.generator.impl;
3 import static org.junit.Assert.*;
6 import java.util.ArrayList;
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
13 import org.opendaylight.controller.sal.binding.model.api.Constant;
14 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
15 import org.opendaylight.controller.sal.binding.model.api.ParameterizedType;
16 import org.opendaylight.controller.sal.binding.model.api.Type;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
20 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
22 public class GeneratedTypesStringTest {
24 private final static List<File> testModels = new ArrayList<File>();
27 public static void loadTestResources() {
28 final File listModelFile = new File(GeneratedTypesStringTest.class.getResource("/simple-string-demo.yang")
30 testModels.add(listModelFile);
34 public void constantGenerationTest() {
35 final YangModelParser parser = new YangParserImpl();
36 final Set<Module> modules = parser.parseYangModels(testModels);
37 final SchemaContext context = parser.resolveSchemaContext(modules);
39 assertNotNull(context);
40 final BindingGenerator bindingGen = new BindingGeneratorImpl();
41 final List<Type> genTypes = bindingGen.generateTypes(context);
43 boolean typedefStringFound = false;
44 boolean constantRegExListFound = false;
45 boolean constantRegExListTypeGeneric = false;
46 boolean constantRegExListTypeContainer = false;
47 boolean noStringInReqExListFound = false;
48 boolean constantRegExListValueOK = false;
49 boolean constantRegExListTypeOneGeneric = false;
50 for (final Type type : genTypes) {
51 if (type instanceof GeneratedTransferObject) {
52 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
54 if (genTO.getName().equals("TypedefString")) {
55 typedefStringFound = true;
57 List<Constant> constants = genTO.getConstantDefinitions();
58 for (Constant con : constants) {
59 if (con.getName().equals("PATTERN_CONSTANTS")) {
60 constantRegExListFound = true;
63 ParameterizedType pType;
64 if (con.getType() instanceof ParameterizedType) {
65 pType = (ParameterizedType) con.getType();
70 if (pType.getName().equals("List")) {
71 constantRegExListTypeContainer = true;
72 types = pType.getActualTypeArguments();
76 if (types.length == 1) {
77 constantRegExListTypeOneGeneric = true;
81 if (types[0].getName().equals("String")) {
82 constantRegExListTypeGeneric = true;
86 if (con.getValue() instanceof List) {
87 constantRegExListValueOK = true;
91 for (Object obj : (List<?>) con.getValue()) {
92 if (!(obj instanceof String)) {
93 noStringInReqExListFound = true;
104 assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
105 assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
106 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
107 assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
108 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
109 assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
110 assertTrue("In list found other type than String", !noStringInReqExListFound);