Skip to content

Commit a8e52f7

Browse files
authored
Merge pull request #1468 from wind57/more-changes
first commit
2 parents cd84eaa + fa4c16d commit a8e52f7

File tree

5 files changed

+130
-29
lines changed

5 files changed

+130
-29
lines changed

util/src/main/java/io/kubernetes/client/apimachinery/GroupVersion.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@
1212
*/
1313
package io.kubernetes.client.apimachinery;
1414

15+
import static io.kubernetes.client.util.Preconditions.precondition;
16+
1517
import io.kubernetes.client.common.KubernetesObject;
1618
import io.kubernetes.client.util.Strings;
1719
import java.util.Objects;
1820

1921
public class GroupVersion {
2022

23+
private static final GroupVersion GROUP_VERSION_V1 = new GroupVersion("", "v1");
24+
2125
public static GroupVersion parse(String apiVersion) {
22-
if (Strings.isNullOrEmpty(apiVersion)) {
23-
throw new IllegalArgumentException("No apiVersion found on object");
24-
}
2526
if ("v1".equals(apiVersion)) {
2627
// legacy api group
27-
return new GroupVersion("", "v1");
28+
return GROUP_VERSION_V1;
2829
}
30+
precondition(apiVersion, Strings::isNullOrEmpty, () -> "apiVersion can not be null");
2931
String[] parts = apiVersion.split("/");
30-
if (parts.length != 2) {
31-
throw new IllegalArgumentException("Invalid apiVersion found on object: " + apiVersion);
32-
}
32+
precondition(parts, x -> x.length != 2, () -> "Invalid apiVersion: " + apiVersion);
3333
return new GroupVersion(parts[0], parts[1]);
3434
}
3535

@@ -38,14 +38,8 @@ public static GroupVersion parse(KubernetesObject obj) {
3838
}
3939

4040
public GroupVersion(String group, String version) {
41-
if (group == null) {
42-
throw new IllegalArgumentException("group must not be null");
43-
}
44-
if (version == null) {
45-
throw new IllegalArgumentException("version must not be null");
46-
}
47-
this.group = group;
48-
this.version = version;
41+
this.group = precondition(group, Objects::isNull, () -> "group must not be null");
42+
this.version = precondition(version, Objects::isNull, () -> "version must not be null");
4943
}
5044

5145
private final String group;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.util;
14+
15+
import java.util.function.Predicate;
16+
import java.util.function.Supplier;
17+
18+
/**
19+
* support for different invariant checks, like for a example a non-null argument being passed to a
20+
* method that does not support nulls.
21+
*
22+
* @author wind57
23+
*/
24+
public final class Preconditions {
25+
26+
private Preconditions() {
27+
throw new AssertionError("not supported");
28+
}
29+
30+
/**
31+
* @param validate argument to be validated
32+
* @param predicate to check against
33+
* @param errorSupplier the error message generator
34+
* @param <T>
35+
* @return T if Predicate does not match
36+
* @throws IllegalArgumentException if predicate returns true
37+
*/
38+
public static <T> T precondition(
39+
T validate, Predicate<? super T> predicate, Supplier<String> errorSupplier) {
40+
if (predicate.test(validate)) {
41+
throw new IllegalArgumentException(errorSupplier.get());
42+
}
43+
return validate;
44+
}
45+
}

util/src/main/java/io/kubernetes/client/util/Strings.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414

1515
import javax.annotation.Nullable;
1616

17-
public class Strings {
17+
public final class Strings {
18+
19+
private Strings() {
20+
throw new AssertionError("not supported");
21+
}
1822

1923
public static boolean isNullOrEmpty(String value) {
20-
return value == null || value.length() == 0;
24+
return value == null || value.isEmpty();
2125
}
2226

2327
public static String nullToEmpty(@Nullable String string) {

util/src/test/java/io/kubernetes/client/apimachinery/GroupVersionTest.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package io.kubernetes.client.apimachinery;
1414

15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1517
import static org.junit.Assert.*;
1618

1719
import io.kubernetes.client.openapi.models.V1Deployment;
@@ -21,17 +23,37 @@
2123
public class GroupVersionTest {
2224

2325
@Test
24-
public void parse() {
25-
assertEquals(new GroupVersion("", "v1"), GroupVersion.parse(new V1Pod().apiVersion("v1")));
26-
assertEquals(
27-
new GroupVersion("apps", "v1"),
28-
GroupVersion.parse(new V1Deployment().apiVersion("apps/v1")));
29-
assertThrows(
30-
IllegalArgumentException.class,
31-
() -> {
32-
GroupVersion.parse(new V1Pod());
33-
GroupVersion.parse(new V1Pod().apiVersion(null));
34-
GroupVersion.parse(new V1Pod().apiVersion("foo/bar/f"));
35-
});
26+
public void parseV1() {
27+
GroupVersion left = new GroupVersion("", "v1");
28+
GroupVersion right = GroupVersion.parse(new V1Pod().apiVersion("v1"));
29+
assertThat(left).isEqualTo(right);
30+
}
31+
32+
@Test
33+
public void parseAppsV1() {
34+
GroupVersion left = new GroupVersion("apps", "v1");
35+
GroupVersion right = GroupVersion.parse(new V1Deployment().apiVersion("apps/v1"));
36+
assertThat(left).isEqualTo(right);
37+
}
38+
39+
@Test
40+
public void parseInvalid1() {
41+
assertThatThrownBy(() -> GroupVersion.parse(new V1Pod()))
42+
.isInstanceOf(IllegalArgumentException.class)
43+
.hasMessage("apiVersion can not be null");
44+
}
45+
46+
@Test
47+
public void parseInvalid2() {
48+
assertThatThrownBy(() -> GroupVersion.parse(new V1Pod().apiVersion(null)))
49+
.isInstanceOf(IllegalArgumentException.class)
50+
.hasMessage("apiVersion can not be null");
51+
}
52+
53+
@Test
54+
public void parseInvalid3() {
55+
assertThatThrownBy(() -> GroupVersion.parse(new V1Pod().apiVersion("foo/bar/f")))
56+
.isInstanceOf(IllegalArgumentException.class)
57+
.hasMessage("Invalid apiVersion: foo/bar/f");
3658
}
3759
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.util;
14+
15+
import static io.kubernetes.client.util.Preconditions.precondition;
16+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
17+
18+
import org.assertj.core.api.Assertions;
19+
import org.junit.Test;
20+
21+
public class PreconditionsTest {
22+
23+
@Test
24+
public void testEmptyString() {
25+
assertThatThrownBy(
26+
() -> precondition("", Strings::isNullOrEmpty, () -> "string can not be empty"))
27+
.isInstanceOf(IllegalArgumentException.class)
28+
.hasMessage("string can not be empty");
29+
}
30+
31+
@Test
32+
public void testNonEmptyString() {
33+
String abc = precondition("abc", Strings::isNullOrEmpty, () -> "string can not be empty");
34+
Assertions.assertThat(abc).isEqualTo("abc");
35+
}
36+
}

0 commit comments

Comments
 (0)