2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.parser.repo;
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertThrows;
16 import static org.junit.Assert.assertTrue;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.io.IOException;
21 import java.util.Optional;
22 import java.util.concurrent.ExecutionException;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
27 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
31 public class YangTextSchemaContextResolverTest {
34 public void testYangTextSchemaContextResolver() throws SchemaSourceException, IOException, YangSyntaxErrorException,
35 InterruptedException, ExecutionException {
36 final YangTextSchemaContextResolver yangTextSchemaContextResolver =
37 YangTextSchemaContextResolver.create("test-bundle");
38 assertNotNull(yangTextSchemaContextResolver);
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);
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);
57 assertEquals(3, yangTextSchemaContextResolver.getAvailableSources().size());
59 final SourceIdentifier fooModuleId = new SourceIdentifier("foo", "2016-09-26");
60 final ListenableFuture<YangTextSchemaSource> foo = yangTextSchemaContextResolver.getSource(fooModuleId);
61 assertTrue(foo.isDone());
62 assertEquals(fooModuleId, foo.get().getIdentifier());
64 final SourceIdentifier barModuleId = new SourceIdentifier("bar", "2016-09-26");
65 final ListenableFuture<YangTextSchemaSource> bar = yangTextSchemaContextResolver.getSource(barModuleId);
66 assertTrue(bar.isDone());
67 assertEquals(barModuleId, bar.get().getIdentifier());
69 final SourceIdentifier bazModuleId = new SourceIdentifier("baz", "2016-09-26");
70 final ListenableFuture<YangTextSchemaSource> baz =
71 yangTextSchemaContextResolver.getSource(bazModuleId);
72 assertTrue(baz.isDone());
73 assertEquals(bazModuleId, baz.get().getIdentifier());
75 final SourceIdentifier foobarModuleId = new SourceIdentifier("foobar", "2016-09-26");
76 final ListenableFuture<YangTextSchemaSource> foobar = yangTextSchemaContextResolver.getSource(foobarModuleId);
77 assertTrue(foobar.isDone());
79 final Throwable cause = assertThrows(ExecutionException.class, foobar::get).getCause();
80 assertThat(cause, instanceOf(MissingSchemaSourceException.class));
81 assertEquals("URL for SourceIdentifier [foobar@2016-09-26] not registered", cause.getMessage());
83 Optional<? extends SchemaContext> schemaContextOptional =
84 yangTextSchemaContextResolver.getEffectiveModelContext();
85 assertTrue(schemaContextOptional.isPresent());
86 SchemaContext schemaContext = schemaContextOptional.orElseThrow();
87 assertEquals(3, schemaContext.getModules().size());
89 registration1.close();
90 registration2.close();
91 registration3.close();
93 assertEquals(0, yangTextSchemaContextResolver.getAvailableSources().size());
95 schemaContextOptional = yangTextSchemaContextResolver.getEffectiveModelContext();
96 assertTrue(schemaContextOptional.isPresent());
97 schemaContext = schemaContextOptional.orElseThrow();
98 assertEquals(0, schemaContext.getModules().size());