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.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.yangtools.yang.data.impl.codecs.TypeDefinitionAwareCodecTestHelper.getCodec;
17 import java.io.FileNotFoundException;
19 import java.net.URISyntaxException;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.data.api.codec.StringCodec;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class StringPatternCheckingCodecTest {
35 private static final Logger LOG = LoggerFactory.getLogger(StringPatternCheckingCodecTest.class);
38 public void testStringPatternCheckingCodec() throws ReactorException, URISyntaxException,
39 FileNotFoundException {
40 final SchemaContext schemaContext = YangParserTestUtils.parseYangResource(
41 "/string-pattern-checking-codec-test.yang");
42 assertNotNull(schemaContext);
44 final QNameModule testModuleQName = QNameModule.create(new URI("string-pattern-checking-codec-test"), null);
46 final Module testModule = schemaContext.findModules("string-pattern-checking-codec-test").iterator().next();
47 final ContainerSchemaNode testContainer = (ContainerSchemaNode) testModule.getDataChildByName(
48 QName.create(testModuleQName, "test-container"));
49 assertNotNull(testContainer);
51 final LeafSchemaNode testLeaf = (LeafSchemaNode) testContainer.getDataChildByName(
52 QName.create(testModuleQName, "string-leaf-with-valid-pattern"));
53 assertNotNull(testLeaf);
55 final StringCodec<String> codec = getCodec(testLeaf.getType(), StringCodec.class);
57 assertEquals("ABCD", codec.serialize("ABCD"));
58 assertEquals("ABCD", codec.deserialize("ABCD"));
61 codec.deserialize("abcd");
62 fail("Exception should have been thrown.");
63 } catch (final IllegalArgumentException ex) {
64 LOG.debug("IllegalArgumentException was thrown as expected: {}", ex);
65 assertTrue(ex.getMessage().contains(
66 "Supplied value does not match the regular expression ^[A-Z]+$. [abcd]"));