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