Skip to content

Commit

Permalink
fix: IntOrString supports null values
Browse files Browse the repository at this point in the history
- For backwards compatibility, IntOrString should still support
null values.

Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Apr 21, 2022
1 parent ca34275 commit cede6aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ public class IntOrString implements Serializable {

private Object value;

public IntOrString() { }
public IntOrString() {
}

//Builders are generated for the first non-empty constructor found.
@Buildable(editableEnabled = false, generateBuilderPackage=true, builderPackage = "io.fabric8.kubernetes.api.builder")
@Buildable(editableEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder")
public IntOrString(Object value) {
if (value instanceof Integer || value instanceof String) {
if (value == null || value instanceof Integer || value instanceof String) {
this.value = value;
} else {
throw new IllegalArgumentException("Either integer or string value needs to be provided");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void builder_withIntVal_shouldSetIntValAndKind() {

// Then
assertThat(intOrString)
.hasFieldOrPropertyWithValue("value", 89);
.hasFieldOrPropertyWithValue("value", 89);
}

@Test
Expand All @@ -83,19 +83,28 @@ void builder_withStrVal_shouldSetStrValAndKind() {

// Then
assertThat(intOrString)
.hasFieldOrPropertyWithValue("value", "89");
.hasFieldOrPropertyWithValue("value", "89");
}

@Test
void builder_withEmpty_shouldThrowException() {
void builder_withEmpty_shouldSetNullValue() {
// Given
IntOrStringBuilder intOrStringBuilder = new IntOrStringBuilder();
// When
final IntOrString result = intOrStringBuilder.build();
// Then
assertThat(result)
.hasFieldOrPropertyWithValue("value", null);
}

@Test
void builder_withBoolean_shouldThrowException() {
// Given
IntOrStringBuilder intOrStringBuilder = new IntOrStringBuilder().withValue(true);
// When
IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, intOrStringBuilder::build);

// Then
assertThat(illegalArgumentException)
.hasMessage("Either integer or string value needs to be provided");
.hasMessage("Either integer or string value needs to be provided");
}
}

0 comments on commit cede6aa

Please sign in to comment.