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