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 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.RevisionSourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
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;
32 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
33 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
34 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
35 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
37 @RunWith(MockitoJUnitRunner.class)
38 public class SchemaSourceTransformerTest {
40 private SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource> schema;
41 private static final Class<YangSchemaSourceRepresentation> SRC_CLASS = YangSchemaSourceRepresentation.class;
42 private static final Class<YinXmlSchemaSource> DST_CLASS = YinXmlSchemaSource.class;
45 private SchemaRepository provider;
48 private SchemaSourceRegistry consumer;
51 private AsyncFunction<YangSchemaSourceRepresentation, YinXmlSchemaSource> function;
54 public void schemaSourceTransformerTest() {
55 this.schema = new SchemaSourceTransformer<>(
56 this.provider, SchemaSourceTransformerTest.SRC_CLASS, this.consumer,
57 SchemaSourceTransformerTest.DST_CLASS, this.function);
58 assertNotNull(this.schema);
62 public void schemaSourceTransformerGetSourceTest() {
63 final Provider p = new Provider();
64 final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
65 PotentialSchemaSource.Costs.IMMEDIATE);
66 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source");
67 reg.register(sourceIdentifier);
68 this.schema = new SchemaSourceTransformer<>(p,
69 SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS,
71 final SchemaSourceProvider<YinXmlSchemaSource> prov = this.schema;
72 final Future<? extends YinXmlSchemaSource> source = prov.getSource(sourceIdentifier);
73 assertNotNull(source);
75 assertTrue(source.isDone());
79 public void schemaSourceRegAndUnregSchemaSourceTest() {
80 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source");
81 final Foo<YangSchemaSourceRepresentation> foo = new Foo<>(sourceIdentifier,
82 SchemaSourceTransformerTest.SRC_CLASS,
83 PotentialSchemaSource.Costs.COMPUTATION);
84 final Provider p = new Provider();
86 final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
87 PotentialSchemaSource.Costs.IMMEDIATE);
88 reg.register(sourceIdentifier);
90 final Consumer c = new Consumer();
91 this.schema = new SchemaSourceTransformer<>(p,
92 SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, this.function);
94 final SchemaSourceListener listener = this.schema;
95 p.registerSchemaSourceListener(listener);
97 final PotentialSchemaSource<?>[] potList = { foo.getPotentialSchemSource() };
98 final Iterable<PotentialSchemaSource<?>> sources = Arrays.asList(potList);
99 listener.schemaSourceRegistered(sources);
100 final ListenableFuture<YinXmlSchemaSource> source = this.schema.getSource(sourceIdentifier);
101 assertNotNull(source);
103 listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
104 final ListenableFuture<YinXmlSchemaSource> source2 = this.schema.getSource(sourceIdentifier);
105 assertNotNull(source2);
108 private class Foo<T extends SchemaSourceRepresentation> {
110 final PotentialSchemaSource<T> src;
112 Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
113 this.src = PotentialSchemaSource.create(sourceIdentifier, representation,
117 public PotentialSchemaSource<T> getPotentialSchemSource() {
123 private class Registrator extends AbstractSchemaSourceCache<YangSchemaSourceRepresentation> {
125 Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
127 super(consumer, srcClass, cost);
131 protected void offer(final YangSchemaSourceRepresentation source) {
136 public ListenableFuture<? extends YangSchemaSourceRepresentation> getSource(
137 final SourceIdentifier sourceIdentifier) {
138 return SettableFuture.create();
143 private class Provider extends AbstractSchemaRepository {
145 public SchemaContextFactory createSchemaContextFactory(final SchemaContextFactoryConfiguration config) {
146 return mock(SchemaContextFactory.class);
150 private class Consumer extends AbstractSchemaRepository {
152 public SchemaContextFactory createSchemaContextFactory(final SchemaContextFactoryConfiguration config) {
153 return mock(SchemaContextFactory.class);