Bug 584: Increase test coverage
[yangtools.git] / code-generator / binding-type-provider / src / test / java / org / opendaylight / yangtools / sal / binding / yang / types / TypeProviderImplTest.java
1 /*
2  * Copyright (c) 2014 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.yang.types;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.base.Optional;
16 import java.io.File;
17 import java.net.URISyntaxException;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.NoSuchElementException;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.ExpectedException;
24 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
32 import org.opendaylight.yangtools.yang.model.util.BinaryType;
33 import org.opendaylight.yangtools.yang.model.util.BooleanType;
34 import org.opendaylight.yangtools.yang.model.util.Decimal64;
35 import org.opendaylight.yangtools.yang.model.util.EmptyType;
36 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
37 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
38 import org.opendaylight.yangtools.yang.model.util.StringType;
39 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
40 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
41 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
42 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
43
44 public class TypeProviderImplTest {
45
46     @Rule
47     public ExpectedException expException = ExpectedException.none();
48
49     @Test
50     public void testMethodsOfTypeProviderImpl() throws URISyntaxException {
51         final YangParserImpl yangParser = new YangParserImpl();
52         final File abstractTopology = new File(BaseYangTypes.class.getResource("/base-yang-types.yang")
53                 .toURI());
54         final SchemaContext schemaContext = yangParser.parseFiles(Arrays.asList(abstractTopology));
55         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
56
57         final SchemaPath refTypePath = SchemaPath.create(true, QName.create("cont1"), QName.create("list1"));
58         final GeneratedTypeBuilderImpl refType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
59         typeProvider.putReferencedType(refTypePath, refType);
60         final StringType stringType = StringType.getInstance();
61         LeafSchemaNodeBuilder leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
62         leafSchemaNodeBuilder.setType(stringType);
63         LeafSchemaNode leafSchemaNode = leafSchemaNodeBuilder.build();
64
65         // test constructor
66         assertNotNull(typeProvider);
67
68         // test getAdditionalTypes() method
69         assertFalse(typeProvider.getAdditionalTypes().isEmpty());
70
71         // test getConstructorPropertyName() method
72         assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
73         assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
74
75         // test getParamNameFromType() method
76         assertEquals("string", typeProvider.getParamNameFromType(stringType));
77
78         // test getTypeDefaultConstruction() method for string type
79         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
80
81         // binary type
82         final BinaryType binaryType = BinaryType.getInstance();
83         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
84         leafSchemaNodeBuilder.setType(binaryType);
85         leafSchemaNode = leafSchemaNodeBuilder.build();
86         assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
87
88         // boolean type
89         final BooleanType booleanType = BooleanType.getInstance();
90         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
91         leafSchemaNodeBuilder.setType(booleanType);
92         leafSchemaNode = leafSchemaNodeBuilder.build();
93         assertEquals("new java.lang.Boolean(\"false\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
94
95         // decimal type
96         final Decimal64 decimalType = Decimal64.create(refTypePath, 4);
97         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
98         leafSchemaNodeBuilder.setType(decimalType);
99         leafSchemaNode = leafSchemaNodeBuilder.build();
100         assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "111"));
101
102         // empty type
103         final EmptyType emptyType = EmptyType.getInstance();
104         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
105         leafSchemaNodeBuilder.setType(emptyType);
106         leafSchemaNode = leafSchemaNodeBuilder.build();
107         assertEquals("new java.lang.Boolean(\"default value\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
108
109         // enum type
110         expException.expect(NoSuchElementException.class);
111         final EnumerationType enumType = EnumerationType.create(refTypePath, new ArrayList<EnumTypeDefinition.EnumPair>(), Optional.<EnumPair> absent());
112         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
113         leafSchemaNodeBuilder.setType(enumType);
114         leafSchemaNode = leafSchemaNodeBuilder.build();
115         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
116
117         // identityref type
118         expException.expect(UnsupportedOperationException.class);
119         expException.expectMessage("Cannot get default construction for identityref type");
120
121         final ModuleBuilder testModBuilder = new ModuleBuilder("test-module", "/test");
122         final IdentitySchemaNodeBuilder identityNodeBuilder = testModBuilder.addIdentity(QName.create("IdentityRefTest"), 111, SchemaPath.ROOT);
123         final IdentitySchemaNode identitySchemaNode = identityNodeBuilder.build();
124         final IdentityrefType identityRef = IdentityrefType.create(refTypePath, identitySchemaNode);
125         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
126         leafSchemaNodeBuilder.setType(identityRef);
127
128         leafSchemaNodeBuilder.setParent(identityNodeBuilder);
129         leafSchemaNode = leafSchemaNodeBuilder.build();
130         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
131     }
132 }