Bug 3899: Milestone: Increase test coverage for Yangtools 18/34318/1
authorIgor Foltin <ifoltin@cisco.com>
Tue, 9 Feb 2016 10:52:34 +0000 (11:52 +0100)
committerIgor Foltin <ifoltin@cisco.com>
Tue, 9 Feb 2016 12:12:43 +0000 (12:12 +0000)
Added unit test for StringPatternCheckingCodec.

Change-Id: I95883efa82d0e987fa0b5b6e507513e105aca267
Signed-off-by: Igor Foltin <ifoltin@cisco.com>
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/test/codecs/StringPatternCheckingCodecTest.java [new file with mode: 0644]
yang/yang-data-impl/src/test/resources/string-pattern-checking-codec-test.yang [new file with mode: 0644]

diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/test/codecs/StringPatternCheckingCodecTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/test/codecs/StringPatternCheckingCodecTest.java
new file mode 100644 (file)
index 0000000..9be9ea4
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.yangtools.yang.data.impl.test.codecs;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.opendaylight.yangtools.yang.data.impl.test.codecs.TypeDefinitionAwareCodecTestHelper.getCodec;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.text.ParseException;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.data.impl.RetestUtils;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
+import org.opendaylight.yangtools.yang.data.api.codec.StringCodec;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class StringPatternCheckingCodecTest {
+
+    private static final Logger LOG = LoggerFactory.getLogger(StringPatternCheckingCodecTest.class);
+    private static final YangStatementSourceImpl TEST_MODULE = new YangStatementSourceImpl
+            ("/string-pattern-checking-codec-test.yang", false);
+
+    @Test
+    public void testStringPatternCheckingCodec() throws ReactorException, ParseException, URISyntaxException {
+        SchemaContext schemaContext = RetestUtils.parseYangSources(TEST_MODULE);
+        assertNotNull(schemaContext);
+
+        QNameModule testModuleQName = QNameModule.create(new URI("string-pattern-checking-codec-test"),
+                SimpleDateFormatUtil.getRevisionFormat().parse("1970-01-01"));
+
+        Module testModule = schemaContext.findModuleByName("string-pattern-checking-codec-test", null);
+        assertNotNull(testModule);
+
+        ContainerSchemaNode testContainer = (ContainerSchemaNode) testModule.getDataChildByName(
+                QName.create(testModuleQName, "test-container"));
+        assertNotNull(testContainer);
+
+        LeafSchemaNode testLeaf = (LeafSchemaNode) testContainer.getDataChildByName(
+                QName.create(testModuleQName, "string-leaf-with-valid-pattern"));
+        assertNotNull(testLeaf);
+
+        StringCodec<String> codec = getCodec(testLeaf.getType(), StringCodec.class);
+        assertNotNull(codec);
+        assertEquals("ABCD", codec.serialize("ABCD"));
+        assertEquals("ABCD", codec.deserialize("ABCD"));
+
+        try {
+            codec.deserialize("abcd");
+            fail("Exception should have been thrown.");
+        } catch (IllegalArgumentException ex) {
+            LOG.debug("IllegalArgumentException was thrown as expected: {}", ex);
+            assertTrue(ex.getMessage().contains("is not valid regular expression. [abcd]"));
+        }
+    }
+}
diff --git a/yang/yang-data-impl/src/test/resources/string-pattern-checking-codec-test.yang b/yang/yang-data-impl/src/test/resources/string-pattern-checking-codec-test.yang
new file mode 100644 (file)
index 0000000..14216c2
--- /dev/null
@@ -0,0 +1,12 @@
+module string-pattern-checking-codec-test {
+    namespace "string-pattern-checking-codec-test";
+    prefix "spcct";
+
+    container test-container {
+        leaf string-leaf-with-valid-pattern {
+            type string {
+                pattern "[A-Z]+";
+            }
+        }
+    }
+}
\ No newline at end of file