BUG-9043: Remove use of CheckedFuture from YANG components
[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.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.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
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             InterruptedException, ExecutionException {
34         final YangTextSchemaContextResolver yangTextSchemaContextResolver =
35                 YangTextSchemaContextResolver.create("test-bundle");
36         assertNotNull(yangTextSchemaContextResolver);
37
38         final URL yangFile1 = getClass().getResource("/yang-text-schema-context-resolver-test/foo.yang");
39         assertNotNull(yangFile1);
40         final URL yangFile2 = getClass().getResource("/yang-text-schema-context-resolver-test/bar.yang");
41         assertNotNull(yangFile2);
42         final URL yangFile3 = getClass().getResource("/yang-text-schema-context-resolver-test/baz.yang");
43         assertNotNull(yangFile2);
44
45         final YangTextSchemaSourceRegistration registration1 =
46                 yangTextSchemaContextResolver.registerSource(yangFile1);
47         assertNotNull(registration1);
48         final YangTextSchemaSourceRegistration registration2 =
49                 yangTextSchemaContextResolver.registerSource(yangFile2);
50         assertNotNull(registration2);
51         final YangTextSchemaSourceRegistration registration3 =
52                 yangTextSchemaContextResolver.registerSource(yangFile3);
53         assertNotNull(registration3);
54
55         assertEquals(3, yangTextSchemaContextResolver.getAvailableSources().size());
56
57         final SourceIdentifier fooModuleId = RevisionSourceIdentifier.create("foo", "2016-09-26");
58         final ListenableFuture<YangTextSchemaSource> foo = yangTextSchemaContextResolver.getSource(fooModuleId);
59         assertTrue(foo.isDone());
60         assertEquals(fooModuleId, foo.get().getIdentifier());
61
62         final SourceIdentifier barModuleId = RevisionSourceIdentifier.create("bar", "2016-09-26");
63         final ListenableFuture<YangTextSchemaSource> bar = yangTextSchemaContextResolver.getSource(barModuleId);
64         assertTrue(bar.isDone());
65         assertEquals(barModuleId, bar.get().getIdentifier());
66
67         final SourceIdentifier bazModuleId = RevisionSourceIdentifier.create("baz", "2016-09-26");
68         final ListenableFuture<YangTextSchemaSource> baz =
69                 yangTextSchemaContextResolver.getSource(bazModuleId);
70         assertTrue(baz.isDone());
71         assertEquals(bazModuleId, baz.get().getIdentifier());
72
73         final SourceIdentifier foobarModuleId = RevisionSourceIdentifier.create("foobar", "2016-09-26");
74         final ListenableFuture<YangTextSchemaSource> foobar = yangTextSchemaContextResolver.getSource(foobarModuleId);
75         assertTrue(foobar.isDone());
76         try {
77             foobar.get();
78             fail("A MissingSchemaSourceException should have been thrown.");
79         } catch (ExecutionException e) {
80             assertEquals("URL for RevisionSourceIdentifier [name=foobar@2016-09-26] not registered",
81                 e.getCause().getMessage());
82         }
83
84         Optional<SchemaContext> schemaContextOptional = yangTextSchemaContextResolver.getSchemaContext();
85         assertTrue(schemaContextOptional.isPresent());
86         SchemaContext schemaContext = schemaContextOptional.get();
87         assertEquals(3, schemaContext.getModules().size());
88
89         registration1.close();
90         registration2.close();
91         registration3.close();
92
93         assertEquals(0, yangTextSchemaContextResolver.getAvailableSources().size());
94
95         schemaContextOptional = yangTextSchemaContextResolver.getSchemaContext();
96         assertTrue(schemaContextOptional.isPresent());
97         schemaContext = schemaContextOptional.get();
98         assertEquals(0, schemaContext.getModules().size());
99     }
100 }