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