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
8 package org.opendaylight.yangtools.yang.parser.repo;
10 import static org.junit.Assert.assertNotNull;
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.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
22 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
24 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
25 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
26 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
28 public class SharedSchemaContextFactoryTest {
30 private final SharedSchemaRepository repository = new SharedSchemaRepository("test");
33 private SchemaSourceFilter filter;
34 private SourceIdentifier s1;
35 private SourceIdentifier s2;
38 public void setUp() throws Exception {
39 MockitoAnnotations.initMocks(this);
41 final YangTextSchemaSource source1 = YangTextSchemaSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang");
42 final YangTextSchemaSource source2 = YangTextSchemaSource.forResource("/ietf/iana-timezones@2012-07-09.yang");
43 s1 = RevisionSourceIdentifier.create("ietf-inet-types", "2010-09-24");
44 s2 = RevisionSourceIdentifier.create("iana-timezones", "2012-07-09");
46 final TextToASTTransformer transformer = TextToASTTransformer.create(repository, repository);
47 repository.registerSchemaSourceListener(transformer);
49 repository.registerSchemaSource(sourceIdentifier -> Futures.immediateCheckedFuture(source1),
50 PotentialSchemaSource.create(s1, YangTextSchemaSource.class, 1));
52 repository.registerSchemaSource(sourceIdentifier -> Futures.immediateCheckedFuture(source2),
53 PotentialSchemaSource.create(s2, YangTextSchemaSource.class, 1));
57 public void testCreateSchemaContextWithDuplicateRequiredSources() throws Exception {
58 final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository,
60 final ListenableFuture<SchemaContext> schemaContext =
61 sharedSchemaContextFactory.createSchemaContext(Arrays.asList(s1, s1, s2));
62 assertNotNull(schemaContext.get());
66 public void testSourceRegisteredWithDifferentSI() throws Exception {
67 final YangTextSchemaSource source1 = YangTextSchemaSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang");
68 final YangTextSchemaSource source2 = YangTextSchemaSource.forResource("/ietf/iana-timezones@2012-07-09.yang");
69 s1 = source1.getIdentifier();
70 s2 = source2.getIdentifier();
72 final SettableSchemaProvider<ASTSchemaSource> provider =
73 SharedSchemaRepositoryTest.getImmediateYangSourceProviderFromResource(
74 "/no-revision/imported@2012-12-12.yang");
76 provider.register(repository);
78 // Register the same provider under source id without revision
79 final SourceIdentifier sIdWithoutRevision = RevisionSourceIdentifier.create(provider.getId().getName());
80 repository.registerSchemaSource(provider, PotentialSchemaSource.create(
81 sIdWithoutRevision, ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
83 final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository,
85 final ListenableFuture<SchemaContext> schemaContext =
86 sharedSchemaContextFactory.createSchemaContext(Arrays.asList(sIdWithoutRevision, provider.getId()));
87 assertNotNull(schemaContext.get());