### Question:
CacheStorageReader { Optional<CachedLayer> retrieve(DescriptorDigest layerDigest) throws IOException, CacheCorruptedException { Path layerDirectory = cacheStorageFiles.getLayerDirectory(layerDigest); 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 " + layerDigest.getHash() + " in directory: " + layerDirectory); } Path layerFile = layerFiles.get(0); return Optional.of( CachedLayer.builder() .setLayerDigest(layerDigest) .setLayerSize(Files.size(layerFile)) .setLayerBlob(Blobs.from(layerFile)) .setLayerDiffId(cacheStorageFiles.getDigestFromFilename(layerFile)) .build()); } } CacheStorageReader(CacheStorageFiles cacheStorageFiles);   }### Answer:
@Test public void testRetrieve() throws IOException, CacheCorruptedException { DescriptorDigest layerDigest = layerDigest1; DescriptorDigest layerDiffId = layerDigest2; Files.createDirectories(cacheStorageFiles.getLayerDirectory(layerDigest)); try (OutputStream out = Files.newOutputStream(cacheStorageFiles.getLayerFile(layerDigest, layerDiffId))) { out.write("layerBlob".getBytes(StandardCharsets.UTF_8)); } Optional<CachedLayer> optionalCachedLayer = cacheStorageReader.retrieve(layerDigest); 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(cacheStorageFiles.getLayerFile(layerDigest, layerDigest)); try { cacheStorageReader.retrieve(layerDigest); Assert.fail("Should have thrown CacheCorruptedException"); } catch (CacheCorruptedException ex) { MatcherAssert.assertThat( ex.getMessage(), CoreMatchers.startsWith( "No or multiple layer files found for layer hash " + layerDigest.getHash() + " in directory: " + cacheStorageFiles.getLayerDirectory(layerDigest))); } }