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.SchemaContextFactoryConfiguration;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
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.Costs;
32 @RunWith(MockitoJUnitRunner.class)
33 public class SchemaSourceTransformerTest {
34 public static final Class<YangSchemaSourceRepresentation> SRC_CLASS = YangSchemaSourceRepresentation.class;
35 public static final Class<YinXmlSchemaSource> DST_CLASS = YinXmlSchemaSource.class;
38 public SchemaRepository provider;
41 public SchemaSourceRegistry consumer;
44 public AsyncFunction<YangSchemaSourceRepresentation, YinXmlSchemaSource> function;
46 public SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource> schema;
49 public void schemaSourceTransformerTest() {
50 schema = new SchemaSourceTransformer<>(
51 provider, SchemaSourceTransformerTest.SRC_CLASS, consumer,
52 SchemaSourceTransformerTest.DST_CLASS, function);
53 assertNotNull(schema);
57 public void schemaSourceTransformerGetSourceTest() {
58 final Provider p = new Provider();
59 final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
60 PotentialSchemaSource.Costs.IMMEDIATE);
61 final SourceIdentifier sourceIdentifier = new SourceIdentifier("source");
62 reg.register(sourceIdentifier);
63 schema = new SchemaSourceTransformer<>(p,
64 SchemaSourceTransformerTest.SRC_CLASS, consumer, SchemaSourceTransformerTest.DST_CLASS,
66 final SchemaSourceProvider<YinXmlSchemaSource> prov = schema;
67 final Future<? extends YinXmlSchemaSource> source = prov.getSource(sourceIdentifier);
68 assertNotNull(source);
70 assertTrue(source.isDone());
74 public void schemaSourceRegAndUnregSchemaSourceTest() {
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 schema = new SchemaSourceTransformer<>(p,
87 SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, function);
89 final SchemaSourceListener listener = 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 ListenableFuture<YinXmlSchemaSource> source = schema.getSource(sourceIdentifier);
96 assertNotNull(source);
98 listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
99 final ListenableFuture<YinXmlSchemaSource> source2 = schema.getSource(sourceIdentifier);
100 assertNotNull(source2);
103 private static class Foo<T extends SchemaSourceRepresentation> {
105 final PotentialSchemaSource<T> src;
107 Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
108 this.src = PotentialSchemaSource.create(sourceIdentifier, representation,
112 public PotentialSchemaSource<T> getPotentialSchemSource() {
118 private static class Registrator extends AbstractSchemaSourceCache<YangSchemaSourceRepresentation> {
120 Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
122 super(consumer, srcClass, cost);
126 protected void offer(final YangSchemaSourceRepresentation source) {
131 public ListenableFuture<? extends YangSchemaSourceRepresentation> getSource(
132 final SourceIdentifier sourceIdentifier) {
133 return SettableFuture.create();
138 private static class Provider extends AbstractSchemaRepository {
140 public EffectiveModelContextFactory createEffectiveModelContextFactory(
141 final SchemaContextFactoryConfiguration config) {
142 return mock(EffectiveModelContextFactory.class);
146 private static class Consumer extends AbstractSchemaRepository {
148 public EffectiveModelContextFactory createEffectiveModelContextFactory(
149 final SchemaContextFactoryConfiguration config) {
150 return mock(EffectiveModelContextFactory.class);