Remove RevisionSourceIdentifier
[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.Assert.assertNotNull;
11 import static org.junit.Assert.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 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;
31
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;
36
37     @Mock
38     public SchemaRepository provider;
39
40     @Mock
41     public SchemaSourceRegistry consumer;
42
43     @Mock
44     public AsyncFunction<YangSchemaSourceRepresentation, YinXmlSchemaSource> function;
45
46     public SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource> schema;
47
48     @Test
49     public void schemaSourceTransformerTest() {
50         schema = new SchemaSourceTransformer<>(
51                 provider, SchemaSourceTransformerTest.SRC_CLASS, consumer,
52                 SchemaSourceTransformerTest.DST_CLASS, function);
53         assertNotNull(schema);
54     }
55
56     @Test
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,
65                 function);
66         final SchemaSourceProvider<YinXmlSchemaSource> prov = schema;
67         final Future<? extends YinXmlSchemaSource> source = prov.getSource(sourceIdentifier);
68         assertNotNull(source);
69         source.cancel(true);
70         assertTrue(source.isDone());
71     }
72
73     @Test
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();
80
81         final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
82                 PotentialSchemaSource.Costs.IMMEDIATE);
83         reg.register(sourceIdentifier);
84
85         final Consumer c = new Consumer();
86         schema = new SchemaSourceTransformer<>(p,
87                 SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, function);
88
89         final SchemaSourceListener listener = schema;
90         p.registerSchemaSourceListener(listener);
91
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);
97
98         listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
99         final ListenableFuture<YinXmlSchemaSource> source2 = schema.getSource(sourceIdentifier);
100         assertNotNull(source2);
101     }
102
103     private static class Foo<T extends SchemaSourceRepresentation> {
104
105         final PotentialSchemaSource<T> src;
106
107         Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
108             this.src = PotentialSchemaSource.create(sourceIdentifier, representation,
109                     cost.getValue());
110         }
111
112         public PotentialSchemaSource<T> getPotentialSchemSource() {
113             return this.src;
114         }
115
116     }
117
118     private static class Registrator extends AbstractSchemaSourceCache<YangSchemaSourceRepresentation> {
119
120         Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
121                 final Costs cost) {
122             super(consumer, srcClass, cost);
123         }
124
125         @Override
126         protected void offer(final YangSchemaSourceRepresentation source) {
127
128         }
129
130         @Override
131         public ListenableFuture<? extends YangSchemaSourceRepresentation> getSource(
132                 final SourceIdentifier sourceIdentifier) {
133             return SettableFuture.create();
134         }
135
136     }
137
138     private static class Provider extends AbstractSchemaRepository {
139         @Override
140         public EffectiveModelContextFactory createEffectiveModelContextFactory(
141                 final SchemaContextFactoryConfiguration config) {
142             return mock(EffectiveModelContextFactory.class);
143         }
144     }
145
146     private static class Consumer extends AbstractSchemaRepository {
147         @Override
148         public EffectiveModelContextFactory createEffectiveModelContextFactory(
149                 final SchemaContextFactoryConfiguration config) {
150             return mock(EffectiveModelContextFactory.class);
151         }
152     }
153 }