Remove deprecated Yin/YangStatementSourceImpl
[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.io.FileNotFoundException;
18 import java.net.URI;
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;
34
35 public class StringPatternCheckingCodecTest {
36
37     private static final Logger LOG = LoggerFactory.getLogger(StringPatternCheckingCodecTest.class);
38
39     @Test
40     public void testStringPatternCheckingCodec() throws ReactorException, ParseException, URISyntaxException,
41             FileNotFoundException {
42         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/string-pattern-checking-codec-test.yang");
43         assertNotNull(schemaContext);
44
45         final QNameModule testModuleQName = QNameModule.create(new URI("string-pattern-checking-codec-test"),
46                 SimpleDateFormatUtil.getRevisionFormat().parse("1970-01-01"));
47
48         final Module testModule = schemaContext.findModuleByName("string-pattern-checking-codec-test", null);
49         assertNotNull(testModule);
50
51         final ContainerSchemaNode testContainer = (ContainerSchemaNode) testModule.getDataChildByName(
52                 QName.create(testModuleQName, "test-container"));
53         assertNotNull(testContainer);
54
55         final LeafSchemaNode testLeaf = (LeafSchemaNode) testContainer.getDataChildByName(
56                 QName.create(testModuleQName, "string-leaf-with-valid-pattern"));
57         assertNotNull(testLeaf);
58
59         final StringCodec<String> codec = getCodec(testLeaf.getType(), StringCodec.class);
60         assertNotNull(codec);
61         assertEquals("ABCD", codec.serialize("ABCD"));
62         assertEquals("ABCD", codec.deserialize("ABCD"));
63
64         try {
65             codec.deserialize("abcd");
66             fail("Exception should have been thrown.");
67         } catch (final IllegalArgumentException ex) {
68             LOG.debug("IllegalArgumentException was thrown as expected: {}", ex);
69             assertTrue(ex.getMessage().contains("Supplied value does not match the regular expression ^[A-Z]+$. [abcd]"));
70         }
71     }
72 }