Repository: GoogleContainerTools/jib
File: jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PullBaseImageStepTest.java (Java)

Code before:
  public void testGetCachedBaseImages_emptyCache()
      throws InvalidImageReferenceException, IOException, CacheCorruptedException,
          UnlistedPlatformInManifestListException, PlatformNotFoundInBaseImageException,
          BadContainerConfigurationFormatException, LayerCountMismatchException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration())
        .thenReturn(ImageConfiguration.builder(imageReference).build());
    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.empty());

    Assert.assertEquals(Arrays.asList(), pullBaseImageStep.getCachedBaseImages());
  }

  @Test
  public void testGetCachedBaseImages_cachedWithoutAllLayers()
      throws InvalidImageReferenceException, CacheCorruptedException, IOException,
          LayerCountMismatchException, PlatformNotFoundInBaseImageException,
          BadContainerConfigurationFormatException, UnlistedPlatformInManifestListException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration())
        .thenReturn(ImageConfiguration.builder(imageReference).build());
    ManifestTemplate manifest = Mockito.mock(ManifestTemplate.class);
    ImageMetadataTemplate imageMetadata =
        new ImageMetadataTemplate(
            null, Arrays.asList(new ManifestAndConfigTemplate(manifest, null)));

    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
    Mockito.when(cache.allLayersCached(manifest)).thenReturn(false);

    Assert.assertEquals(Arrays.asList(), pullBaseImageStep.getCachedBaseImages());
  }

  @Test
  public void testGetCachedBaseImages_v21ManifestCached()
      throws InvalidImageReferenceException, IOException, CacheCorruptedException,
          UnlistedPlatformInManifestListException, BadContainerConfigurationFormatException,
          LayerCountMismatchException, DigestException, PlatformNotFoundInBaseImageException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration())
        .thenReturn(ImageConfiguration.builder(imageReference).build());

    DescriptorDigest layerDigest =
        DescriptorDigest.fromHash(
            "1111111111111111111111111111111111111111111111111111111111111111");
    V21ManifestTemplate v21Manifest = Mockito.mock(V21ManifestTemplate.class);
    Mockito.when(v21Manifest.getLayerDigests()).thenReturn(Arrays.asList(layerDigest));
    ImageMetadataTemplate imageMetadata =
        new ImageMetadataTemplate(
            null, Arrays.asList(new ManifestAndConfigTemplate(v21Manifest, null)));

    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
    Mockito.when(cache.allLayersCached(v21Manifest)).thenReturn(true);

Review comment (performance):
```suggestion
Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
```

Code after:
  @Test
  public void testGetCachedBaseImages_emptyCache()
      throws InvalidImageReferenceException, IOException, CacheCorruptedException,
          UnlistedPlatformInManifestListException, PlatformNotFoundInBaseImageException,
          BadContainerConfigurationFormatException, LayerCountMismatchException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration())
        .thenReturn(ImageConfiguration.builder(imageReference).build());
    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.empty());

    Assert.assertEquals(Arrays.asList(), pullBaseImageStep.getCachedBaseImages());
  }

  @Test
  public void testGetCachedBaseImages_partiallyCached_emptyListReturned()
      throws InvalidImageReferenceException, CacheCorruptedException, IOException,
          LayerCountMismatchException, PlatformNotFoundInBaseImageException,
          BadContainerConfigurationFormatException, UnlistedPlatformInManifestListException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration())
        .thenReturn(ImageConfiguration.builder(imageReference).build());
    ManifestTemplate manifest = Mockito.mock(ManifestTemplate.class);
    ImageMetadataTemplate imageMetadata =
        new ImageMetadataTemplate(
            null, Arrays.asList(new ManifestAndConfigTemplate(manifest, null)));
    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
    Mockito.when(cache.areAllLayersCached(manifest)).thenReturn(false);

    assertThat(pullBaseImageStep.getCachedBaseImages()).isEmpty();
  }

  @Test
  public void testGetCachedBaseImages_v21ManifestCached()
      throws InvalidImageReferenceException, IOException, CacheCorruptedException,
          UnlistedPlatformInManifestListException, BadContainerConfigurationFormatException,
          LayerCountMismatchException, DigestException, PlatformNotFoundInBaseImageException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration())
        .thenReturn(ImageConfiguration.builder(imageReference).build());

    DescriptorDigest layerDigest =
        DescriptorDigest.fromHash(
            "1111111111111111111111111111111111111111111111111111111111111111");
    V21ManifestTemplate v21Manifest = Mockito.mock(V21ManifestTemplate.class);
    Mockito.when(v21Manifest.getLayerDigests()).thenReturn(Arrays.asList(layerDigest));
    ImageMetadataTemplate imageMetadata =
        new ImageMetadataTemplate(
            null, Arrays.asList(new ManifestAndConfigTemplate(v21Manifest, null)));

    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
    Mockito.when(cache.areAllLayersCached(v21Manifest)).thenReturn(true);