Martin Mucha has uploaded a new change for review. Change subject: core: refactored NetworkAttachmentCompleter + tests. ......................................................................
core: refactored NetworkAttachmentCompleter + tests. • added tests • unfied implementation so both methods uses BusinessEntityMap Change-Id: I933333afdcb11bfcf9f761572229f7a7a7814381 Signed-off-by: Martin Mucha <mmu...@redhat.com> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/NetworkAttachmentCompleter.java A backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/NetworkAttachmentCompleterTest.java M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/BusinessEntityMap.java 3 files changed, 119 insertions(+), 12 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/42/36142/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/NetworkAttachmentCompleter.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/NetworkAttachmentCompleter.java index d94b5bf..44ddae5 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/NetworkAttachmentCompleter.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/NetworkAttachmentCompleter.java @@ -2,14 +2,11 @@ import java.util.Collections; import java.util.List; -import java.util.Map; import org.ovirt.engine.core.common.businessentities.BusinessEntityMap; -import org.ovirt.engine.core.common.businessentities.Entities; import org.ovirt.engine.core.common.businessentities.network.Bond; import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment; import org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface; -import org.ovirt.engine.core.compat.Guid; /** * Network interface can be provided as a parameter and identified either by ID or by name.<br> @@ -18,10 +15,10 @@ */ public class NetworkAttachmentCompleter { - private List<VdsNetworkInterface> existingNics; + private BusinessEntityMap<VdsNetworkInterface> existingNicsMap; public NetworkAttachmentCompleter(List<VdsNetworkInterface> existingNics) { - this.existingNics = existingNics; + existingNicsMap = new BusinessEntityMap<>(existingNics); } public void completeNicName(NetworkAttachment attachment) { @@ -29,9 +26,15 @@ } public void completeNicNames(List<NetworkAttachment> networkAttachments) { - BusinessEntityMap<VdsNetworkInterface> nics = new BusinessEntityMap<VdsNetworkInterface>(existingNics); for (NetworkAttachment attachment : networkAttachments) { - VdsNetworkInterface nic = nics.get(attachment.getNicId(), attachment.getNicName()); + + /*TODO MM: if we're are able to find nic by its name stored in attachment, + then the name probably will be correct and need not to be updated. + Am I missing something or can we just search for nics by nic id? + + follow-up-todo: if that's possible, then rewrite following code, so it read same as completeBondNames + */ + VdsNetworkInterface nic = existingNicsMap.get(attachment.getNicId(), attachment.getNicName()); if (nic != null) { attachment.setNicName(nic.getName()); } @@ -39,10 +42,9 @@ } public void completeBondNames(List<Bond> bonds) { - Map<Guid, VdsNetworkInterface> nicsById = Entities.businessEntitiesById(existingNics); for (Bond bond : bonds) { - if (bond.getName() == null && nicsById.containsKey(bond.getId())) { - bond.setName(nicsById.get(bond.getId()).getName()); + if (bond.getName() == null && existingNicsMap.containsKey(bond.getId())) { + bond.setName(existingNicsMap.get(bond.getId()).getName()); } } } diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/NetworkAttachmentCompleterTest.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/NetworkAttachmentCompleterTest.java new file mode 100644 index 0000000..faeacf9 --- /dev/null +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/NetworkAttachmentCompleterTest.java @@ -0,0 +1,105 @@ +package org.ovirt.engine.core.bll.network.host; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.ovirt.engine.core.common.businessentities.network.Bond; +import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment; +import org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface; +import org.ovirt.engine.core.compat.Guid; + +public class NetworkAttachmentCompleterTest { + private VdsNetworkInterface nic; + private NetworkAttachmentCompleter completer; + + /* + • complete by nicId + • complete by nic name + • nothing is completed when nicId nor nicname is not sufficient to find nic + • + */ + + + @Before + public void setUp() throws Exception { + nic = new VdsNetworkInterface(); + nic.setId(Guid.newGuid()); + nic.setName("existingNic"); + + List<VdsNetworkInterface> nics = Arrays.asList(nic); + completer = new NetworkAttachmentCompleter(nics); + } + + @Test + public void testCompleteNetworkAttachmentsNicName() throws Exception { + NetworkAttachment uncompletableNetworkAttachment = Mockito.mock(NetworkAttachment.class); + completer.completeNicName(uncompletableNetworkAttachment); + verify(uncompletableNetworkAttachment, never()).setNicName(anyString()); + } + + @Test + public void testCompleteNetworkAttachmentsNicNameWhenNoRelevantNicExist() throws Exception { + NetworkAttachment networkAttachment = Mockito.mock(NetworkAttachment.class); + Mockito.when(networkAttachment.getNicId()).thenReturn(Guid.newGuid()); + Mockito.when(networkAttachment.getNicName()).thenReturn("notAExistingNicName"); + + completer.completeNicName(networkAttachment); + verify(networkAttachment, never()).setNicName(anyString()); + } + + @Test + public void testCompleteNetworkAttachmentsNicNameCompletedByNicId() throws Exception { + NetworkAttachment networkAttachmentWithId = new NetworkAttachment(); + networkAttachmentWithId.setNicId(nic.getId()); + + completer.completeNicName(networkAttachmentWithId); + assertThat(networkAttachmentWithId.getNicName(), is(nic.getName())); + } +/* + @Test + public void testCompleteNetworkAttachmentsNicNameCompletedByNicName() throws Exception { + NetworkAttachment networkAttachmentWithId = new NetworkAttachment(); + networkAttachmentWithId.setNicName(nic.getName()); + + completer.completeNicName(networkAttachmentWithId); + assertThat(networkAttachmentWithId.getNicName(), is(nic.getName())); + }*/ + + @Test + public void testCompleteBondName() throws Exception { + + Bond uncompletableBond = Mockito.mock(Bond.class); + completer.completeBondNames(Collections.singletonList(uncompletableBond)); + verify(uncompletableBond, never()).setName(anyString()); + } + + + @Test + public void testCompleteBondNameWhenNoRelevantNicExist() throws Exception { + Bond bond = Mockito.mock(Bond.class); + Mockito.when(bond.getId()).thenReturn(Guid.newGuid()); + + completer.completeBondNames(Collections.singletonList(bond)); + verify(bond, never()).setName(anyString()); + } + + @Test + public void testCompleteBondNameCompletedByBondId() throws Exception { + Bond bond = new Bond(); + bond.setId(nic.getId()); + + completer.completeBondNames(Collections.singletonList(bond)); + assertThat(bond.getName(), is(nic.getName())); + } + +} diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/BusinessEntityMap.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/BusinessEntityMap.java index 7fa5d22..ef77c41 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/BusinessEntityMap.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/BusinessEntityMap.java @@ -20,8 +20,8 @@ } public BusinessEntityMap(Collection<E> entities) { - entitiesByName = new HashMap<String, E>(); - entitiesById = new HashMap<Guid, E>(); + entitiesByName = new HashMap<>(); + entitiesById = new HashMap<>(); for (E e : entities) { if (e.getName() != null) { -- To view, visit http://gerrit.ovirt.org/36142 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I933333afdcb11bfcf9f761572229f7a7a7814381 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Martin Mucha <mmu...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches