Promote SchemaSourceRepresentation
[yangtools.git] / yang / yang-repo-spi / src / test / java / org / opendaylight / yangtools / yang / model / repo / spi / SchemaSourceTransformerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.repo.spi;
9
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;
13
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;
30
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;
35
36     @Mock
37     public SchemaRepository provider;
38
39     @Mock
40     public SchemaSourceRegistry consumer;
41
42     @Mock
43     public AsyncFunction<YangSourceRepresentation, YinXmlSource> function;
44
45     public SchemaSourceTransformer<YangSourceRepresentation, YinXmlSource> schema;
46
47     @Test
48     void schemaSourceTransformerTest() {
49         schema = new SchemaSourceTransformer<>(
50                 provider, SchemaSourceTransformerTest.SRC_CLASS, consumer,
51                 SchemaSourceTransformerTest.DST_CLASS, function);
52         assertNotNull(schema);
53     }
54
55     @Test
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,
64                 function);
65         final var prov = schema;
66         final var source = prov.getSource(sourceIdentifier);
67         assertNotNull(source);
68         source.cancel(true);
69         assertTrue(source.isDone());
70     }
71
72     @Test
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();
79
80         final var reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
81                 PotentialSchemaSource.Costs.IMMEDIATE);
82         reg.register(sourceIdentifier);
83
84         final var c = new Consumer();
85         schema = new SchemaSourceTransformer<>(p, SchemaSourceTransformerTest.SRC_CLASS, c,
86             SchemaSourceTransformerTest.DST_CLASS, function);
87
88         final var listener = schema;
89         p.registerSchemaSourceListener(listener);
90
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);
96
97         listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
98         final var source2 = schema.getSource(sourceIdentifier);
99         assertNotNull(source2);
100     }
101
102     private static class Foo<T extends SourceRepresentation> {
103         final PotentialSchemaSource<T> src;
104
105         Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
106             src = PotentialSchemaSource.create(sourceIdentifier, representation, cost.getValue());
107         }
108
109         public PotentialSchemaSource<T> getPotentialSchemSource() {
110             return src;
111         }
112     }
113
114     private static class Registrator extends AbstractSchemaSourceCache<YangSourceRepresentation> {
115         Registrator(final SchemaSourceRegistry consumer, final Class<YangSourceRepresentation> srcClass,
116                 final Costs cost) {
117             super(consumer, srcClass, cost);
118         }
119
120         @Override
121         protected void offer(final YangSourceRepresentation source) {
122
123         }
124
125         @Override
126         public ListenableFuture<? extends YangSourceRepresentation> getSource(final SourceIdentifier sourceId) {
127             return SettableFuture.create();
128         }
129     }
130
131     private static final class Provider extends AbstractSchemaRepository {
132         @Override
133         public EffectiveModelContextFactory createEffectiveModelContextFactory(
134                 final SchemaContextFactoryConfiguration config) {
135             return mock(EffectiveModelContextFactory.class);
136         }
137     }
138
139     private static final class Consumer extends AbstractSchemaRepository {
140         @Override
141         public EffectiveModelContextFactory createEffectiveModelContextFactory(
142                 final SchemaContextFactoryConfiguration config) {
143             return mock(EffectiveModelContextFactory.class);
144         }
145     }
146 }