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

Code before:
    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);

    List<Image> images = pullBaseImageStep.getCachedBaseImages();

    Assert.assertEquals(1, images.size());

Review comment (suggestion):
Let's use `Truth` for newer assertions. It's a little easier to read since the expected is always on the right side and provides better error messaging. We'll have to add the following import statement:
```
import static com.google.common.truth.Truth.assertThat;
```

Code after:
          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);

    List<Image> images = pullBaseImageStep.getCachedBaseImages();

    Assert.assertEquals(1, images.size());