Skip to content

Commit

Permalink
adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vaidikcode committed Oct 21, 2024
1 parent 3c7f69c commit 963b83e
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions util/src/test/java/io/kubernetes/client/util/YamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.kubernetes.client.Resources;
import io.kubernetes.client.common.KubernetesType;
Expand All @@ -29,11 +30,14 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Reader;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.yaml.snakeyaml.error.MarkedYAMLException;

class YamlTest {
Expand Down Expand Up @@ -265,4 +269,50 @@ void loadDumpCRDWithIntOrStringExtension() {
String dumped = Yaml.dump(crd);
assertThat(dumped).isEqualTo(data);
}
@Test
void testLoadAndSubmitResourceSuccess() throws Exception {
String yamlContent =
"apiVersion: v1\n" +
"kind: Pod\n" +
"metadata:\n" +
" name: test-pod\n";

Reader reader = new StringReader(yamlContent);

try (MockedStatic<Yaml> yamlMock = Mockito.mockStatic(Yaml.class)) {
V1Pod mockPod = new V1Pod();
yamlMock.when(() -> Yaml.load(reader)).thenReturn(mockPod);

Object result = Yaml.loadAndSubmitResource(reader);

assertThat(result).isNotNull();
assertThat(result).isInstanceOf(V1Pod.class);
}
}

@Test
void testLoadAndSubmitResourceInvalidYaml() {
String invalidYamlContent = "invalid: yaml";
Reader reader = new StringReader(invalidYamlContent);

Exception exception = assertThrows(Exception.class, () -> {
Yaml.loadAndSubmitResource(reader);
});

assertThat(exception.getMessage()).contains("Invalid YAML");
}

@Test
void testSubmitResourceToApiFailure() throws Exception {
V1Pod pod = new V1Pod();
String group = "apps";
String version = "v1";
String resourcePlural = "pods";

Exception exception = assertThrows(RuntimeException.class, () -> {
Yaml.submitResourceToApi(pod, V1Pod.class, group, version, resourcePlural);
});

assertThat(exception.getMessage()).contains("Failed to create resource");
}
}

0 comments on commit 963b83e

Please sign in to comment.