forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManifestTest.java
More file actions
188 lines (168 loc) · 7.41 KB
/
Copy pathManifestTest.java
File metadata and controls
188 lines (168 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*-
* =LICENSE=
* ORAS Java SDK
* ===
* Copyright (C) 2024 - 2026 ORAS
* ===
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =LICENSEEND=
*/
package land.oras;
import static org.junit.jupiter.api.Assertions.*;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
@Execution(ExecutionMode.CONCURRENT)
class ManifestTest {
@Test
void shouldReadManifest() {
String json = sampleManifest();
Manifest manifest = Manifest.fromJson(json);
// Assert manifest
assertEquals(2, manifest.getSchemaVersion());
assertEquals("application/vnd.oci.image.manifest.v1+json", manifest.getMediaType());
assertEquals(7023, manifest.getConfig().getSize());
assertEquals(
"sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
manifest.getConfig().getDigest());
assertEquals(
"application/vnd.oci.image.config.v1+json", manifest.getConfig().getMediaType());
assertEquals(2, manifest.getLayers().size());
assertEquals(0, manifest.getAnnotations().size());
// Assert layer
assertEquals(32654, manifest.getLayers().get(0).getSize());
assertEquals(
"sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
manifest.getLayers().get(0).getDigest());
assertEquals(
"application/vnd.oci.image.layer.v1.tar+gzip",
manifest.getLayers().get(0).getMediaType());
assertEquals(1048576, manifest.getLayers().get(1).getSize());
assertEquals(
"sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
manifest.getLayers().get(1).getDigest());
assertEquals(
"application/vnd.oci.image.layer.v1.tar+gzip",
manifest.getLayers().get(1).getMediaType());
manifest.toJson();
}
@Test
void shouldHaveEmptyManifest() {
assertEquals(
Manifest.fromJson(emptyManifest()).toJson(), Manifest.empty().toJson());
}
@Test
void testEqualsAndHashCode() {
// Empty
Manifest empty1 = Manifest.empty();
Manifest empty2 = Manifest.empty();
assertEquals(empty1, empty2);
// Manifest data
Manifest object1 = Manifest.fromJson(sampleManifest());
Manifest object2 = Manifest.fromJson(sampleManifest());
assertEquals(object1, object2);
assertEquals(object1.hashCode(), object2.hashCode());
// Not equals
object2 = object2.withArtifactType(ArtifactType.from("test/plain"));
assertNotEquals(object1, object2);
assertNotEquals(object1.hashCode(), object2.hashCode());
// Different type
assertNotEquals("not a manifest", object1);
}
@Test
void testToString() {
Manifest manifest = Manifest.fromJson(sampleManifest());
String json = manifest.toString();
assertNotNull(json);
assertEquals(
"{\"schemaVersion\":2,\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"config\":{\"mediaType\":\"application/vnd.oci.image.config.v1+json\",\"digest\":\"sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890\",\"size\":7023},\"layers\":[{\"mediaType\":\"application/vnd.oci.image.layer.v1.tar+gzip\",\"digest\":\"sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890\",\"size\":32654},{\"mediaType\":\"application/vnd.oci.image.layer.v1.tar+gzip\",\"digest\":\"sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\",\"size\":1048576}]}",
json);
}
@Test
void shouldReadDigestTopLevelDescriptor() {
String json =
"{\"schemaVersion\":2,\"mediaType\":\"application/vnd.oci.image.index.v1+json\",\"digest\":\"sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\",\"size\":7023}";
Manifest manifest = Manifest.fromJson(json);
assertEquals(
"sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", manifest.getDigest());
}
@Test
void shouldHaveNoLayerForIndex() {
String json =
"{\"schemaVersion\":2,\"mediaType\":\"application/vnd.oci.image.index.v1+json\",\"annotations\":{}}";
Manifest manifest = Manifest.fromJson(json);
assertEquals(0, manifest.getLayers().size());
assertEquals(json, manifest.getJson());
}
@Test
void shouldReadFromPath() {
Path path = Path.of(
"src/test/resources/oci/artifact/blobs/sha256/cb1d49baba271af2c56d493d66dddb112ecf1c2c52f47e6f45f3617bb2155d34");
Manifest manifest = Manifest.fromPath(path);
assertNotNull(manifest.getJson());
}
@Test
void shouldGetArtifactTest() {
Manifest manifest1 = Manifest.empty().withArtifactType(ArtifactType.from("test/plain"));
assertEquals("test/plain", manifest1.getArtifactType().getMediaType());
Manifest manifest2 = Manifest.empty().withConfig(Config.empty().withMediaType("test/plain"));
assertEquals("test/plain", manifest2.getArtifactType().getMediaType());
}
private String emptyManifest() {
return """
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"config": {
"mediaType": "application/vnd.oci.empty.v1+json",
"digest": "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
"size": 2,
"data": "e30="
},
"layers": [],
"annotations": {}
}
""";
}
/**
* A sample manifest
* @return The manifest
*/
private String sampleManifest() {
// language=JSON
return """
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"config": {
"mediaType": "application/vnd.oci.image.config.v1+json",
"digest": "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
"size": 7023
},
"layers": [
{
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
"digest": "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
"size": 32654
},
{
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
"digest": "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"size": 1048576
}
]
}
""";
}
}