use of io.micronaut.http.hateoas.JsonError in project check-ins by objectcomputing.
the class QuestionCategoryControllerTest method testPUTQuestionCategoryNotFound.
@Test
public void testPUTQuestionCategoryNotFound() {
QuestionCategory requestBody = new QuestionCategory();
requestBody.setId(UUID.randomUUID());
requestBody.setName("Fake Category");
HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, () -> {
client.toBlocking().exchange(HttpRequest.PUT("/", requestBody).basicAuth(MEMBER_ROLE, MEMBER_ROLE));
});
JsonError responseBody = thrown.getResponse().getBody(JsonError.class).get();
assertEquals(("No category with id " + requestBody.getId()), responseBody.getMessage());
assertEquals(HttpStatus.NOT_FOUND, thrown.getStatus());
}
use of io.micronaut.http.hateoas.JsonError in project check-ins by objectcomputing.
the class QuestionCategoryControllerTest method testPUTNoIDSupplied.
@Test
public void testPUTNoIDSupplied() {
QuestionCategoryCreateDTO requestBody = new QuestionCategoryCreateDTO();
requestBody.setName("Fake Category");
HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, () -> {
client.toBlocking().exchange(HttpRequest.PUT("/", requestBody).basicAuth(ADMIN_ROLE, ADMIN_ROLE));
});
JsonError responseBody = thrown.getResponse().getBody(JsonError.class).get();
assertEquals("This question category does not exist", responseBody.getMessage());
assertEquals(HttpStatus.BAD_REQUEST, thrown.getStatus());
}
use of io.micronaut.http.hateoas.JsonError in project kestra by kestra-io.
the class ErrorControllerTest method type.
@Test
void type() {
Map<String, Object> flow = ImmutableMap.of("id", IdUtils.create(), "namespace", "io.kestra.test", "tasks", Collections.singletonList(ImmutableMap.of("id", IdUtils.create(), "type", "io.kestra.invalid")));
HttpClientResponseException exception = assertThrows(HttpClientResponseException.class, () -> {
client.toBlocking().retrieve(POST("/api/v1/flows", flow), Argument.of(Flow.class), Argument.of(JsonError.class));
});
assertThat(exception.getStatus(), is(HttpStatus.UNPROCESSABLE_ENTITY));
String response = exception.getResponse().getBody(String.class).get();
assertThat(response, containsString("Invalid type: io.kestra.invalid"));
assertThat(response, containsString("\"path\":\"io.kestra.core.models.flows.Flow[\\\"tasks\\\"] > java.util.ArrayList[0]\""));
assertThat(response, containsString("Failed to convert argument"));
// missing getter & setter on JsonError
// assertThat(exception.getResponse().getBody(JsonError.class).get().getEmbedded().get("errors").get().get(0).getPath(), containsInAnyOrder("tasks"));
}
use of io.micronaut.http.hateoas.JsonError in project kestra by kestra-io.
the class FlowControllerTest method invalidUpdateFlow.
@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
void invalidUpdateFlow() {
String flowId = IdUtils.create();
Flow flow = generateFlow(flowId, "io.kestra.unittest", "a");
Flow result = client.toBlocking().retrieve(POST("/api/v1/flows", flow), Flow.class);
assertThat(result.getId(), is(flow.getId()));
Flow finalFlow = generateFlow(IdUtils.create(), "io.kestra.unittest2", "b");
;
HttpClientResponseException e = assertThrows(HttpClientResponseException.class, () -> {
client.toBlocking().exchange(PUT("/api/v1/flows/" + flow.getNamespace() + "/" + flowId, finalFlow), Argument.of(Flow.class), Argument.of(JsonError.class));
});
String jsonError = e.getResponse().getBody(String.class).get();
assertThat(e.getStatus(), is(UNPROCESSABLE_ENTITY));
assertThat(jsonError, containsString("flow.id"));
assertThat(jsonError, containsString("flow.namespace"));
}
use of io.micronaut.http.hateoas.JsonError in project kestra by kestra-io.
the class ErrorController method error.
@Error(global = true)
public HttpResponse<JsonError> error(HttpRequest<?> request, ConversionErrorException e) {
if (e.getConversionError().getCause() instanceof InvalidTypeIdException) {
try {
InvalidTypeIdException invalidTypeIdException = ((InvalidTypeIdException) e.getConversionError().getCause());
String path = path(invalidTypeIdException);
Field typeField = InvalidTypeIdException.class.getDeclaredField("_typeId");
typeField.setAccessible(true);
Object typeClass = typeField.get(invalidTypeIdException);
JsonError error = new JsonError("Invalid type: " + typeClass).link(Link.SELF, Link.of(request.getUri())).embedded("errors", Arrays.asList(new JsonError("Invalid type: " + typeClass).path(path), new JsonError(e.getMessage()).path(path)));
return jsonError(error, HttpStatus.UNPROCESSABLE_ENTITY, "Invalid entity");
} catch (Exception ignored) {
}
} else if (e.getConversionError().getCause() instanceof JsonMappingException) {
try {
JsonMappingException jsonMappingException = ((JsonMappingException) e.getConversionError().getCause());
String path = path(jsonMappingException);
JsonError error = new JsonError("Invalid json mapping").link(Link.SELF, Link.of(request.getUri())).embedded("errors", Collections.singletonList(new JsonError(e.getMessage()).path(path)));
return jsonError(error, HttpStatus.UNPROCESSABLE_ENTITY, "Invalid json mapping");
} catch (Exception ignored) {
}
}
return jsonError(request, e, HttpStatus.UNPROCESSABLE_ENTITY, "Internal server error");
}
Aggregations