Bug 3899: Milestone: Increase test coverage for Yangtools
[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<YangSchemaSourceRepresentation, YinXmlSchemaSource>(
53                 this.provider, SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS, this.function);
54         Assert.assertNotNull(this.schema);
55     }
56
57     @Test
58     public void schemaSourceTransformerGetSourceTest() throws Exception {
59         final Provider p = new Provider();
60         final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, PotentialSchemaSource.Costs.IMMEDIATE);
61         final SourceIdentifier sourceIdentifier = new SourceIdentifier("source");
62         reg.register(sourceIdentifier);
63         this.schema = new SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource>(p,
64                 SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS,
65                 this.function);
66         final SchemaSourceProvider<YinXmlSchemaSource> provider = this.schema;
67         final Future<? extends YinXmlSchemaSource> source = provider.getSource(sourceIdentifier);
68         Assert.assertNotNull(source);
69         source.cancel(true);
70         Assert.assertTrue(source.isDone());
71     }
72
73     @Test
74     public void schemaSourceRegAndUnregSchemaSourceTest() throws Exception {
75         final SourceIdentifier sourceIdentifier = new SourceIdentifier("source");
76         final Foo<YangSchemaSourceRepresentation> foo = new Foo<>(sourceIdentifier,
77                 SchemaSourceTransformerTest.SRC_CLASS,
78                 PotentialSchemaSource.Costs.COMPUTATION);
79         final Provider p = new Provider();
80
81         final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
82                 PotentialSchemaSource.Costs.IMMEDIATE);
83         reg.register(sourceIdentifier);
84
85         final Consumer c = new Consumer();
86         this.schema = new SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource>(p,
87                 SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, this.function);
88
89         final SchemaSourceListener listener = this.schema;
90         p.registerSchemaSourceListener(listener);
91
92         final PotentialSchemaSource<?>[] potList = { foo.getPotentialSchemSource() };
93         final Iterable<PotentialSchemaSource<?>> sources = Arrays.asList(potList);
94         listener.schemaSourceRegistered(sources);
95         final CheckedFuture<YinXmlSchemaSource, SchemaSourceException> source = this.schema.getSource(sourceIdentifier);
96         Assert.assertNotNull(source);
97
98         listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
99         final CheckedFuture<YinXmlSchemaSource, SchemaSourceException> source2 = this.schema
100                 .getSource(sourceIdentifier);
101         Assert.assertNotNull(source2);
102     }
103
104     private class Foo<T extends SchemaSourceRepresentation> {
105
106         final PotentialSchemaSource<T> src;
107
108         public 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         protected 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 CheckedFuture<? extends YangSchemaSourceRepresentation, SchemaSourceException> getSource(
133                 final SourceIdentifier sourceIdentifier) {
134             return Mockito.mock(CheckedFuture.class);
135         }
136
137     }
138
139     private class Provider extends AbstractSchemaRepository {
140
141         @Override
142         public SchemaContextFactory createSchemaContextFactory(final SchemaSourceFilter filter) {
143             return null;
144         }
145
146     }
147
148     private class Consumer extends AbstractSchemaRepository {
149
150         @Override
151         public SchemaContextFactory createSchemaContextFactory(final SchemaSourceFilter filter) {
152             return null;
153         }
154
155     }
156 }