OVSDB-453: Use findModules instead of findModule by specific revision 90/69290/2
authorTom Pantelis <tompantelis@gmail.com>
Thu, 8 Mar 2018 18:52:09 +0000 (13:52 -0500)
committerTom Pantelis <tompantelis@gmail.com>
Thu, 8 Mar 2018 19:54:59 +0000 (14:54 -0500)
findModules returns a list sorted by latest revision so take the first
element.

Change-Id: Ib2fb52e59c45476f6e3ec6aff63a2ea3333514fa
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/InstanceIdentifierCodec.java
southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/InstanceIdentifierCodecTest.java

index 0b2d188133b41fc68678e67fcef35340c37ded7a..e80ccd5b2453cb60abe26cccc10a8775d44d2a83 100644 (file)
@@ -9,11 +9,9 @@
 package org.opendaylight.ovsdb.southbound;
 
 import java.net.URI;
-
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
@@ -28,11 +26,10 @@ public class InstanceIdentifierCodec extends AbstractModuleStringInstanceIdentif
     implements SchemaContextListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(InstanceIdentifierCodec.class);
-    private static final Revision REVISION = Revision.of("2013-10-21");
 
     private DataSchemaContextTree dataSchemaContextTree;
     private SchemaContext context;
-    private BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer;
+    private final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer;
 
     public InstanceIdentifierCodec(DOMSchemaService schemaService,
             BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
@@ -47,12 +44,12 @@ public class InstanceIdentifierCodec extends AbstractModuleStringInstanceIdentif
 
     @Override
     protected Module moduleForPrefix(final String prefix) {
-        return context.findModule(prefix, REVISION).orElse(null);
+        return context.findModules(prefix).stream().findFirst().orElse(null);
     }
 
     @Override
     protected String prefixForNamespace(final URI namespace) {
-        return context.findModule(namespace, REVISION).map(Module::getName).orElse(null);
+        return context.findModules(namespace).stream().map(Module::getName).findFirst().orElse(null);
     }
 
     @Override
index 101a62c9a51c79551973ed698060a9f47d9126ec..4d34e71faded543714d4d4fc4b58d0ffa78df18f 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.ovsdb.southbound;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
@@ -16,9 +17,10 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import static org.powermock.api.support.membermodification.MemberMatcher.field;
 
+import com.google.common.collect.ImmutableSet;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.util.Optional;
+import java.util.Collections;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -28,7 +30,6 @@ import org.mockito.stubbing.Answer;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
@@ -76,20 +77,31 @@ public class InstanceIdentifierCodecTest {
     @Test
     public void testModuleForPrefix() {
         Module module = mock(Module.class);
-        when(context.findModule(anyString(), any(Revision.class))).thenReturn(Optional.of(module));
-        assertEquals("Error, did not return correct Module object", module, instanceIdCodec.moduleForPrefix(""));
+        when(context.findModules("")).thenReturn(ImmutableSet.of(module));
+        assertEquals("Found Module", module, instanceIdCodec.moduleForPrefix(""));
+
+        when(context.findModules("foo")).thenReturn(ImmutableSet.of(module, mock(Module.class)));
+        assertEquals("Found Module", module, instanceIdCodec.moduleForPrefix("foo"));
+
+        when(context.findModules("bar")).thenReturn(Collections.emptySet());
+        assertNull(instanceIdCodec.moduleForPrefix("bar"));
     }
 
     @Test
     public void testPrefixForNamespace() throws URISyntaxException {
         Module module = mock(Module.class);
-        URI namespace = new URI("");
-        when(context.findModule(any(URI.class), any(Revision.class))).thenReturn(Optional.empty()).thenReturn(
-                Optional.of(module));
-        when(module.getName()).thenReturn("");
-        assertEquals("Error, null should have been returned", null, instanceIdCodec.prefixForNamespace(namespace));
-        assertEquals("Error, did not return the correct module name", anyString(),
-                instanceIdCodec.prefixForNamespace(namespace));
+        final String prefix = "prefix";
+        when(module.getName()).thenReturn(prefix);
+
+        URI namespace = new URI("foo");
+        when(context.findModules(namespace)).thenReturn(Collections.emptySet());
+        assertNull(instanceIdCodec.prefixForNamespace(namespace));
+
+        when(context.findModules(namespace)).thenReturn(ImmutableSet.of(module));
+        assertEquals("Found prefix", prefix, instanceIdCodec.prefixForNamespace(namespace));
+
+        when(context.findModules(namespace)).thenReturn(ImmutableSet.of(module, mock(Module.class)));
+        assertEquals("Found prefix", prefix, instanceIdCodec.prefixForNamespace(namespace));
     }
 
     @Test
@@ -106,7 +118,7 @@ public class InstanceIdentifierCodecTest {
         YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
         when(bindingNormalizedNodeSerializer.toYangInstanceIdentifier(iid)).thenReturn(yiid);
 
-        when((PowerMockito.mock(AbstractModuleStringInstanceIdentifierCodec.class)).serialize(yiid))
+        when(PowerMockito.mock(AbstractModuleStringInstanceIdentifierCodec.class).serialize(yiid))
                 .thenReturn("Serialized IID");
         assertEquals("Error, did not return correct string", anyString(), instanceIdCodec.serialize(iid));
     }
@@ -114,7 +126,7 @@ public class InstanceIdentifierCodecTest {
     @Test
     public void testBindingDeserializer() throws Exception {
         YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
-        when((PowerMockito.mock(AbstractModuleStringInstanceIdentifierCodec.class)).deserialize(anyString()))
+        when(PowerMockito.mock(AbstractModuleStringInstanceIdentifierCodec.class).deserialize(anyString()))
                 .thenReturn(yiid);
 
         mock(InstanceIdentifier.class);