Cleanup DataTree interfaces and InMemmoryDataTreeFactory
[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.fail;
14 import static org.opendaylight.yangtools.yang.data.impl.codecs.TypeDefinitionAwareCodecTestHelper.getCodec;
15
16 import java.net.URI;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.data.api.codec.StringCodec;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class StringPatternCheckingCodecTest {
30
31     private static final Logger LOG = LoggerFactory.getLogger(StringPatternCheckingCodecTest.class);
32
33     @Test
34     public void testStringPatternCheckingCodec() {
35         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource(
36             "/string-pattern-checking-codec-test.yang");
37         assertNotNull(schemaContext);
38
39         final QNameModule testModuleQName = QNameModule.create(URI.create("string-pattern-checking-codec-test"));
40
41         final Module testModule = schemaContext.findModules("string-pattern-checking-codec-test").iterator().next();
42         final ContainerSchemaNode testContainer = (ContainerSchemaNode) testModule.getDataChildByName(
43                 QName.create(testModuleQName, "test-container"));
44         assertNotNull(testContainer);
45
46         final LeafSchemaNode testLeaf = (LeafSchemaNode) testContainer.getDataChildByName(
47                 QName.create(testModuleQName, "string-leaf-with-valid-pattern"));
48         assertNotNull(testLeaf);
49
50         final StringCodec<String> codec = getCodec(testLeaf.getType(), StringCodec.class);
51         assertNotNull(codec);
52         assertEquals("ABCD", codec.serialize("ABCD"));
53         assertEquals("ABCD", codec.deserialize("ABCD"));
54
55         try {
56             codec.deserialize("abcd");
57             fail("Exception should have been thrown.");
58         } catch (final IllegalArgumentException ex) {
59             LOG.debug("IllegalArgumentException was thrown as expected: {}", ex);
60             assertEquals("Supplied value does not match the regular expression ^[A-Z]+$.", ex.getMessage());
61         }
62     }
63 }