Skip to content

Commit

Permalink
refactor: use java streams api to get rid of nested if statements in …
Browse files Browse the repository at this point in the history
…KubernetesClientUtil (3072)

refactor: use java streams api to get rid of nested if statements in KubernetesClientUtil

Signed-off-by: Rohit Satya <[email protected]>
---
add unit tests

Signed-off-by: Rohit Satya <[email protected]>
---
review : Address review comments related to imports and builders in KubernetesClientUtilTest

Signed-off-by: Rohan Kumar <[email protected]>

Co-authored-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohit-satya and rohanKanojia authored Jul 18, 2024
1 parent 5ba84cd commit 9fb1085
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,11 @@ protected static String getPodCondition(Pod pod) {
return "";
}


for (PodCondition condition : conditions) {
String type = condition.getType();
if (StringUtils.isNotBlank(type)) {
if ("ready".equalsIgnoreCase(type)) {
String statusText = condition.getStatus();
if (StringUtils.isNotBlank(statusText)) {
if (Boolean.parseBoolean(statusText)) {
return type;
}
}
}
}
}
return "";
return conditions.stream()
.filter(condition -> "ready".equalsIgnoreCase(condition.getType()) && Boolean.parseBoolean(condition.getStatus()))
.map(PodCondition::getType)
.findFirst()
.orElse("");
}

public static GenericKubernetesResource doGetCustomResource(KubernetesClient kubernetesClient, GenericKubernetesResource resource, String namespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,61 @@ void getPodStatusMessagePostfix_whenActionIsError_shouldReturnErrorMessage() {
assertThat(messagePostfix).isEqualTo(": Error");
}

@Test
void getPodCondition_whenPodConditionIsReadyAndTrue_shouldReturnReady() {
// Given
Pod pod = new PodBuilder()
.withNewStatus()
.addNewCondition()
.withType("ready")
.withStatus("True")
.endCondition()
.endStatus()
.build();

// When
String condition = KubernetesClientUtil.getPodCondition(pod);

// Then
assertThat(condition).isEqualTo("ready");
}

@Test
void getPodCondition_whenPodConditionIsReadyAndFalse_shouldReturnEmptyString() {
// Given
Pod pod = new PodBuilder()
.withNewStatus()
.addNewCondition()
.withType("ready")
.withStatus("False")
.endCondition()
.endStatus()
.build();

// When
String condition = KubernetesClientUtil.getPodCondition(pod);

// Then
assertThat(condition).isEmpty();
}

@Test
void getPodCondition_whenPodConditionIsNotReady_shouldReturnEmptyString() {
// Given
Pod pod = new PodBuilder()
.withNewStatus()
.addNewCondition()
.withType("notready")
.withStatus("True")
.endCondition()
.endStatus()
.build();

// When
String condition = KubernetesClientUtil.getPodCondition(pod);

// Then
assertThat(condition).isEmpty();
}

}

0 comments on commit 9fb1085

Please sign in to comment.