cfbd0fd35d3540b0d9d932e7ccd43e61907f04ce
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codecs / StringPatternCheckingCodecTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.data.impl.codecs;
10
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;
16
17 import java.net.URI;
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;
34
35 public class StringPatternCheckingCodecTest {
36
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);
40
41     @Test
42     public void testStringPatternCheckingCodec() throws ReactorException, ParseException, URISyntaxException {
43         SchemaContext schemaContext = TestUtils.parseYangSources(TEST_MODULE);
44         assertNotNull(schemaContext);
45
46         QNameModule testModuleQName = QNameModule.create(new URI("string-pattern-checking-codec-test"),
47                 SimpleDateFormatUtil.getRevisionFormat().parse("1970-01-01"));
48
49         Module testModule = schemaContext.findModuleByName("string-pattern-checking-codec-test", null);
50         assertNotNull(testModule);
51
52         ContainerSchemaNode testContainer = (ContainerSchemaNode) testModule.getDataChildByName(
53                 QName.create(testModuleQName, "test-container"));
54         assertNotNull(testContainer);
55
56         LeafSchemaNode testLeaf = (LeafSchemaNode) testContainer.getDataChildByName(
57                 QName.create(testModuleQName, "string-leaf-with-valid-pattern"));
58         assertNotNull(testLeaf);
59
60         StringCodec<String> codec = getCodec(testLeaf.getType(), StringCodec.class);
61         assertNotNull(codec);
62         assertEquals("ABCD", codec.serialize("ABCD"));
63         assertEquals("ABCD", codec.deserialize("ABCD"));
64
65         try {
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]"));
71         }
72     }
73 }