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