Populate parser/ hierarchy
[yangtools.git] / parser / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / SharedEffectiveModelContextFactoryTest.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 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
12
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.concurrent.ExecutionException;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.junit.MockitoJUnitRunner;
19 import org.opendaylight.yangtools.yang.common.Revision;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
25 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
28
29 @RunWith(MockitoJUnitRunner.StrictStubs.class)
30 public class SharedEffectiveModelContextFactoryTest {
31
32     private final SharedSchemaRepository repository = new SharedSchemaRepository("test");
33
34     private final SchemaContextFactoryConfiguration config = SchemaContextFactoryConfiguration.getDefault();
35     private SourceIdentifier s1;
36     private SourceIdentifier s2;
37
38     @Before
39     public void setUp() {
40         final YangTextSchemaSource source1 = YangTextSchemaSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang");
41         final YangTextSchemaSource source2 = YangTextSchemaSource.forResource("/ietf/iana-timezones@2012-07-09.yang");
42         s1 = RevisionSourceIdentifier.create("ietf-inet-types", Revision.of("2010-09-24"));
43         s2 = RevisionSourceIdentifier.create("iana-timezones", Revision.of("2012-07-09"));
44
45         final TextToIRTransformer transformer = TextToIRTransformer.create(repository, repository);
46         repository.registerSchemaSourceListener(transformer);
47
48         repository.registerSchemaSource(sourceIdentifier -> immediateFluentFuture(source1),
49             PotentialSchemaSource.create(s1, YangTextSchemaSource.class, 1));
50
51         repository.registerSchemaSource(sourceIdentifier -> immediateFluentFuture(source2),
52             PotentialSchemaSource.create(s2, YangTextSchemaSource.class, 1));
53     }
54
55     @Test
56     public void testCreateSchemaContextWithDuplicateRequiredSources() throws InterruptedException, ExecutionException {
57         final SharedEffectiveModelContextFactory sharedSchemaContextFactory =
58             new SharedEffectiveModelContextFactory(repository, config);
59         final ListenableFuture<EffectiveModelContext> schemaContext =
60                 sharedSchemaContextFactory.createEffectiveModelContext(s1, s1, s2);
61         assertNotNull(schemaContext.get());
62     }
63
64     @Test
65     public void testSourceRegisteredWithDifferentSI() throws Exception {
66         final YangTextSchemaSource source1 = YangTextSchemaSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang");
67         final YangTextSchemaSource source2 = YangTextSchemaSource.forResource("/ietf/iana-timezones@2012-07-09.yang");
68         s1 = source1.getIdentifier();
69         s2 = source2.getIdentifier();
70
71         final SettableSchemaProvider<IRSchemaSource> provider =
72                 SharedSchemaRepositoryTest.getImmediateYangSourceProviderFromResource(
73                     "/no-revision/imported@2012-12-12.yang");
74         provider.setResult();
75         provider.register(repository);
76
77         // Register the same provider under source id without revision
78         final SourceIdentifier sIdWithoutRevision = RevisionSourceIdentifier.create(provider.getId().getName());
79         repository.registerSchemaSource(provider, PotentialSchemaSource.create(
80                 sIdWithoutRevision, IRSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
81
82         final SharedEffectiveModelContextFactory sharedSchemaContextFactory =
83             new SharedEffectiveModelContextFactory(repository, config);
84         final ListenableFuture<EffectiveModelContext> schemaContext =
85                 sharedSchemaContextFactory.createEffectiveModelContext(sIdWithoutRevision, provider.getId());
86         assertNotNull(schemaContext.get());
87     }
88 }