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
8 package org.opendaylight.yangtools.yang.parser.repo;
10 import static org.junit.Assert.assertNotNull;
12 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
14 import com.google.common.collect.Lists;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
28 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
29 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
32 public class SharedSchemaContextFactoryTest {
34 private final SharedSchemaRepository repository = new SharedSchemaRepository("test");
37 private SchemaSourceFilter filter;
38 private SourceIdentifier s1;
39 private SourceIdentifier s2;
42 public void setUp() throws Exception {
43 MockitoAnnotations.initMocks(this);
45 final ResourceYangSource source1 = new ResourceYangSource("/ietf/ietf-inet-types@2010-09-24.yang");
46 final ResourceYangSource source2 = new ResourceYangSource("/ietf/iana-timezones@2012-07-09.yang");
47 s1 = RevisionSourceIdentifier.create("ietf-inet-types", "2010-09-24");
48 s2 = RevisionSourceIdentifier.create("iana-timezones", "2012-07-09");
50 final TextToASTTransformer transformer = TextToASTTransformer.create(repository, repository);
51 repository.registerSchemaSourceListener(transformer);
53 repository.registerSchemaSource(new SchemaSourceProvider<YangTextSchemaSource>() {
55 public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
56 return Futures.immediateCheckedFuture(source1);
58 }, PotentialSchemaSource.create(s1, YangTextSchemaSource.class, 1));
60 repository.registerSchemaSource(new SchemaSourceProvider<YangTextSchemaSource>() {
62 public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
63 return Futures.immediateCheckedFuture(source2);
65 }, PotentialSchemaSource.create(s2, YangTextSchemaSource.class, 1));
69 public void testCreateSchemaContextWithDuplicateRequiredSources() throws Exception {
70 final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository, filter);
71 final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContext = sharedSchemaContextFactory.createSchemaContext(Lists.newArrayList(s1, s1, s2));
72 assertNotNull(schemaContext.checkedGet());
76 public void testSourceRegisteredWithDifferentSI() throws Exception {
77 final ResourceYangSource source1 = new ResourceYangSource("/ietf/ietf-inet-types@2010-09-24.yang");
78 final ResourceYangSource source2 = new ResourceYangSource("/ietf/iana-timezones@2012-07-09.yang");
79 s1 = source1.getIdentifier();
80 s2 = source2.getIdentifier();
82 final SettableSchemaProvider<ASTSchemaSource> provider = SharedSchemaRepositoryTest.getImmediateYangSourceProviderFromResource("/no-revision/imported@2012-12-12.yang");
84 provider.register(repository);
86 // Register the same provider under source id without revision
87 final SourceIdentifier sIdWithoutRevision = RevisionSourceIdentifier.create(provider.getId().getName());
88 repository.registerSchemaSource(provider, PotentialSchemaSource.create(
89 sIdWithoutRevision, ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
91 final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository, filter);
92 final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContext = sharedSchemaContextFactory.createSchemaContext(Lists.newArrayList(sIdWithoutRevision, provider.getId()));
93 assertNotNull(schemaContext.checkedGet());