Update BindingRuntime{Context,Generator,Types}
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / GeneratedTypesStringTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl;
9
10 import static org.junit.Assert.assertTrue;
11
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import org.junit.Test;
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.test.util.YangParserTestUtils;
21
22 public class GeneratedTypesStringTest {
23     @Test
24     public void constantGenerationTest() {
25         final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
26             "/simple-string-demo.yang"));
27
28         boolean typedefStringFound = false;
29         boolean constantRegExListFound = false;
30         boolean constantRegExListTypeGeneric = false;
31         boolean constantRegExListTypeContainer = false;
32         boolean noStringInReqExListFound = false;
33         boolean constantRegExListValueOK = false;
34         boolean constantRegExListTypeOneGeneric = false;
35         for (final Type type : genTypes) {
36             if (type instanceof GeneratedTransferObject) {
37                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
38
39                 if (genTO.getName().equals("TypedefString")) {
40                     typedefStringFound = true;
41
42                     List<Constant> constants = genTO.getConstantDefinitions();
43                     for (Constant con : constants) {
44                         if (con.getName().equals("PATTERN_CONSTANTS")) {
45                             constantRegExListFound = true;
46                         } else {
47                             break;
48                         }
49                         ParameterizedType paramType;
50                         if (con.getType() instanceof ParameterizedType) {
51                             paramType = (ParameterizedType) con.getType();
52                         } else {
53                             break;
54                         }
55
56                         Type[] types;
57                         if (paramType.getName().equals("List")) {
58                             constantRegExListTypeContainer = true;
59                             types = paramType.getActualTypeArguments();
60                         } else {
61                             break;
62                         }
63
64                         if (types.length == 1) {
65                             constantRegExListTypeOneGeneric = true;
66                         } else {
67                             break;
68                         }
69
70                         if (types[0].getName().equals("String")) {
71                             constantRegExListTypeGeneric = true;
72                         } else {
73                             break;
74                         }
75
76                         if (con.getValue() instanceof Map) {
77                             constantRegExListValueOK = true;
78                         } else {
79                             break;
80                         }
81
82                         for (Entry<?, ?> e : ((Map<?, ?>) con.getValue()).entrySet()) {
83                             if (!(e.getKey() instanceof String) || !(e.getKey() instanceof String)) {
84                                 noStringInReqExListFound = true;
85                                 break;
86                             }
87                         }
88
89                     }
90                 }
91             }
92
93         }
94
95         assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
96         assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
97         assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
98         assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
99         assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
100         assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
101         assertTrue("In list found other type than String", !noStringInReqExListFound);
102     }
103 }