|
1 | 1 | package io.javaoperatorsdk.webhook.admission; |
2 | 2 |
|
3 | 3 | import java.util.concurrent.CompletableFuture; |
4 | | -import java.util.concurrent.ExecutionException; |
5 | | - |
6 | | -import org.junit.jupiter.api.Test; |
| 4 | +import java.util.concurrent.CompletionException; |
7 | 5 |
|
8 | 6 | import io.fabric8.kubernetes.api.model.HasMetadata; |
9 | 7 | import io.javaoperatorsdk.webhook.admission.mutation.AsyncMutator; |
| 8 | +import io.javaoperatorsdk.webhook.admission.mutation.Mutator; |
| 9 | +import io.javaoperatorsdk.webhook.admission.validation.Validator; |
| 10 | +import org.junit.jupiter.api.Test; |
10 | 11 |
|
11 | | -import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.*; |
| 12 | +import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.LABEL_KEY; |
| 13 | +import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.LABEL_TEST_VALUE; |
| 14 | +import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.MISSING_REQUIRED_LABEL; |
12 | 15 |
|
13 | 16 | class AsyncAdmissionControllerTest { |
14 | 17 |
|
15 | 18 | AdmissionTestSupport admissionTestSupport = new AdmissionTestSupport(); |
16 | 19 |
|
17 | 20 | @Test |
18 | | - void validatesResource() throws ExecutionException, InterruptedException { |
19 | | - AsyncAdmissionController<HasMetadata> admissionController = |
20 | | - new AsyncAdmissionController<>((resource, operation) -> { |
21 | | - if (resource.getMetadata().getLabels().get(LABEL_KEY) == null) { |
22 | | - throw new NotAllowedException(MISSING_REQUIRED_LABEL); |
23 | | - } |
24 | | - }); |
25 | | - |
| 21 | + void validatesResource() { |
| 22 | + var admissionController = new AsyncAdmissionController<HasMetadata>((resource, operation) -> { |
| 23 | + }); |
26 | 24 |
|
27 | 25 | admissionTestSupport |
28 | 26 | .validatesResource(res -> admissionController.handle(res).toCompletableFuture().join()); |
| 27 | + } |
29 | 28 |
|
| 29 | + @Test |
| 30 | + void validatesResource_whenNotAllowedException() { |
| 31 | + var admissionController = |
| 32 | + new AsyncAdmissionController<>((Validator<HasMetadata>) (resource, operation) -> { |
| 33 | + throw new NotAllowedException(MISSING_REQUIRED_LABEL); |
| 34 | + }); |
| 35 | + |
| 36 | + admissionTestSupport |
| 37 | + .notAllowedException(res -> admissionController.handle(res).toCompletableFuture().join()); |
| 38 | + } |
30 | 39 |
|
| 40 | + @Test |
| 41 | + void validatesResource_whenOtherException() { |
| 42 | + var admissionController = |
| 43 | + new AsyncAdmissionController<>((Validator<HasMetadata>) (resource, operation) -> { |
| 44 | + throw new IllegalArgumentException("Invalid resource"); |
| 45 | + }); |
| 46 | + |
| 47 | + admissionTestSupport.assertThatThrownBy( |
| 48 | + res -> admissionController.handle(res).toCompletableFuture() |
| 49 | + .join()) |
| 50 | + .isInstanceOf(CompletionException.class) |
| 51 | + .hasCauseInstanceOf(IllegalStateException.class) |
| 52 | + .hasRootCauseInstanceOf(IllegalArgumentException.class) |
| 53 | + .hasRootCauseMessage("Invalid resource"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void mutatesResource_withMutator() { |
| 58 | + var admissionController = |
| 59 | + new AsyncAdmissionController<>((Mutator<HasMetadata>) (resource, |
| 60 | + operation) -> { |
| 61 | + resource.getMetadata().getLabels().putIfAbsent(LABEL_KEY, LABEL_TEST_VALUE); |
| 62 | + return resource; |
| 63 | + }); |
| 64 | + |
| 65 | + admissionTestSupport |
| 66 | + .mutatesResource(res -> admissionController.handle(res).toCompletableFuture().join()); |
31 | 67 | } |
32 | 68 |
|
33 | 69 | @Test |
34 | | - void mutatesResource() throws ExecutionException, InterruptedException { |
35 | | - AsyncAdmissionController<HasMetadata> admissionController = |
| 70 | + void mutatesResource_withAsyncMutator() { |
| 71 | + var admissionController = |
36 | 72 | new AsyncAdmissionController<>((AsyncMutator<HasMetadata>) (resource, |
37 | 73 | operation) -> CompletableFuture.supplyAsync(() -> { |
38 | | - resource.getMetadata().getLabels().putIfAbsent(LABEL_KEY, LABEL_TEST_VALUE); |
39 | | - return resource; |
40 | | - })); |
| 74 | + resource.getMetadata().getLabels().putIfAbsent(LABEL_KEY, LABEL_TEST_VALUE); |
| 75 | + return resource; |
| 76 | + })); |
41 | 77 |
|
42 | 78 | admissionTestSupport |
43 | 79 | .mutatesResource(res -> admissionController.handle(res).toCompletableFuture().join()); |
44 | 80 | } |
45 | 81 |
|
| 82 | + @Test |
| 83 | + void mutatesResource_withMutator_whenNotAllowedException() { |
| 84 | + var admissionController = |
| 85 | + new AsyncAdmissionController<>((Mutator<HasMetadata>) (resource, |
| 86 | + operation) -> { |
| 87 | + throw new NotAllowedException(MISSING_REQUIRED_LABEL); |
| 88 | + }); |
| 89 | + |
| 90 | + admissionTestSupport.notAllowedException( |
| 91 | + res -> admissionController.handle(res).toCompletableFuture().join()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void mutatesResource_withAsyncMutator_whenNotAllowedException() { |
| 96 | + var admissionController = |
| 97 | + new AsyncAdmissionController<>((AsyncMutator<HasMetadata>) (resource, |
| 98 | + operation) -> CompletableFuture.supplyAsync(() -> { |
| 99 | + throw new NotAllowedException(MISSING_REQUIRED_LABEL); |
| 100 | + })); |
| 101 | + |
| 102 | + admissionTestSupport.notAllowedException( |
| 103 | + res -> admissionController.handle(res).toCompletableFuture().join()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void mutatesResource_withMutator_whenOtherException() { |
| 108 | + var admissionController = |
| 109 | + new AsyncAdmissionController<>((Mutator<HasMetadata>) (resource, |
| 110 | + operation) -> { |
| 111 | + throw new IllegalArgumentException("Invalid resource"); |
| 112 | + }); |
| 113 | + |
| 114 | + admissionTestSupport.assertThatThrownBy( |
| 115 | + res -> admissionController.handle(res).toCompletableFuture() |
| 116 | + .join()) |
| 117 | + .isInstanceOf(CompletionException.class) |
| 118 | + .hasCauseInstanceOf(IllegalStateException.class) |
| 119 | + .hasRootCauseInstanceOf(IllegalArgumentException.class) |
| 120 | + .hasRootCauseMessage("Invalid resource"); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void mutatesResource_withAsyncMutator_whenOtherException() { |
| 125 | + var admissionController = |
| 126 | + new AsyncAdmissionController<>((AsyncMutator<HasMetadata>) (resource, |
| 127 | + operation) -> CompletableFuture.supplyAsync(() -> { |
| 128 | + throw new IllegalArgumentException("Invalid resource"); |
| 129 | + })); |
| 130 | + |
| 131 | + admissionTestSupport.assertThatThrownBy( |
| 132 | + res -> admissionController.handle(res).toCompletableFuture() |
| 133 | + .join()) |
| 134 | + .isInstanceOf(CompletionException.class) |
| 135 | + .hasCauseInstanceOf(IllegalStateException.class) |
| 136 | + .hasRootCauseInstanceOf(IllegalArgumentException.class) |
| 137 | + .hasRootCauseMessage("Invalid resource"); |
| 138 | + } |
46 | 139 | } |
0 commit comments