Openstack { public @Nonnull List<String> getImageIdsFor(String nameOrId) { final Collection<Image> sortedObjects = new TreeSet<>(IMAGE_DATE_COMPARATOR); final Map<String, String> query = new HashMap<>(2); query.put("name", nameOrId); query.put("status", "active"); final List<? extends Image> findByName = clientProvider.get().imagesV2().list(query); sortedObjects.addAll(findByName); if (nameOrId.matches("[0-9a-f-]{36}")) { final Image findById = clientProvider.get().imagesV2().get(nameOrId); if (findById != null && findById.getStatus() == Image.ImageStatus.ACTIVE) { sortedObjects.add(findById); } } final List<String> ids = new ArrayList<>(); for (Image 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 getImageIdsForGivenNameThenReturnsMatchingImageIdsSortedByAge() { 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 ImageService mockIS = mock(ImageService.class); final List images = Arrays.asList(mockImageNamedBar1, mockImageNamedBar2, mockImageNamedBar3); when(mockIS.list(anyMapOf(String.class, String.class))).thenReturn(images); final ArrayList<String> expected = new ArrayList<>( Arrays.asList("mockImageNamedBar2Id", "mockImageNamedBar3Id", "mockImageNamedBar1Id")); final OSClient mockClient = mock(OSClient.class); when(mockClient.imagesV2()).thenReturn(mockIS); final Openstack instance = new Openstack(mockClient); final List<String> actual = instance.getImageIdsFor("Bar"); final Map<String, String> expectedFilteringParams = new HashMap<>(2); expectedFilteringParams.put("name", "Bar"); expectedFilteringParams.put("status", "active"); verify(mockIS).list(argThat(equalTo(expectedFilteringParams))); verifyNoMoreInteractions(mockIS); assertThat(new ArrayList<>(actual), equalTo(expected)); }
@Test public void getImageIdsForGivenUnknownThenReturnsEmpty() { final ImageService mockIS = mock(ImageService.class); when(mockIS.list(anyMapOf(String.class, String.class))).thenReturn(Collections.EMPTY_LIST); final OSClient mockClient = mock(OSClient.class); when(mockClient.imagesV2()).thenReturn(mockIS); final ArrayList<String> expected = new ArrayList<>(); final Openstack instance = new Openstack(mockClient); final List<String> actual = instance.getImageIdsFor("NameNotFound"); final Map<String, String> expectedFilteringParams = new HashMap<>(2); expectedFilteringParams.put("name", "NameNotFound"); expectedFilteringParams.put("status", "active"); verify(mockIS).list(argThat(equalTo(expectedFilteringParams))); verifyNoMoreInteractions(mockIS); assertThat(new ArrayList<>(actual), equalTo(expected)); }
@Test public void getImageIdsForGivenIdOfActiveImageThenReturnsId() { final Image mockImageNamedFoo = mock(Image.class); final String imageId = "cfd083b4-2422-4c5f-bf61-d975709375ab"; when(mockImageNamedFoo.getId()).thenReturn(imageId); when(mockImageNamedFoo.getName()).thenReturn("Foo"); when(mockImageNamedFoo.getStatus()).thenReturn(Image.ImageStatus.ACTIVE); final ImageService mockIS = mock(ImageService.class); when(mockIS.list(anyMapOf(String.class, String.class))).thenReturn(Collections.EMPTY_LIST); when(mockIS.get(imageId)).thenReturn(mockImageNamedFoo); final OSClient mockClient = mock(OSClient.class); when(mockClient.imagesV2()).thenReturn(mockIS); final ArrayList<String> expected = new ArrayList<>(Arrays.asList(mockImageNamedFoo.getId())); final Openstack instance = new Openstack(mockClient); final List<String> actual = instance.getImageIdsFor(imageId); verify(mockIS).list(anyMapOf(String.class, String.class)); verify(mockIS).get(imageId); verifyNoMoreInteractions(mockIS); assertThat(new ArrayList<>(actual), equalTo(expected)); }