2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.sal.binding.generator.impl.stmt.parser.retest;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
13 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
14 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
16 import org.opendaylight.yangtools.sal.binding.generator.impl.BindingGeneratorImpl;
18 import java.io.IOException;
19 import java.net.URISyntaxException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
25 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
26 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
27 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
28 import org.opendaylight.yangtools.sal.binding.model.api.Type;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 public class GeneratedTypesStringTest {
33 private final static List<File> testModels = new ArrayList<File>();
36 public static void loadTestResources() throws URISyntaxException {
37 final File listModelFile = new File(GeneratedTypesStringTest.class.getResource("/simple-string-demo.yang")
39 testModels.add(listModelFile);
43 public void constantGenerationTest() throws IOException, SourceException, ReactorException {
44 final SchemaContext context = RetestUtils.parseYangSources(testModels);
46 assertNotNull(context);
47 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
48 final List<Type> genTypes = bindingGen.generateTypes(context);
50 boolean typedefStringFound = false;
51 boolean constantRegExListFound = false;
52 boolean constantRegExListTypeGeneric = false;
53 boolean constantRegExListTypeContainer = false;
54 boolean noStringInReqExListFound = false;
55 boolean constantRegExListValueOK = false;
56 boolean constantRegExListTypeOneGeneric = false;
57 for (final Type type : genTypes) {
58 if (type instanceof GeneratedTransferObject) {
59 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
61 if (genTO.getName().equals("TypedefString")) {
62 typedefStringFound = true;
64 List<Constant> constants = genTO.getConstantDefinitions();
65 for (Constant con : constants) {
66 if (con.getName().equals("PATTERN_CONSTANTS")) {
67 constantRegExListFound = true;
71 ParameterizedType pType;
72 if (con.getType() instanceof ParameterizedType) {
73 pType = (ParameterizedType) con.getType();
79 if (pType.getName().equals("List")) {
80 constantRegExListTypeContainer = true;
81 types = pType.getActualTypeArguments();
86 if (types.length == 1) {
87 constantRegExListTypeOneGeneric = true;
92 if (types[0].getName().equals("String")) {
93 constantRegExListTypeGeneric = true;
98 if (con.getValue() instanceof List) {
99 constantRegExListValueOK = true;
104 for (Object obj : (List<?>) con.getValue()) {
105 if (!(obj instanceof String)) {
106 noStringInReqExListFound = true;
117 assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
118 assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
119 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
120 assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
121 assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
122 assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
123 assertTrue("In list found other type than String", !noStringInReqExListFound);