Update StmtTestUtils
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug7480Test.java
1 /*
2  * Copyright (c) 2017 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.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import java.net.URI;
16 import java.util.Set;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
22
23 public class Bug7480Test {
24     @Test
25     public void libSourcesTest() throws Exception {
26         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
27         assertNotNull(context);
28
29         final Set<Module> modules = context.getModules();
30         assertEquals(7, modules.size());
31
32         assertNotNull(context.findModuleByNamespaceAndRevision(new URI("foo-imp"), SimpleDateFormatUtil
33                 .getRevisionFormat().parse("2017-01-23")));
34         assertEquals(1, context.findModuleByNamespace(new URI("foo-imp-2")).size());
35         assertEquals(1, context.findModuleByNamespace(new URI("foo-imp-imp")).size());
36         assertEquals(1, context.findModuleByNamespace(new URI("bar")).size());
37         assertEquals(1, context.findModuleByNamespace(new URI("baz")).size());
38         assertNotNull(context.findModuleByNamespaceAndRevision(new URI("baz-imp"), SimpleDateFormatUtil
39                 .getRevisionFormat().parse("2002-01-01")));
40         final Set<Module> foo = context.findModuleByNamespace(new URI("foo"));
41         assertEquals(1, foo.size());
42         final Set<Module> subFoos = foo.iterator().next().getSubmodules();
43         assertEquals(1, subFoos.size());
44     }
45
46     @Test
47     public void missingRelevantImportTest() throws Exception {
48         try {
49             StmtTestUtils.parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2");
50             fail("Test should fail due to missing import of required yang source from library");
51         } catch (final SomeModifiersUnresolvedException e) {
52             final String message = e.getSuppressed().length > 0 ? e.getSuppressed()[0].getCause().getMessage() : e
53                     .getCause().getCause().getMessage();
54             assertTrue(message.startsWith("Imported module [missing-lib] was not found."));
55         }
56     }
57 }