f852cd17cc3f7b9ddab16531228a2514e11e36c6
[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.RevisionSourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
30 import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
31 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
32
33 @RunWith(MockitoJUnitRunner.class)
34 public class SchemaSourceTransformerTest {
35     public static final Class<YangSchemaSourceRepresentation> SRC_CLASS = YangSchemaSourceRepresentation.class;
36     public static final Class<YinXmlSchemaSource> DST_CLASS = YinXmlSchemaSource.class;
37
38     @Mock
39     public SchemaRepository provider;
40
41     @Mock
42     public SchemaSourceRegistry consumer;
43
44     @Mock
45     public AsyncFunction<YangSchemaSourceRepresentation, YinXmlSchemaSource> function;
46
47     public SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource> schema;
48
49     @Test
50     public void schemaSourceTransformerTest() {
51         this.schema = new SchemaSourceTransformer<>(
52                 this.provider, SchemaSourceTransformerTest.SRC_CLASS, this.consumer,
53                 SchemaSourceTransformerTest.DST_CLASS, this.function);
54         assertNotNull(this.schema);
55     }
56
57     @Test
58     public void schemaSourceTransformerGetSourceTest() {
59         final Provider p = new Provider();
60         final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
61                 PotentialSchemaSource.Costs.IMMEDIATE);
62         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source");
63         reg.register(sourceIdentifier);
64         this.schema = new SchemaSourceTransformer<>(p,
65                 SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS,
66                 this.function);
67         final SchemaSourceProvider<YinXmlSchemaSource> prov = this.schema;
68         final Future<? extends YinXmlSchemaSource> source = prov.getSource(sourceIdentifier);
69         assertNotNull(source);
70         source.cancel(true);
71         assertTrue(source.isDone());
72     }
73
74     @Test
75     public void schemaSourceRegAndUnregSchemaSourceTest() {
76         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source");
77         final Foo<YangSchemaSourceRepresentation> foo = new Foo<>(sourceIdentifier,
78                 SchemaSourceTransformerTest.SRC_CLASS,
79                 PotentialSchemaSource.Costs.COMPUTATION);
80         final Provider p = new Provider();
81
82         final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
83                 PotentialSchemaSource.Costs.IMMEDIATE);
84         reg.register(sourceIdentifier);
85
86         final Consumer c = new Consumer();
87         this.schema = new SchemaSourceTransformer<>(p,
88                 SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, this.function);
89
90         final SchemaSourceListener listener = this.schema;
91         p.registerSchemaSourceListener(listener);
92
93         final PotentialSchemaSource<?>[] potList = { foo.getPotentialSchemSource() };
94         final Iterable<PotentialSchemaSource<?>> sources = Arrays.asList(potList);
95         listener.schemaSourceRegistered(sources);
96         final ListenableFuture<YinXmlSchemaSource> source = this.schema.getSource(sourceIdentifier);
97         assertNotNull(source);
98
99         listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
100         final ListenableFuture<YinXmlSchemaSource> source2 = this.schema.getSource(sourceIdentifier);
101         assertNotNull(source2);
102     }
103
104     private class Foo<T extends SchemaSourceRepresentation> {
105
106         final PotentialSchemaSource<T> src;
107
108         Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
109             this.src = PotentialSchemaSource.create(sourceIdentifier, representation,
110                     cost.getValue());
111         }
112
113         public PotentialSchemaSource<T> getPotentialSchemSource() {
114             return this.src;
115         }
116
117     }
118
119     private class Registrator extends AbstractSchemaSourceCache<YangSchemaSourceRepresentation> {
120
121         Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
122                 final Costs cost) {
123             super(consumer, srcClass, cost);
124         }
125
126         @Override
127         protected void offer(final YangSchemaSourceRepresentation source) {
128
129         }
130
131         @Override
132         public ListenableFuture<? extends YangSchemaSourceRepresentation> getSource(
133                 final SourceIdentifier sourceIdentifier) {
134             return SettableFuture.create();
135         }
136
137     }
138
139     private class Provider extends AbstractSchemaRepository {
140         @Override
141         public EffectiveModelContextFactory createEffectiveModelContextFactory(
142                 final SchemaContextFactoryConfiguration config) {
143             return mock(EffectiveModelContextFactory.class);
144         }
145     }
146
147     private class Consumer extends AbstractSchemaRepository {
148         @Override
149         public EffectiveModelContextFactory createEffectiveModelContextFactory(
150                 final SchemaContextFactoryConfiguration config) {
151             return mock(EffectiveModelContextFactory.class);
152         }
153     }
154 }