Promote SchemaSourceRepresentation
[yangtools.git] / parser / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / OpenconfigVerSharedSchemaRepositoryTest.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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import org.junit.jupiter.api.Test;
15 import org.opendaylight.yangtools.yang.ir.YangIRSchemaSource;
16 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
17 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
18 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
19
20 public class OpenconfigVerSharedSchemaRepositoryTest {
21     @Test
22     public void testSharedSchemaRepository() throws Exception {
23         final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-test");
24
25         final var bar = getImmediateYangSourceProviderFromResource(
26                 "/openconfig-version/shared-schema-repository/bar@2016-01-01.yang");
27         bar.register(sharedSchemaRepository);
28         bar.setResult();
29         final var foo = getImmediateYangSourceProviderFromResource(
30                 "/openconfig-version/shared-schema-repository/foo.yang");
31         foo.register(sharedSchemaRepository);
32         foo.setResult();
33         final var semVer = getImmediateYangSourceProviderFromResource(
34                 "/openconfig-version/shared-schema-repository/openconfig-extensions.yang");
35         semVer.register(sharedSchemaRepository);
36         semVer.setResult();
37
38         final var fact = sharedSchemaRepository.createEffectiveModelContextFactory();
39         final var inetAndTopologySchemaContextFuture =
40                 fact.createEffectiveModelContext(bar.getId(), foo.getId(), semVer.getId());
41         assertTrue(inetAndTopologySchemaContextFuture.isDone());
42         assertSchemaContext(inetAndTopologySchemaContextFuture.get(), 3);
43
44         final var barSchemaContextFuture = fact.createEffectiveModelContext(bar.getId(), semVer.getId());
45         assertTrue(barSchemaContextFuture.isDone());
46         assertSchemaContext(barSchemaContextFuture.get(), 2);
47     }
48
49     private static void assertSchemaContext(final EffectiveModelContext schemaContext, final int moduleSize) {
50         assertNotNull(schemaContext);
51         assertEquals(moduleSize, schemaContext.getModules().size());
52     }
53
54     static SettableSchemaProvider<YangIRSchemaSource> getImmediateYangSourceProviderFromResource(
55             final String resourceName) throws Exception {
56         return SettableSchemaProvider.createImmediate(
57             TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)), YangIRSchemaSource.class);
58     }
59 }