2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.data.impl.codecs;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.yangtools.yang.data.impl.codecs.TypeDefinitionAwareCodecTestHelper.getCodec;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.data.api.codec.StringCodec;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class StringPatternCheckingCodecTest {
31 private static final Logger LOG = LoggerFactory.getLogger(StringPatternCheckingCodecTest.class);
34 public void testStringPatternCheckingCodec() {
35 final SchemaContext schemaContext = YangParserTestUtils.parseYangResource(
36 "/string-pattern-checking-codec-test.yang");
37 assertNotNull(schemaContext);
39 final QNameModule testModuleQName = QNameModule.create(URI.create("string-pattern-checking-codec-test"));
41 final Module testModule = schemaContext.findModules("string-pattern-checking-codec-test").iterator().next();
42 final ContainerSchemaNode testContainer = (ContainerSchemaNode) testModule.getDataChildByName(
43 QName.create(testModuleQName, "test-container"));
44 assertNotNull(testContainer);
46 final LeafSchemaNode testLeaf = (LeafSchemaNode) testContainer.getDataChildByName(
47 QName.create(testModuleQName, "string-leaf-with-valid-pattern"));
48 assertNotNull(testLeaf);
50 final StringCodec<String> codec = getCodec(testLeaf.getType(), StringCodec.class);
52 assertEquals("ABCD", codec.serialize("ABCD"));
53 assertEquals("ABCD", codec.deserialize("ABCD"));
56 codec.deserialize("abcd");
57 fail("Exception should have been thrown.");
58 } catch (final IllegalArgumentException ex) {
59 LOG.debug("IllegalArgumentException was thrown as expected", ex);
60 assertEquals("Supplied value does not match the regular expression ^(?:[A-Z]+)$.", ex.getMessage());