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