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