BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6871Test.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.parser.stmt.rfc7950;
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
16 import java.util.Set;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
22 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
23 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
27
28 public class Bug6871Test {
29
30     @Test
31     public void testValidYang11Model() throws Exception {
32         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6871/foo.yang");
33         assertNotNull(schemaContext);
34
35         final Module foo = schemaContext.findModule("foo", QName.parseRevision("2016-12-14")).get();
36
37         final Set<NotificationDefinition> notifications = foo.getNotifications();
38         assertEquals(1, notifications.size());
39         final NotificationDefinition myNotification = notifications.iterator().next();
40         Set<MustDefinition> mustConstraints = myNotification.getConstraints().getMustConstraints();
41         assertEquals(2, mustConstraints.size());
42
43         final Set<RpcDefinition> rpcs = foo.getRpcs();
44         assertEquals(1, rpcs.size());
45         final RpcDefinition myRpc = rpcs.iterator().next();
46
47         final ContainerSchemaNode input = myRpc.getInput();
48         assertNotNull(input);
49         mustConstraints = input.getConstraints().getMustConstraints();
50         assertEquals(2, mustConstraints.size());
51
52         final ContainerSchemaNode output = myRpc.getOutput();
53         assertNotNull(output);
54         mustConstraints = output.getConstraints().getMustConstraints();
55         assertEquals(2, mustConstraints.size());
56     }
57
58     @Test
59     public void testInvalidYang10Model() throws Exception {
60         assertException("/rfc7950/bug6871/foo10.yang", "MUST is not valid for NOTIFICATION");
61         assertException("/rfc7950/bug6871/bar10.yang", "MUST is not valid for INPUT");
62         assertException("/rfc7950/bug6871/baz10.yang", "MUST is not valid for OUTPUT");
63     }
64
65     private static void assertException(final String sourcePath, final String exceptionMessage) throws Exception {
66         try {
67             StmtTestUtils.parseYangSource(sourcePath);
68             fail("Test should fail due to invalid Yang 1.0");
69         } catch (final ReactorException ex) {
70             assertTrue(ex.getCause().getMessage().startsWith(exceptionMessage));
71         }
72     }
73 }