HONEYCOMB-437: implement ReflexiveListReaderCustomizer.readCurrentAttributes 86/12586/2
authorMarek Gradzki <mgradzki@cisco.com>
Wed, 16 May 2018 08:30:28 +0000 (10:30 +0200)
committerMarek Gradzki <mgradzki@cisco.com>
Wed, 16 May 2018 10:07:20 +0000 (12:07 +0200)
ReflexiveListReaderCustomizer used default NOOP implementation,
which caused issues during serialization, because
BindingNormalizedNodeCodecRegistry.toNormalizedNode requrires key to be set.

Change-Id: I7822e07efe57cbdee2c539583776f8cc677ddab7
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java
infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizerTest.java

index b6430a3..e058964 100644 (file)
 
 package io.fd.honeycomb.translate.util.read;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+
 import com.google.common.base.Optional;
 import io.fd.honeycomb.translate.read.ReadContext;
 import io.fd.honeycomb.translate.read.ReadFailedException;
 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
 import io.fd.honeycomb.translate.util.ReflectionUtils;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.List;
+import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.concepts.Builder;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.Identifiable;
 import org.opendaylight.yangtools.yang.binding.Identifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-
-import javax.annotation.Nonnull;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Collections;
-import java.util.List;
-
-import static com.google.common.base.Preconditions.*;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 
 /**
  * Might be slow.
@@ -43,12 +45,29 @@ public class ReflexiveListReaderCustomizer<C extends DataObject & Identifiable<K
         implements ListReaderCustomizer<C, K, B> {
 
     private final List<K> staticKeys;
+    private final Class<? extends Identifier> keyType;
 
     public ReflexiveListReaderCustomizer(@Nonnull final Class<C> typeClass, @Nonnull final Class<B> builderClass,
                                          @Nonnull final List<K> staticKeys) {
         super(typeClass, builderClass);
         this.staticKeys = checkNotNull(staticKeys, "Static keys cannot be null");
         checkState(!this.staticKeys.isEmpty(), "No static keys provided");
+        keyType = staticKeys.get(0).getClass();
+    }
+
+    @Override
+    public void readCurrentAttributes(final InstanceIdentifier<C> id, final B builder, final ReadContext context)
+        throws ReadFailedException {
+        final Optional<Method> method =
+            ReflectionUtils.findMethodReflex(builder.getClass(), "setKey",
+                Collections.singletonList(keyType), builder.getClass());
+        checkArgument(method.isPresent(), "Unable to setKey to %s", builder);
+
+        try {
+            method.get().invoke(builder, ((KeyedInstanceIdentifier)id).getKey());
+        } catch (IllegalAccessException | InvocationTargetException e) {
+            throw new IllegalArgumentException("Unable to setKey to " + builder, e);
+        }
     }
 
     @Override
index fe12ff6..2cc9345 100644 (file)
  */
 package io.fd.honeycomb.translate.util.read;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.hasSize;
+import static org.junit.Assert.assertEquals;
+
 import io.fd.honeycomb.translate.read.ReadContext;
 import io.fd.honeycomb.translate.read.ReadFailedException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.opendaylight.yangtools.concepts.Builder;
-import org.opendaylight.yangtools.yang.binding.*;
+import org.opendaylight.yangtools.yang.binding.DataContainer;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.Identifiable;
+import org.opendaylight.yangtools.yang.binding.Identifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
-import java.util.Arrays;
-import java.util.List;
+public class ReflexiveListReaderCustomizerTest {
 
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.contains;
-import static org.hamcrest.Matchers.hasSize;
+    @Mock
+    private ReadContext readContext;
 
-public class ReflexiveListReaderCustomizerTest {
+    @Before
+    public void init() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testReadCurrentAttributes() throws ReadFailedException {
+        final TestingListObject.TestingListKey keyOne = new TestingListObject.TestingListKey("1");
+        final TestingListObject.TestingListKey keyTwo = new TestingListObject.TestingListKey("2");
+        final List<TestingListObject.TestingListKey> staticKeys = Arrays.asList(keyOne, keyTwo);
+
+        final ReflexiveListReaderCustomizer<TestingListObject, TestingListObject.TestingListKey, TestingListObjectBuilder> customizer
+            = new ReflexiveListReaderCustomizer<>(TestingListObject.class, TestingListObjectBuilder.class, staticKeys);
+
+        final TestingListObjectBuilder builder = new TestingListObjectBuilder();
+        final InstanceIdentifier<TestingListObject> id =
+            (InstanceIdentifier<TestingListObject>) InstanceIdentifier.create(
+                Collections.singletonList(new InstanceIdentifier.IdentifiableItem<>(TestingListObject.class, keyOne)));
+        customizer.readCurrentAttributes(id, builder, readContext);
+
+        assertEquals(keyOne, builder.getKey());
+    }
+
+    @Test
+    public void testGetAllIds() throws ReadFailedException {
+        final TestingListObject.TestingListKey keyOne = new TestingListObject.TestingListKey("1");
+        final TestingListObject.TestingListKey keyTwo = new TestingListObject.TestingListKey("2");
+        final List<TestingListObject.TestingListKey> staticKeys = Arrays.asList(keyOne, keyTwo);
+
+        final ReflexiveListReaderCustomizer<TestingListObject, TestingListObject.TestingListKey, TestingListObjectBuilder> customizer
+            = new ReflexiveListReaderCustomizer<>(TestingListObject.class, TestingListObjectBuilder.class, staticKeys);
+
+        final List<TestingListObject.TestingListKey> allIds =
+            customizer.getAllIds(InstanceIdentifier.create(TestingListObject.class), readContext);
+
+        assertThat(allIds, hasSize(2));
+        assertThat(allIds, contains(keyOne, keyTwo));
+    }
 
     static class TestingListObject implements DataObject, Identifiable<TestingListObject.TestingListKey> {
 
@@ -78,38 +126,20 @@ public class ReflexiveListReaderCustomizerTest {
 
     static class TestingListObjectBuilder implements Builder<TestingListObject> {
 
-        private final TestingListObject.TestingListKey key;
-
-        TestingListObjectBuilder(final TestingListObject.TestingListKey key) {
-            this.key = key;
-        }
+        private TestingListObject.TestingListKey key;
 
         @Override
         public TestingListObject build() {
             return new TestingListObject(key);
         }
-    }
-
-    @Mock
-    private ReadContext readContext;
 
-    @Before
-    public void init() {
-        MockitoAnnotations.initMocks(this);
-    }
-
-    @Test
-    public void testStaticKeys() throws ReadFailedException {
-        final TestingListObject.TestingListKey keyOne = new TestingListObject.TestingListKey("1");
-        final TestingListObject.TestingListKey keyTwo = new TestingListObject.TestingListKey("2");
-        final List<TestingListObject.TestingListKey> staticKeys = Arrays.asList(keyOne, keyTwo);
-
-        final ReflexiveListReaderCustomizer<TestingListObject, TestingListObject.TestingListKey, TestingListObjectBuilder> customizer
-                = new ReflexiveListReaderCustomizer<>(TestingListObject.class, TestingListObjectBuilder.class, staticKeys);
-
-        final List<TestingListObject.TestingListKey> allIds = customizer.getAllIds(InstanceIdentifier.create(TestingListObject.class), readContext);
+        public TestingListObjectBuilder setKey(final TestingListObject.TestingListKey key) {
+            this.key = key;
+            return this;
+        }
 
-        assertThat(allIds, hasSize(2));
-        assertThat(allIds, contains(keyOne, keyTwo));
+        public TestingListObject.TestingListKey getKey() {
+            return key;
+        }
     }
 }