From e7a8d81ce8ba43384b42a98b0ded5769e89fe1be Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Mon, 12 Sep 2016 13:48:48 +0200 Subject: HONEYCOMB-194 Raise test coverage for translate-api to 80% Change-Id: I6f055ae926aaf876863adb7eddadde0abefdda91 Signed-off-by: Maros Marsalek --- .../honeycomb/translate/TranslationException.java | 10 +-- .../translate/write/DataObjectUpdate.java | 5 +- .../honeycomb/translate/ModificationCacheTest.java | 52 ++++++++++++++ .../translate/write/DataObjectUpdateTest.java | 63 +++++++++++++++++ .../translate/write/WriteFailedExceptionTest.java | 80 ++++++++++++++++++++++ .../write/registry/BulkUpdateExceptionTest.java | 53 ++++++++++++++ .../write/registry/DataObjectUpdatesTest.java | 63 +++++++++++++++++ .../write/registry/RevertFailedExceptionTest.java | 38 ++++++++++ 8 files changed, 356 insertions(+), 8 deletions(-) create mode 100644 infra/translate-api/src/test/java/io/fd/honeycomb/translate/ModificationCacheTest.java create mode 100644 infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/DataObjectUpdateTest.java create mode 100644 infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/WriteFailedExceptionTest.java create mode 100644 infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/BulkUpdateExceptionTest.java create mode 100644 infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/DataObjectUpdatesTest.java create mode 100644 infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/RevertFailedExceptionTest.java (limited to 'infra/translate-api/src') diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/TranslationException.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/TranslationException.java index 6037391e2..1aef7d01c 100644 --- a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/TranslationException.java +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/TranslationException.java @@ -19,17 +19,17 @@ package io.fd.honeycomb.translate; import com.google.common.annotations.Beta; /** - * Base exception for the translation layer + * Base exception for the translation layer. */ @Beta public class TranslationException extends Exception { - public TranslationException(final String s) { - super(s); + public TranslationException(final String message) { + super(message); } - public TranslationException(final String s, final Throwable cause) { - super(s, cause); + public TranslationException(final String message, final Throwable cause) { + super(message, cause); } public TranslationException(final Throwable cause) { diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/write/DataObjectUpdate.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/write/DataObjectUpdate.java index e76d76fcd..583c7b781 100644 --- a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/write/DataObjectUpdate.java +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/write/DataObjectUpdate.java @@ -57,8 +57,8 @@ public class DataObjectUpdate { } public static DataObjectUpdate create(@Nonnull final InstanceIdentifier id, - @Nullable final DataObject dataBefore, - @Nullable final DataObject dataAfter) { + @Nullable final DataObject dataBefore, + @Nullable final DataObject dataAfter) { checkArgument(!(dataBefore == null && dataAfter == null), "Both before and after data are null"); if (dataBefore != null) { checkArgument(id.getTargetType().isAssignableFrom(dataBefore.getClass())); @@ -84,7 +84,6 @@ public class DataObjectUpdate { final DataObjectUpdate that = (DataObjectUpdate) o; return id.equals(that.id); - } @Override diff --git a/infra/translate-api/src/test/java/io/fd/honeycomb/translate/ModificationCacheTest.java b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/ModificationCacheTest.java new file mode 100644 index 000000000..9e6db31e7 --- /dev/null +++ b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/ModificationCacheTest.java @@ -0,0 +1,52 @@ +/* + * 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +public class ModificationCacheTest { + + private ModificationCache cache; + + @Before + public void setUp() throws Exception { + cache = new ModificationCache(); + } + + @Test + public void get() throws Exception { + final Object o = new Object(); + assertNull(cache.get(o)); + assertFalse(cache.containsKey(o)); + + assertNull(cache.put(o, o)); + + assertTrue(cache.containsKey(o)); + assertEquals(o, cache.get(o)); + assertEquals(o, cache.put(o, o)); + + cache.close(); + assertFalse(cache.containsKey(o)); + } + +} \ No newline at end of file diff --git a/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/DataObjectUpdateTest.java b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/DataObjectUpdateTest.java new file mode 100644 index 000000000..73dfadbfb --- /dev/null +++ b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/DataObjectUpdateTest.java @@ -0,0 +1,63 @@ +/* + * 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.write; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +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 DataObjectUpdateTest { + + private InstanceIdentifier id = InstanceIdentifier.create(DataObject.class); + @Mock + private DataObject first; + @Mock + private DataObject second; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testDataObjectUpdate() throws Exception { + final DataObjectUpdate dataObjectUpdate = DataObjectUpdate.create(id, first, second); + assertEquals(id, dataObjectUpdate.getId()); + assertEquals(first, dataObjectUpdate.getDataBefore()); + assertEquals(second, dataObjectUpdate.getDataAfter()); + + final DataObjectUpdate reverse = dataObjectUpdate.reverse(); + // DataObjectUpdate is identifiable only by the ID + assertEquals(dataObjectUpdate, reverse); + assertEquals(dataObjectUpdate.hashCode(), reverse.hashCode()); + + assertEquals(dataObjectUpdate, reverse.reverse()); + } + + @Test + public void testDataObjectDelete() throws Exception { + final DataObjectUpdate dataObjectUpdate = DataObjectUpdate.create(id, first, null); + + assertTrue(DataObjectUpdate.DataObjectDelete.class.isAssignableFrom(dataObjectUpdate.getClass())); + } +} \ No newline at end of file diff --git a/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/WriteFailedExceptionTest.java b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/WriteFailedExceptionTest.java new file mode 100644 index 000000000..6c03b67b3 --- /dev/null +++ b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/WriteFailedExceptionTest.java @@ -0,0 +1,80 @@ +/* + * 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.write; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertThat; + +import org.hamcrest.CoreMatchers; +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 WriteFailedExceptionTest { + + private InstanceIdentifier id = InstanceIdentifier.create(DataObject.class); + @Mock + private DataObject dataAfter; + @Mock + private DataObject dataBefore; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testCreateFailed() throws Exception { + final WriteFailedException.CreateFailedException cause = + new WriteFailedException.CreateFailedException(id, dataAfter); + final WriteFailedException.CreateFailedException createFailedException = + new WriteFailedException.CreateFailedException(id, dataAfter, cause); + + assertEquals(createFailedException.getFailedId(), id); + assertEquals(createFailedException.getData(), dataAfter); + assertEquals(createFailedException.getCause(), cause); + assertThat(createFailedException.getMessage(), CoreMatchers.containsString("Failed to create")); + } + + @Test + public void testUpdateFailed() throws Exception { + final WriteFailedException.UpdateFailedException cause = + new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter); + final WriteFailedException.UpdateFailedException createFailedException = + new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, cause); + + assertEquals(createFailedException.getFailedId(), id); + assertEquals(createFailedException.getDataBefore(), dataBefore); + assertEquals(createFailedException.getDataAfter(), dataAfter); + assertEquals(createFailedException.getCause(), cause); + assertThat(createFailedException.getMessage(), CoreMatchers.containsString("Failed to update")); + } + + @Test + public void testDeleteFailed() throws Exception { + final WriteFailedException.DeleteFailedException cause = new WriteFailedException.DeleteFailedException(id); + final WriteFailedException.DeleteFailedException createFailedException = + new WriteFailedException.DeleteFailedException(id, cause); + + assertEquals(createFailedException.getFailedId(), id); + assertEquals(createFailedException.getCause(), cause); + assertThat(createFailedException.getMessage(), CoreMatchers.containsString("Failed to delete")); + } +} \ No newline at end of file diff --git a/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/BulkUpdateExceptionTest.java b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/BulkUpdateExceptionTest.java new file mode 100644 index 000000000..c1880ca26 --- /dev/null +++ b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/BulkUpdateExceptionTest.java @@ -0,0 +1,53 @@ +/* + * 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.write.registry; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.verify; + +import com.google.common.collect.Sets; +import java.util.HashSet; +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 BulkUpdateExceptionTest { + + private InstanceIdentifier id = InstanceIdentifier.create(DataObject.class); + @Mock + private WriterRegistry.Reverter reverter; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testRevert() throws Exception { + final HashSet> failedIds = Sets.newHashSet(id); + final WriterRegistry.BulkUpdateException bulkUpdateException = + new WriterRegistry.BulkUpdateException(failedIds, reverter, new RuntimeException()); + + assertEquals(failedIds, bulkUpdateException.getFailedIds()); + + bulkUpdateException.revertChanges(); + verify(reverter).revert(); + } +} \ No newline at end of file diff --git a/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/DataObjectUpdatesTest.java b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/DataObjectUpdatesTest.java new file mode 100644 index 000000000..2c576eb6b --- /dev/null +++ b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/DataObjectUpdatesTest.java @@ -0,0 +1,63 @@ +/* + * 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.write.registry; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimaps; +import com.google.common.collect.SetMultimap; +import io.fd.honeycomb.translate.write.DataObjectUpdate; +import java.util.Collections; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class DataObjectUpdatesTest { + + @Mock + private DataObjectUpdate update1; + @Mock + private DataObjectUpdate.DataObjectDelete update2; + private InstanceIdentifier id = InstanceIdentifier.create(DataObject.class); + + @Test + public void testUpdates() throws Exception { + final SetMultimap, DataObjectUpdate> ups = + Multimaps.forMap(Collections.singletonMap(id, update1)); + final SetMultimap, DataObjectUpdate.DataObjectDelete> dels = + Multimaps.forMap(Collections.singletonMap(id, update2)); + + final WriterRegistry.DataObjectUpdates updates = new WriterRegistry.DataObjectUpdates(ups, dels); + + assertSame(ups, updates.getUpdates()); + assertSame(dels, updates.getDeletes()); + assertTrue(updates.containsOnlySingleType()); + assertThat(updates.getTypeIntersection().size(), is(1)); + assertThat(updates.getTypeIntersection(), hasItem(id)); + assertFalse(updates.isEmpty()); + + assertTrue(updates.equals(updates)); + assertFalse(updates.equals(new WriterRegistry.DataObjectUpdates(HashMultimap.create(), HashMultimap.create()))); + } +} \ No newline at end of file diff --git a/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/RevertFailedExceptionTest.java b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/RevertFailedExceptionTest.java new file mode 100644 index 000000000..721941d1e --- /dev/null +++ b/infra/translate-api/src/test/java/io/fd/honeycomb/translate/write/registry/RevertFailedExceptionTest.java @@ -0,0 +1,38 @@ +/* + * 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.write.registry; + +import static org.junit.Assert.assertEquals; + +import com.google.common.collect.Sets; +import java.util.Set; +import org.junit.Test; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class RevertFailedExceptionTest { + + private InstanceIdentifier id = InstanceIdentifier.create(DataObject.class); + + @Test + public void testNonRevert() throws Exception { + final Set> notReverted = Sets.newHashSet(id); + final WriterRegistry.Reverter.RevertFailedException revertFailedException = + new WriterRegistry.Reverter.RevertFailedException(notReverted, new RuntimeException()); + assertEquals(notReverted, revertFailedException.getNotRevertedChanges()); + } +} \ No newline at end of file -- cgit 1.2.3-korg