*/
package org.opendaylight.yangtools.yang.parser.impl;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.io.File;
-import java.io.FileNotFoundException;
+import java.math.BigInteger;
import java.net.URI;
+import java.util.Arrays;
import java.util.List;
import java.util.Set;
-
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
-import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
import org.opendaylight.yangtools.yang.model.util.BitsType;
import org.opendaylight.yangtools.yang.model.util.EnumerationType;
import org.opendaylight.yangtools.yang.model.util.ExtendedType;
import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
-import org.opendaylight.yangtools.yang.model.util.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
import org.opendaylight.yangtools.yang.model.util.UnionType;
public class TypesResolutionTest {
private Set<Module> testedModules;
@Before
- public void init() throws FileNotFoundException {
- File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
- File dependenciesDir = new File(getClass().getResource("/ietf").getPath());
- YangModelParser parser = new YangParserImpl();
- testedModules = parser.parseYangModels(yangFile, dependenciesDir);
+ public void init() throws Exception {
+ File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
+ File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
+ YangContextParser parser = new YangParserImpl();
+ testedModules = parser.parseFile(yangFile, dependenciesDir).getModules();
assertEquals(4, testedModules.size());
}
ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
assertTrue(ipv4.getBaseType() instanceof StringTypeDefinition);
- String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
- + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?";
+ String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
+ + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$";
assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
- expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
+ expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
+ "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|" + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
- + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?";
+ + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
- expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
- + "(%.+)?";
+ expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
+ + "(%.+)?$";
assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
}
assertTrue(type.getBaseType() instanceof StringTypeDefinition);
List<PatternConstraint> patterns = type.getPatternConstraints();
assertEquals(1, patterns.size());
- String expectedPattern = "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
- + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.";
+ String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
+ + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
List<LengthConstraint> lengths = type.getLengthConstraints();
assertEquals(1, lengths.size());
LengthConstraint length = type.getLengthConstraints().get(0);
- assertEquals(1L, length.getMin());
- assertEquals(253L, length.getMax());
+ assertEquals(BigInteger.ONE, length.getMin());
+ assertEquals(BigInteger.valueOf(253), length.getMax());
}
@Test
public void testInstanceIdentifier1() {
Module tested = TestUtils.findModule(testedModules, "custom-types-test");
LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf1");
- InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
+ InstanceIdentifierType leafType = (InstanceIdentifierType) leaf.getType();
assertFalse(leafType.requireInstance());
+ assertEquals(1, leaf.getUnknownSchemaNodes().size());
}
@Test
public void testInstanceIdentifier2() {
Module tested = TestUtils.findModule(testedModules, "custom-types-test");
LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf2");
- InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
+ InstanceIdentifierType leafType = (InstanceIdentifierType) leaf.getType();
assertTrue(leafType.requireInstance());
}
assertEquals(5, identities.size());
IdentitySchemaNode cryptoAlg = null;
IdentitySchemaNode cryptoBase = null;
+ IdentitySchemaNode cryptoId = null;
for (IdentitySchemaNode id : identities) {
if (id.getQName().getLocalName().equals("crypto-alg")) {
cryptoAlg = id;
} else if ("crypto-base".equals(id.getQName().getLocalName())) {
cryptoBase = id;
+ } else if ("crypto-id".equals(id.getQName().getLocalName())) {
+ cryptoId = id;
}
}
assertNotNull(cryptoAlg);
assertNotNull(cryptoBase);
assertNull(cryptoBase.getBaseIdentity());
assertEquals(3, cryptoBase.getDerivedIdentities().size());
+
+ assertNotNull(cryptoId);
+ assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
}
@Test
String expectedDesc = "A timezone location as defined by the IANA timezone";
assertTrue(testedType.getDescription().contains(expectedDesc));
- assertNull(testedType.getReference());
+ assertTrue(testedType.getReference().isEmpty());
assertEquals(Status.CURRENT, testedType.getStatus());
QName testedTypeQName = testedType.getQName();
assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
- assertEquals("ianatz", testedTypeQName.getPrefix());
assertEquals("iana-timezone", testedTypeQName.getLocalName());
EnumerationType enumType = (EnumerationType) testedType.getBaseType();
List<PatternConstraint> patterns = testedType.getPatternConstraints();
assertEquals(1, patterns.size());
PatternConstraint pattern = patterns.get(0);
- assertEquals("\\d*(\\.\\d*){1,127}", pattern.getRegularExpression());
+ assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression());
QName testedTypeQName = testedType.getQName();
assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
- assertEquals("yang", testedTypeQName.getPrefix());
assertEquals("object-identifier-128", testedTypeQName.getLocalName());
ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
assertEquals(1, patterns.size());
pattern = patterns.get(0);
- assertEquals("(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
+ assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
pattern.getRegularExpression());
QName testedTypeBaseQName = testedTypeBase.getQName();
assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
- assertEquals("yang", testedTypeBaseQName.getPrefix());
assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
}
QName identity = baseType.getIdentity().getQName();
assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
- assertEquals("iit", identity.getPrefix());
assertEquals("service-type", identity.getLocalName());
LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName("type");
assertNotNull(type);
}
+ @Test
+ public void testUnionWithExt() throws Exception {
+ File extdef = new File(getClass().getResource("/types/union-with-ext/extdef.yang").toURI());
+ File unionbug = new File(getClass().getResource("/types/union-with-ext/unionbug.yang").toURI());
+ File inet = new File(getClass().getResource("/ietf/ietf-inet-types@2010-09-24.yang").toURI());
+ YangContextParser parser = new YangParserImpl();
+ parser.parseFiles(Arrays.asList(extdef, unionbug, inet));
+ }
+
}