ea11312091cc27d69c1b6678f2b5081b38a3c47d
[yangtools.git] / parser / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / YangTextSchemaContextResolverTest.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.repo;
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 com.google.common.util.concurrent.ListenableFuture;
17 import java.io.IOException;
18 import java.net.URL;
19 import java.util.Optional;
20 import java.util.concurrent.ExecutionException;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
26 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
27 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
28 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
29
30 public class YangTextSchemaContextResolverTest {
31
32     @Test
33     public void testYangTextSchemaContextResolver() throws SchemaSourceException, IOException, YangSyntaxErrorException,
34             InterruptedException, ExecutionException {
35         final YangTextSchemaContextResolver yangTextSchemaContextResolver =
36                 YangTextSchemaContextResolver.create("test-bundle");
37         assertNotNull(yangTextSchemaContextResolver);
38
39         final URL yangFile1 = getClass().getResource("/yang-text-schema-context-resolver-test/foo.yang");
40         assertNotNull(yangFile1);
41         final URL yangFile2 = getClass().getResource("/yang-text-schema-context-resolver-test/bar.yang");
42         assertNotNull(yangFile2);
43         final URL yangFile3 = getClass().getResource("/yang-text-schema-context-resolver-test/baz.yang");
44         assertNotNull(yangFile2);
45
46         final YangTextSchemaSourceRegistration registration1 =
47                 yangTextSchemaContextResolver.registerSource(yangFile1);
48         assertNotNull(registration1);
49         final YangTextSchemaSourceRegistration registration2 =
50                 yangTextSchemaContextResolver.registerSource(yangFile2);
51         assertNotNull(registration2);
52         final YangTextSchemaSourceRegistration registration3 =
53                 yangTextSchemaContextResolver.registerSource(yangFile3);
54         assertNotNull(registration3);
55
56         assertEquals(3, yangTextSchemaContextResolver.getAvailableSources().size());
57
58         final SourceIdentifier fooModuleId = RevisionSourceIdentifier.create("foo", Revision.of("2016-09-26"));
59         final ListenableFuture<YangTextSchemaSource> foo = yangTextSchemaContextResolver.getSource(fooModuleId);
60         assertTrue(foo.isDone());
61         assertEquals(fooModuleId, foo.get().getIdentifier());
62
63         final SourceIdentifier barModuleId = RevisionSourceIdentifier.create("bar", Revision.of("2016-09-26"));
64         final ListenableFuture<YangTextSchemaSource> bar = yangTextSchemaContextResolver.getSource(barModuleId);
65         assertTrue(bar.isDone());
66         assertEquals(barModuleId, bar.get().getIdentifier());
67
68         final SourceIdentifier bazModuleId = RevisionSourceIdentifier.create("baz", Revision.of("2016-09-26"));
69         final ListenableFuture<YangTextSchemaSource> baz =
70                 yangTextSchemaContextResolver.getSource(bazModuleId);
71         assertTrue(baz.isDone());
72         assertEquals(bazModuleId, baz.get().getIdentifier());
73
74         final SourceIdentifier foobarModuleId = RevisionSourceIdentifier.create("foobar", Revision.of("2016-09-26"));
75         final ListenableFuture<YangTextSchemaSource> foobar = yangTextSchemaContextResolver.getSource(foobarModuleId);
76         assertTrue(foobar.isDone());
77         try {
78             foobar.get();
79             fail("A MissingSchemaSourceException should have been thrown.");
80         } catch (ExecutionException e) {
81             assertEquals("URL for RevisionSourceIdentifier [name=foobar@2016-09-26] not registered",
82                 e.getCause().getMessage());
83         }
84
85         Optional<? extends SchemaContext> schemaContextOptional =
86             yangTextSchemaContextResolver.getEffectiveModelContext();
87         assertTrue(schemaContextOptional.isPresent());
88         SchemaContext schemaContext = schemaContextOptional.get();
89         assertEquals(3, schemaContext.getModules().size());
90
91         registration1.close();
92         registration2.close();
93         registration3.close();
94
95         assertEquals(0, yangTextSchemaContextResolver.getAvailableSources().size());
96
97         schemaContextOptional = yangTextSchemaContextResolver.getEffectiveModelContext();
98         assertTrue(schemaContextOptional.isPresent());
99         schemaContext = schemaContextOptional.get();
100         assertEquals(0, schemaContext.getModules().size());
101     }
102 }