forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutRefTest.java
More file actions
126 lines (109 loc) · 4.74 KB
/
Copy pathLayoutRefTest.java
File metadata and controls
126 lines (109 loc) · 4.74 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
/*-
* =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 land.oras.exception.OrasException;
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 LayoutRefTest {
@TempDir
public static Path tempDir;
@TempDir
public static Path ociLayoutTempDir;
@TempDir
public static Path otherOciLayout;
@Test
void shouldParseLayoutWithAllParts() {
String ociLayout = tempDir.resolve("foo").toString();
LayoutRef layoutRef = LayoutRef.parse("%s:v1".formatted(ociLayout));
assertEquals("v1", layoutRef.getTag());
assertEquals(ociLayout, layoutRef.getFolder().toString());
assertEquals(ociLayout, layoutRef.getRepository());
assertFalse(layoutRef.isValidDigest(), "v1 is not a valid digest");
}
@Test
void shouldParseLayoutWithDigest() {
String ociLayout = tempDir.resolve("foo").toString();
LayoutRef layoutRef = LayoutRef.parse(
"%s@sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824".formatted(ociLayout));
assertEquals("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", layoutRef.getTag());
assertEquals(ociLayout, layoutRef.getFolder().toString());
assertEquals(ociLayout, layoutRef.getRepository());
assertTrue(
layoutRef.isValidDigest(),
"sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 should be a valid digest pattern");
}
@Test
void shouldParseFolderNameOnly() {
LayoutRef layoutRef = LayoutRef.parse("foo");
assertNull(layoutRef.getTag());
assertEquals("foo", layoutRef.getFolder().toString());
}
@Test
void shouldReturnForOciLayout() {
OCILayout ociLayout = OCILayout.builder().defaults(ociLayoutTempDir).build();
LayoutRef layoutRef =
LayoutRef.parse(otherOciLayout.getFileName().toString()).forLayout(ociLayout);
assertEquals(layoutRef.getFolder(), ociLayoutTempDir, "Path should be the same");
}
@Test
void shouldFailWithInvalidRef() {
assertThrows(OrasException.class, () -> LayoutRef.parse(""));
}
@Test
void shouldGetAlgorithm() {
LayoutRef layoutRef = LayoutRef.parse("foo");
assertEquals("sha256", layoutRef.getAlgorithm().getPrefix());
layoutRef = LayoutRef.parse("foo@sha256:");
assertEquals("sha256", layoutRef.getAlgorithm().getPrefix());
layoutRef = LayoutRef.parse(
"foo@sha512:cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
assertEquals("sha512", layoutRef.getAlgorithm().getPrefix());
}
@Test
void testEqualsAndHashCode() {
LayoutRef layoutRef1 = LayoutRef.parse("foo:v1");
LayoutRef layoutRef2 = LayoutRef.parse("foo:v1");
LayoutRef layoutRef3 = LayoutRef.parse("bar:v1");
assertEquals(layoutRef1, layoutRef2);
assertNotEquals(layoutRef1, layoutRef3);
assertEquals(layoutRef1.hashCode(), layoutRef2.hashCode());
// Not equals
assertNotEquals(layoutRef1.withTag("newtag"), layoutRef1);
assertNotEquals("foo", layoutRef1);
assertNotEquals(null, layoutRef1);
}
@Test
void testToString() {
LayoutRef layoutRef = LayoutRef.parse(
"foo@sha512:cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
assertEquals(
"foo@sha512:cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
layoutRef.toString());
layoutRef = LayoutRef.parse("foo:latest");
assertEquals("foo:latest", layoutRef.toString());
layoutRef = LayoutRef.parse("foo");
assertEquals("foo", layoutRef.toString());
}
}