Openstack { public @Nonnull Map<String, List<Image>> getImages() { final List<? extends Image> list = getAllImages(); TreeMap<String, List<Image>> data = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); for (Image image : list) { final String name = Util.fixNull(image.getName()); final String nameOrId = name.isEmpty() ? image.getId() : name; List<Image> sameNamed = data.get(nameOrId); if (sameNamed == null) { sameNamed = new ArrayList<>(); data.putIfAbsent(nameOrId, sameNamed); } sameNamed.add(image); } for (List<Image> sameNamed : data.values()) { sameNamed.sort(IMAGE_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 getImagesReturnsImagesIndexedByNameSortedByAge() { final Image mockImageWithNullName = mock(Image.class); when(mockImageWithNullName.getId()).thenReturn("mockImageWithNullNameId"); final Image mockImageNamedFoo = mock(Image.class); when(mockImageNamedFoo.getId()).thenReturn("mockImageNamedFooId"); when(mockImageNamedFoo.getName()).thenReturn("Foo"); final Image mockImageNamedBar1 = mock(Image.class); when(mockImageNamedBar1.getId()).thenReturn("mockImageNamedBar1Id"); when(mockImageNamedBar1.getName()).thenReturn("Bar"); when(mockImageNamedBar1.getUpdatedAt()).thenReturn(new Date(11111)); when(mockImageNamedBar1.getCreatedAt()).thenReturn(new Date(1111)); final Image mockImageNamedBar2 = mock(Image.class); when(mockImageNamedBar2.getId()).thenReturn("mockImageNamedBar2Id"); when(mockImageNamedBar2.getName()).thenReturn("Bar"); when(mockImageNamedBar2.getUpdatedAt()).thenReturn(new Date(10000)); when(mockImageNamedBar2.getCreatedAt()).thenReturn(new Date(1111)); final Image mockImageNamedBar3 = mock(Image.class); when(mockImageNamedBar3.getId()).thenReturn("mockImageNamedBar3Id"); when(mockImageNamedBar3.getName()).thenReturn("Bar"); when(mockImageNamedBar3.getUpdatedAt()).thenReturn(new Date(11111)); when(mockImageNamedBar3.getCreatedAt()).thenReturn(new Date(1000)); final List images = Arrays.asList(mockImageNamedBar1, mockImageWithNullName, mockImageNamedBar2, mockImageNamedFoo, mockImageNamedBar3); final OSClient mockClient = mock(OSClient.class, RETURNS_DEEP_STUBS); when(mockClient.imagesV2().list(any())).thenReturn(images); final Collection<Image> images0 = new ArrayList<>( Arrays.asList(mockImageNamedBar2, mockImageNamedBar3, mockImageNamedBar1)); final Collection<Image> images1 = new ArrayList<>(Arrays.asList(mockImageNamedFoo)); final Collection<Image> images2 = new ArrayList<>(Arrays.asList(mockImageWithNullName)); final Openstack instance = new Openstack(mockClient); final Map<String, List<Image>> actual = instance.getImages(); final Iterator<Map.Entry<String, List<Image>>> iterator = actual.entrySet().iterator(); final Map.Entry<String, List<Image>> entry0 = iterator.next(); assertThat(entry0.getKey(), equalTo("Bar")); assertThat(new ArrayList<>(entry0.getValue()), equalTo(images0)); final Map.Entry<String, List<Image>> entry1 = iterator.next(); assertThat(entry1.getKey(), equalTo("Foo")); assertThat(new ArrayList<>(entry1.getValue()), equalTo(images1)); final Map.Entry<String, List<Image>> entry2 = iterator.next(); assertThat(entry2.getKey(), equalTo("mockImageWithNullNameId")); assertThat(new ArrayList<>(entry2.getValue()), equalTo(images2)); assertThat(iterator.hasNext(), equalTo(false)); }