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;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.util.ArrayList;
17 import java.util.List;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
22 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
24 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
25 import org.opendaylight.yangtools.sal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 public class GeneratedTypesStringTest {
32 private final static List<File> testModels = new ArrayList<File>();
35 public static void loadTestResources() throws URISyntaxException {
36 final File listModelFile = new File(GeneratedTypesStringTest.class.getResource("/simple-string-demo.yang")
38 testModels.add(listModelFile);
42 public void constantGenerationTest() throws IOException {
43 final YangContextParser parser = new YangParserImpl();
44 final SchemaContext context = parser.parseFiles(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);