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