diff options
author | Maros Marsalek <mmarsale@cisco.com> | 2016-09-13 16:50:00 +0200 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2016-09-16 08:43:18 +0000 |
commit | 963a3601284b033ce8ae23a6c22789c8d1245d7f (patch) | |
tree | 216ff7db7b48da47dede5e412fbf327cf8ab1ed5 | |
parent | cedfd619866ee2d3a1b9a74bc5540697aa0e275d (diff) |
HONEYCOMB-194 Raise unit test coverage of translation-util to 80%
Change-Id: I6689f6d1f6a4a1376465783dad2650f5137142d2
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
16 files changed, 1054 insertions, 15 deletions
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/RealtimeMappingContext.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/RealtimeMappingContext.java index 3d1b5222f..82079f0e5 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/RealtimeMappingContext.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/RealtimeMappingContext.java @@ -38,7 +38,7 @@ public final class RealtimeMappingContext implements MappingContext { @Override public <T extends DataObject> Optional<T> read(@Nonnull final InstanceIdentifier<T> currentId) { - try(ReadOnlyTransaction tx = contextBindingBrokerDependency.newReadOnlyTransaction()) { + try (ReadOnlyTransaction tx = contextBindingBrokerDependency.newReadOnlyTransaction()) { try { return tx.read(LogicalDatastoreType.OPERATIONAL, currentId).checkedGet(); } catch (ReadFailedException e) { diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java index 03df15175..b7a38e97f 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/BindingBrokerReader.java @@ -33,7 +33,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; * Simple DataBroker backed reader allowing to delegate reads to different brokers. */ public final class BindingBrokerReader<D extends DataObject, B extends Builder<D>> - implements Reader<D, B>, AutoCloseable { + implements Reader<D, B> { private final InstanceIdentifier<D> instanceIdentifier; private final DataBroker dataBroker; @@ -88,9 +88,4 @@ public final class BindingBrokerReader<D extends DataObject, B extends Builder<D public InstanceIdentifier<D> getManagedDataObjectType() { return instanceIdentifier; } - - @Override - public void close() throws Exception { - // Noop - } } diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java index f8d1458c0..5ab9bc59e 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapper.java @@ -39,7 +39,8 @@ import org.slf4j.LoggerFactory; * Reader wrapper that periodically invokes a read to determine whether reads are still fully functional. * In case a specific error occurs, Keep-alive failure listener gets notified. */ -public final class KeepaliveReaderWrapper<D extends DataObject, B extends Builder<D>> implements Reader<D, B>, Runnable, Closeable { +public final class KeepaliveReaderWrapper<D extends DataObject, B extends Builder<D>> + implements Reader<D, B>, Runnable, Closeable { private static final Logger LOG = LoggerFactory.getLogger(KeepaliveReaderWrapper.class); @@ -51,7 +52,7 @@ public final class KeepaliveReaderWrapper<D extends DataObject, B extends Builde private final ScheduledFuture<?> scheduledFuture; /** - * Create new Keepalive wrapper + * Create new Keepalive wrapper. * * @param delegate underlying reader performing actual reads * @param executor scheduled executor service to schedule keepalive calls @@ -123,7 +124,7 @@ public final class KeepaliveReaderWrapper<D extends DataObject, B extends Builde } /** - * Listener that gets called whenever keepalive fails as expected + * Listener that gets called whenever keepalive fails as expected. */ public interface KeepaliveFailureListener { @@ -145,7 +146,6 @@ public final class KeepaliveReaderWrapper<D extends DataObject, B extends Builde @Override public <T extends DataObject> void put(final InstanceIdentifier<T> path, final T data) {} - @Override public void close() {} } diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java index bf2d109c7..62dbcd6c9 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java @@ -32,7 +32,7 @@ import org.opendaylight.yangtools.yang.binding.Identifiable; import org.opendaylight.yangtools.yang.binding.Identifier; /** - * Might be slow ! + * Might be slow. */ public abstract class ReflexiveListReaderCustomizer<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>> extends ReflexiveReaderCustomizer<C, B> diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java index 6ceb7a2b1..18e6285f3 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizer.java @@ -31,7 +31,7 @@ import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** - * Might be slow ! + * Might be slow. */ class ReflexiveReaderCustomizer<C extends DataObject, B extends Builder<C>> extends NoopReaderCustomizer<C, B> { diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/DataObjects.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/DataObjects.java index e602adbb6..f2f664efe 100644 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/DataObjects.java +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/DataObjects.java @@ -18,6 +18,8 @@ package io.fd.honeycomb.translate.util; import org.opendaylight.yangtools.yang.binding.ChildOf; 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; public class DataObjects { @@ -49,4 +51,14 @@ public class DataObjects { InstanceIdentifier<DataObject42> IID = DataObject4.IID.child(DataObject42.class); } } + + public interface DataObjectK extends DataObject, Identifiable<DataObjectKey> { + InstanceIdentifier<DataObjectK> IID = InstanceIdentifier.create(DataObjectK.class); + + interface DataObjectK1 extends DataObject, ChildOf<DataObjectK> { + InstanceIdentifier<DataObjectK1> IID = DataObjectK.IID.child(DataObjectK1.class); + } + } + + public static class DataObjectKey implements Identifier<DataObjectK> {} } diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/RealtimeMappingContextTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/RealtimeMappingContextTest.java new file mode 100644 index 000000000..1ee06c1b7 --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/RealtimeMappingContextTest.java @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.util; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class RealtimeMappingContextTest { + + @Mock + private DataBroker broker; + private RealtimeMappingContext ctx; + private InstanceIdentifier<DataObject> id = InstanceIdentifier.create(DataObject.class); + @Mock + private ReadOnlyTransaction readTx; + @Mock + private WriteTransaction writeTx; + @Mock + private DataObject data; + private TransactionCommitFailedException ex = new TransactionCommitFailedException("test fail"); + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + ctx = new RealtimeMappingContext(broker); + + when(broker.newReadOnlyTransaction()).thenReturn(readTx); + when(broker.newWriteOnlyTransaction()).thenReturn(writeTx); + when(writeTx.submit()).thenReturn(Futures.immediateCheckedFuture(null)); + } + + @Test + public void testRead() throws Exception { + final CheckedFuture<Optional<DataObject>, ReadFailedException> futureData = + Futures.immediateCheckedFuture(Optional.of((data))); + when(readTx.read(LogicalDatastoreType.OPERATIONAL, id)).thenReturn(futureData); + + assertSame(ctx.read(id).get(), data); + verify(broker).newReadOnlyTransaction(); + verify(readTx).read(LogicalDatastoreType.OPERATIONAL, id); + + when(readTx.read(LogicalDatastoreType.OPERATIONAL, id)).thenReturn(Futures.immediateCheckedFuture(Optional.absent())); + assertFalse(ctx.read(id).isPresent()); + } + + @Test + public void testMerge() throws Exception { + ctx.merge(id, data); + verify(broker).newWriteOnlyTransaction(); + verify(writeTx).merge(LogicalDatastoreType.OPERATIONAL, id, data); + } + + @Test(expected = IllegalStateException.class) + public void testMergeFailure() throws Exception { + when(writeTx.submit()).thenReturn(Futures.immediateFailedCheckedFuture(ex)); + ctx.merge(id, data); + } + + @Test + public void testPut() throws Exception { + ctx.put(id, data); + verify(broker).newWriteOnlyTransaction(); + verify(writeTx).put(LogicalDatastoreType.OPERATIONAL, id, data); + } + + @Test(expected = IllegalStateException.class) + public void testPutFailure() throws Exception { + when(writeTx.submit()).thenReturn(Futures.immediateFailedCheckedFuture(ex)); + ctx.put(id, data); + } + + @Test + public void testDelete() throws Exception { + ctx.delete(id); + verify(broker).newWriteOnlyTransaction(); + verify(writeTx).delete(LogicalDatastoreType.OPERATIONAL, id); + } + + @Test(expected = IllegalStateException.class) + public void testDeleteFailure() throws Exception { + when(writeTx.submit()).thenReturn(Futures.immediateFailedCheckedFuture(ex)); + ctx.delete(id); + } +}
\ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/TransactionMappingContextTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/TransactionMappingContextTest.java new file mode 100644 index 000000000..2dcee889a --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/TransactionMappingContextTest.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.util; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class TransactionMappingContextTest { + + private TransactionMappingContext ctx; + private InstanceIdentifier<DataObject> id = InstanceIdentifier.create(DataObject.class); + @Mock + private ReadWriteTransaction writeTx; + @Mock + private DataObject data; + private ReadFailedException ex = new ReadFailedException("test fail"); + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + ctx = new TransactionMappingContext(writeTx); + + when(writeTx.submit()).thenReturn(Futures.immediateCheckedFuture(null)); + } + + @Test + public void testRead() throws Exception { + final CheckedFuture<Optional<DataObject>, ReadFailedException> futureData = + Futures.immediateCheckedFuture(Optional.of((data))); + when(writeTx.read(LogicalDatastoreType.OPERATIONAL, id)).thenReturn(futureData); + + assertSame(ctx.read(id).get(), data); + verify(writeTx).read(LogicalDatastoreType.OPERATIONAL, id); + + when(writeTx.read(LogicalDatastoreType.OPERATIONAL, id)).thenReturn(Futures.immediateCheckedFuture(Optional.absent())); + assertFalse(ctx.read(id).isPresent()); + } + + + @Test(expected = IllegalStateException.class) + public void testReadFailure() throws Exception { + final CheckedFuture<Optional<DataObject>, ReadFailedException> futureData = + Futures.immediateFailedCheckedFuture(ex); + when(writeTx.read(LogicalDatastoreType.OPERATIONAL, id)).thenReturn(futureData); + assertSame(ctx.read(id).get(), data); + } + + @Test + public void testMerge() throws Exception { + ctx.merge(id, data); + verify(writeTx).merge(LogicalDatastoreType.OPERATIONAL, id, data, true); + } + + @Test + public void testPut() throws Exception { + ctx.put(id, data); + verify(writeTx).put(LogicalDatastoreType.OPERATIONAL, id, data, true); + } + + @Test + public void testDelete() throws Exception { + ctx.delete(id); + verify(writeTx).delete(LogicalDatastoreType.OPERATIONAL, id); + } + + @Test + public void testClose() throws Exception { + ctx.close(); + verify(writeTx).cancel(); + } + +}
\ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/BindingBrokerReaderTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/BindingBrokerReaderTest.java new file mode 100644 index 000000000..b6af72125 --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/BindingBrokerReaderTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.util.read; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.base.Optional; +import com.google.common.util.concurrent.Futures; +import io.fd.honeycomb.translate.read.ReadContext; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class BindingBrokerReaderTest { + + @Mock + private DataBroker broker; + private InstanceIdentifier<DataObject> id = InstanceIdentifier.create(DataObject.class); + @Mock + private ReadContext ctx; + @Mock + private ReadOnlyTransaction tx; + @Mock + private DataObject data; + private BindingBrokerReader<DataObject, DataObjectBuilder> bbReader; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + when(broker.newReadOnlyTransaction()).thenReturn(tx); + when(tx.read(LogicalDatastoreType.CONFIGURATION, id)).thenReturn(Futures.immediateCheckedFuture(Optional.of(data))); + bbReader = new BindingBrokerReader<>(id, broker, LogicalDatastoreType.CONFIGURATION, DataObjectBuilder.class); + } + + @Test + public void testRead() throws Exception { + assertEquals(id, bbReader.getManagedDataObjectType()); + assertNotNull(bbReader.getBuilder(id)); + + final Optional<? extends DataObject> read = bbReader.read(id, ctx); + assertSame(data, read.get()); + verify(broker).newReadOnlyTransaction(); + } + + @Test(expected = io.fd.honeycomb.translate.read.ReadFailedException.class) + public void testFailedRead() throws Exception { + when(tx.read(LogicalDatastoreType.CONFIGURATION, id)) + .thenReturn(Futures.immediateFailedCheckedFuture(new ReadFailedException("failing"))); + bbReader.read(id, ctx); + } + + @Test(expected = UnsupportedOperationException.class) + public void testReadCurrentAttrs() throws Exception { + bbReader.readCurrentAttributes(id, new DataObjectBuilder(), ctx); + } + + static final class DataObjectBuilder implements Builder<DataObject> { + + @Override + public DataObject build() { + return mock(DataObject.class); + } + } +}
\ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapperTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapperTest.java new file mode 100644 index 000000000..ca2872139 --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/KeepaliveReaderWrapperTest.java @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.util.read; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.Reader; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import org.junit.After; +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.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class KeepaliveReaderWrapperTest { + + @Mock + private ReadContext ctx; + @Mock + private Reader<DataObject, Builder<DataObject>> delegate; + @Mock + private Builder<DataObject> builder; + + private InstanceIdentifier<DataObject> id = InstanceIdentifier.create(DataObject.class); + private ScheduledExecutorService exec; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + exec = Executors.newScheduledThreadPool(1); + when(delegate.read(any(InstanceIdentifier.class), any(ReadContext.class))).thenThrow(TestingException.class); + } + + @After + public void tearDown() throws Exception { + exec.shutdownNow(); + } + + @Test(timeout = 10000) + public void testKeepalive() throws Exception { + final CapturingFailListener listener = new CapturingFailListener(); + + final KeepaliveReaderWrapper<DataObject, Builder<DataObject>> keepaliveWrapper = + new KeepaliveReaderWrapper<>(delegate, exec, TestingException.class, 1, listener); + + keepaliveWrapper.readCurrentAttributes(id, builder, ctx); + verify(delegate).readCurrentAttributes(id, builder, ctx); + + keepaliveWrapper.getBuilder(id); + verify(delegate).getBuilder(id); + + + assertTrue(listener.getTriggerFuture().get()); + verify(delegate).read(any(InstanceIdentifier.class), any(ReadContext.class)); + + keepaliveWrapper.close(); + } + + @Test(timeout = 10000) + public void testKeepaliveCancel() throws Exception { + final CapturingFailListener listener = new CapturingFailListener(); + + final KeepaliveReaderWrapper<DataObject, Builder<DataObject>> keepaliveWrapper = + new KeepaliveReaderWrapper<>(delegate, exec, TestingException.class, 100000, listener); + keepaliveWrapper.close(); + assertFalse(listener.getTriggerFuture().isDone()); + } + + private static final class TestingException extends RuntimeException {} + + private static class CapturingFailListener implements KeepaliveReaderWrapper.KeepaliveFailureListener { + private CompletableFuture<Boolean> booleanCompletableFuture = new CompletableFuture<>(); + + @Override + public synchronized void onKeepaliveFailure() { + booleanCompletableFuture.complete(true); + } + + synchronized Future<Boolean> getTriggerFuture() { + return booleanCompletableFuture; + } + } +}
\ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizerTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizerTest.java new file mode 100644 index 000000000..7fb67be6e --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizerTest.java @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.util.read; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verifyZeroInteractions; + +import com.google.common.collect.Lists; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import java.util.Collections; +import java.util.List; +import javax.annotation.Nonnull; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.Augmentation; +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; + +public class ReflexiveReaderCustomizerTest { + + @Mock + private ReadContext ctx; + private InstanceIdentifier<TestingDataObject> id = InstanceIdentifier.create(TestingDataObject.class); + + @Test + public void testReflexCustomizer() throws Exception { + final ReflexiveReaderCustomizer<TestingDataObject, TestingBuilder> reflexReaderCustomizer = + new ReflexiveReaderCustomizer<>(TestingDataObject.class, TestingBuilder.class); + + assertEquals(TestingDataObject.class, reflexReaderCustomizer.getTypeClass()); + assertEquals(TestingBuilder.class, reflexReaderCustomizer.getBuilderClass()); + + assertNotNull(reflexReaderCustomizer.getBuilder(id)); + + // NOOP + final TestingBuilder builder = spy(new TestingBuilder()); + reflexReaderCustomizer.readCurrentAttributes(id, builder, ctx); + verifyZeroInteractions(builder); + + final TestingBuilderParent parentBuilder = new TestingBuilderParent(); + final TestingDataObject readValue = new TestingDataObject(); + reflexReaderCustomizer.merge(parentBuilder, readValue); + assertEquals(readValue, parentBuilder.getTestingDataObject()); + } + + @Test + public void testReflexCustomizerAugment() throws Exception { + final ReflexiveReaderCustomizer<TestingAugmentation, TestingAugmentBuilder> reflexReaderCustomizer = + new ReflexiveReaderCustomizer<>(TestingAugmentation.class, TestingAugmentBuilder.class); + + final TestingBuilderParent parentBuilder = new TestingBuilderParent(); + final TestingAugmentation readValue = new TestingAugmentation(); + reflexReaderCustomizer.merge(parentBuilder, readValue); + assertEquals(readValue, parentBuilder.getAugmentation()); + } + + @Test + public void testReflexCustomizerList() throws Exception { + final ReflexiveListReaderCustomizer<TestingListObject, TestingListObject.TestingListKey, TestingListBuilder> + reflexReaderCustomizer = + new ReflexiveListReaderCustomizer<TestingListObject, TestingListObject.TestingListKey, TestingListBuilder> + (TestingListObject.class, TestingListBuilder.class) { + + @Nonnull + @Override + public List<TestingListObject.TestingListKey> getAllIds( + @Nonnull final InstanceIdentifier<TestingListObject> id, + @Nonnull final ReadContext context) throws ReadFailedException { + return Lists.newArrayList(); + } + }; + + final TestingBuilderParent parentBuilder = new TestingBuilderParent(); + final List<TestingListObject> readValue = Lists.newArrayList(new TestingListObject(), new TestingListObject()); + reflexReaderCustomizer.merge(parentBuilder, readValue); + assertEquals(readValue, parentBuilder.getTestingListObject()); + + final TestingListObject single = new TestingListObject(); + reflexReaderCustomizer.merge(parentBuilder, single); + assertEquals(Collections.singletonList(single), parentBuilder.getTestingListObject()); + } + + static class TestingDataObject implements DataObject { + + @Override + public Class<? extends DataContainer> getImplementedInterface() { + return DataObject.class; + } + } + + static class TestingAugmentation implements DataObject, Augmentation<DataObject> { + + @Override + public Class<? extends DataContainer> getImplementedInterface() { + return DataObject.class; + } + } + + static class TestingListObject implements DataObject, Identifiable<TestingListObject.TestingListKey> { + + @Override + public Class<? extends DataContainer> getImplementedInterface() { + return DataObject.class; + } + + @Override + public TestingListKey getKey() { + return new TestingListKey(); + } + + static class TestingListKey implements Identifier<TestingListObject> { + } + } + + + static class TestingBuilder implements Builder<TestingDataObject> { + + @Override + public TestingDataObject build() { + return new TestingDataObject(); + } + } + + static class TestingAugmentBuilder implements Builder<TestingAugmentation> { + + @Override + public TestingAugmentation build() { + return new TestingAugmentation(); + } + } + + static class TestingListBuilder implements Builder<TestingListObject> { + + @Override + public TestingListObject build() { + return new TestingListObject(); + } + } + + static class TestingBuilderParent implements Builder<DataObject> { + + private TestingDataObject object; + private Augmentation<DataObject> augmentation; + private List<TestingListObject> listObjects; + + public TestingBuilderParent setTestingDataObject(@Nonnull final TestingDataObject object) { + this.object = object; + return this; + } + + public TestingDataObject getTestingDataObject() { + return object; + } + + public TestingBuilderParent setTestingListObject(@Nonnull final List<TestingListObject> listObjects) { + this.listObjects = listObjects; + return this; + } + + public List<TestingListObject> getTestingListObject() { + return listObjects; + } + + public TestingBuilderParent addAugmentation(Class<? extends Augmentation<DataObject>> augmentationType, + Augmentation<DataObject> augmentation) { + this.augmentation = augmentation; + return this; + } + + public Augmentation<DataObject> getAugmentation() { + return augmentation; + } + + @Override + public TestingDataObject build() { + return new TestingDataObject(); + } + } +}
\ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java new file mode 100644 index 000000000..06cb8498f --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderRegistryTest.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.util.read.registry; + +import static io.fd.honeycomb.translate.util.DataObjects.DataObject3; +import static io.fd.honeycomb.translate.util.DataObjects.DataObject3.DataObject31; +import static io.fd.honeycomb.translate.util.DataObjects.DataObject4; +import static io.fd.honeycomb.translate.util.DataObjects.DataObject4.DataObject41; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.Reader; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class CompositeReaderRegistryTest { + + @Mock + private ReadContext ctx; + private CompositeReaderRegistry reg; + private Reader<DataObject31, Builder<DataObject31>> reader31; + private Reader<DataObject3, Builder<DataObject3>> rootReader3; + private Reader<DataObject41, Builder<DataObject41>> reader41; + private Reader<DataObject4, Builder<DataObject4>> rootReader4; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + reader31 = mockReader(DataObject31.class); + rootReader3 = + spy(CompositeReader.createForReader( + mockReader(DataObject3.class), + ImmutableMap.of(DataObject31.class, reader31))); + + reader41 = mockReader(DataObject41.class); + rootReader4 = + spy(CompositeReader.createForReader( + mockReader(DataObject4.class), ImmutableMap.of( + DataObject41.class, reader41))); + + reg = new CompositeReaderRegistry(Lists.newArrayList(rootReader3, rootReader4)); + } + + @Test + public void testReadAll() throws Exception { + reg.readAll(ctx); + + // Invoked according to composite ordering + final InOrder inOrder = inOrder(rootReader3, rootReader4, reader31, reader41); + inOrder.verify(rootReader3).read(any(InstanceIdentifier.class), any(ReadContext.class)); + inOrder.verify(reader31).read(any(InstanceIdentifier.class), any(ReadContext.class)); + inOrder.verify(rootReader4).read(any(InstanceIdentifier.class), any(ReadContext.class)); + inOrder.verify(reader41).read(any(InstanceIdentifier.class), any(ReadContext.class)); + } + + @Test + public void testReadSingleRoot() throws Exception { + reg.read(DataObject3.IID, ctx); + + // Invoked according to composite ordering + final InOrder inOrder = inOrder(rootReader3, rootReader4, reader31, reader41); + inOrder.verify(rootReader3).read(any(InstanceIdentifier.class), any(ReadContext.class)); + inOrder.verify(reader31).read(any(InstanceIdentifier.class), any(ReadContext.class)); + + // Only subtree under DataObject3 should be read + verify(rootReader4, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class)); + verify(reader41, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class)); + } + + @SuppressWarnings("unchecked") + static <D extends DataObject, B extends Builder<D>> Reader<D, B> mockReader(final Class<D> dataType) + throws Exception { + final Reader r = mock(Reader.class); + final Object iid = dataType.getDeclaredField("IID").get(null); + when(r.getManagedDataObjectType()).thenReturn((InstanceIdentifier) iid); + final Builder builder = mock(Builder.class); + when(builder.build()).thenReturn(mock(dataType)); + when(r.getBuilder(any(InstanceIdentifier.class))).thenReturn(builder); + when(r.read(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(Optional.of(mock(dataType))); + return (Reader<D, B>) r; + } +}
\ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java new file mode 100644 index 000000000..9ae036013 --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/registry/CompositeReaderTest.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.util.read.registry; + +import static io.fd.honeycomb.translate.util.DataObjects.DataObject4; +import static io.fd.honeycomb.translate.util.DataObjects.DataObject4.DataObject41; +import static io.fd.honeycomb.translate.util.DataObjects.DataObjectK; +import static io.fd.honeycomb.translate.util.DataObjects.DataObjectKey; +import static io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryTest.mockReader; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import io.fd.honeycomb.translate.read.ListReader; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.Reader; +import io.fd.honeycomb.translate.util.DataObjects; +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.DataObject; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class CompositeReaderTest { + + @Mock + private ReadContext ctx; + private Reader<DataObject41, Builder<DataObject41>> reader41; + private Reader<DataObject4, Builder<DataObject4>> reader4; + private Reader<DataObject4, Builder<DataObject4>> compositeReader; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + reader41 = mockReader(DataObject41.class); + reader4 = mockReader(DataObject4.class); + compositeReader = CompositeReader + .createForReader(reader4, ImmutableMap.of(DataObject41.class, reader41)); + } + + @Test + public void testReadCurrent() throws Exception { + compositeReader.read(DataObject4.IID, ctx); + verify(reader4).readCurrentAttributes(eq(DataObject4.IID), any(Builder.class), eq(ctx)); + verify(reader41).read(DataObject41.IID, ctx); + } + + @Test + public void testReadJustChild() throws Exception { + // Delegating read to child + compositeReader.read(DataObject41.IID, ctx); + verify(reader4, times(0)) + .readCurrentAttributes(any(InstanceIdentifier.class), any(Builder.class), any(ReadContext.class)); + verify(reader41).read(DataObject41.IID, ctx); + } + + @Test + public void testReadFallback() throws Exception { + // Delegating read to delegate as a fallback since IID does not fit, could be handled by the delegate if its + // a subtree handler + compositeReader.read(DataObjects.DataObject4.DataObject42.IID, ctx); + verify(reader4).read(DataObjects.DataObject4.DataObject42.IID, ctx); + verify(reader41, times(0)).read(any(InstanceIdentifier.class), any(ReadContext.class)); + } + + @Test + public void testList() throws Exception { + final Reader<DataObjectK.DataObjectK1, Builder<DataObjectK.DataObjectK1>> readerK1 = + mockReader(DataObjectK.DataObjectK1.class); + final ListReader<DataObjectK, DataObjectKey, Builder<DataObjectK>> readerK = + mockListReader(DataObjectK.class, Lists.newArrayList(new DataObjectKey(), new DataObjectKey())); + final ListReader<DataObjectK, DataObjectKey, Builder<DataObjectK>> + compositeReaderK = (ListReader<DataObjectK, DataObjectKey, Builder<DataObjectK>>) + CompositeReader.createForReader(readerK, ImmutableMap.of(DataObject41.class, readerK1)); + + compositeReaderK.readList(DataObjectK.IID, ctx); + + verify(readerK).getAllIds(DataObjectK.IID, ctx); + verify(readerK, times(2)) + .readCurrentAttributes(any(InstanceIdentifier.class), any(Builder.class), any(ReadContext.class)); + } + + @SuppressWarnings("unchecked") + static <D extends DataObject & Identifiable<K>, K extends Identifier<D>, B extends Builder<D>> ListReader<D, K, B> mockListReader( + final Class<D> dataType, List<K> keys) + throws Exception { + final ListReader r = mock(ListReader.class); + final Object iid = dataType.getDeclaredField("IID").get(null); + when(r.getManagedDataObjectType()).thenReturn((InstanceIdentifier) iid); + final Builder builder = mock(Builder.class); + when(builder.build()).thenReturn(mock(dataType)); + when(r.getBuilder(any(InstanceIdentifier.class))).thenReturn(builder); + when(r.read(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(Optional.of(mock(dataType))); + when(r.getAllIds(any(InstanceIdentifier.class), any(ReadContext.class))).thenReturn(keys); + return (ListReader<D, K, B>) r; + } + +}
\ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/AbstractGenericWriterTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/AbstractGenericWriterTest.java new file mode 100644 index 000000000..14c9c6044 --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/AbstractGenericWriterTest.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.util.write; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.translate.write.WriteFailedException; +import javax.annotation.Nonnull; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class AbstractGenericWriterTest { + + private InstanceIdentifier<DataObject> id = InstanceIdentifier.create(DataObject.class); + @Mock + private DataObject before; + @Mock + private DataObject after; + @Mock + private WriteContext ctx; + private TestingWriter t; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + t = spy(new TestingWriter()); + } + + @Test + public void testDelete() throws Exception { + before = mock(DataObject.class); + t.update(id, before, null, ctx); + + verify(t).deleteCurrentAttributes(id, before, ctx); + } + + @Test + public void testUpdate() throws Exception { + before = mock(DataObject.class); + t.update(id, before, after, ctx); + + verify(t).updateCurrentAttributes(id, before, after, ctx); + } + + @Test + public void testNoUpdate() throws Exception { + before = mock(DataObject.class); + t.update(id, before, before, ctx); + + verify(t, times(0)).updateCurrentAttributes(id, before, after, ctx); + } + + @Test + public void testCreate() throws Exception { + before = mock(DataObject.class); + t.update(id, null, after, ctx); + + verify(t).writeCurrentAttributes(id, after, ctx); + } + + private static class TestingWriter extends AbstractGenericWriter<DataObject> { + + TestingWriter() { + super(InstanceIdentifier.create(DataObject.class)); + } + + @Override + protected void writeCurrentAttributes(@Nonnull final InstanceIdentifier<DataObject> id, + @Nonnull final DataObject data, @Nonnull final WriteContext ctx) + throws WriteFailedException { + + } + + @Override + protected void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<DataObject> id, + @Nonnull final DataObject dataBefore, @Nonnull final WriteContext ctx) + throws WriteFailedException { + + } + + @Override + protected void updateCurrentAttributes(@Nonnull final InstanceIdentifier<DataObject> id, + @Nonnull final DataObject dataBefore, + @Nonnull final DataObject dataAfter, + @Nonnull final WriteContext ctx) throws WriteFailedException { + + } + } + +}
\ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/NoopWriterRegistryTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/NoopWriterRegistryTest.java new file mode 100644 index 000000000..411f24bc3 --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/NoopWriterRegistryTest.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.util.write; + +import org.junit.Test; + +public class NoopWriterRegistryTest { + + @Test + public void testNoop() throws Exception { + final NoopWriterRegistry noopWriterRegistry = new NoopWriterRegistry(); + noopWriterRegistry.update(null, null); + noopWriterRegistry.close(); + } +}
\ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilderTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilderTest.java index 79590a5a0..4f7ab2894 100644 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilderTest.java +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilderTest.java @@ -7,15 +7,22 @@ import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; +import com.google.common.collect.Multimaps; import com.google.common.collect.Sets; import io.fd.honeycomb.translate.util.DataObjects; +import io.fd.honeycomb.translate.write.DataObjectUpdate; +import io.fd.honeycomb.translate.write.WriteContext; import io.fd.honeycomb.translate.write.Writer; +import io.fd.honeycomb.translate.write.registry.WriterRegistry; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.junit.Test; import org.opendaylight.yangtools.yang.binding.DataObject; @@ -23,7 +30,6 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; public class FlatWriterRegistryBuilderTest { - @Test public void testRelationsBefore() throws Exception { final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); @@ -47,6 +53,38 @@ public class FlatWriterRegistryBuilderTest { } @Test + public void testBuild() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + final Writer<? extends DataObject> writer = mockWriter(DataObjects.DataObject3.class); + flatWriterRegistryBuilder.add(writer); + final WriterRegistry build = flatWriterRegistryBuilder.build(); + final InstanceIdentifier<DataObjects.DataObject3> id = InstanceIdentifier.create(DataObjects.DataObject3.class); + final DataObjectUpdate update = mock(DataObjectUpdate.class); + WriterRegistry.DataObjectUpdates updates = new WriterRegistry.DataObjectUpdates( + Multimaps.forMap(Collections.singletonMap(id, update)), + Multimaps.forMap(Collections.emptyMap())); + build.update(updates, mock(WriteContext.class)); + + verify(writer) + .update(any(InstanceIdentifier.class), any(DataObject.class), any(DataObject.class), any(WriteContext.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void testBuildUnknownWriter() throws Exception { + final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); + final Writer<? extends DataObject> writer = mockWriter(DataObjects.DataObject3.class); + flatWriterRegistryBuilder.add(writer); + final WriterRegistry build = flatWriterRegistryBuilder.build(); + + final InstanceIdentifier<DataObjects.DataObject1> id2 = InstanceIdentifier.create(DataObjects.DataObject1.class); + final DataObjectUpdate update2 = mock(DataObjectUpdate.class); + final WriterRegistry.DataObjectUpdates updates = new WriterRegistry.DataObjectUpdates( + Multimaps.forMap(Collections.singletonMap(id2, update2)), + Multimaps.forMap(Collections.emptyMap())); + build.update(updates, mock(WriteContext.class)); + } + + @Test public void testRelationsAfter() throws Exception { final FlatWriterRegistryBuilder flatWriterRegistryBuilder = new FlatWriterRegistryBuilder(); /* @@ -56,7 +94,8 @@ public class FlatWriterRegistryBuilderTest { flatWriterRegistryBuilder.add(mockWriter(DataObjects.DataObject1.class)); flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject2.class), DataObjects.DataObject1.IID); flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject3.class), DataObjects.DataObject2.IID); - flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject4.class), DataObjects.DataObject2.IID); + flatWriterRegistryBuilder.addAfter(mockWriter(DataObjects.DataObject4.class), + Lists.newArrayList(DataObjects.DataObject2.IID, DataObjects.DataObject3.IID)); final ImmutableMap<InstanceIdentifier<?>, Writer<?>> mappedWriters = flatWriterRegistryBuilder.getMappedHandlers(); |