BUG-4688: Make SourceIdentifier use Revision
[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 com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Arrays;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mock;
18 import org.mockito.MockitoAnnotations;
19 import org.opendaylight.yangtools.yang.common.Revision;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
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.util.ASTSchemaSource;
27 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
28
29 public class SharedSchemaContextFactoryTest {
30
31     private final SharedSchemaRepository repository = new SharedSchemaRepository("test");
32
33     @Mock
34     private SchemaSourceFilter filter;
35     private SourceIdentifier s1;
36     private SourceIdentifier s2;
37
38     @Before
39     public void setUp() throws Exception {
40         MockitoAnnotations.initMocks(this);
41
42         final YangTextSchemaSource source1 = YangTextSchemaSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang");
43         final YangTextSchemaSource source2 = YangTextSchemaSource.forResource("/ietf/iana-timezones@2012-07-09.yang");
44         s1 = RevisionSourceIdentifier.create("ietf-inet-types", Revision.valueOf("2010-09-24"));
45         s2 = RevisionSourceIdentifier.create("iana-timezones", Revision.valueOf("2012-07-09"));
46
47         final TextToASTTransformer transformer = TextToASTTransformer.create(repository, repository);
48         repository.registerSchemaSourceListener(transformer);
49
50         repository.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(source1),
51             PotentialSchemaSource.create(s1, YangTextSchemaSource.class, 1));
52
53         repository.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(source2),
54             PotentialSchemaSource.create(s2, YangTextSchemaSource.class, 1));
55     }
56
57     @Test
58     public void testCreateSchemaContextWithDuplicateRequiredSources() throws Exception {
59         final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository,
60             filter);
61         final ListenableFuture<SchemaContext> schemaContext =
62                 sharedSchemaContextFactory.createSchemaContext(Arrays.asList(s1, s1, s2));
63         assertNotNull(schemaContext.get());
64     }
65
66     @Test
67     public void testSourceRegisteredWithDifferentSI() throws Exception {
68         final YangTextSchemaSource source1 = YangTextSchemaSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang");
69         final YangTextSchemaSource source2 = YangTextSchemaSource.forResource("/ietf/iana-timezones@2012-07-09.yang");
70         s1 = source1.getIdentifier();
71         s2 = source2.getIdentifier();
72
73         final SettableSchemaProvider<ASTSchemaSource> provider =
74                 SharedSchemaRepositoryTest.getImmediateYangSourceProviderFromResource(
75                     "/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,
85             filter);
86         final ListenableFuture<SchemaContext> schemaContext =
87                 sharedSchemaContextFactory.createSchemaContext(Arrays.asList(sIdWithoutRevision, provider.getId()));
88         assertNotNull(schemaContext.get());
89     }
90 }