2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.parser.repo;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.Lists;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
22 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
23 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
24 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
25 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
27 public class SemVerSharedSchemaRepositoryTest {
30 public void testSemVerSharedSchemaRepository() throws Exception {
31 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
32 "sem-ver-shared-schema-repo-test");
34 final SettableSchemaProvider<ASTSchemaSource> bar = getImmediateYangSourceProviderFromResource(
35 "/semantic-version/semver-shared-schema-repository/bar@2016-01-01.yang");
36 bar.register(sharedSchemaRepository);
38 final SettableSchemaProvider<ASTSchemaSource> foo = getImmediateYangSourceProviderFromResource(
39 "/semantic-version/semver-shared-schema-repository/foo.yang");
40 foo.register(sharedSchemaRepository);
42 final SettableSchemaProvider<ASTSchemaSource> semVer = getImmediateYangSourceProviderFromResource(
43 "/semantic-version/semver-shared-schema-repository/semantic-version.yang");
44 semVer.register(sharedSchemaRepository);
47 final SchemaContextFactory fact = sharedSchemaRepository
48 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
50 final CheckedFuture<SchemaContext, SchemaResolutionException> inetAndTopologySchemaContextFuture = fact
51 .createSchemaContext(Lists.newArrayList(bar.getId(), foo.getId(), semVer.getId()),
52 StatementParserMode.SEMVER_MODE);
53 assertTrue(inetAndTopologySchemaContextFuture.isDone());
54 assertSchemaContext(inetAndTopologySchemaContextFuture.checkedGet(), 3);
56 final CheckedFuture<SchemaContext, SchemaResolutionException> barSchemaContextFuture = fact
57 .createSchemaContext(Lists.newArrayList(bar.getId(), semVer.getId()), StatementParserMode.SEMVER_MODE);
58 assertTrue(barSchemaContextFuture.isDone());
59 assertSchemaContext(barSchemaContextFuture.checkedGet(), 2);
63 public void testSharedSchemaRepository() throws Exception {
64 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-test");
66 final SettableSchemaProvider<ASTSchemaSource> bar = getImmediateYangSourceProviderFromResource(
67 "/semantic-version/shared-schema-repository/bar@2016-01-01.yang");
68 bar.register(sharedSchemaRepository);
70 final SettableSchemaProvider<ASTSchemaSource> foo = getImmediateYangSourceProviderFromResource(
71 "/semantic-version/shared-schema-repository/foo.yang");
72 foo.register(sharedSchemaRepository);
74 final SettableSchemaProvider<ASTSchemaSource> semVer = getImmediateYangSourceProviderFromResource(
75 "/semantic-version/shared-schema-repository/semantic-version.yang");
76 semVer.register(sharedSchemaRepository);
79 final SchemaContextFactory fact = sharedSchemaRepository
80 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
82 final CheckedFuture<SchemaContext, SchemaResolutionException> inetAndTopologySchemaContextFuture = fact
83 .createSchemaContext(Lists.newArrayList(bar.getId(), foo.getId(), semVer.getId()));
84 assertTrue(inetAndTopologySchemaContextFuture.isDone());
85 assertSchemaContext(inetAndTopologySchemaContextFuture.checkedGet(), 3);
87 final CheckedFuture<SchemaContext, SchemaResolutionException> barSchemaContextFuture = fact
88 .createSchemaContext(Lists.newArrayList(bar.getId(), semVer.getId()));
89 assertTrue(barSchemaContextFuture.isDone());
90 assertSchemaContext(barSchemaContextFuture.checkedGet(), 2);
93 private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
94 assertNotNull(schemaContext);
95 assertEquals(moduleSize, schemaContext.getModules().size());
98 static SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(final String resourceName)
100 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
101 return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
102 ASTSchemaSource.class);