diff --git a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesResponseComposerTest.java b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesResponseComposerTest.java new file mode 100644 index 00000000000..dc6817c534f --- /dev/null +++ b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesResponseComposerTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * 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.fabric8.kubernetes.client.server.mock; + +import io.fabric8.kubernetes.api.model.KubernetesResourceList; +import io.fabric8.kubernetes.client.utils.Serialization; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.stream.Stream; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +class KubernetesResponseComposerTest { + private KubernetesResponseComposer kubernetesResponseComposer; + + @BeforeEach + void setUp() { + kubernetesResponseComposer = new KubernetesResponseComposer(); + } + + static Stream composeInput() { + return Stream.of( + // https://github.com/fabric8io/kubernetes-client/issues/6220 +// arguments("Secret", "SecretList"), + arguments("", "List")); + } + + @ParameterizedTest + @MethodSource("composeInput") + void compose_whenKindProvidedInListItem_shouldInferListKindCorrectly(String providedKind, String parsedListKind) { + // Given + Collection collection = new ArrayList<>(); + collection.add(String.format("{\"kind\": \"%s\", \"apiVersion\": \"v1\", \"metadata\": {}}", providedKind)); + + // When + String listString = kubernetesResponseComposer.compose(collection, "1"); + + // Then + assertThat(Serialization.unmarshal(listString, KubernetesResourceList.class)) + .hasFieldOrPropertyWithValue("kind", parsedListKind); + } +} diff --git a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherGetTest.java b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherGetTest.java new file mode 100644 index 00000000000..0a7ee7cfe0c --- /dev/null +++ b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherGetTest.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * 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.fabric8.kubernetes.client.server.mock.crud; + +import io.fabric8.kubernetes.api.model.ConfigMap; +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; +import io.fabric8.kubernetes.api.model.ConfigMapList; +import io.fabric8.kubernetes.client.dsl.NonDeletingOperation; +import org.assertj.core.api.InstanceOfAssertFactories; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class KubernetesCrudDispatcherGetTest extends KubernetesCrudDispatcherTestBase { + + @Nested + class WithNoItems { + + @BeforeEach + void clearItems() { + client.resources(ConfigMap.class).delete(); + } + + @Test + void singleItem() { + assertThat(client.configMaps().withName("config-map-1").get()).isNull(); + } + + @Test + @Disabled("#6220: Need to figure out a way to extract the singular from the plural form in URL") + void multipleItems_shouldReturnListObjectWithCorrectKind() { + // When + final ConfigMapList result = client.configMaps().list(); + // Then + assertThat(result) + .hasFieldOrPropertyWithValue("kind", "ConfigMapList") + .extracting(ConfigMapList::getItems) + .asInstanceOf(InstanceOfAssertFactories.list(ConfigMap.class)) + .isEmpty(); + } + + } + + @Nested + class WithValidItems { + + @BeforeEach + void addConsistentItems() { + for (int it = 0; it < 5; it++) { + client.resource(new ConfigMapBuilder().withNewMetadata().withName("config-map-" + it).endMetadata().build()) + .createOr(NonDeletingOperation::update); + } + } + + @Test + void singleItem() { + assertThat(client.configMaps().withName("config-map-1").get()) + .isInstanceOf(ConfigMap.class); + } + + @Test + @Disabled("#6220: Need to figure out a way to extract the singular from the plural form in URL") + void multipleItems_shouldReturnListObjectWithCorrectKind() { + // When + final ConfigMapList result = client.configMaps().list(); + // Then + assertThat(result) + .hasFieldOrPropertyWithValue("kind", "ConfigMapList") + .extracting(ConfigMapList::getItems) + .asInstanceOf(InstanceOfAssertFactories.list(ConfigMap.class)) + .extracting("kind") + .containsOnly("ConfigMap"); + } + } + + @Nested + class WithInvalidItems { + @BeforeEach + void addInvalidItems() throws Exception { + client.configMaps().resource(new ConfigMapBuilder() + .withKind("NotConfigMap") + .withNewMetadata().withName("NotConfigMap").endMetadata() + .build()) + .create(); + for (int it = 0; it < 5; it++) { + client.resource(new ConfigMapBuilder().withNewMetadata().withName("config-map-" + it).endMetadata().build()) + .createOr(NonDeletingOperation::update); + } + client.getHttpClient().sendAsync(client.getHttpClient().newHttpRequestBuilder() + .uri(server.url("/api/v1/namespaces/test/configmaps")) + .post("application/json", "{\"kind\":\"NotConfigMap\"}") + .build(), String.class) + .get(10, TimeUnit.SECONDS); + } + + @BeforeEach + void addConsistentItems() { + for (int it = 0; it < 10; it++) { + client.resource(new ConfigMapBuilder().withNewMetadata().withName("config-map-" + it).endMetadata().build()) + .createOr(NonDeletingOperation::update); + } + } + + @Test + void singleItem() { + assertThat(client.configMaps().withName("config-map-1").get()) + .isInstanceOf(ConfigMap.class); + } + + @Test + @Disabled("#6220: Need to figure out a way to extract the singular from the plural form in URL") + void multipleItems_shouldReturnListObjectWithCorrectKind() { + // When + final ConfigMapList result = client.configMaps().list(); + // Then + assertThat(result) + .hasFieldOrPropertyWithValue("kind", "ConfigMapList") + .extracting(ConfigMapList::getItems) + .asInstanceOf(InstanceOfAssertFactories.list(ConfigMap.class)) + .extracting("kind") + .containsOnly("ConfigMap"); + } + } +}