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.jupiter.api.Assertions.assertNotNull;
11 import static org.junit.jupiter.api.Assertions.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 org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.extension.ExtendWith;
20 import org.mockito.Mock;
21 import org.mockito.junit.jupiter.MockitoExtension;
22 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
24 import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
25 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
28 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
29 import org.opendaylight.yangtools.yang.model.spi.source.YinXmlSource;
31 @ExtendWith(MockitoExtension.class)
32 class SchemaSourceTransformerTest {
33 public static final Class<YangSourceRepresentation> SRC_CLASS = YangSourceRepresentation.class;
34 public static final Class<YinXmlSource> DST_CLASS = YinXmlSource.class;
37 public SchemaRepository provider;
40 public SchemaSourceRegistry consumer;
43 public AsyncFunction<YangSourceRepresentation, YinXmlSource> function;
45 public SchemaSourceTransformer<YangSourceRepresentation, YinXmlSource> schema;
48 void schemaSourceTransformerTest() {
49 schema = new SchemaSourceTransformer<>(
50 provider, SchemaSourceTransformerTest.SRC_CLASS, consumer,
51 SchemaSourceTransformerTest.DST_CLASS, function);
52 assertNotNull(schema);
56 void schemaSourceTransformerGetSourceTest() {
57 final var p = new Provider();
58 final var reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
59 PotentialSchemaSource.Costs.IMMEDIATE);
60 final var sourceIdentifier = new SourceIdentifier("source");
61 reg.register(sourceIdentifier);
62 schema = new SchemaSourceTransformer<>(p,
63 SchemaSourceTransformerTest.SRC_CLASS, consumer, SchemaSourceTransformerTest.DST_CLASS,
65 final var prov = schema;
66 final var source = prov.getSource(sourceIdentifier);
67 assertNotNull(source);
69 assertTrue(source.isDone());
73 void schemaSourceRegAndUnregSchemaSourceTest() {
74 final var sourceIdentifier = new SourceIdentifier("source");
75 final var foo = new Foo<>(sourceIdentifier,
76 SchemaSourceTransformerTest.SRC_CLASS,
77 PotentialSchemaSource.Costs.COMPUTATION);
78 final var p = new Provider();
80 final var reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
81 PotentialSchemaSource.Costs.IMMEDIATE);
82 reg.register(sourceIdentifier);
84 final var c = new Consumer();
85 schema = new SchemaSourceTransformer<>(p, SchemaSourceTransformerTest.SRC_CLASS, c,
86 SchemaSourceTransformerTest.DST_CLASS, function);
88 final var listener = schema;
89 p.registerSchemaSourceListener(listener);
91 final var potList = new PotentialSchemaSource<?>[]{ foo.getPotentialSchemSource() };
92 final var sources = Arrays.asList(potList);
93 listener.schemaSourceRegistered(sources);
94 final var source = schema.getSource(sourceIdentifier);
95 assertNotNull(source);
97 listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
98 final var source2 = schema.getSource(sourceIdentifier);
99 assertNotNull(source2);
102 private static class Foo<T extends SourceRepresentation> {
103 final PotentialSchemaSource<T> src;
105 Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
106 src = PotentialSchemaSource.create(sourceIdentifier, representation, cost.getValue());
109 public PotentialSchemaSource<T> getPotentialSchemSource() {
114 private static class Registrator extends AbstractSchemaSourceCache<YangSourceRepresentation> {
115 Registrator(final SchemaSourceRegistry consumer, final Class<YangSourceRepresentation> srcClass,
117 super(consumer, srcClass, cost);
121 protected void offer(final YangSourceRepresentation source) {
126 public ListenableFuture<? extends YangSourceRepresentation> getSource(final SourceIdentifier sourceId) {
127 return SettableFuture.create();
131 private static final class Provider extends AbstractSchemaRepository {
133 public EffectiveModelContextFactory createEffectiveModelContextFactory(
134 final SchemaContextFactoryConfiguration config) {
135 return mock(EffectiveModelContextFactory.class);
139 private static final class Consumer extends AbstractSchemaRepository {
141 public EffectiveModelContextFactory createEffectiveModelContextFactory(
142 final SchemaContextFactoryConfiguration config) {
143 return mock(EffectiveModelContextFactory.class);