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<>(
53 this.provider, SchemaSourceTransformerTest.SRC_CLASS, this.consumer,
54 SchemaSourceTransformerTest.DST_CLASS, this.function);
55 Assert.assertNotNull(this.schema);
59 public void schemaSourceTransformerGetSourceTest() throws Exception {
60 final Provider p = new Provider();
61 final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, PotentialSchemaSource.Costs.IMMEDIATE);
62 final SourceIdentifier sourceIdentifier = new SourceIdentifier("source");
63 reg.register(sourceIdentifier);
64 this.schema = new SchemaSourceTransformer<>(p,
65 SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS,
67 final SchemaSourceProvider<YinXmlSchemaSource> provider = this.schema;
68 final Future<? extends YinXmlSchemaSource> source = provider.getSource(sourceIdentifier);
69 Assert.assertNotNull(source);
71 Assert.assertTrue(source.isDone());
75 public void schemaSourceRegAndUnregSchemaSourceTest() throws Exception {
76 final SourceIdentifier sourceIdentifier = new SourceIdentifier("source");
77 final Foo<YangSchemaSourceRepresentation> foo = new Foo<>(sourceIdentifier,
78 SchemaSourceTransformerTest.SRC_CLASS,
79 PotentialSchemaSource.Costs.COMPUTATION);
80 final Provider p = new Provider();
82 final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
83 PotentialSchemaSource.Costs.IMMEDIATE);
84 reg.register(sourceIdentifier);
86 final Consumer c = new Consumer();
87 this.schema = new SchemaSourceTransformer<>(p,
88 SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, this.function);
90 final SchemaSourceListener listener = this.schema;
91 p.registerSchemaSourceListener(listener);
93 final PotentialSchemaSource<?>[] potList = { foo.getPotentialSchemSource() };
94 final Iterable<PotentialSchemaSource<?>> sources = Arrays.asList(potList);
95 listener.schemaSourceRegistered(sources);
96 final CheckedFuture<YinXmlSchemaSource, SchemaSourceException> source = this.schema.getSource(sourceIdentifier);
97 Assert.assertNotNull(source);
99 listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
100 final CheckedFuture<YinXmlSchemaSource, SchemaSourceException> source2 = this.schema
101 .getSource(sourceIdentifier);
102 Assert.assertNotNull(source2);
105 private class Foo<T extends SchemaSourceRepresentation> {
107 final PotentialSchemaSource<T> src;
109 public Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
110 this.src = PotentialSchemaSource.create(sourceIdentifier, representation,
114 public PotentialSchemaSource<T> getPotentialSchemSource() {
120 private class Registrator extends AbstractSchemaSourceCache<YangSchemaSourceRepresentation> {
122 protected Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
124 super(consumer, srcClass, cost);
128 protected void offer(final YangSchemaSourceRepresentation source) {
133 public CheckedFuture<? extends YangSchemaSourceRepresentation, SchemaSourceException> getSource(
134 final SourceIdentifier sourceIdentifier) {
135 return Mockito.mock(CheckedFuture.class);
140 private class Provider extends AbstractSchemaRepository {
143 public SchemaContextFactory createSchemaContextFactory(final SchemaSourceFilter filter) {
149 private class Consumer extends AbstractSchemaRepository {
152 public SchemaContextFactory createSchemaContextFactory(final SchemaSourceFilter filter) {