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.spi;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.mock;
14 import com.google.common.util.concurrent.AsyncFunction;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.Arrays;
18 import java.util.concurrent.Future;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
24 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
30 import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
31 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
33 @RunWith(MockitoJUnitRunner.class)
34 public class SchemaSourceTransformerTest {
35 public static final Class<YangSchemaSourceRepresentation> SRC_CLASS = YangSchemaSourceRepresentation.class;
36 public static final Class<YinXmlSchemaSource> DST_CLASS = YinXmlSchemaSource.class;
39 public SchemaRepository provider;
42 public SchemaSourceRegistry consumer;
45 public AsyncFunction<YangSchemaSourceRepresentation, YinXmlSchemaSource> function;
47 public SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource> schema;
50 public void schemaSourceTransformerTest() {
51 this.schema = new SchemaSourceTransformer<>(
52 this.provider, SchemaSourceTransformerTest.SRC_CLASS, this.consumer,
53 SchemaSourceTransformerTest.DST_CLASS, this.function);
54 assertNotNull(this.schema);
58 public void schemaSourceTransformerGetSourceTest() {
59 final Provider p = new Provider();
60 final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
61 PotentialSchemaSource.Costs.IMMEDIATE);
62 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source");
63 reg.register(sourceIdentifier);
64 this.schema = new SchemaSourceTransformer<>(p,
65 SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS,
67 final SchemaSourceProvider<YinXmlSchemaSource> prov = this.schema;
68 final Future<? extends YinXmlSchemaSource> source = prov.getSource(sourceIdentifier);
69 assertNotNull(source);
71 assertTrue(source.isDone());
75 public void schemaSourceRegAndUnregSchemaSourceTest() {
76 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("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 ListenableFuture<YinXmlSchemaSource> source = this.schema.getSource(sourceIdentifier);
97 assertNotNull(source);
99 listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
100 final ListenableFuture<YinXmlSchemaSource> source2 = this.schema.getSource(sourceIdentifier);
101 assertNotNull(source2);
104 private class Foo<T extends SchemaSourceRepresentation> {
106 final PotentialSchemaSource<T> src;
108 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 Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
123 super(consumer, srcClass, cost);
127 protected void offer(final YangSchemaSourceRepresentation source) {
132 public ListenableFuture<? extends YangSchemaSourceRepresentation> getSource(
133 final SourceIdentifier sourceIdentifier) {
134 return SettableFuture.create();
139 private class Provider extends AbstractSchemaRepository {
141 public EffectiveModelContextFactory createEffectiveModelContextFactory(
142 final SchemaContextFactoryConfiguration config) {
143 return mock(EffectiveModelContextFactory.class);
147 private class Consumer extends AbstractSchemaRepository {
149 public EffectiveModelContextFactory createEffectiveModelContextFactory(
150 final SchemaContextFactoryConfiguration config) {
151 return mock(EffectiveModelContextFactory.class);