Move ASTSchemaSource and its components into rfc6020.repo
[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 java.util.concurrent.ExecutionException;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.yangtools.yang.common.Revision;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
27 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.ASTSchemaSource;
28 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.TextToASTTransformer;
29
30 public class SharedSchemaContextFactoryTest {
31
32     private final SharedSchemaRepository repository = new SharedSchemaRepository("test");
33
34     @Mock
35     private SchemaSourceFilter filter;
36     private SourceIdentifier s1;
37     private SourceIdentifier s2;
38
39     @Before
40     public void setUp() throws Exception {
41         MockitoAnnotations.initMocks(this);
42
43         final YangTextSchemaSource source1 = YangTextSchemaSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang");
44         final YangTextSchemaSource source2 = YangTextSchemaSource.forResource("/ietf/iana-timezones@2012-07-09.yang");
45         s1 = RevisionSourceIdentifier.create("ietf-inet-types", Revision.of("2010-09-24"));
46         s2 = RevisionSourceIdentifier.create("iana-timezones", Revision.of("2012-07-09"));
47
48         final TextToASTTransformer transformer = TextToASTTransformer.create(repository, repository);
49         repository.registerSchemaSourceListener(transformer);
50
51         repository.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(source1),
52             PotentialSchemaSource.create(s1, YangTextSchemaSource.class, 1));
53
54         repository.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(source2),
55             PotentialSchemaSource.create(s2, YangTextSchemaSource.class, 1));
56     }
57
58     @Test
59     public void testCreateSchemaContextWithDuplicateRequiredSources() throws InterruptedException, ExecutionException {
60         final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository,
61             filter);
62         final ListenableFuture<SchemaContext> schemaContext =
63                 sharedSchemaContextFactory.createSchemaContext(Arrays.asList(s1, s1, s2));
64         assertNotNull(schemaContext.get());
65     }
66
67     @Test
68     public void testSourceRegisteredWithDifferentSI() throws Exception {
69         final YangTextSchemaSource source1 = YangTextSchemaSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang");
70         final YangTextSchemaSource source2 = YangTextSchemaSource.forResource("/ietf/iana-timezones@2012-07-09.yang");
71         s1 = source1.getIdentifier();
72         s2 = source2.getIdentifier();
73
74         final SettableSchemaProvider<ASTSchemaSource> provider =
75                 SharedSchemaRepositoryTest.getImmediateYangSourceProviderFromResource(
76                     "/no-revision/imported@2012-12-12.yang");
77         provider.setResult();
78         provider.register(repository);
79
80         // Register the same provider under source id without revision
81         final SourceIdentifier sIdWithoutRevision = RevisionSourceIdentifier.create(provider.getId().getName());
82         repository.registerSchemaSource(provider, PotentialSchemaSource.create(
83                 sIdWithoutRevision, ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
84
85         final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository,
86             filter);
87         final ListenableFuture<SchemaContext> schemaContext =
88                 sharedSchemaContextFactory.createSchemaContext(Arrays.asList(sIdWithoutRevision, provider.getId()));
89         assertNotNull(schemaContext.get());
90     }
91 }