Repository: GoogleContainerTools/jib
File: jib-core/src/test/java/com/google/cloud/tools/jib/cache/CacheStorageReaderTest.java (Java)

Code before:
        new ManifestAndConfigTemplate(
            new OciManifestTemplate(), new ContainerConfigurationTemplate(), "sha256:digest");
    ImageMetadataTemplate metadata =
        new ImageMetadataTemplate(null, Arrays.asList(manifestAndConfig));
    CacheStorageReader.verifyImageMetadata(metadata, Paths.get("/cache/dir"));
    // should pass without CacheCorruptedException
  }

  @Test
  public void testVerifyImageMetadata_validOciImageIndex() throws CacheCorruptedException {
    ManifestAndConfigTemplate manifestAndConfig =
        new ManifestAndConfigTemplate(
            new OciManifestTemplate(), new ContainerConfigurationTemplate(), "sha256:digest");
    ImageMetadataTemplate metadata =
        new ImageMetadataTemplate(
            new OciIndexTemplate(), Arrays.asList(manifestAndConfig, manifestAndConfig));
    CacheStorageReader.verifyImageMetadata(metadata, Paths.get("/cache/dir"));
    // should pass without CacheCorruptedException
  }

  @Test
  public void testAllLayersCached_v21SingleManifest()
      throws IOException, CacheCorruptedException, DigestException, URISyntaxException {

    setupCachedMetadataV21(cacheDirectory);
    ImageMetadataTemplate metadata =
        cacheStorageReader.retrieveMetadata(ImageReference.of("test", "image", "tag")).get();

    V21ManifestTemplate manifest =
        (V21ManifestTemplate) metadata.getManifestsAndConfigs().get(0).getManifest();
    Assert.assertEquals(2, manifest.getLayerDigests().size());
    ManifestAndConfigTemplate manifestAndConfig =
        new ManifestAndConfigTemplate(manifest, new ContainerConfigurationTemplate());

    // Create only one of the layer directories.
    DescriptorDigest firstLayerDigest =
        DescriptorDigest.fromHash(manifest.getLayerDigests().get(0).getHash());
    Files.createDirectories(cacheStorageFiles.getLayerDirectory(firstLayerDigest));
    Assert.assertFalse(cacheStorageReader.allLayersCached(manifestAndConfig.getManifest()));

    // Create the other layer directory.
    DescriptorDigest secondLayerDigest =
        DescriptorDigest.fromHash(manifest.getLayerDigests().get(1).getHash());
    Files.createDirectories(cacheStorageFiles.getLayerDirectory(secondLayerDigest));
    Assert.assertTrue(cacheStorageReader.allLayersCached(manifestAndConfig.getManifest()));
  }

  @Test
  public void testAllLayersCached_v22SingleManifest()
      throws IOException, CacheCorruptedException, DigestException, URISyntaxException {


Review comment (suggestion):
Minor suggestion: If possible, let's format the test into three blocks: arrange, act and assert, with a space between each of these blocks. The `arrange` block will take care of all setup, the `act` block will call the method we're testing and the `assert` block will do all the verification. 

Code after:
  public void testVerifyImageMetadata_validOci() throws CacheCorruptedException {
    ManifestAndConfigTemplate manifestAndConfig =
        new ManifestAndConfigTemplate(
            new OciManifestTemplate(), new ContainerConfigurationTemplate(), "sha256:digest");
    ImageMetadataTemplate metadata =
        new ImageMetadataTemplate(null, Arrays.asList(manifestAndConfig));
    CacheStorageReader.verifyImageMetadata(metadata, Paths.get("/cache/dir"));
    // should pass without CacheCorruptedException
  }

  @Test
  public void testVerifyImageMetadata_validOciImageIndex() throws CacheCorruptedException {
    ManifestAndConfigTemplate manifestAndConfig =
        new ManifestAndConfigTemplate(
            new OciManifestTemplate(), new ContainerConfigurationTemplate(), "sha256:digest");
    ImageMetadataTemplate metadata =
        new ImageMetadataTemplate(
            new OciIndexTemplate(), Arrays.asList(manifestAndConfig, manifestAndConfig));
    CacheStorageReader.verifyImageMetadata(metadata, Paths.get("/cache/dir"));
    // should pass without CacheCorruptedException
  }

  @Test
  public void testAllLayersCached_v21SingleManifest()
      throws IOException, CacheCorruptedException, DigestException, URISyntaxException {
    setupCachedMetadataV21(cacheDirectory);
    ImageMetadataTemplate metadata =
        cacheStorageReader.retrieveMetadata(ImageReference.of("test", "image", "tag")).get();
    V21ManifestTemplate manifest =
        (V21ManifestTemplate) metadata.getManifestsAndConfigs().get(0).getManifest();
    DescriptorDigest firstLayerDigest = manifest.getLayerDigests().get(0);
    DescriptorDigest secondLayerDigest = manifest.getLayerDigests().get(1);

    // Create only one of the layer directories so that layers are only partially cached.
    Files.createDirectories(cacheStorageFiles.getLayerDirectory(firstLayerDigest));
    boolean checkWithPartialLayersCached = cacheStorageReader.areAllLayersCached(manifest);
    // Create the other layer directory so that all layers are cached.
    Files.createDirectories(cacheStorageFiles.getLayerDirectory(secondLayerDigest));
    boolean checkWithAllLayersCached = cacheStorageReader.areAllLayersCached(manifest);

    assertThat(checkWithPartialLayersCached).isFalse();
    assertThat(checkWithAllLayersCached).isTrue();
  }

  @Test
  public void testAllLayersCached_v22SingleManifest()
      throws IOException, CacheCorruptedException, DigestException, URISyntaxException {
    setupCachedMetadataV22(cacheDirectory);
    ImageMetadataTemplate metadata =
        cacheStorageReader.retrieveMetadata(ImageReference.of("test", "image", "tag")).get();
    V22ManifestTemplate manifest =