* 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.mdsal.binding.dom.adapter;
-import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-import com.google.common.collect.ImmutableBiMap;
-import com.google.common.collect.Multimaps;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.SetMultimap;
-import com.google.common.util.concurrent.Uninterruptibles;
import java.lang.reflect.Method;
-import java.net.URI;
-import java.util.Collections;
+import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReference;
-import javassist.ClassPool;
+import java.util.stream.Collectors;
import org.junit.Test;
import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractSchemaAwareTest;
-import org.opendaylight.mdsal.binding.dom.codec.gen.impl.DataObjectSerializerGenerator;
-import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
-import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
-import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
-import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
+import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext;
+import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.spi.AbstractSchemaContext;
public class BindingNormalizedCodecTest extends AbstractSchemaAwareTest {
private static final YangInstanceIdentifier BI_TOP_LEVEL_LIST = YangInstanceIdentifier.builder().node(Top.QNAME)
.node(TopLevelList.QNAME).nodeWithKey(TopLevelList.QNAME, NAME_QNAME, TOP_FOO_KEY.getName()).build();
- private BindingToNormalizedNodeCodec codec;
- private SchemaContext context;
+ private CurrentAdapterSerializer serializer;
@Override
- protected void setupWithSchema(final SchemaContext schemaContext) {
- this.context = schemaContext;
- final DataObjectSerializerGenerator streamWriter = StreamWriterGenerator.create(JavassistUtils.forClassPool(
- ClassPool.getDefault()));
- final BindingNormalizedNodeCodecRegistry registry = new BindingNormalizedNodeCodecRegistry(streamWriter);
- this.codec = new BindingToNormalizedNodeCodec(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(),
- registry, true);
+ protected void setupWithRuntimeContext(final BindingRuntimeContext runtimeContext) {
+ serializer = new CurrentAdapterSerializer(new BindingCodecContext(runtimeContext));
}
@Test
public void testComplexAugmentationSerialization() {
- this.codec.onGlobalContextUpdated(this.context);
- final PathArgument lastArg = this.codec.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
+ final PathArgument lastArg = serializer.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
assertTrue(lastArg instanceof AugmentationIdentifier);
}
-
@Test
public void testLeafOnlyAugmentationSerialization() {
- this.codec.onGlobalContextUpdated(this.context);
- final PathArgument leafOnlyLastArg = this.codec.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY)
+ final PathArgument leafOnlyLastArg = serializer.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY)
.getLastPathArgument();
assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
}
@Test
- @SuppressWarnings("checkstyle:illegalCatch")
- public void testToYangInstanceIdentifierBlocking() {
- this.codec.onGlobalContextUpdated(new EmptySchemaContext());
-
- final CountDownLatch done = new CountDownLatch(1);
- final AtomicReference<YangInstanceIdentifier> yangId = new AtomicReference<>();
- final AtomicReference<RuntimeException> error = new AtomicReference<>();
- new Thread(() -> {
- try {
- yangId.set(BindingNormalizedCodecTest.this.codec.toYangInstanceIdentifierBlocking(BA_TOP_LEVEL_LIST));
- } catch (final RuntimeException e) {
- error.set(e);
- } finally {
- done.countDown();
- }
- }).start();
-
- Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
- this.codec.onGlobalContextUpdated(this.context);
-
- assertTrue("toYangInstanceIdentifierBlocking completed",
- Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
- if (error.get() != null) {
- throw error.get();
- }
-
- assertEquals("toYangInstanceIdentifierBlocking", BI_TOP_LEVEL_LIST, yangId.get());
- }
-
- @Test
- public void testGetRpcMethodToSchemaPathWithNoInitialSchemaContext() {
- testGetRpcMethodToSchemaPath();
- }
-
- @Test
- public void testGetRpcMethodToSchemaPathBlocking() {
- this.codec.onGlobalContextUpdated(new EmptySchemaContext());
- testGetRpcMethodToSchemaPath();
- }
-
- @SuppressWarnings("checkstyle:illegalCatch")
- private void testGetRpcMethodToSchemaPath() {
- final CountDownLatch done = new CountDownLatch(1);
- final AtomicReference<ImmutableBiMap<Method, SchemaPath>> retMap = new AtomicReference<>();
- final AtomicReference<RuntimeException> error = new AtomicReference<>();
- new Thread(() -> {
- try {
- retMap.set(BindingNormalizedCodecTest.this.codec.getRpcMethodToSchemaPath(
- OpendaylightTestRpcServiceService.class));
- } catch (final RuntimeException e) {
- error.set(e);
- } finally {
- done.countDown();
- }
- }).start();
-
- Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
- this.codec.onGlobalContextUpdated(this.context);
-
- assertTrue("getRpcMethodToSchemaPath completed",
- Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
- if (error.get() != null) {
- throw error.get();
- }
-
- for (final Method method : retMap.get().keySet()) {
- if (method.getName().equals("rockTheHouse")) {
- return;
- }
- }
-
- fail("rockTheHouse RPC method not found");
+ public void testGetRpcMethodToQName() {
+ final List<String> retMap = serializer
+ .getRpcMethodToQName(OpendaylightTestRpcServiceService.class).keySet().stream()
+ .map(Method::getName)
+ .collect(Collectors.toList());
+ assertTrue(retMap.contains("rockTheHouse"));
}
- static class EmptySchemaContext extends AbstractSchemaContext {
+ static class EmptyEffectiveModelContext extends AbstractSchemaContext implements EffectiveModelContext {
@Override
public Set<Module> getModules() {
- return Collections.emptySet();
+ return ImmutableSet.of();
}
@Override
- protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
- return Collections.emptyMap();
+ protected Map<QNameModule, Module> getModuleMap() {
+ return ImmutableMap.of();
}
@Override
- protected SetMultimap<URI, Module> getNamespaceToModules() {
- return Multimaps.forMap(Collections.emptyMap());
+ protected SetMultimap<XMLNamespace, Module> getNamespaceToModules() {
+ return ImmutableSetMultimap.of();
}
@Override
protected SetMultimap<String, Module> getNameToModules() {
- return Multimaps.forMap(Collections.emptyMap());
+ return ImmutableSetMultimap.of();
+ }
+
+ @Override
+ public Map<QNameModule, ModuleEffectiveStatement> getModuleStatements() {
+ return ImmutableMap.of();
}
}
}