Openstack { public @Nonnull List<String> getVolumeSnapshotIdsFor(String nameOrId) { final Collection<VolumeSnapshot> sortedObjects = new TreeSet<>(VOLUMESNAPSHOT_DATE_COMPARATOR); final Map<String, List<VolumeSnapshot>> allVolumeSnapshots = getVolumeSnapshots(); final Collection<VolumeSnapshot> findByName = allVolumeSnapshots.get(nameOrId); if (findByName != null) { sortedObjects.addAll(findByName); } if (nameOrId.matches("[0-9a-f-]{36}")) { final VolumeSnapshot findById = clientProvider.get().blockStorage().snapshots().get(nameOrId); if (findById != null && findById.getStatus() == Status.AVAILABLE) { sortedObjects.add(findById); } } final List<String> ids = new ArrayList<>(); for (VolumeSnapshot i : sortedObjects) { ids.add(i.getId()); } return ids; } private  Openstack(@Nonnull String endPointUrl, boolean ignoreSsl, @Nonnull OpenstackCredential auth, @CheckForNull String region);  Openstack(@Nonnull final OSClient<?> client); static @Nonnull String getFlavorInfo(@Nonnull Flavor f); @Nonnull String getInfo(); @VisibleForTesting @Nonnull List<? extends Network> _listNetworks(); @Nonnull Map<String, Network> getNetworks(@Nonnull List<String> nameOrIds); @Nonnull Map<Network, Integer> getNetworksCapacity(@Nonnull Map<String, Network> declaredNetworks); @Nonnull Map<String, List<Image>> getImages(); @Nonnull Map<String, List<VolumeSnapshot>> getVolumeSnapshots(); @Nonnull Collection<? extends Flavor> getSortedFlavors(); @Nonnull List<String> getSortedIpPools(); @Nonnull List<? extends AvailabilityZone> getAvailabilityZones(); @Nonnull List<Server> getRunningNodes(); @Nonnull List<String> getFreeFipIds(); @Nonnull List<String> getSortedKeyPairNames(); @Nonnull List<String> getImageIdsFor(String nameOrId); @Nonnull List<String> getVolumeSnapshotIdsFor(String nameOrId); @CheckForNull String getVolumeSnapshotDescription(String volumeSnapshotId); void setVolumeNameAndDescription(String volumeId, String newVolumeName, String newVolumeDescription); static boolean isOccupied(@Nonnull Server server); @Nonnull Server getServerById(@Nonnull String id); @Nonnull List<Server> getServersByName(@Nonnull String name); @Nonnull Server bootAndWaitActive(@Nonnull ServerCreateBuilder request, @Nonnegative int timeout); @Restricted(NoExternalUse.class) // Test hook Server _bootAndWaitActive(@Nonnull ServerCreateBuilder request, @Nonnegative int timeout); @Nonnull Server updateInfo(@Nonnull Server server); void destroyServer(@Nonnull Server server); @Nonnull NetFloatingIP assignFloatingIp(@Nonnull Server server, @Nonnull String poolName); void destroyFip(String fip); static @CheckForNull String getAccessIpAddress(@Nonnull Server server); @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT") @CheckForNull Throwable sanityCheck(); static final String FINGERPRINT_KEY; }
@Test public void getVolumeSnapshotIdsForGivenNameThenReturnsMatchingVolumeSnapshotIdsSortedByAge() { final VolumeSnapshot mockVolumeSnapshotNamedFoo = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedFoo.getId()).thenReturn("mockVolumeSnapshotNamedFooId"); when(mockVolumeSnapshotNamedFoo.getName()).thenReturn("Foo"); when(mockVolumeSnapshotNamedFoo.getStatus()).thenReturn(Volume.Status.AVAILABLE); final VolumeSnapshot mockVolumeSnapshotNamedBar1 = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedBar1.getId()).thenReturn("mockVolumeSnapshotNamedBar1Id"); when(mockVolumeSnapshotNamedBar1.getName()).thenReturn("Bar"); when(mockVolumeSnapshotNamedBar1.getCreated()).thenReturn(new Date(11111)); when(mockVolumeSnapshotNamedBar1.getStatus()).thenReturn(Volume.Status.AVAILABLE); final VolumeSnapshot mockVolumeSnapshotNamedBar2 = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedBar2.getId()).thenReturn("mockVolumeSnapshotNamedBar2Id"); when(mockVolumeSnapshotNamedBar2.getName()).thenReturn("Bar"); when(mockVolumeSnapshotNamedBar2.getCreated()).thenReturn(new Date(10000)); when(mockVolumeSnapshotNamedBar2.getStatus()).thenReturn(Volume.Status.AVAILABLE); final VolumeSnapshot mockVolumeSnapshotNamedBar3 = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedBar3.getId()).thenReturn("mockVolumeSnapshotNamedBar3Id"); when(mockVolumeSnapshotNamedBar3.getName()).thenReturn("Bar"); when(mockVolumeSnapshotNamedBar3.getCreated()).thenReturn(new Date(11110)); when(mockVolumeSnapshotNamedBar3.getStatus()).thenReturn(Volume.Status.AVAILABLE); final BlockVolumeSnapshotService mockBVSS = mock(BlockVolumeSnapshotService.class); final List volumeSnapshots = Arrays.asList(mockVolumeSnapshotNamedFoo, mockVolumeSnapshotNamedBar1, mockVolumeSnapshotNamedBar2, mockVolumeSnapshotNamedBar3); when(mockBVSS.list()).thenReturn(volumeSnapshots); final OSClient mockClient = mock(OSClient.class, RETURNS_DEEP_STUBS); when(mockClient.blockStorage().snapshots()).thenReturn(mockBVSS); final ArrayList<String> expected = new ArrayList<>(Arrays.asList("mockVolumeSnapshotNamedBar2Id", "mockVolumeSnapshotNamedBar3Id", "mockVolumeSnapshotNamedBar1Id")); final Openstack instance = new Openstack(mockClient); final List<String> actual = instance.getVolumeSnapshotIdsFor("Bar"); final Map<String, String> expectedFilteringParams = new HashMap<>(2); expectedFilteringParams.put("name", "Bar"); expectedFilteringParams.put("status", "active"); verify(mockBVSS).list(); verifyNoMoreInteractions(mockBVSS); assertThat(new ArrayList<>(actual), equalTo(expected)); }
@Test public void getVolumeSnapshotIdsForGivenUnknownThenReturnsEmpty() { final VolumeSnapshot mockVolumeSnapshotNamedFoo = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedFoo.getId()).thenReturn("mockVolumeSnapshotNamedFooId"); when(mockVolumeSnapshotNamedFoo.getName()).thenReturn("Foo"); when(mockVolumeSnapshotNamedFoo.getStatus()).thenReturn(Volume.Status.AVAILABLE); final VolumeSnapshot mockVolumeSnapshotNamedBar1 = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedBar1.getId()).thenReturn("mockVolumeSnapshotNamedBar1Id"); when(mockVolumeSnapshotNamedBar1.getName()).thenReturn("Bar"); when(mockVolumeSnapshotNamedBar1.getCreated()).thenReturn(new Date(11111)); when(mockVolumeSnapshotNamedBar1.getStatus()).thenReturn(Volume.Status.AVAILABLE); final VolumeSnapshot mockVolumeSnapshotNamedBar2 = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedBar2.getId()).thenReturn("mockVolumeSnapshotNamedBar2Id"); when(mockVolumeSnapshotNamedBar2.getName()).thenReturn("Bar"); when(mockVolumeSnapshotNamedBar2.getCreated()).thenReturn(new Date(10000)); when(mockVolumeSnapshotNamedBar2.getStatus()).thenReturn(Volume.Status.AVAILABLE); final VolumeSnapshot mockVolumeSnapshotNamedBar3 = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedBar3.getId()).thenReturn("mockVolumeSnapshotNamedBar3Id"); when(mockVolumeSnapshotNamedBar3.getName()).thenReturn("Bar"); when(mockVolumeSnapshotNamedBar3.getCreated()).thenReturn(new Date(11110)); when(mockVolumeSnapshotNamedBar3.getStatus()).thenReturn(Volume.Status.AVAILABLE); final BlockVolumeSnapshotService mockBVSS = mock(BlockVolumeSnapshotService.class); final List volumeSnapshots = Arrays.asList(mockVolumeSnapshotNamedFoo, mockVolumeSnapshotNamedBar1, mockVolumeSnapshotNamedBar2, mockVolumeSnapshotNamedBar3); when(mockBVSS.list()).thenReturn(volumeSnapshots); final OSClient mockClient = mock(OSClient.class, RETURNS_DEEP_STUBS); when(mockClient.blockStorage().snapshots()).thenReturn(mockBVSS); final ArrayList<String> expected = new ArrayList<>(); final Openstack instance = new Openstack(mockClient); final List<String> actual = instance.getVolumeSnapshotIdsFor("NameNotFound"); verify(mockBVSS).list(); verifyNoMoreInteractions(mockBVSS); assertThat(new ArrayList<>(actual), equalTo(expected)); }
@Test public void getVolumeSnapshotIdsForGivenIdOfActiveVolumeSnapshotThenReturnsId() { final VolumeSnapshot mockVolumeSnapshotNamedFoo = mock(VolumeSnapshot.class); final String volumeSnapshotId = "cfd083b4-2422-4c5f-bf61-d975709375ab"; when(mockVolumeSnapshotNamedFoo.getId()).thenReturn(volumeSnapshotId); when(mockVolumeSnapshotNamedFoo.getName()).thenReturn("Foo"); when(mockVolumeSnapshotNamedFoo.getStatus()).thenReturn(Volume.Status.AVAILABLE); final VolumeSnapshot mockVolumeSnapshotNamedBar1 = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedBar1.getId()).thenReturn("mockVolumeSnapshotNamedBar1Id"); when(mockVolumeSnapshotNamedBar1.getName()).thenReturn("Bar"); when(mockVolumeSnapshotNamedBar1.getCreated()).thenReturn(new Date(11111)); when(mockVolumeSnapshotNamedBar1.getStatus()).thenReturn(Volume.Status.AVAILABLE); final VolumeSnapshot mockVolumeSnapshotNamedBar2 = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedBar2.getId()).thenReturn("mockVolumeSnapshotNamedBar2Id"); when(mockVolumeSnapshotNamedBar2.getName()).thenReturn("Bar"); when(mockVolumeSnapshotNamedBar2.getCreated()).thenReturn(new Date(10000)); when(mockVolumeSnapshotNamedBar2.getStatus()).thenReturn(Volume.Status.AVAILABLE); final VolumeSnapshot mockVolumeSnapshotNamedBar3 = mock(VolumeSnapshot.class); when(mockVolumeSnapshotNamedBar3.getId()).thenReturn("mockVolumeSnapshotNamedBar3Id"); when(mockVolumeSnapshotNamedBar3.getName()).thenReturn("Bar"); when(mockVolumeSnapshotNamedBar3.getCreated()).thenReturn(new Date(11110)); when(mockVolumeSnapshotNamedBar3.getStatus()).thenReturn(Volume.Status.AVAILABLE); final List volumeSnapshots = Arrays.asList(mockVolumeSnapshotNamedFoo, mockVolumeSnapshotNamedBar1, mockVolumeSnapshotNamedBar2, mockVolumeSnapshotNamedBar3); final BlockVolumeSnapshotService mockBVSS = mock(BlockVolumeSnapshotService.class); when(mockBVSS.list()).thenReturn(volumeSnapshots); when(mockBVSS.get(volumeSnapshotId)).thenReturn(mockVolumeSnapshotNamedFoo); final OSClient mockClient = mock(OSClient.class, RETURNS_DEEP_STUBS); when(mockClient.blockStorage().snapshots()).thenReturn(mockBVSS); final ArrayList<String> expected = new ArrayList<>(Arrays.asList(mockVolumeSnapshotNamedFoo.getId())); final Openstack instance = new Openstack(mockClient); final List<String> actual = instance.getVolumeSnapshotIdsFor(volumeSnapshotId); verify(mockBVSS).list(); verify(mockBVSS).get(volumeSnapshotId); verifyNoMoreInteractions(mockBVSS); assertThat(new ArrayList<>(actual), equalTo(expected)); }