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