Skip to content

Commit 41b00bd

Browse files
authored
feat: conversion error test (#41)
1 parent 237d191 commit 41b00bd

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/admission/sample/quarkus/conversion/ConversionEndpointTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,43 @@ void asyncConversion() {
3131
testConversion(ASYNC_CONVERSION_PATH);
3232
}
3333

34+
@Test
35+
void errorConversion() {
36+
testErrorConversion(CONVERSION_PATH);
37+
}
38+
39+
@Test
40+
void asyncErrorConversion() {
41+
testErrorConversion(ASYNC_CONVERSION_PATH);
42+
}
43+
44+
private void testErrorConversion(String conversionPath) {
45+
given().contentType(ContentType.JSON)
46+
.body(errorRequest())
47+
.when().post("/" + conversionPath)
48+
.then()
49+
.statusCode(500);
50+
}
51+
3452
public void testConversion(String path) {
3553
given().contentType(ContentType.JSON)
36-
.body(jsonRequest())
54+
.body(request())
3755
.when().post("/" + path)
3856
.then()
3957
.statusCode(200)
4058
.body(is(expectedResult));
4159
}
4260

43-
private String jsonRequest() {
44-
try (InputStream is = this.getClass().getResourceAsStream("/conversion-request.json")) {
61+
private String errorRequest() {
62+
return request("/conversion-error-request.json");
63+
}
64+
65+
private String request() {
66+
return request("/conversion-request.json");
67+
}
68+
69+
private String request(String path) {
70+
try (InputStream is = this.getClass().getResourceAsStream(path)) {
4571
return new String(is.readAllBytes(), StandardCharsets.UTF_8);
4672
} catch (IOException e) {
4773
throw new IllegalStateException(e);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"apiVersion": "apiextensions.k8s.io/v1",
3+
"kind": "ConversionReview",
4+
"request": {
5+
6+
"uid": "705ab4f5-6393-11e8-b7cc-42010a800002",
7+
8+
"desiredAPIVersion": "sample.javaoperatorsdk/v2",
9+
10+
"objects": [
11+
{
12+
"kind": "MultiVersionCustomResource",
13+
"apiVersion": "sample.javaoperatorsdk/v2",
14+
"metadata": {
15+
"creationTimestamp": "2021-09-04T14:03:02Z",
16+
"name": "resource1",
17+
"namespace": "default",
18+
"resourceVersion": "143",
19+
"uid": "3415a7fc-162b-4300-b5da-fd6083580d66"
20+
},
21+
"spec": {
22+
"value": "non integer"
23+
},
24+
"status":{
25+
"ready": true
26+
}
27+
}
28+
]
29+
}
30+
}

samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionEndpointTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.nio.file.Files;
55

6+
import org.jetbrains.annotations.NotNull;
67
import org.junit.jupiter.api.Test;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.beans.factory.annotation.Value;
@@ -32,6 +33,8 @@ class ConversionEndpointTest {
3233
@Value("classpath:conversion-request.json")
3334
private Resource request;
3435

36+
@Value("classpath:conversion-error-request.json")
37+
private Resource errorRequest;
3538

3639
@Test
3740
void convert() {
@@ -43,6 +46,23 @@ void asyncConvert() {
4346
testConversion(ASYNC_CONVERSION_PATH);
4447
}
4548

49+
@Test
50+
void errorConversion() {
51+
testErrorConversion(CONVERSION_PATH);
52+
}
53+
54+
@Test
55+
void asyncErrorConversion() {
56+
testErrorConversion(ASYNC_CONVERSION_PATH);
57+
}
58+
59+
private void testErrorConversion(String conversionPath) {
60+
webClient.post().uri("/" + conversionPath).contentType(MediaType.APPLICATION_JSON)
61+
.body(errorRequest())
62+
.exchange()
63+
.expectStatus().is5xxServerError();
64+
}
65+
4666
public void testConversion(String path) {
4767
webClient.post().uri("/" + path).contentType(MediaType.APPLICATION_JSON)
4868
.body(request())
@@ -57,11 +77,19 @@ public void testConversion(String path) {
5777
}
5878

5979
private BodyInserter<String, ReactiveHttpOutputMessage> request() {
80+
return requestFromResource(request);
81+
}
82+
83+
private BodyInserter<String, ReactiveHttpOutputMessage> errorRequest() {
84+
return requestFromResource(errorRequest);
85+
}
86+
87+
@NotNull
88+
private BodyInserter<String, ReactiveHttpOutputMessage> requestFromResource(Resource request) {
6089
try {
6190
return BodyInserters.fromValue(new String(Files.readAllBytes(request.getFile().toPath())));
6291
} catch (IOException e) {
6392
throw new IllegalStateException(e);
6493
}
6594
}
66-
6795
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"apiVersion": "apiextensions.k8s.io/v1",
3+
"kind": "ConversionReview",
4+
"request": {
5+
6+
"uid": "705ab4f5-6393-11e8-b7cc-42010a800002",
7+
8+
"desiredAPIVersion": "sample.javaoperatorsdk/v2",
9+
10+
"objects": [
11+
{
12+
"kind": "MultiVersionCustomResource",
13+
"apiVersion": "sample.javaoperatorsdk/v2",
14+
"metadata": {
15+
"creationTimestamp": "2021-09-04T14:03:02Z",
16+
"name": "resource1",
17+
"namespace": "default",
18+
"resourceVersion": "143",
19+
"uid": "3415a7fc-162b-4300-b5da-fd6083580d66"
20+
},
21+
"spec": {
22+
"value": "non integer"
23+
},
24+
"status":{
25+
"ready": true
26+
}
27+
}
28+
]
29+
}
30+
}

0 commit comments

Comments
 (0)