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 com.google.common.util.concurrent.AsyncFunction;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import java.util.Arrays;
13 import java.util.concurrent.Future;
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
27 import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
28 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
29 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
30 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
31 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
32 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
34 @RunWith(MockitoJUnitRunner.class)
35 public class SchemaSourceTransformerTest {
37 private SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource> schema;
38 private static final Class<YangSchemaSourceRepresentation> SRC_CLASS = YangSchemaSourceRepresentation.class;
39 private static final Class<YinXmlSchemaSource> DST_CLASS = YinXmlSchemaSource.class;
42 private SchemaRepository provider;
45 private SchemaSourceRegistry consumer;
48 private AsyncFunction<YangSchemaSourceRepresentation, YinXmlSchemaSource> function;
51 public void schemaSourceTransformerTest() {
52 this.schema = new SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource>(
53 this.provider, SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS, this.function);
54 Assert.assertNotNull(this.schema);
58 public void schemaSourceTransformerGetSourceTest() throws Exception {
59 final Provider p = new Provider();
60 final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, PotentialSchemaSource.Costs.IMMEDIATE);
61 final SourceIdentifier sourceIdentifier = new SourceIdentifier("source");
62 reg.register(sourceIdentifier);
63 this.schema = new SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource>(p,
64 SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS,
66 final SchemaSourceProvider<YinXmlSchemaSource> provider = this.schema;
67 final Future<? extends YinXmlSchemaSource> source = provider.getSource(sourceIdentifier);
68 Assert.assertNotNull(source);
70 Assert.assertTrue(source.isDone());
74 public void schemaSourceRegAndUnregSchemaSourceTest() throws Exception {
75 final SourceIdentifier sourceIdentifier = new SourceIdentifier("source");
76 final Foo<YangSchemaSourceRepresentation> foo = new Foo<>(sourceIdentifier,
77 SchemaSourceTransformerTest.SRC_CLASS,
78 PotentialSchemaSource.Costs.COMPUTATION);
79 final Provider p = new Provider();
81 final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
82 PotentialSchemaSource.Costs.IMMEDIATE);
83 reg.register(sourceIdentifier);
85 final Consumer c = new Consumer();
86 this.schema = new SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource>(p,
87 SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, this.function);
89 final SchemaSourceListener listener = this.schema;
90 p.registerSchemaSourceListener(listener);
92 final PotentialSchemaSource<?>[] potList = { foo.getPotentialSchemSource() };
93 final Iterable<PotentialSchemaSource<?>> sources = Arrays.asList(potList);
94 listener.schemaSourceRegistered(sources);
95 final CheckedFuture<YinXmlSchemaSource, SchemaSourceException> source = this.schema.getSource(sourceIdentifier);
96 Assert.assertNotNull(source);
98 listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
99 final CheckedFuture<YinXmlSchemaSource, SchemaSourceException> source2 = this.schema
100 .getSource(sourceIdentifier);
101 Assert.assertNotNull(source2);
104 private class Foo<T extends SchemaSourceRepresentation> {
106 final PotentialSchemaSource<T> src;
108 public Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
109 this.src = PotentialSchemaSource.create(sourceIdentifier, representation,
113 public PotentialSchemaSource<T> getPotentialSchemSource() {
119 private class Registrator extends AbstractSchemaSourceCache<YangSchemaSourceRepresentation> {
121 protected Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
123 super(consumer, srcClass, cost);
127 protected void offer(final YangSchemaSourceRepresentation source) {
132 public CheckedFuture<? extends YangSchemaSourceRepresentation, SchemaSourceException> getSource(
133 final SourceIdentifier sourceIdentifier) {
134 return Mockito.mock(CheckedFuture.class);
139 private class Provider extends AbstractSchemaRepository {
142 public SchemaContextFactory createSchemaContextFactory(final SchemaSourceFilter filter) {
148 private class Consumer extends AbstractSchemaRepository {
151 public SchemaContextFactory createSchemaContextFactory(final SchemaSourceFilter filter) {