0038007cc691c190104d917660b599dd713436e0
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaContextFactoryTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.repo;
9
10 import static org.junit.Assert.assertNotNull;
11
12 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
13
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;
31
32 public class SharedSchemaContextFactoryTest {
33
34     private final SharedSchemaRepository repository = new SharedSchemaRepository("test");
35
36     @Mock
37     private SchemaSourceFilter filter;
38     private SourceIdentifier s1;
39     private SourceIdentifier s2;
40
41     @Before
42     public void setUp() throws Exception {
43         MockitoAnnotations.initMocks(this);
44
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");
49
50         final TextToASTTransformer transformer = TextToASTTransformer.create(repository, repository);
51         repository.registerSchemaSourceListener(transformer);
52
53         repository.registerSchemaSource(new SchemaSourceProvider<YangTextSchemaSource>() {
54             @Override
55             public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
56                 return Futures.immediateCheckedFuture(source1);
57             }
58         }, PotentialSchemaSource.create(s1, YangTextSchemaSource.class, 1));
59
60         repository.registerSchemaSource(new SchemaSourceProvider<YangTextSchemaSource>() {
61             @Override
62             public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
63                 return Futures.immediateCheckedFuture(source2);
64             }
65         }, PotentialSchemaSource.create(s2, YangTextSchemaSource.class, 1));
66     }
67
68     @Test
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());
73     }
74
75     @Test
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();
81
82         final SettableSchemaProvider<ASTSchemaSource> provider = SharedSchemaRepositoryTest.getImmediateYangSourceProviderFromResource("/no-revision/imported@2012-12-12.yang");
83         provider.setResult();
84         provider.register(repository);
85
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()));
90
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());
94     }
95 }