From: Igor Foltin Date: Tue, 9 Feb 2016 10:52:34 +0000 (+0100) Subject: Bug 3899: Milestone: Increase test coverage for Yangtools X-Git-Tag: release/boron~227 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=31cd2233ff7a33f0c3d4b9fb56d3da24820bb835;hp=f45c04d93c92101cb5938b72b8f6d6312e680abb;p=yangtools.git Bug 3899: Milestone: Increase test coverage for Yangtools Added unit test for StringPatternCheckingCodec. Change-Id: I95883efa82d0e987fa0b5b6e507513e105aca267 Signed-off-by: Igor Foltin --- 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 index 0000000000..9be9ea482f --- /dev/null +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/test/codecs/StringPatternCheckingCodecTest.java @@ -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 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 index 0000000000..14216c20a2 --- /dev/null +++ b/yang/yang-data-impl/src/test/resources/string-pattern-checking-codec-test.yang @@ -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