10000 used Preconditions.precondition + add tests · tony-clarke-amdocs/java@ac49aec · GitHub
[go: up one dir, main page]

Skip to content

Commit ac49aec

Browse files
committed
used Preconditions.precondition + add tests
1 parent 614f0de commit ac49aec

File tree

7 files changed

+120
-11
lines changed

7 files changed

+120
-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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@
1313
package io.kubernetes.client.apimachinery;
1414

1515
import java.util.Objects;
16+
import static io.kubernetes.client.util.Preconditions.precondition;
1617

1718
public class GroupVersionResource extends GroupVersion {
1819

1920
private final String resource;
2021

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

2927
@Override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
package io.kubernetes.client.apimachinery;
1414

1515
import java.util.Objects;
16+
import static io.kubernetes.client.util.Preconditions.precondition;
1617

1718
public class NamespaceName {
1819

1920
private final String namespace;
2021
private final String name;
2122

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

2728
public String getNamespace() {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/** @author wind57 */
21+
public class GroupVersionKindTest {
22+
23+
@Test
24+
public void testInvalidKind() {
25+
assertThatThrownBy(() -> new GroupVersionKind("group", "version", null))
26+
.isInstanceOf(IllegalArgumentException.class)
27+
.hasMessage("kind must not be null");
28+
}
29+
30+
@Test
31+
public void testValidKind() {
32+
Assertions.assertThatNoException()
33+
.isThrownBy(() -> new GroupVersionKind("group", "version", "kind"));
34+
}
35+
}
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+
/** @author wind57 */
21+
public class GroupVersionResourceTest {
22+
@Test
23+
public void testInvalidResource() {
24+
assertThatThrownBy(() -> new GroupVersionResource("group", "version", null))
25+
.isInstanceOf(IllegalArgumentException.class)
26+
.hasMessage("resource must not be null");
27+
}
28+
29+
@Test
30+
public void testValidResource() {
31+
Assertions.assertThatNoException()
32+
.isThrownBy(() -> new GroupVersionResource("group", "version", "resource"));
33+
}
34+
}

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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
/** @author wind57 */
21+
public class NamespaceNameTest {
22+
23+
@Test
24+
public void testInvalidNamespace() {
25+
assertThatThrownBy(() -> new NamespaceName(null, "name"))
26+
.isInstanceOf(IllegalArgumentException.class)
27+
.hasMessage("namespace must not be null");
28+
}
29+
30+
@Test
31+
public void testInvalidName() {
32+
assertThatThrownBy(() -> new NamespaceName("namespace", null))
33+
.isInstanceOf(IllegalArgumentException.class)
34+
.hasMessage("name must not be null");
35+
}
36+
37+
@Test
38+
public void testValidNamespaceName() {
39+
Assertions.assertThatNoException()
40+
.isThrownBy(() -> new NamespaceName("namespace", "name"));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)
0