@Test public void testGetHostAffinity() throws Throwable { VmEntity vm = new VmEntity(); vm.setAffinities(null); assertThat(vm.getHostAffinity(), nullValue()); vm.setAffinities(new ArrayList<LocalityEntity>()); LocalityEntity localityEntity1 = new LocalityEntity(); localityEntity1.setKind(Host.KIND); localityEntity1.setResourceId("host-id1"); vm.setAffinities(ImmutableList.of(localityEntity1)); assertThat(vm.getHostAffinity(), is("host-id1")); LocalityEntity localityEntity2 = new LocalityEntity(); localityEntity2.setKind(Host.KIND); localityEntity2.setResourceId("host-id2"); vm.setAffinities(ImmutableList.of(localityEntity1, localityEntity2)); try { vm.getHostAffinity(); fail("should have failed with MoreThanOneHostAffinityException"); } catch (MoreThanOneHostAffinityException e) { } }
public String getHostAffinity() throws MoreThanOneHostAffinityException { if (affinities == null) { return null; } boolean isHostAffinityPresent = false; String hostAffinity = null; for (LocalityEntity affinity : affinities) { if (java.util.Objects.equals(affinity.getKind(), Host.KIND)) { if (isHostAffinityPresent) { throw new MoreThanOneHostAffinityException(); } hostAffinity = affinity.getResourceId(); isHostAffinityPresent = true; } } return hostAffinity; }
VmEntity extends InfrastructureEntity { public String getHostAffinity() throws MoreThanOneHostAffinityException { if (affinities == null) { return null; } boolean isHostAffinityPresent = false; String hostAffinity = null; for (LocalityEntity affinity : affinities) { if (java.util.Objects.equals(affinity.getKind(), Host.KIND)) { if (isHostAffinityPresent) { throw new MoreThanOneHostAffinityException(); } hostAffinity = affinity.getResourceId(); isHostAffinityPresent = true; } } return hostAffinity; } }
VmEntity extends InfrastructureEntity { public String getHostAffinity() throws MoreThanOneHostAffinityException { if (affinities == null) { return null; } boolean isHostAffinityPresent = false; String hostAffinity = null; for (LocalityEntity affinity : affinities) { if (java.util.Objects.equals(affinity.getKind(), Host.KIND)) { if (isHostAffinityPresent) { throw new MoreThanOneHostAffinityException(); } hostAffinity = affinity.getResourceId(); isHostAffinityPresent = true; } } return hostAffinity; }  }
VmEntity extends InfrastructureEntity { public String getHostAffinity() throws MoreThanOneHostAffinityException { if (affinities == null) { return null; } boolean isHostAffinityPresent = false; String hostAffinity = null; for (LocalityEntity affinity : affinities) { if (java.util.Objects.equals(affinity.getKind(), Host.KIND)) { if (isHostAffinityPresent) { throw new MoreThanOneHostAffinityException(); } hostAffinity = affinity.getResourceId(); isHostAffinityPresent = true; } } return hostAffinity; }  @Override String getKind(); List<AttachedDiskEntity> getAttachedDisks(); void setAttachedDisks(List<AttachedDiskEntity> attachedDisks); void removeAttachedDisks(List<AttachedDiskEntity> attachedDisks); void addAttachedDisk(AttachedDiskEntity attachedDisk); void removeAttachedDisk(AttachedDiskEntity attachedDisk); List<LocalityEntity> getAffinities(); void setAffinities(List<LocalityEntity> affinities); List<String> getAffinities(String kind); String getHostAffinity(); Map<String, String> getMetadata(); void setMetadata(Map<String, String> metadata); List<String> getNetworks(); void setNetworks(List<String> networks); String getAgent(); void setAgent(String agent); Map<String, String> getEnvironment(); void setEnvironment(Map<String, String> environment); VmState getState(); void setState(VmState state); String getDefaultGateway(); void setDefaultGateway(String defaultGateway); String getDatastore(); void setDatastore(String datastore); String getDatastoreName(); void setDatastoreName(String datastoreName); String getImageId(); void setImageId(String imageId); String getHost(); void setHost(String host); List<IsoEntity> getIsos(); void setIsos(List<IsoEntity> isos); void addIso(IsoEntity iso); void removeIso(IsoEntity iso); List<Throwable> getWarnings(); void setWarnings(List<Throwable> warnings); void addWarning(Throwable warning); String getFloatingIp(); void setFloatingIp(String floatingIp); void setHasMACAddresses(boolean hasMACAddresses); boolean getHasMACAddresses(); String buildVmFolderPath(); }
VmEntity extends InfrastructureEntity { public String getHostAffinity() throws MoreThanOneHostAffinityException { if (affinities == null) { return null; } boolean isHostAffinityPresent = false; String hostAffinity = null; for (LocalityEntity affinity : affinities) { if (java.util.Objects.equals(affinity.getKind(), Host.KIND)) { if (isHostAffinityPresent) { throw new MoreThanOneHostAffinityException(); } hostAffinity = affinity.getResourceId(); isHostAffinityPresent = true; } } return hostAffinity; }  @Override String getKind(); List<AttachedDiskEntity> getAttachedDisks(); void setAttachedDisks(List<AttachedDiskEntity> attachedDisks); void removeAttachedDisks(List<AttachedDiskEntity> attachedDisks); void addAttachedDisk(AttachedDiskEntity attachedDisk); void removeAttachedDisk(AttachedDiskEntity attachedDisk); List<LocalityEntity> getAffinities(); void setAffinities(List<LocalityEntity> affinities); List<String> getAffinities(String kind); String getHostAffinity(); Map<String, String> getMetadata(); void setMetadata(Map<String, String> metadata); List<String> getNetworks(); void setNetworks(List<String> networks); String getAgent(); void setAgent(String agent); Map<String, String> getEnvironment(); void setEnvironment(Map<String, String> environment); VmState getState(); void setState(VmState state); String getDefaultGateway(); void setDefaultGateway(String defaultGateway); String getDatastore(); void setDatastore(String datastore); String getDatastoreName(); void setDatastoreName(String datastoreName); String getImageId(); void setImageId(String imageId); String getHost(); void setHost(String host); List<IsoEntity> getIsos(); void setIsos(List<IsoEntity> isos); void addIso(IsoEntity iso); void removeIso(IsoEntity iso); List<Throwable> getWarnings(); void setWarnings(List<Throwable> warnings); void addWarning(Throwable warning); String getFloatingIp(); void setFloatingIp(String floatingIp); void setHasMACAddresses(boolean hasMACAddresses); boolean getHasMACAddresses(); String buildVmFolderPath();  }