public void writeTo(OutputStream out) throws IOException {
    if (image.getImageFormat() == OciManifestTemplate.class) {
      ociWriteTo(out);
    } else {
      dockerWriteTo(out);
    }
  }
@Test
  public void testWriteTo_oci()
      throws InvalidImageReferenceException, IOException, LayerPropertyNotFoundException {
    Image testImage =
        Image.builder(OciManifestTemplate.class).addLayer(mockLayer1).addLayer(mockLayer2).build();
    ImageTarball imageTarball =
        new ImageTarball(
            testImage,
            ImageReference.parse("my/image:tag"),
            ImmutableSet.of("tag", "another-tag", "tag3"));

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    imageTarball.writeTo(out);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(in)) {

      // Verifies layer with fileA was added.
      TarArchiveEntry headerFileALayer = tarArchiveInputStream.getNextTarEntry();
      Assert.assertEquals("blobs/sha256/" + fakeDigestA.getHash(), headerFileALayer.getName());
      String fileAString =
          CharStreams.toString(
              new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
      Assert.assertEquals(Blobs.writeToString(Blobs.from(fileA)), fileAString);

      // Verifies layer with fileB was added.
      TarArchiveEntry headerFileBLayer = tarArchiveInputStream.getNextTarEntry();
      Assert.assertEquals("blobs/sha256/" + fakeDigestB.getHash(), headerFileBLayer.getName());
      String fileBString =
          CharStreams.toString(
              new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
      Assert.assertEquals(Blobs.writeToString(Blobs.from(fileB)), fileBString);

      // Verifies container configuration was added.
      TarArchiveEntry headerContainerConfiguration = tarArchiveInputStream.getNextTarEntry();
      Assert.assertEquals(
          "blobs/sha256/011212cff4d5d6b18c7d3a00a7a2701514a1fdd3ec0d250a03756f84f3d955d4",
          headerContainerConfiguration.getName());
      JsonTemplateMapper.readJson(tarArchiveInputStream, ContainerConfigurationTemplate.class);

      // Verifies manifest was added.
      TarArchiveEntry headerManifest = tarArchiveInputStream.getNextTarEntry();
      Assert.assertEquals(
          "blobs/sha256/1543d061159a8d6877087938bfd62681cdeff873e1fa3e1fcf12dec358c112a4",
          headerManifest.getName());
      JsonTemplateMapper.readJson(tarArchiveInputStream, OciManifestTemplate.class);

      // Verifies oci-layout was added.
      TarArchiveEntry headerOciLayout = tarArchiveInputStream.getNextTarEntry();
      Assert.assertEquals("oci-layout", headerOciLayout.getName());
      String ociLayoutJson =
          CharStreams.toString(
              new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
      Assert.assertEquals("{\"imageLayoutVersion\": \"1.0.0\"}", ociLayoutJson);

      // Verifies index.json was added.
      TarArchiveEntry headerIndex = tarArchiveInputStream.getNextTarEntry();
      Assert.assertEquals("index.json", headerIndex.getName());
      OciIndexTemplate index =
          JsonTemplateMapper.readJson(tarArchiveInputStream, OciIndexTemplate.class);
      BuildableManifestTemplate.ContentDescriptorTemplate indexManifest =
          index.getManifests().get(0);
      Assert.assertEquals(
          "1543d061159a8d6877087938bfd62681cdeff873e1fa3e1fcf12dec358c112a4",
          indexManifest.getDigest().getHash());
    }
  }