BUG-4688: eliminate SimpleDateFormatUtil.DEFAULT_DATE_REV
[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 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;
32
33 public class StringPatternCheckingCodecTest {
34
35     private static final Logger LOG = LoggerFactory.getLogger(StringPatternCheckingCodecTest.class);
36
37     @Test
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);
43
44         final QNameModule testModuleQName = QNameModule.create(new URI("string-pattern-checking-codec-test"), null);
45
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);
50
51         final LeafSchemaNode testLeaf = (LeafSchemaNode) testContainer.getDataChildByName(
52                 QName.create(testModuleQName, "string-leaf-with-valid-pattern"));
53         assertNotNull(testLeaf);
54
55         final StringCodec<String> codec = getCodec(testLeaf.getType(), StringCodec.class);
56         assertNotNull(codec);
57         assertEquals("ABCD", codec.serialize("ABCD"));
58         assertEquals("ABCD", codec.deserialize("ABCD"));
59
60         try {
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]"));
67         }
68     }
69 }