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