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