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.model.repo.util;
10 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
12 import com.google.common.util.concurrent.AsyncFunction;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.Arrays;
15 import java.util.concurrent.Future;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
27 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
29 import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
30 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
31 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
32 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
33 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
34 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
36 @RunWith(MockitoJUnitRunner.class)
37 public class SchemaSourceTransformerTest {
39 private SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource> schema;
40 private static final Class<YangSchemaSourceRepresentation> SRC_CLASS = YangSchemaSourceRepresentation.class;
41 private static final Class<YinXmlSchemaSource> DST_CLASS = YinXmlSchemaSource.class;
44 private SchemaRepository provider;
47 private SchemaSourceRegistry consumer;
50 private AsyncFunction<YangSchemaSourceRepresentation, YinXmlSchemaSource> function;
53 public void schemaSourceTransformerTest() {
54 this.schema = new SchemaSourceTransformer<>(
55 this.provider, SchemaSourceTransformerTest.SRC_CLASS, this.consumer,
56 SchemaSourceTransformerTest.DST_CLASS, this.function);
57 Assert.assertNotNull(this.schema);
61 public void schemaSourceTransformerGetSourceTest() throws Exception {
62 final Provider p = new Provider();
63 final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, PotentialSchemaSource.Costs.IMMEDIATE);
64 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source");
65 reg.register(sourceIdentifier);
66 this.schema = new SchemaSourceTransformer<>(p,
67 SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS,
69 final SchemaSourceProvider<YinXmlSchemaSource> provider = this.schema;
70 final Future<? extends YinXmlSchemaSource> source = provider.getSource(sourceIdentifier);
71 Assert.assertNotNull(source);
73 Assert.assertTrue(source.isDone());
77 public void schemaSourceRegAndUnregSchemaSourceTest() throws Exception {
78 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source");
79 final Foo<YangSchemaSourceRepresentation> foo = new Foo<>(sourceIdentifier,
80 SchemaSourceTransformerTest.SRC_CLASS,
81 PotentialSchemaSource.Costs.COMPUTATION);
82 final Provider p = new Provider();
84 final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
85 PotentialSchemaSource.Costs.IMMEDIATE);
86 reg.register(sourceIdentifier);
88 final Consumer c = new Consumer();
89 this.schema = new SchemaSourceTransformer<>(p,
90 SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, this.function);
92 final SchemaSourceListener listener = this.schema;
93 p.registerSchemaSourceListener(listener);
95 final PotentialSchemaSource<?>[] potList = { foo.getPotentialSchemSource() };
96 final Iterable<PotentialSchemaSource<?>> sources = Arrays.asList(potList);
97 listener.schemaSourceRegistered(sources);
98 final CheckedFuture<YinXmlSchemaSource, SchemaSourceException> source = this.schema.getSource(sourceIdentifier);
99 Assert.assertNotNull(source);
101 listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
102 final CheckedFuture<YinXmlSchemaSource, SchemaSourceException> source2 = this.schema
103 .getSource(sourceIdentifier);
104 Assert.assertNotNull(source2);
107 private class Foo<T extends SchemaSourceRepresentation> {
109 final PotentialSchemaSource<T> src;
111 public Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
112 this.src = PotentialSchemaSource.create(sourceIdentifier, representation,
116 public PotentialSchemaSource<T> getPotentialSchemSource() {
122 private class Registrator extends AbstractSchemaSourceCache<YangSchemaSourceRepresentation> {
124 protected Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
126 super(consumer, srcClass, cost);
130 protected void offer(final YangSchemaSourceRepresentation source) {
135 public CheckedFuture<? extends YangSchemaSourceRepresentation, SchemaSourceException> getSource(
136 final SourceIdentifier sourceIdentifier) {
137 return Mockito.mock(CheckedFuture.class);
142 private class Provider extends AbstractSchemaRepository {
145 public SchemaContextFactory createSchemaContextFactory(final SchemaSourceFilter filter) {
151 private class Consumer extends AbstractSchemaRepository {
154 public SchemaContextFactory createSchemaContextFactory(final SchemaSourceFilter filter) {