Skip to content

Commit cc50f38

Browse files
authored
Merge pull request #1490 from wind57/more-changes
use Preconditions::precondition + add tests
2 parents 3111c13 + db55f66 commit cc50f38

File tree

7 files changed

+117
-11
lines changed

7 files changed

+117
-11
lines changed

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

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

15+
import static io.kubernetes.client.util.Preconditions.precondition;
16+
1517
import java.util.Objects;
1618

1719
public class GroupVersionKind extends GroupVersion {
@@ -20,10 +22,7 @@ public class GroupVersionKind extends GroupVersion {
2022

2123
public GroupVersionKind(String group, String version, String kind) {
2224
super(group, version);
23-
if (kind == null) {
24-
throw new IllegalArgumentException("kind must not be null");
25-
}
26-
this.kind = kind;
25+
this.kind = precondition(kind, Objects::isNull, () -> "kind must not be null");
2726
}
2827

2928
public String getKind() {

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

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

15+
import static io.kubernetes.client.util.Preconditions.precondition;
16+
1517
import java.util.Objects;
1618

1719
public class GroupVersionResource extends GroupVersion {
@@ -20,10 +22,7 @@ public class GroupVersionResource extends GroupVersion {
2022

2123
public GroupVersionResource(String group, String version, String resource) {
2224
super(group, version);
23-
if (resource == null) {
24-
throw new IllegalArgumentException("resource must not be null");
25-
}
26-
this.resource = resource;
25+
this.resource = precondition(resource, Objects::isNull, () -> "resource must not be null");
2726
}
2827

2928
@Override

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

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

15+
import static io.kubernetes.client.util.Preconditions.precondition;
16+
1517
import java.util.Objects;
1618

1719
public class NamespaceName {
@@ -20,8 +22,8 @@ public class NamespaceName {
2022
private final String name;
2123

2224
public NamespaceName(String namespace, String name) {
23-
this.namespace = namespace;
24-
this.name = name;
25+
this.namespace = precondition(namespace, Objects::isNull, () -> "namespace must not be null");
26+
this.name = precondition(name, Objects::isNull, () -> "name must not be null");
2527
}
2628

2729
public String getNamespace() {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.apimachinery;
14+
15+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
16+
17+
import org.assertj.core.api.Assertions;
18+
import org.junit.Test;
19+
20+
public class GroupVersionKindTest {
21+
22+
@Test
23+
public void testInvalidKind() {
24+
assertThatThrownBy(() -> new GroupVersionKind("group", "version", null))
25+
.isInstanceOf(IllegalArgumentException.class)
26+
.hasMessage("kind must not be null");
27+
}
28+
29+
@Test
30+
public void testValidKind() {
31+
Assertions.assertThatNoException()
32+
.isThrownBy(() -> new GroupVersionKind("group", "version", "kind"));
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.apimachinery;
14+
15+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
16+
17+
import org.assertj.core.api.Assertions;
18+
import org.junit.Test;
19+
20+
public class GroupVersionResourceTest {
21+
@Test
22+
public void testInvalidResource() {
23+
assertThatThrownBy(() -> new GroupVersionResource("group", "version", null))
24+
.isInstanceOf(IllegalArgumentException.class)
25+
.hasMessage("resource must not be null");
26+
}
27+
28+
@Test
29+
public void testValidResource() {
30+
Assertions.assertThatNoException()
31+
.isThrownBy(() -> new GroupVersionResource("group", "version", "resource"));
32+
}
33+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import static org.assertj.core.api.Assertions.assertThat;
1616
import static org.assertj.core.api.Assertions.assertThatThrownBy;
17-
import static org.junit.Assert.*;
1817

1918
import io.kubernetes.client.openapi.models.V1Deployment;
2019
import io.kubernetes.client.openapi.models.V1Pod;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.apimachinery;
14+
15+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
16+
17+
import org.assertj.core.api.Assertions;
18+
import org.junit.Test;
19+
20+
public class NamespaceNameTest {
21+
22+
@Test
23+
public void testInvalidNamespace() {
24+
assertThatThrownBy(() -> new NamespaceName(null, "name"))
25+
.isInstanceOf(IllegalArgumentException.class)
26+
.hasMessage("namespace must not be null");
27+
}
28+
29+
@Test
30+
public void testInvalidName() {
31+
assertThatThrownBy(() -> new NamespaceName("namespace", null))
32+
.isInstanceOf(IllegalArgumentException.class)
33+
.hasMessage("name must not be null");
34+
}
35+
36+
@Test
37+
public void testValidNamespaceName() {
38+
Assertions.assertThatNoException().isThrownBy(() -> new NamespaceName("namespace", "name"));
39+
}
40+
}

0 commit comments

Comments
 (0)