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