d4fdc97fd31d8cd63edc7c24d9a21951292aa21e
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaContextFactoryTest.java
1 package org.opendaylight.yangtools.yang.parser.repo;
2
3 import static org.junit.Assert.assertNotNull;
4
5 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
6
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;
24
25 public class SharedSchemaContextFactoryTest {
26
27     private final SharedSchemaRepository repository = new SharedSchemaRepository("test");
28
29     @Mock
30     private SchemaSourceFilter filter;
31     private SourceIdentifier s1;
32     private SourceIdentifier s2;
33
34     @Before
35     public void setUp() throws Exception {
36         MockitoAnnotations.initMocks(this);
37
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");
42
43         final TextToASTTransformer transformer = TextToASTTransformer.create(repository, repository);
44         repository.registerSchemaSourceListener(transformer);
45
46         repository.registerSchemaSource(new SchemaSourceProvider<YangTextSchemaSource>() {
47             @Override
48             public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
49                 return Futures.immediateCheckedFuture(source1);
50             }
51         }, PotentialSchemaSource.create(s1, YangTextSchemaSource.class, 1));
52
53         repository.registerSchemaSource(new SchemaSourceProvider<YangTextSchemaSource>() {
54             @Override
55             public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
56                 return Futures.immediateCheckedFuture(source2);
57             }
58         }, PotentialSchemaSource.create(s2, YangTextSchemaSource.class, 1));
59     }
60
61     @Test
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());
66     }
67
68     @Test
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();
74
75         final SettableSchemaProvider<ASTSchemaSource> provider = SharedSchemaRepositoryTest.getImmediateYangSourceProviderFromResource("/no-revision/imported@2012-12-12.yang");
76         provider.setResult();
77         provider.register(repository);
78
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()));
83
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());
87     }
88 }