Migrate yang-repo-spi to JUnit5
[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.repo.api.EffectiveModelContextFactory;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
26 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
27 import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
28 import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
29 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
30
31 @ExtendWith(MockitoExtension.class)
32 class SchemaSourceTransformerTest {
33     public static final Class<YangSchemaSourceRepresentation> SRC_CLASS = YangSchemaSourceRepresentation.class;
34     public static final Class<YinXmlSchemaSource> DST_CLASS = YinXmlSchemaSource.class;
35
36     @Mock
37     public SchemaRepository provider;
38
39     @Mock
40     public SchemaSourceRegistry consumer;
41
42     @Mock
43     public AsyncFunction<YangSchemaSourceRepresentation, YinXmlSchemaSource> function;
44
45     public SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource> 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 SchemaSourceRepresentation> {
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<YangSchemaSourceRepresentation> {
115         Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
116                 final Costs cost) {
117             super(consumer, srcClass, cost);
118         }
119
120         @Override
121         protected void offer(final YangSchemaSourceRepresentation source) {
122
123         }
124
125         @Override
126         public ListenableFuture<? extends YangSchemaSourceRepresentation> getSource(
127                 final SourceIdentifier sourceIdentifier) {
128             return SettableFuture.create();
129         }
130     }
131
132     private static final class Provider extends AbstractSchemaRepository {
133         @Override
134         public EffectiveModelContextFactory createEffectiveModelContextFactory(
135                 final SchemaContextFactoryConfiguration config) {
136             return mock(EffectiveModelContextFactory.class);
137         }
138     }
139
140     private static final class Consumer extends AbstractSchemaRepository {
141         @Override
142         public EffectiveModelContextFactory createEffectiveModelContextFactory(
143                 final SchemaContextFactoryConfiguration config) {
144             return mock(EffectiveModelContextFactory.class);
145         }
146     }
147 }