Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 4 additions & 22 deletions src/main/java/land/oras/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
@NullUnmarked
public final class Config extends Descriptor {

private final String digest;
private final long size;

/**
* The base 64 encoded data
*/
Expand All @@ -50,10 +47,11 @@ public final class Config extends Descriptor {
*/
private Config(String mediaType, String digest, long size, @Nullable String data, Annotations annotations) {
super(
digest,
size,
mediaType,
!annotations.configAnnotations().isEmpty() ? Map.copyOf(annotations.configAnnotations()) : null);
this.digest = digest;
this.size = size;
!annotations.configAnnotations().isEmpty() ? Map.copyOf(annotations.configAnnotations()) : null,
null);
this.data = data;
}

Expand All @@ -66,22 +64,6 @@ private Config(String mediaType, String digest, long size, @Nullable String data
return annotations;
}

/**
* Get the digest
* @return The digest
*/
public String getDigest() {
return digest;
}

/**
* Get the size
* @return The size
*/
public long getSize() {
return size;
}

/**
* Create a new config with annotations
* @param annotations The annotations
Expand Down
37 changes: 36 additions & 1 deletion src/main/java/land/oras/Descriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@ public abstract sealed class Descriptor permits Config, Manifest, Layer, Index {
*/
protected final @Nullable Map<String, String> annotations;

protected Descriptor(String mediaType, Map<String, String> annotations) {
protected final @Nullable String digest;
protected final @Nullable Long size;
protected final @Nullable String artifactType;

protected Descriptor(
String digest, Long size, String mediaType, Map<String, String> annotations, String artifactType) {
this.digest = digest;
this.size = size;
this.mediaType = mediaType;
this.annotations = annotations;
this.artifactType = artifactType;
}

/**
Expand All @@ -64,6 +72,33 @@ public final String getMediaType() {
return mediaType;
}

/**
* Get the digest
* @return The digest
*/
public @Nullable String getDigest() {
return digest;
}

/**
* Get the size
* @return The size
*/
public @Nullable Long getSize() {
return size;
}

/**
* Get the artifact type
* @return The artifact type
*/
public @Nullable ArtifactType getArtifactType() {
if (artifactType != null) {
return ArtifactType.from(artifactType);
}
return null;
}

/**
* Return the JSON representation of this descriptor
* @return The JSON string
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/land/oras/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
public final class Index extends Descriptor {

private final int schemaVersion;
private final String artifactType;
private final List<ManifestDescriptor> manifests;

/**
Expand All @@ -54,10 +53,9 @@ private Index(
List<ManifestDescriptor> manifests,
ManifestDescriptor descriptor,
String json) {
super(mediaType, Map.of());
super(null, null, mediaType, Map.of(), artifactType);
this.schemaVersion = schemaVersion;
this.descriptor = descriptor;
this.artifactType = artifactType;
this.manifests = manifests;
this.json = json;
}
Expand All @@ -70,14 +68,6 @@ public int getSchemaVersion() {
return schemaVersion;
}

/**
* Get the artifact type
* @return The artifact type
*/
public String getArtifactType() {
return artifactType;
}

/**
* Get the list of manifests
* @return The list of manifests
Expand Down
34 changes: 2 additions & 32 deletions src/main/java/land/oras/Layer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@
@NullMarked
public final class Layer extends Descriptor {

/**
* The digest of the layer
*/
private final String digest;

/**
* The size of the layer
*/
private final long size;

/**
* The base 64 encoded data. Might be null if path is set
*/
Expand All @@ -71,9 +61,7 @@ private Layer(
long size,
@Nullable String data,
@Nullable Map<String, String> annotations) {
super(mediaType, annotations);
this.digest = digest;
this.size = size;
super(digest, size, mediaType, annotations, null);
this.data = data;
this.blobPath = null;
}
Expand All @@ -86,29 +74,11 @@ private Layer(
* @param blobPath The path to the blob
*/
private Layer(String mediaType, String digest, long size, Path blobPath, Map<String, String> annotations) {
super(mediaType, annotations);
this.digest = digest;
this.size = size;
super(digest, size, mediaType, annotations, null);
this.data = null;
this.blobPath = blobPath;
}

/**
* Get the digest
* @return The digest
*/
public String getDigest() {
return digest;
}

/**
* Get the size
* @return The size
*/
public long getSize() {
return size;
}

/**
* Get the data
* @return The data
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/land/oras/Manifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
public final class Manifest extends Descriptor {

private final int schemaVersion;
private final String artifactType;
private final Config config;
private final Subject subject;
private final List<Layer> layers;
Expand All @@ -62,9 +61,13 @@ private Manifest(
List<Layer> layers,
Annotations annotations,
String json) {
super(mediaType, Map.copyOf(annotations.manifestAnnotations()));
super(
null,
null,
mediaType,
Map.copyOf(annotations.manifestAnnotations()),
artifactType != null ? artifactType.getMediaType() : null);
this.schemaVersion = schemaVersion;
this.artifactType = artifactType != null ? artifactType.getMediaType() : null;
this.descriptor = descriptor;
this.config = config;
this.subject = subject;
Expand All @@ -80,10 +83,7 @@ public int getSchemaVersion() {
return schemaVersion;
}

/**
* Get the artifact type
* @return The artifact type
*/
@Override
public @NonNull ArtifactType getArtifactType() {
if (artifactType != null) {
return ArtifactType.from(artifactType);
Expand Down
19 changes: 18 additions & 1 deletion src/test/java/land/oras/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
package land.oras;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
Expand All @@ -34,7 +36,7 @@ public class ConfigTest {
void shouldSerializeEmptyConfig() {
Config config = Config.empty();
assertEquals(
"{\"digest\":\"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a\",\"size\":2,\"data\":\"e30=\",\"mediaType\":\"application/vnd.oci.empty.v1+json\"}",
"{\"data\":\"e30=\",\"mediaType\":\"application/vnd.oci.empty.v1+json\",\"digest\":\"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a\",\"size\":2}",
config.toJson());
}

Expand All @@ -58,4 +60,19 @@ void shouldDeserializeConfigWithAnnotations() {
assertEquals(1, config.getAnnotations().size());
assertEquals("value", config.getAnnotations().get("key"));
}

@Test
void shouldSaveNullForEmptyAnnotations() {
Config config = Config.empty();
config = config.withAnnotations(Annotations.ofConfig(Map.of()));
assertNull(config.getAnnotations(), "Annotations should be null");
}

@Test
void shouldSaveNonEmptyAnnotations() {
Config config = Config.empty();
config = config.withAnnotations(Annotations.ofConfig(Map.of("foo", "bar")));
assertNotNull(config.getAnnotations(), "Annotations should be null");
assertEquals("bar", config.getAnnotations().get("foo"));
}
}
17 changes: 17 additions & 0 deletions src/test/java/land/oras/IndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ void shouldReadAndWriteIndex() {
index.toJson();
}

@Test
void shouldReadAndWriteIndexWithArtifactType() {
String json =
"{\"schemaVersion\":2,\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559}],\"artifactType\":\"foo/bar\"}";
Index index = Index.fromJson(json);
assertNull(index.getMediaType());
assertEquals(2, index.getSchemaVersion());
assertEquals(1, index.getManifests().size());
assertEquals("foo/bar", index.getArtifactType().getMediaType());
assertNull(index.getDescriptor());
assertEquals(
"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27",
index.getManifests().get(0).getDigest());
assertEquals(559, index.getManifests().get(0).getSize());
assertEquals(json, index.toJson());
}

@Test
void shouldAddManifest() {
Index index = Index.fromManifests(List.of());
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/land/oras/ManifestDescriptorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
import java.util.Map;
import land.oras.utils.Const;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.CONCURRENT)
public class ManifestDescriptorTest {

@Test
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/land/oras/OCILayoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
@Execution(ExecutionMode.CONCURRENT)
public class OCILayoutTest {

private static final Logger LOG = LoggerFactory.getLogger(OCILayoutTest.class);

@TempDir
private Path extractDir;

Expand Down Expand Up @@ -666,6 +670,7 @@ private void assertOciLayout(Path layoutPath) {
private void assertIndex(Path ociLayoutPath, Manifest manifest) {
assertTrue(Files.exists(ociLayoutPath.resolve(Const.OCI_LAYOUT_INDEX)));
Index index = JsonUtils.fromJson(ociLayoutPath.resolve(Const.OCI_LAYOUT_INDEX), Index.class);
LOG.debug("Index is {}", index.toJson());
assertEquals(2, index.getSchemaVersion());
assertEquals(1, index.getManifests().size());
assertEquals(Const.DEFAULT_INDEX_MEDIA_TYPE, index.getMediaType());
Expand Down