forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayerTest.java
More file actions
168 lines (152 loc) · 6.14 KB
/
Copy pathLayerTest.java
File metadata and controls
168 lines (152 loc) · 6.14 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
/*-
* =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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.nio.file.Files;
import java.nio.file.Path;
import land.oras.utils.SupportedAlgorithm;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
@Execution(ExecutionMode.CONCURRENT)
class LayerTest {
@TempDir
public static Path tempDir;
@Test
void shouldReadLayer() {
String json = sampleLayer();
Layer layer = Layer.fromJson(json);
assertEquals("application/vnd.oci.image.layer.v1.tar+gzip", layer.getMediaType());
assertEquals("sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", layer.getDigest());
assertEquals(32654, layer.getSize());
assertEquals(json, layer.toJson());
}
@Test
void shouldReadLayerFromFile() throws Exception {
Path file = tempDir.resolve("hi.txt");
Files.writeString(file, "hi");
Layer layer = Layer.fromFile(file);
assertEquals("application/vnd.oci.image.layer.v1.tar", layer.getMediaType());
assertEquals("sha256:8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4", layer.getDigest());
assertEquals(2, layer.getSize());
layer = Layer.fromFile(file, SupportedAlgorithm.SHA384);
assertEquals("application/vnd.oci.image.layer.v1.tar", layer.getMediaType());
assertEquals(
"sha384:0791006df8128477244f53d0fdce210db81f55757510e26acee35c18a6bceaa28dcdbbfd6dc041b9b4dc7b1b54e37f52",
layer.getDigest());
assertEquals(2, layer.getSize());
layer = Layer.fromFile(file, SupportedAlgorithm.SHA512);
assertEquals("application/vnd.oci.image.layer.v1.tar", layer.getMediaType());
assertEquals(
"sha512:150a14ed5bea6cc731cf86c41566ac427a8db48ef1b9fd626664b3bfbb99071fa4c922f33dde38719b8c8354e2b7ab9d77e0e67fc12843920a712e73d558e197",
layer.getDigest());
assertEquals(2, layer.getSize());
}
@Test
void shouldHaveEmptyLayer() {
String json = emptyLayer();
assertEquals(Layer.fromJson(json).toJson(), Layer.empty().toJson());
}
@Test
void shouldReadNullAnnotations() {
String json =
"""
{
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
"digest": "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
"size": 32654
}
""";
Layer layer = Layer.fromJson(json);
assertEquals("application/vnd.oci.image.layer.v1.tar+gzip", layer.getMediaType());
assertEquals("sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", layer.getDigest());
assertEquals(32654, layer.getSize());
assertEquals(0, layer.getAnnotations().size());
}
@Test
void shouldReadBlobData() {
String json =
"""
{
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
"digest": "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
"data": "e30="
}
""";
Layer layer = Layer.fromJson(json);
assertEquals("application/vnd.oci.image.layer.v1.tar+gzip", layer.getMediaType());
assertEquals("sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", layer.getDigest());
assertEquals("e30=", layer.getData());
}
@Test
void testEqualsAndHashCode() {
// Empty
Layer empty1 = Layer.empty();
Layer empty2 = Layer.empty();
assertEquals(empty1, empty2);
// Layer data
Layer object1 = Layer.fromJson(sampleLayer());
Layer object2 = Layer.fromJson(sampleLayer());
assertEquals(object1, object2);
assertEquals(object1.hashCode(), object2.hashCode());
// Not equals
Layer different = Layer.fromJson(emptyLayer()).withMediaType("fake/bar");
assertNotEquals(object1, different);
assertNotEquals(object1.hashCode(), different.hashCode());
assertNotEquals("foo", object1);
assertNotEquals(null, object1);
}
@Test
void testToString() {
Layer layer = Layer.fromJson(sampleLayer());
String json = layer.toString();
assertEquals(
"{\"mediaType\":\"application/vnd.oci.image.layer.v1.tar+gzip\",\"digest\":\"sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890\",\"size\":32654}",
json);
}
private String emptyLayer() {
return """
{
"mediaType": "application/vnd.oci.empty.v1+json",
"digest": "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
"size": 2,
"data": "e30=",
"annotations": {}
}
""";
}
/**
* A sample manifest
* @return The manifest
*/
private String sampleLayer() {
return Layer.fromJson(
"""
{
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
"digest": "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
"size": 32654
}
""")
.toJson();
}
}