Skip to content

feat: conversion error test #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,43 @@ void asyncConversion() {
testConversion(ASYNC_CONVERSION_PATH);
}

@Test
void errorConversion() {
testErrorConversion(CONVERSION_PATH);
}

@Test
void asyncErrorConversion() {
testErrorConversion(ASYNC_CONVERSION_PATH);
}

private void testErrorConversion(String conversionPath) {
given().contentType(ContentType.JSON)
.body(errorRequest())
.when().post("/" + conversionPath)
.then()
.statusCode(500);
}

public void testConversion(String path) {
given().contentType(ContentType.JSON)
.body(jsonRequest())
.body(request())
.when().post("/" + path)
.then()
.statusCode(200)
.body(is(expectedResult));
}

private String jsonRequest() {
try (InputStream is = this.getClass().getResourceAsStream("/conversion-request.json")) {
private String errorRequest() {
return request("/conversion-error-request.json");
}

private String request() {
return request("/conversion-request.json");
}

private String request(String path) {
try (InputStream is = this.getClass().getResourceAsStream(path)) {
return new String(is.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException(e);
Expand Down
30 changes: 30 additions & 0 deletions samples/quarkus/src/test/resources/conversion-error-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "ConversionReview",
"request": {

"uid": "705ab4f5-6393-11e8-b7cc-42010a800002",

"desiredAPIVersion": "sample.javaoperatorsdk/v2",

"objects": [
{
"kind": "MultiVersionCustomResource",
"apiVersion": "sample.javaoperatorsdk/v2",
"metadata": {
"creationTimestamp": "2021-09-04T14:03:02Z",
"name": "resource1",
"namespace": "default",
"resourceVersion": "143",
"uid": "3415a7fc-162b-4300-b5da-fd6083580d66"
},
"spec": {
"value": "non integer"
},
"status":{
"ready": true
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.nio.file.Files;

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

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

@Test
void convert() {
Expand All @@ -43,6 +46,23 @@ void asyncConvert() {
testConversion(ASYNC_CONVERSION_PATH);
}

@Test
void errorConversion() {
testErrorConversion(CONVERSION_PATH);
}

@Test
void asyncErrorConversion() {
testErrorConversion(ASYNC_CONVERSION_PATH);
}

private void testErrorConversion(String conversionPath) {
webClient.post().uri("/" + conversionPath).contentType(MediaType.APPLICATION_JSON)
.body(errorRequest())
.exchange()
.expectStatus().is5xxServerError();
}

public void testConversion(String path) {
webClient.post().uri("/" + path).contentType(MediaType.APPLICATION_JSON)
.body(request())
Expand All @@ -57,11 +77,19 @@ public void testConversion(String path) {
}

private BodyInserter<String, ReactiveHttpOutputMessage> request() {
return requestFromResource(request);
}

private BodyInserter<String, ReactiveHttpOutputMessage> errorRequest() {
return requestFromResource(errorRequest);
}

@NotNull
private BodyInserter<String, ReactiveHttpOutputMessage> requestFromResource(Resource request) {
try {
return BodyInserters.fromValue(new String(Files.readAllBytes(request.getFile().toPath())));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "ConversionReview",
"request": {

"uid": "705ab4f5-6393-11e8-b7cc-42010a800002",

"desiredAPIVersion": "sample.javaoperatorsdk/v2",

"objects": [
{
"kind": "MultiVersionCustomResource",
"apiVersion": "sample.javaoperatorsdk/v2",
"metadata": {
"creationTimestamp": "2021-09-04T14:03:02Z",
"name": "resource1",
"namespace": "default",
"resourceVersion": "143",
"uid": "3415a7fc-162b-4300-b5da-fd6083580d66"
},
"spec": {
"value": "non integer"
},
"status":{
"ready": true
}
}
]
}
}