Minimize surefire overrides
[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 static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.mock;
13
14 import com.google.common.util.concurrent.AsyncFunction;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.Arrays;
18 import java.util.concurrent.Future;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
24 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
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     public static final Class<YangSchemaSourceRepresentation> SRC_CLASS = YangSchemaSourceRepresentation.class;
40     public static final Class<YinXmlSchemaSource> DST_CLASS = YinXmlSchemaSource.class;
41
42     @Mock
43     public SchemaRepository provider;
44
45     @Mock
46     public SchemaSourceRegistry consumer;
47
48     @Mock
49     public AsyncFunction<YangSchemaSourceRepresentation, YinXmlSchemaSource> function;
50
51     public SchemaSourceTransformer<YangSchemaSourceRepresentation, YinXmlSchemaSource> schema;
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         assertNotNull(this.schema);
59     }
60
61     @Test
62     public void schemaSourceTransformerGetSourceTest() {
63         final Provider p = new Provider();
64         final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
65                 PotentialSchemaSource.Costs.IMMEDIATE);
66         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source");
67         reg.register(sourceIdentifier);
68         this.schema = new SchemaSourceTransformer<>(p,
69                 SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS,
70                 this.function);
71         final SchemaSourceProvider<YinXmlSchemaSource> prov = this.schema;
72         final Future<? extends YinXmlSchemaSource> source = prov.getSource(sourceIdentifier);
73         assertNotNull(source);
74         source.cancel(true);
75         assertTrue(source.isDone());
76     }
77
78     @Test
79     public void schemaSourceRegAndUnregSchemaSourceTest() {
80         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source");
81         final Foo<YangSchemaSourceRepresentation> foo = new Foo<>(sourceIdentifier,
82                 SchemaSourceTransformerTest.SRC_CLASS,
83                 PotentialSchemaSource.Costs.COMPUTATION);
84         final Provider p = new Provider();
85
86         final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS,
87                 PotentialSchemaSource.Costs.IMMEDIATE);
88         reg.register(sourceIdentifier);
89
90         final Consumer c = new Consumer();
91         this.schema = new SchemaSourceTransformer<>(p,
92                 SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, this.function);
93
94         final SchemaSourceListener listener = this.schema;
95         p.registerSchemaSourceListener(listener);
96
97         final PotentialSchemaSource<?>[] potList = { foo.getPotentialSchemSource() };
98         final Iterable<PotentialSchemaSource<?>> sources = Arrays.asList(potList);
99         listener.schemaSourceRegistered(sources);
100         final ListenableFuture<YinXmlSchemaSource> source = this.schema.getSource(sourceIdentifier);
101         assertNotNull(source);
102
103         listener.schemaSourceUnregistered(foo.getPotentialSchemSource());
104         final ListenableFuture<YinXmlSchemaSource> source2 = this.schema.getSource(sourceIdentifier);
105         assertNotNull(source2);
106     }
107
108     private class Foo<T extends SchemaSourceRepresentation> {
109
110         final PotentialSchemaSource<T> src;
111
112         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         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 ListenableFuture<? extends YangSchemaSourceRepresentation> getSource(
137                 final SourceIdentifier sourceIdentifier) {
138             return SettableFuture.create();
139         }
140
141     }
142
143     private class Provider extends AbstractSchemaRepository {
144         @Override
145         public EffectiveModelContextFactory createEffectiveModelContextFactory(
146                 final SchemaContextFactoryConfiguration config) {
147             return mock(EffectiveModelContextFactory.class);
148         }
149     }
150
151     private class Consumer extends AbstractSchemaRepository {
152         @Override
153         public EffectiveModelContextFactory createEffectiveModelContextFactory(
154                 final SchemaContextFactoryConfiguration config) {
155             return mock(EffectiveModelContextFactory.class);
156         }
157     }
158 }