Optional<ImageMetadataTemplate> retrieveMetadata(ImageReference imageReference)
      throws IOException, CacheCorruptedException {
    Path imageDirectory = cacheStorageFiles.getImageDirectory(imageReference);
    Path metadataPath = imageDirectory.resolve("manifests_configs.json");
    if (!Files.exists(metadataPath)) {
      return Optional.empty();
    }

    ImageMetadataTemplate metadata;
    try (LockFile ignored = LockFile.lock(imageDirectory.resolve("lock"))) {
      metadata = JsonTemplateMapper.readJsonFromFile(metadataPath, ImageMetadataTemplate.class);
    }
    verifyImageMetadata(metadata, imageDirectory);
    return Optional.of(metadata);
  }
@Test
  public void testRetrieveMetadata_v22ManifestList()
      throws IOException, URISyntaxException, CacheCorruptedException {
    setupCachedMetadataV22ManifestList(cacheDirectory);

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

    MatcherAssert.assertThat(
        metadata.getManifestList(), CoreMatchers.instanceOf(V22ManifestListTemplate.class));
    List<ManifestDescriptorTemplate> manifestDescriptors =
        ((V22ManifestListTemplate) metadata.getManifestList()).getManifests();

    Assert.assertEquals(3, manifestDescriptors.size());
    Assert.assertEquals(
        "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
        manifestDescriptors.get(0).getDigest());
    Assert.assertEquals(
        "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
        manifestDescriptors.get(1).getDigest());
    Assert.assertEquals(
        "sha256:cccbcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501999",
        manifestDescriptors.get(2).getDigest());

    Assert.assertEquals(2, metadata.getManifestsAndConfigs().size());
    ManifestAndConfigTemplate manifestAndConfig1 = metadata.getManifestsAndConfigs().get(0);
    ManifestAndConfigTemplate manifestAndConfig2 = metadata.getManifestsAndConfigs().get(1);

    V22ManifestTemplate manifest1 = (V22ManifestTemplate) manifestAndConfig1.getManifest();
    V22ManifestTemplate manifest2 = (V22ManifestTemplate) manifestAndConfig2.getManifest();
    Assert.assertEquals(2, manifest1.getSchemaVersion());
    Assert.assertEquals(2, manifest2.getSchemaVersion());
    Assert.assertEquals(1, manifest1.getLayers().size());
    Assert.assertEquals(1, manifest2.getLayers().size());
    Assert.assertEquals(
        "4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236",
        manifest1.getLayers().get(0).getDigest().getHash());
    Assert.assertEquals(
        "8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad",
        manifest2.getLayers().get(0).getDigest().getHash());
    Assert.assertEquals(
        "8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad",
        manifest1.getContainerConfiguration().getDigest().getHash());
    Assert.assertEquals(
        "2000a70a1ce8bba401c493376fdb9eb81bcf7155212f4ce4c6ff96e577718a49",
        manifest2.getContainerConfiguration().getDigest().getHash());

    Assert.assertEquals("wasm", manifestAndConfig1.getConfig().getArchitecture());
    Assert.assertEquals("wasm", manifestAndConfig2.getConfig().getArchitecture());
  }