Skip to content

Commit

Permalink
graal: reflect-config.json for hateoas classes (#10547)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo authored Mar 12, 2024
1 parent 270b7e1 commit 985c271
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.http.server.tck.tests.hateoas;

import io.micronaut.core.value.OptionalMultiValues;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.http.hateoas.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

import static io.micronaut.http.tck.TestScenario.asserts;
import static org.junit.jupiter.api.Assertions.*;


@SuppressWarnings({
"java:S5960", // We're allowed assertions, as these are used in tests only
"checkstyle:MissingJavadocType",
"checkstyle:DesignForExtension"
})
public class JsonErrorTest {
private static final String SPEC_NAME = "JsonErrorTest";

/**
* @throws IOException Exception thrown while getting the server under test.
*/
@Test
public void responseCanBeBoundToJsonError() throws IOException {
asserts(SPEC_NAME,
HttpRequest.GET("/jsonError"),
(server, request) -> {
Executable e = () -> server.exchange(request, JsonError.class);
HttpClientResponseException ex = Assertions.assertThrows(HttpClientResponseException.class, e);
Optional<JsonError> jsonErrorOptional = ex.getResponse().getBody(JsonError.class);
assertTrue(jsonErrorOptional.isPresent());
JsonError jsonError = jsonErrorOptional.get();
assertEquals("Not Found", jsonError.getMessage());
OptionalMultiValues<Link> links = jsonError.getLinks();
assertFalse(links.isEmpty());
links.getFirst("self").ifPresent(link -> {
assertEquals("/jsonError", link.getHref());
assertFalse(link.isTemplated());
});
OptionalMultiValues<Resource> resourceOptionalMultiValues = jsonError.getEmbedded();
assertFalse(resourceOptionalMultiValues.isEmpty());

Optional<List<Resource>> errorsOptional = resourceOptionalMultiValues.get("errors");
assertTrue(errorsOptional.isPresent());
List<Resource> resources = errorsOptional.get();
Optional<GenericResource> genericResourceOptional = resources.stream()
.filter(resource -> resource instanceof GenericResource)
.map(GenericResource.class::cast)
.findFirst();
assertTrue(genericResourceOptional.isPresent());
assertEquals("Page Not Found", genericResourceOptional.get().getAdditionalProperties().get("message"));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.http.server.tck.tests.hateoas;

import io.micronaut.core.value.OptionalMultiValues;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.http.hateoas.GenericResource;
import io.micronaut.http.hateoas.Link;
import io.micronaut.http.hateoas.Resource;
import io.micronaut.http.hateoas.VndError;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

import static io.micronaut.http.tck.TestScenario.asserts;
import static org.junit.jupiter.api.Assertions.*;


@SuppressWarnings({
"java:S5960", // We're allowed assertions, as these are used in tests only
"checkstyle:MissingJavadocType",
"checkstyle:DesignForExtension"
})
public class VndErrorTest {
private static final String SPEC_NAME = "VndErrorTest";

/**
* @throws IOException Exception thrown while getting the server under test.
*/
@Test
public void responseCanBeBoundToVndError() throws IOException {
asserts(SPEC_NAME,
HttpRequest.GET("/vndError"),
(server, request) -> {
Executable e = () -> server.exchange(request, VndError.class);
HttpClientResponseException ex = Assertions.assertThrows(HttpClientResponseException.class, e);
Optional<VndError> vndErrorOptional = ex.getResponse().getBody(VndError.class);
assertTrue(vndErrorOptional.isPresent());
VndError vndError = vndErrorOptional.get();
assertEquals("Not Found", vndError.getMessage());
OptionalMultiValues<Link> links = vndError.getLinks();
assertFalse(links.isEmpty());
links.getFirst("self").ifPresent(link -> {
assertEquals("/vndError", link.getHref());
assertFalse(link.isTemplated());
});
OptionalMultiValues<Resource> resourceOptionalMultiValues = vndError.getEmbedded();
assertFalse(resourceOptionalMultiValues.isEmpty());

Optional<List<Resource>> errorsOptional = resourceOptionalMultiValues.get("errors");
assertTrue(errorsOptional.isPresent());
List<Resource> resources = errorsOptional.get();
Optional<GenericResource> genericResourceOptional = resources.stream()
.filter(resource -> resource instanceof GenericResource)
.map(GenericResource.class::cast)
.findFirst();
assertTrue(genericResourceOptional.isPresent());
assertEquals("Page Not Found", genericResourceOptional.get().getAdditionalProperties().get("message"));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"name": "io.micronaut.http.hateoas.GenericResource",
"queryAllDeclaredMethods": true,
"allDeclaredFields": true,
"queryAllDeclaredConstructors": true,
"methods": [
{
"name": "addProperty",
"parameterTypes": [
"java.lang.String",
"java.lang.Object"
]
}
]
},
{
"name": "io.micronaut.http.hateoas.Resource",
"queryAllDeclaredMethods": true,
"methods": [
{
"name": "deserialize",
"parameterTypes": [
"io.micronaut.http.hateoas.GenericResource"
]
}
]
},
{
"name": "io.micronaut.http.hateoas.JsonError",
"queryAllDeclaredMethods": true,
"allDeclaredFields": true,
"queryAllDeclaredConstructors": true
}
]

0 comments on commit 985c271

Please sign in to comment.