Bug 6859 #2 Binding generator v1 refactoring
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / 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.yangtools.sal.binding.generator.impl;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.File;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
21 import org.opendaylight.mdsal.binding.model.api.Constant;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
24 import org.opendaylight.mdsal.binding.model.api.Type;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29
30 public class GeneratedTypesStringTest {
31
32     private final static List<File> testModels = new ArrayList<>();
33
34     @BeforeClass
35     public static void loadTestResources() throws URISyntaxException {
36         final File listModelFile = new File(GeneratedTypesStringTest.class.getResource("/simple-string-demo.yang")
37                 .toURI());
38         testModels.add(listModelFile);
39     }
40
41     @Test
42     public void constantGenerationTest() throws IOException, SourceException, ReactorException {
43         final SchemaContext context = YangParserTestUtils.parseYangSources(testModels);
44
45         assertNotNull(context);
46         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
47         final List<Type> genTypes = bindingGen.generateTypes(context);
48
49         boolean typedefStringFound = false;
50         boolean constantRegExListFound = false;
51         boolean constantRegExListTypeGeneric = false;
52         boolean constantRegExListTypeContainer = false;
53         boolean noStringInReqExListFound = false;
54         boolean constantRegExListValueOK = false;
55         boolean constantRegExListTypeOneGeneric = false;
56         for (final Type type : genTypes) {
57             if (type instanceof GeneratedTransferObject) {
58                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
59
60                 if (genTO.getName().equals("TypedefString")) {
61                     typedefStringFound = true;
62
63                     List<Constant> constants = genTO.getConstantDefinitions();
64                     for (Constant con : constants) {
65                         if (con.getName().equals("PATTERN_CONSTANTS")) {
66                             constantRegExListFound = true;
67                         } else {
68                             break;
69                         }
70                         ParameterizedType pType;
71                         if (con.getType() instanceof ParameterizedType) {
72                             pType = (ParameterizedType) con.getType();
73                         } else {
74                             break;
75                         }
76
77                         Type[] types;
78                         if (pType.getName().equals("List")) {
79                             constantRegExListTypeContainer = true;
80                             types = pType.getActualTypeArguments();
81                         } else {
82                             break;
83                         }
84
85                         if (types.length == 1) {
86                             constantRegExListTypeOneGeneric = true;
87                         } else {
88                             break;
89                         }
90
91                         if (types[0].getName().equals("String")) {
92                             constantRegExListTypeGeneric = true;
93                         } else {
94                             break;
95                         }
96
97                         if (con.getValue() instanceof List) {
98                             constantRegExListValueOK = true;
99                         } else {
100                             break;
101                         }
102
103                         for (Object obj : (List<?>) con.getValue()) {
104                             if (!(obj instanceof String)) {
105                                 noStringInReqExListFound = true;
106                                 break;
107                             }
108                         }
109
110                     }
111                 }
112             }
113
114         }
115
116         assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
117         assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
118         assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
119         assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
120         assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
121         assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
122         assertTrue("In list found other type than String", !noStringInReqExListFound);
123
124     }
125
126 }