Propagate @Nonnull and @Nullable 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 javax.annotation.Nonnull;
11 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
12
13 import com.google.common.util.concurrent.AsyncFunction;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import java.util.Arrays;
16 import java.util.concurrent.Future;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
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;
32 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
33 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
34 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
35 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
36
37 @RunWith(MockitoJUnitRunner.class)
38 public class SchemaSourceTransformerTest {
39
40     private SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource> schema;
41     private static final Class<YangSchemaSourceRepresentation> SRC_CLASS = YangSchemaSourceRepresentation.class;
42     private static final Class<YinXmlSchemaSource> DST_CLASS = YinXmlSchemaSource.class;
43
44     @Mock
45     private SchemaRepository provider;
46
47     @Mock
48     private SchemaSourceRegistry consumer;
49
50     @Mock
51     private AsyncFunction<YangSchemaSourceRepresentation, YinXmlSchemaSource> function;
52
53     @Test
54     public void schemaSourceTransformerTest() {
55         this.schema = new SchemaSourceTransformer<>(
56                 this.provider, SchemaSourceTransformerTest.SRC_CLASS, this.consumer,
57                 SchemaSourceTransformerTest.DST_CLASS, this.function);
58         Assert.assertNotNull(this.schema);
59     }
60
61     @Test
62     public void schemaSourceTransformerGetSourceTest() throws Exception {
63         final Provider p = new Provider();
64         final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, 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> provider = this.schema;
71         final Future<? extends YinXmlSchemaSource> source = provider.getSource(sourceIdentifier);
72         Assert.assertNotNull(source);
73         source.cancel(true);
74         Assert.assertTrue(source.isDone());
75     }
76
77     @Test
78     public void schemaSourceRegAndUnregSchemaSourceTest() throws Exception {
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 CheckedFuture<YinXmlSchemaSource, SchemaSourceException> source = this.schema.getSource(sourceIdentifier);
100         Assert.assertNotNull(source);
101
102         listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
103         final CheckedFuture<YinXmlSchemaSource, SchemaSourceException> source2 = this.schema
104                 .getSource(sourceIdentifier);
105         Assert.assertNotNull(source2);
106     }
107
108     private class Foo<T extends SchemaSourceRepresentation> {
109
110         final PotentialSchemaSource<T> src;
111
112         public Foo(final SourceIdentifier sourceIdentifier, final Class<T> representation, final Costs cost) {
113             this.src = PotentialSchemaSource.create(sourceIdentifier, representation,
114                     cost.getValue());
115         }
116
117         public PotentialSchemaSource<T> getPotentialSchemSource() {
118             return this.src;
119         }
120
121     }
122
123     private class Registrator extends AbstractSchemaSourceCache<YangSchemaSourceRepresentation> {
124
125         protected Registrator(final SchemaSourceRegistry consumer, final Class<YangSchemaSourceRepresentation> srcClass,
126                 final Costs cost) {
127             super(consumer, srcClass, cost);
128         }
129
130         @Override
131         protected void offer(final YangSchemaSourceRepresentation source) {
132
133         }
134
135         @Override
136         public CheckedFuture<? extends YangSchemaSourceRepresentation, SchemaSourceException> getSource(
137                 final SourceIdentifier sourceIdentifier) {
138             return Mockito.mock(CheckedFuture.class);
139         }
140
141     }
142
143     private class Provider extends AbstractSchemaRepository {
144
145         @Override
146         public SchemaContextFactory createSchemaContextFactory(@Nonnull final SchemaSourceFilter filter) {
147             return null;
148         }
149
150     }
151
152     private class Consumer extends AbstractSchemaRepository {
153
154         @Override
155         public SchemaContextFactory createSchemaContextFactory(@Nonnull final SchemaSourceFilter filter) {
156             return null;
157         }
158
159     }
160 }