8fe0350d0f96fcbcf2699e4954775ad4a8af27d3
[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, SchemaSourceTransformerTest.SRC_CLASS, c,
87             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         final PotentialSchemaSource<T> src;
105
106         Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
107             src = PotentialSchemaSource.create(sourceIdentifier, representation, cost.getValue());
108         }
109
110         public PotentialSchemaSource<T> getPotentialSchemSource() {
111             return src;
112         }
113     }
114
115     private static class Registrator extends AbstractSchemaSourceCache<YangSchemaSourceRepresentation> {
116         Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
117                 final Costs cost) {
118             super(consumer, srcClass, cost);
119         }
120
121         @Override
122         protected void offer(final YangSchemaSourceRepresentation source) {
123
124         }
125
126         @Override
127         public ListenableFuture<? extends YangSchemaSourceRepresentation> getSource(
128                 final SourceIdentifier sourceIdentifier) {
129             return SettableFuture.create();
130         }
131     }
132
133     private static final class Provider extends AbstractSchemaRepository {
134         @Override
135         public EffectiveModelContextFactory createEffectiveModelContextFactory(
136                 final SchemaContextFactoryConfiguration config) {
137             return mock(EffectiveModelContextFactory.class);
138         }
139     }
140
141     private static final class Consumer extends AbstractSchemaRepository {
142         @Override
143         public EffectiveModelContextFactory createEffectiveModelContextFactory(
144                 final SchemaContextFactoryConfiguration config) {
145             return mock(EffectiveModelContextFactory.class);
146         }
147     }
148 }