Skip to content

Commit

Permalink
Merge pull request #1468 from wind57/more-changes
Browse files Browse the repository at this point in the history
first commit
  • Loading branch information
k8s-ci-robot committed Jan 3, 2021
2 parents cd84eaa + fa4c16d commit a8e52f7
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 29 deletions.
Expand Up @@ -12,24 +12,24 @@
*/
package io.kubernetes.client.apimachinery;

import static io.kubernetes.client.util.Preconditions.precondition;

import io.kubernetes.client.common.KubernetesObject;
import io.kubernetes.client.util.Strings;
import java.util.Objects;

public class GroupVersion {

private static final GroupVersion GROUP_VERSION_V1 = new GroupVersion("", "v1");

public static GroupVersion parse(String apiVersion) {
if (Strings.isNullOrEmpty(apiVersion)) {
throw new IllegalArgumentException("No apiVersion found on object");
}
if ("v1".equals(apiVersion)) {
// legacy api group
return new GroupVersion("", "v1");
return GROUP_VERSION_V1;
}
precondition(apiVersion, Strings::isNullOrEmpty, () -> "apiVersion can not be null");
String[] parts = apiVersion.split("/");
if (parts.length != 2) {
throw new IllegalArgumentException("Invalid apiVersion found on object: " + apiVersion);
}
precondition(parts, x -> x.length != 2, () -> "Invalid apiVersion: " + apiVersion);
return new GroupVersion(parts[0], parts[1]);
}

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

public GroupVersion(String group, String version) {
if (group == null) {
throw new IllegalArgumentException("group must not be null");
}
if (version == null) {
throw new IllegalArgumentException("version must not be null");
}
this.group = group;
this.version = version;
this.group = precondition(group, Objects::isNull, () -> "group must not be null");
this.version = precondition(version, Objects::isNull, () -> "version must not be null");
}

private final String group;
Expand Down
45 changes: 45 additions & 0 deletions util/src/main/java/io/kubernetes/client/util/Preconditions.java
@@ -0,0 +1,45 @@
/*
Copyright 2021 The Kubernetes Authors.
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.
*/
package io.kubernetes.client.util;

import java.util.function.Predicate;
import java.util.function.Supplier;

/**
* support for different invariant checks, like for a example a non-null argument being passed to a
* method that does not support nulls.
*
* @author wind57
*/
public final class Preconditions {

private Preconditions() {
throw new AssertionError("not supported");
}

/**
* @param validate argument to be validated
* @param predicate to check against
* @param errorSupplier the error message generator
* @param <T>
* @return T if Predicate does not match
* @throws IllegalArgumentException if predicate returns true
*/
public static <T> T precondition(
T validate, Predicate<? super T> predicate, Supplier<String> errorSupplier) {
if (predicate.test(validate)) {
throw new IllegalArgumentException(errorSupplier.get());
}
return validate;
}
}
8 changes: 6 additions & 2 deletions util/src/main/java/io/kubernetes/client/util/Strings.java
Expand Up @@ -14,10 +14,14 @@

import javax.annotation.Nullable;

public class Strings {
public final class Strings {

private Strings() {
throw new AssertionError("not supported");
}

public static boolean isNullOrEmpty(String value) {
return value == null || value.length() == 0;
return value == null || value.isEmpty();
}

public static String nullToEmpty(@Nullable String string) {
Expand Down
Expand Up @@ -12,6 +12,8 @@
*/
package io.kubernetes.client.apimachinery;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.*;

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

@Test
public void parse() {
assertEquals(new GroupVersion("", "v1"), GroupVersion.parse(new V1Pod().apiVersion("v1")));
assertEquals(
new GroupVersion("apps", "v1"),
GroupVersion.parse(new V1Deployment().apiVersion("apps/v1")));
assertThrows(
IllegalArgumentException.class,
() -> {
GroupVersion.parse(new V1Pod());
GroupVersion.parse(new V1Pod().apiVersion(null));
GroupVersion.parse(new V1Pod().apiVersion("foo/bar/f"));
});
public void parseV1() {
GroupVersion left = new GroupVersion("", "v1");
GroupVersion right = GroupVersion.parse(new V1Pod().apiVersion("v1"));
assertThat(left).isEqualTo(right);
}

@Test
public void parseAppsV1() {
GroupVersion left = new GroupVersion("apps", "v1");
GroupVersion right = GroupVersion.parse(new V1Deployment().apiVersion("apps/v1"));
assertThat(left).isEqualTo(right);
}

@Test
public void parseInvalid1() {
assertThatThrownBy(() -> GroupVersion.parse(new V1Pod()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("apiVersion can not be null");
}

@Test
public void parseInvalid2() {
assertThatThrownBy(() -> GroupVersion.parse(new V1Pod().apiVersion(null)))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("apiVersion can not be null");
}

@Test
public void parseInvalid3() {
assertThatThrownBy(() -> GroupVersion.parse(new V1Pod().apiVersion("foo/bar/f")))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid apiVersion: foo/bar/f");
}
}
@@ -0,0 +1,36 @@
/*
Copyright 2021 The Kubernetes Authors.
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.
*/
package io.kubernetes.client.util;

import static io.kubernetes.client.util.Preconditions.precondition;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.assertj.core.api.Assertions;
import org.junit.Test;

public class PreconditionsTest {

@Test
public void testEmptyString() {
assertThatThrownBy(
() -> precondition("", Strings::isNullOrEmpty, () -> "string can not be empty"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("string can not be empty");
}

@Test
public void testNonEmptyString() {
String abc = precondition("abc", Strings::isNullOrEmpty, () -> "string can not be empty");
Assertions.assertThat(abc).isEqualTo("abc");
}
}

0 comments on commit a8e52f7

Please sign in to comment.