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;
18 import java.net.URISyntaxException;
19 import java.text.ParseException;
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.common.SimpleDateFormatUtil;
24 import org.opendaylight.yangtools.yang.data.impl.TestUtils;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
31 import org.opendaylight.yangtools.yang.data.api.codec.StringCodec;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class StringPatternCheckingCodecTest {
37 private static final Logger LOG = LoggerFactory.getLogger(StringPatternCheckingCodecTest.class);
38 private static final YangStatementSourceImpl TEST_MODULE = new YangStatementSourceImpl
39 ("/string-pattern-checking-codec-test.yang", false);
42 public void testStringPatternCheckingCodec() throws ReactorException, ParseException, URISyntaxException {
43 SchemaContext schemaContext = TestUtils.parseYangSources(TEST_MODULE);
44 assertNotNull(schemaContext);
46 QNameModule testModuleQName = QNameModule.create(new URI("string-pattern-checking-codec-test"),
47 SimpleDateFormatUtil.getRevisionFormat().parse("1970-01-01"));
49 Module testModule = schemaContext.findModuleByName("string-pattern-checking-codec-test", null);
50 assertNotNull(testModule);
52 ContainerSchemaNode testContainer = (ContainerSchemaNode) testModule.getDataChildByName(
53 QName.create(testModuleQName, "test-container"));
54 assertNotNull(testContainer);
56 LeafSchemaNode testLeaf = (LeafSchemaNode) testContainer.getDataChildByName(
57 QName.create(testModuleQName, "string-leaf-with-valid-pattern"));
58 assertNotNull(testLeaf);
60 StringCodec<String> codec = getCodec(testLeaf.getType(), StringCodec.class);
62 assertEquals("ABCD", codec.serialize("ABCD"));
63 assertEquals("ABCD", codec.deserialize("ABCD"));
66 codec.deserialize("abcd");
67 fail("Exception should have been thrown.");
68 } catch (IllegalArgumentException ex) {
69 LOG.debug("IllegalArgumentException was thrown as expected: {}", ex);
70 assertTrue(ex.getMessage().contains("is not valid regular expression. [abcd]"));