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