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.common.Revision;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
25 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
26 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
27 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
29 public class SharedSchemaContextFactoryTest {
31 private final SharedSchemaRepository repository = new SharedSchemaRepository("test");
34 private SchemaSourceFilter filter;
35 private SourceIdentifier s1;
36 private SourceIdentifier s2;
39 public void setUp() throws Exception {
40 MockitoAnnotations.initMocks(this);
42 final YangTextSchemaSource source1 = YangTextSchemaSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang");
43 final YangTextSchemaSource source2 = YangTextSchemaSource.forResource("/ietf/iana-timezones@2012-07-09.yang");
44 s1 = RevisionSourceIdentifier.create("ietf-inet-types", Revision.valueOf("2010-09-24"));
45 s2 = RevisionSourceIdentifier.create("iana-timezones", Revision.valueOf("2012-07-09"));
47 final TextToASTTransformer transformer = TextToASTTransformer.create(repository, repository);
48 repository.registerSchemaSourceListener(transformer);
50 repository.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(source1),
51 PotentialSchemaSource.create(s1, YangTextSchemaSource.class, 1));
53 repository.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(source2),
54 PotentialSchemaSource.create(s2, YangTextSchemaSource.class, 1));
58 public void testCreateSchemaContextWithDuplicateRequiredSources() throws Exception {
59 final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository,
61 final ListenableFuture<SchemaContext> schemaContext =
62 sharedSchemaContextFactory.createSchemaContext(Arrays.asList(s1, s1, s2));
63 assertNotNull(schemaContext.get());
67 public void testSourceRegisteredWithDifferentSI() throws Exception {
68 final YangTextSchemaSource source1 = YangTextSchemaSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang");
69 final YangTextSchemaSource source2 = YangTextSchemaSource.forResource("/ietf/iana-timezones@2012-07-09.yang");
70 s1 = source1.getIdentifier();
71 s2 = source2.getIdentifier();
73 final SettableSchemaProvider<ASTSchemaSource> provider =
74 SharedSchemaRepositoryTest.getImmediateYangSourceProviderFromResource(
75 "/no-revision/imported@2012-12-12.yang");
77 provider.register(repository);
79 // Register the same provider under source id without revision
80 final SourceIdentifier sIdWithoutRevision = RevisionSourceIdentifier.create(provider.getId().getName());
81 repository.registerSchemaSource(provider, PotentialSchemaSource.create(
82 sIdWithoutRevision, ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
84 final SharedSchemaContextFactory sharedSchemaContextFactory = new SharedSchemaContextFactory(repository,
86 final ListenableFuture<SchemaContext> schemaContext =
87 sharedSchemaContextFactory.createSchemaContext(Arrays.asList(sIdWithoutRevision, provider.getId()));
88 assertNotNull(schemaContext.get());