### Question:
CacheStorageReader { Optional<CachedLayer> retrieveTarLayer(DescriptorDigest diffId) throws IOException, CacheCorruptedException { Path layerDirectory = cacheStorageFiles.getLocalDirectory().resolve(diffId.getHash()); if (!Files.exists(layerDirectory)) { return Optional.empty(); } try (Stream<Path> files = Files.list(layerDirectory)) { List<Path> layerFiles = files.filter(CacheStorageFiles::isLayerFile).collect(Collectors.toList()); if (layerFiles.size() != 1) { throw new CacheCorruptedException( cacheStorageFiles.getCacheDirectory(), "No or multiple layer files found for layer hash " + diffId.getHash() + " in directory: " + layerDirectory); } Path layerFile = layerFiles.get(0); return Optional.of( CachedLayer.builder() .setLayerDigest(cacheStorageFiles.getDigestFromFilename(layerFile)) .setLayerSize(Files.size(layerFile)) .setLayerBlob(Blobs.from(layerFile)) .setLayerDiffId(diffId) .build()); } } CacheStorageReader(CacheStorageFiles cacheStorageFiles);   }### Answer:
@Test public void testRetrieveTarLayer() throws IOException, CacheCorruptedException { Path localDirectory = cacheStorageFiles.getLocalDirectory(); DescriptorDigest layerDigest = layerDigest1; DescriptorDigest layerDiffId = layerDigest2; Files.createDirectories(localDirectory.resolve(layerDiffId.getHash())); try (OutputStream out = Files.newOutputStream( localDirectory.resolve(layerDiffId.getHash()).resolve(layerDigest.getHash()))) { out.write("layerBlob".getBytes(StandardCharsets.UTF_8)); } Optional<CachedLayer> optionalCachedLayer = cacheStorageReader.retrieveTarLayer(layerDiffId); Assert.assertTrue(optionalCachedLayer.isPresent()); Assert.assertEquals(layerDigest, optionalCachedLayer.get().getDigest()); Assert.assertEquals(layerDiffId, optionalCachedLayer.get().getDiffId()); Assert.assertEquals("layerBlob".length(), optionalCachedLayer.get().getSize()); Assert.assertEquals("layerBlob", Blobs.writeToString(optionalCachedLayer.get().getBlob())); Files.createFile(localDirectory.resolve(layerDiffId.getHash()).resolve(layerDiffId.getHash())); try { cacheStorageReader.retrieveTarLayer(layerDiffId); Assert.fail("Should have thrown CacheCorruptedException"); } catch (CacheCorruptedException ex) { MatcherAssert.assertThat( ex.getMessage(), CoreMatchers.startsWith( "No or multiple layer files found for layer hash " + layerDiffId.getHash() + " in directory: " + localDirectory.resolve(layerDiffId.getHash()))); } }