BUG-2022: String Type pattern parsing and resolving fix.
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / TypesResolutionTest.java
index 4bde6510e0a7005a7becc0177f73d666aff83233..a2fa589a564e9a130135edf0c6e9d1e2caf2abfb 100644 (file)
@@ -14,7 +14,9 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
+import java.math.BigInteger;
 import java.net.URI;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
 import org.junit.Before;
@@ -35,7 +37,7 @@ 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 {
@@ -121,20 +123,20 @@ public class TypesResolutionTest {
 
         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());
     }
 
@@ -146,22 +148,22 @@ public class TypesResolutionTest {
         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());
     }
@@ -170,7 +172,7 @@ public class TypesResolutionTest {
     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());
     }
 
@@ -260,13 +262,12 @@ public class TypesResolutionTest {
 
         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();
@@ -293,12 +294,11 @@ public class TypesResolutionTest {
         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();
@@ -306,13 +306,12 @@ public class TypesResolutionTest {
         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());
     }
 
@@ -325,11 +324,19 @@ public class TypesResolutionTest {
         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));
+    }
+
 }