Skip to content

Commit cd8288a

Browse files
committed
chore: add tests and refactor null check
Signed-off-by: Ruben Romero Montes <[email protected]>
1 parent bdab5da commit cd8288a

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/main/java/com/redhat/exhort/api/PackageRef.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,13 @@ public PackageURL purl() {
5252
}
5353

5454
public String name() {
55+
if (purl.getNamespace() == null) {
56+
return purl.getName();
57+
}
5558
switch (purl.getType()) {
5659
case Constants.GOLANG_PKG_MANAGER:
57-
if (purl().getNamespace() != null) {
58-
return new StringBuffer(purl.getNamespace())
59-
.append("/")
60-
.append(purl.getName())
61-
.toString();
62-
}
63-
return purl.getName();
60+
return new StringBuffer(purl.getNamespace()).append("/").append(purl.getName()).toString();
6461
default:
65-
if (purl.getNamespace() == null) {
66-
return purl.getName();
67-
}
6862
return new StringBuilder(purl.getNamespace()).append(":").append(purl.getName()).toString();
6963
}
7064
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2023 Red Hat, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.redhat.exhort.api;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
public class PackageRefTest {
27+
28+
@Test
29+
public void testNamespace() {
30+
PackageRef ref =
31+
new PackageRef("pkg:golang/google.golang.org/genproto#googleapis/api/annotations");
32+
assertEquals("google.golang.org/genproto", ref.name());
33+
34+
ref = new PackageRef("pkg:golang/[email protected]");
35+
assertEquals("go.opencensus.io", ref.name());
36+
37+
ref = new PackageRef("pkg:npm/[email protected]");
38+
assertEquals("foobar", ref.name());
39+
40+
ref = new PackageRef("pkg:maven/org.apache.xmlgraphics/[email protected]?packaging=sources");
41+
assertEquals("org.apache.xmlgraphics:batik-anim", ref.name());
42+
}
43+
44+
@Test
45+
public void testVersion() {
46+
PackageRef ref =
47+
new PackageRef("pkg:golang/google.golang.org/genproto#googleapis/api/annotations");
48+
assertNull(ref.version());
49+
50+
ref = new PackageRef("pkg:golang/[email protected]");
51+
assertEquals("v0.21.0", ref.version());
52+
}
53+
}

0 commit comments

Comments
 (0)