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