|
| 1 | +/** |
| 2 | + * Copyright (C) 2015 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.fabric8.kubernetes.client.mock; |
| 17 | + |
| 18 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 19 | +import io.fabric8.kubernetes.api.model.flowcontrol.v1.FlowSchema; |
| 20 | +import io.fabric8.kubernetes.api.model.flowcontrol.v1.FlowSchemaBuilder; |
| 21 | +import io.fabric8.kubernetes.api.model.flowcontrol.v1.FlowSchemaList; |
| 22 | +import io.fabric8.kubernetes.api.model.flowcontrol.v1.FlowSchemaListBuilder; |
| 23 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 24 | +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; |
| 25 | +import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; |
| 26 | +import org.assertj.core.api.AssertionsForClassTypes; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.net.HttpURLConnection; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; |
| 33 | + |
| 34 | +@EnableKubernetesMockClient |
| 35 | +class V1FlowSchemaTest { |
| 36 | + private KubernetesMockServer server; |
| 37 | + private KubernetesClient client; |
| 38 | + |
| 39 | + @Test |
| 40 | + void load() { |
| 41 | + List<HasMetadata> items = client.load(getClass().getResourceAsStream("/v1-flowschema.yaml")).items(); |
| 42 | + assertThat(items).isNotNull().hasSize(1); |
| 43 | + AssertionsForClassTypes.assertThat(items.get(0)) |
| 44 | + .isInstanceOf(FlowSchema.class) |
| 45 | + .hasFieldOrPropertyWithValue("metadata.name", "health-for-strangers"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void get() { |
| 50 | + // Given |
| 51 | + server.expect().get().withPath("/apis/flowcontrol.apiserver.k8s.io/v1/flowschemas/exempt") |
| 52 | + .andReturn(HttpURLConnection.HTTP_OK, createFlowSchema("exempt")) |
| 53 | + .once(); |
| 54 | + |
| 55 | + // When |
| 56 | + FlowSchema flowSchema = client.flowControl().v1().flowSchema().withName("exempt").get(); |
| 57 | + |
| 58 | + // Then |
| 59 | + AssertionsForClassTypes.assertThat(flowSchema) |
| 60 | + .isNotNull() |
| 61 | + .hasFieldOrPropertyWithValue("metadata.name", "exempt"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void list() { |
| 66 | + // Given |
| 67 | + server.expect().get().withPath("/apis/flowcontrol.apiserver.k8s.io/v1/flowschemas") |
| 68 | + .andReturn(HttpURLConnection.HTTP_OK, new FlowSchemaListBuilder() |
| 69 | + .addToItems(createFlowSchema("exempt")) |
| 70 | + .build()) |
| 71 | + .once(); |
| 72 | + |
| 73 | + // When |
| 74 | + FlowSchemaList flowSchemas = client.flowControl().v1().flowSchema().list(); |
| 75 | + |
| 76 | + // Then |
| 77 | + AssertionsForClassTypes.assertThat(flowSchemas).isNotNull(); |
| 78 | + assertThat(flowSchemas.getItems()).hasSize(1); |
| 79 | + AssertionsForClassTypes.assertThat(flowSchemas.getItems().get(0)) |
| 80 | + .hasFieldOrPropertyWithValue("metadata.name", "exempt"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void create() { |
| 85 | + // Given |
| 86 | + FlowSchema flowSchema = createFlowSchema("flowschema1"); |
| 87 | + server.expect().post().withPath("/apis/flowcontrol.apiserver.k8s.io/v1/flowschemas") |
| 88 | + .andReturn(HttpURLConnection.HTTP_OK, flowSchema) |
| 89 | + .once(); |
| 90 | + |
| 91 | + // When |
| 92 | + FlowSchema createdFlowSchema = client.flowControl().v1().flowSchema().resource(flowSchema).create(); |
| 93 | + |
| 94 | + // Then |
| 95 | + AssertionsForClassTypes.assertThat(createdFlowSchema).isNotNull(); |
| 96 | + AssertionsForClassTypes.assertThat(createdFlowSchema) |
| 97 | + .hasFieldOrPropertyWithValue("metadata.name", "flowschema1"); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void delete() { |
| 102 | + // Given |
| 103 | + FlowSchema flowSchema = createFlowSchema("flowschema1"); |
| 104 | + server.expect().delete().withPath("/apis/flowcontrol.apiserver.k8s.io/v1/flowschemas/flowschema1") |
| 105 | + .andReturn(HttpURLConnection.HTTP_OK, flowSchema) |
| 106 | + .once(); |
| 107 | + |
| 108 | + // When |
| 109 | + boolean isDeleted = client.flowControl().v1().flowSchema().withName("flowschema1").delete().size() == 1; |
| 110 | + |
| 111 | + // Then |
| 112 | + AssertionsForClassTypes.assertThat(isDeleted).isTrue(); |
| 113 | + } |
| 114 | + |
| 115 | + private FlowSchema createFlowSchema(String name) { |
| 116 | + return new FlowSchemaBuilder() |
| 117 | + .withNewMetadata().withName(name).endMetadata() |
| 118 | + .withNewSpec() |
| 119 | + .withMatchingPrecedence(1) |
| 120 | + .withNewPriorityLevelConfiguration() |
| 121 | + .withName(name) |
| 122 | + .endPriorityLevelConfiguration() |
| 123 | + .addNewRule() |
| 124 | + .addNewNonResourceRule() |
| 125 | + .addToNonResourceURLs("*") |
| 126 | + .addToVerbs("*") |
| 127 | + .endNonResourceRule() |
| 128 | + .addNewResourceRule() |
| 129 | + .addToApiGroups("*") |
| 130 | + .endResourceRule() |
| 131 | + .addNewSubject() |
| 132 | + .withNewGroup() |
| 133 | + .withName("system:masters") |
| 134 | + .endGroup() |
| 135 | + .withKind("Group") |
| 136 | + .endSubject() |
| 137 | + .endRule() |
| 138 | + .endSpec() |
| 139 | + .build(); |
| 140 | + } |
| 141 | +} |
0 commit comments