Openstack { public @Nonnull Map<String, List<VolumeSnapshot>> getVolumeSnapshots() { final List<? extends VolumeSnapshot> list = clientProvider.get().blockStorage().snapshots().list(); TreeMap<String, List<VolumeSnapshot>> data = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); for (VolumeSnapshot vs : list) { if (vs.getStatus() != Status.AVAILABLE) { continue; } final String name = Util.fixNull(vs.getName()); final String nameOrId = name.isEmpty() ? vs.getId() : name; List<VolumeSnapshot> sameNamed = data.get(nameOrId); if (sameNamed == null) { sameNamed = new ArrayList<>(); data.putIfAbsent(nameOrId, sameNamed); } sameNamed.add(vs); } for (List<VolumeSnapshot> sameNamed : data.values()) { sameNamed.sort(VOLUMESNAPSHOT_DATE_COMPARATOR); } return data; } 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 getVolumeSnapshotsReturnsVolumeSnapshotsIndexedByNameSortedByAge() { final VolumeSnapshot mockVolumeSnapshotWithNullName = mock(VolumeSnapshot.class); when(mockVolumeSnapshotWithNullName.getId()).thenReturn("mockVolumeSnapshotWithNullNameId"); when(mockVolumeSnapshotWithNullName.getStatus()).thenReturn(Volume.Status.AVAILABLE); 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 VolumeSnapshot mockVolumeSnapshotUnavailable = mock(VolumeSnapshot.class); when(mockVolumeSnapshotUnavailable.getId()).thenReturn("mockVolumeSnapshotUnavailable"); when(mockVolumeSnapshotUnavailable.getName()).thenReturn("ShouldNotBeInResult"); when(mockVolumeSnapshotUnavailable.getCreated()).thenReturn(new Date(123)); when(mockVolumeSnapshotUnavailable.getStatus()).thenReturn(Volume.Status.ATTACHING); final List volumeSnapshots = Arrays.asList(mockVolumeSnapshotNamedBar1, mockVolumeSnapshotWithNullName, mockVolumeSnapshotNamedBar2, mockVolumeSnapshotNamedFoo, mockVolumeSnapshotNamedBar3, mockVolumeSnapshotUnavailable); final OSClient mockClient = mock(OSClient.class, RETURNS_DEEP_STUBS); when(mockClient.blockStorage().snapshots().list()).thenReturn(volumeSnapshots); final Collection<VolumeSnapshot> volumeSnapshots0 = new ArrayList<>( Arrays.asList(mockVolumeSnapshotNamedBar2, mockVolumeSnapshotNamedBar3, mockVolumeSnapshotNamedBar1)); final Collection<VolumeSnapshot> volumeSnapshots1 = new ArrayList<>(Arrays.asList(mockVolumeSnapshotNamedFoo)); final Collection<VolumeSnapshot> volumeSnapshots2 = new ArrayList<>( Arrays.asList(mockVolumeSnapshotWithNullName)); final Openstack instance = new Openstack(mockClient); final Map<String, List<VolumeSnapshot>> actual = instance.getVolumeSnapshots(); final Iterator<Map.Entry<String, List<VolumeSnapshot>>> iterator = actual.entrySet().iterator(); final Map.Entry<String, List<VolumeSnapshot>> entry0 = iterator.next(); assertThat(entry0.getKey(), equalTo("Bar")); assertThat(new ArrayList<>(entry0.getValue()), equalTo(volumeSnapshots0)); final Map.Entry<String, List<VolumeSnapshot>> entry1 = iterator.next(); assertThat(entry1.getKey(), equalTo("Foo")); assertThat(new ArrayList<>(entry1.getValue()), equalTo(volumeSnapshots1)); final Map.Entry<String, List<VolumeSnapshot>> entry2 = iterator.next(); assertThat(entry2.getKey(), equalTo("mockVolumeSnapshotWithNullNameId")); assertThat(new ArrayList<>(entry2.getValue()), equalTo(volumeSnapshots2)); assertThat(iterator.hasNext(), equalTo(false)); }