/* * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.model.repo.spi; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; import java.util.Arrays; import java.util.concurrent.Future; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory; import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration; import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository; import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation; import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation; import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource; import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs; @RunWith(MockitoJUnitRunner.class) public class SchemaSourceTransformerTest { public static final Class SRC_CLASS = YangSchemaSourceRepresentation.class; public static final Class DST_CLASS = YinXmlSchemaSource.class; @Mock public SchemaRepository provider; @Mock public SchemaSourceRegistry consumer; @Mock public AsyncFunction function; public SchemaSourceTransformer schema; @Test public void schemaSourceTransformerTest() { this.schema = new SchemaSourceTransformer<>( this.provider, SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS, this.function); assertNotNull(this.schema); } @Test public void schemaSourceTransformerGetSourceTest() { final Provider p = new Provider(); final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, PotentialSchemaSource.Costs.IMMEDIATE); final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source"); reg.register(sourceIdentifier); this.schema = new SchemaSourceTransformer<>(p, SchemaSourceTransformerTest.SRC_CLASS, this.consumer, SchemaSourceTransformerTest.DST_CLASS, this.function); final SchemaSourceProvider prov = this.schema; final Future source = prov.getSource(sourceIdentifier); assertNotNull(source); source.cancel(true); assertTrue(source.isDone()); } @Test public void schemaSourceRegAndUnregSchemaSourceTest() { final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("source"); final Foo foo = new Foo<>(sourceIdentifier, SchemaSourceTransformerTest.SRC_CLASS, PotentialSchemaSource.Costs.COMPUTATION); final Provider p = new Provider(); final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, PotentialSchemaSource.Costs.IMMEDIATE); reg.register(sourceIdentifier); final Consumer c = new Consumer(); this.schema = new SchemaSourceTransformer<>(p, SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, this.function); final SchemaSourceListener listener = this.schema; p.registerSchemaSourceListener(listener); final PotentialSchemaSource[] potList = { foo.getPotentialSchemSource() }; final Iterable> sources = Arrays.asList(potList); listener.schemaSourceRegistered(sources); final ListenableFuture source = this.schema.getSource(sourceIdentifier); assertNotNull(source); listener.schemaSourceUnregistered(foo.getPotentialSchemSource()); final ListenableFuture source2 = this.schema.getSource(sourceIdentifier); assertNotNull(source2); } private class Foo { final PotentialSchemaSource src; Foo(final SourceIdentifier sourceIdentifier, final Class representation, final Costs cost) { this.src = PotentialSchemaSource.create(sourceIdentifier, representation, cost.getValue()); } public PotentialSchemaSource getPotentialSchemSource() { return this.src; } } private class Registrator extends AbstractSchemaSourceCache { Registrator(final SchemaSourceRegistry consumer, final Class srcClass, final Costs cost) { super(consumer, srcClass, cost); } @Override protected void offer(final YangSchemaSourceRepresentation source) { } @Override public ListenableFuture getSource( final SourceIdentifier sourceIdentifier) { return SettableFuture.create(); } } private class Provider extends AbstractSchemaRepository { @Override public EffectiveModelContextFactory createEffectiveModelContextFactory( final SchemaContextFactoryConfiguration config) { return mock(EffectiveModelContextFactory.class); } } private class Consumer extends AbstractSchemaRepository { @Override public EffectiveModelContextFactory createEffectiveModelContextFactory( final SchemaContextFactoryConfiguration config) { return mock(EffectiveModelContextFactory.class); } } }