1 package org.opendaylight.controller.sal.binding.generator.impl;
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
7 import java.util.ArrayList;
11 import org.junit.BeforeClass;
12 import org.junit.Test;
13 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
14 import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;
15 import org.opendaylight.controller.sal.binding.model.api.Constant;
16 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
17 import org.opendaylight.controller.sal.binding.model.api.ParameterizedType;
18 import org.opendaylight.controller.sal.binding.model.api.Type;
19 import org.opendaylight.controller.yang.model.api.Module;
20 import org.opendaylight.controller.yang.model.api.SchemaContext;
21 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
22 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
24 public class GeneratedTypesStringTest {
26 private final static List<File> testModels = new ArrayList<File>();
29 public static void loadTestResources() {
30 final File listModelFile = new File(GeneratedTypesStringTest.class.getResource("/simple-string-demo.yang")
32 testModels.add(listModelFile);
36 public void constantGenerationTest() {
37 final YangModelParser parser = new YangParserImpl();
38 final Set<Module> modules = parser.parseYangModels(testModels);
39 final SchemaContext context = parser.resolveSchemaContext(modules);
41 assertNotNull(context);
42 final BindingGenerator bindingGen = new BindingGeneratorImpl();
43 final List<Type> genTypes = bindingGen.generateTypes(context);
45 boolean typedefStringFound = false;
46 boolean constantRegExListFound = false;
47 boolean constantRegExListTypeGeneric = false;
48 boolean constantRegExListTypeContainer = false;
49 boolean noStringInReqExListFound = false;
50 boolean constantRegExListValueOK = false;
51 boolean constantRegExListTypeOneGeneric = false;
52 for (final Type type : genTypes) {
53 if (type instanceof GeneratedTransferObject) {
54 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
56 if (genTO.getName().equals("TypedefString")) {
57 typedefStringFound = true;
59 List<Constant> constants = genTO.getConstantDefinitions();
60 for (Constant con : constants) {
61 if (con.getName().equals("PATTERN_CONSTANTS")) {
62 constantRegExListFound = true;
65 ParameterizedType pType;
66 if (con.getType() instanceof ParameterizedType) {
67 pType = (ParameterizedType) con.getType();
72 if (pType.getName().equals("List")) {
73 constantRegExListTypeContainer = true;
74 types = pType.getActualTypeArguments();
78 if (types.length == 1) {
79 constantRegExListTypeOneGeneric = true;
83 if (types[0].getName().equals("String")) {
84 constantRegExListTypeGeneric = true;
88 if (con.getValue() instanceof List) {
89 constantRegExListValueOK = true;
93 for (Object obj : (List<?>) con.getValue()) {
94 if (!(obj instanceof String)) {
95 noStringInReqExListFound = true;
106 assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
107 assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
108 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
109 assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
110 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
111 assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
112 assertTrue("In list found other type than String", !noStringInReqExListFound);