|
| 1 | +/* |
| 2 | + * Copyright 2017-2024 original authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micronaut.http.server.tck.tests.hateoas; |
| 17 | + |
| 18 | +import io.micronaut.http.hateoas.JsonError; |
| 19 | +import io.micronaut.http.hateoas.Link; |
| 20 | +import io.micronaut.http.hateoas.Resource; |
| 21 | +import io.micronaut.http.tck.ServerUnderTest; |
| 22 | +import io.micronaut.http.tck.ServerUnderTestProviderUtils; |
| 23 | +import io.micronaut.json.JsonMapper; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Collections; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 33 | + |
| 34 | +@SuppressWarnings({ |
| 35 | + "java:S5960", // We're allowed assertions, as these are used in tests only |
| 36 | + "checkstyle:MissingJavadocType", |
| 37 | + "checkstyle:DesignForExtension" |
| 38 | +}) |
| 39 | +public class JsonErrorSerdeTest { |
| 40 | + |
| 41 | + private static final String JSON_ERROR = """ |
| 42 | + {"_links":{"self":[{"href":"/resolve","templated":false}]},"_embedded":{"errors":[{"message":"Internal Server Error: Something bad happened"}]},"message":"Internal Server Error"}"""; |
| 43 | + private static final String SPEC_NAME = "JsonErrorSerdeTest"; |
| 44 | + |
| 45 | + /** |
| 46 | + * @throws IOException Exception thrown while getting the server under test. |
| 47 | + */ |
| 48 | + @Test |
| 49 | + void canDeserializeAJsonErrorAsAGenericResource() throws IOException { |
| 50 | + try (ServerUnderTest server = ServerUnderTestProviderUtils.getServerUnderTestProvider().getServer(SPEC_NAME, Collections.emptyMap())) { |
| 51 | + JsonMapper jsonMapper = server.getApplicationContext().getBean(JsonMapper.class); |
| 52 | + //when: |
| 53 | + Resource resource = jsonMapper.readValue(JSON_ERROR, Resource.class); |
| 54 | + //then: |
| 55 | + testResource(resource); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @throws IOException Exception thrown while getting the server under test. |
| 61 | + */ |
| 62 | + @Test |
| 63 | + void jsonErrorShouldBeDeserializableFromAString() throws IOException { |
| 64 | + try (ServerUnderTest server = ServerUnderTestProviderUtils.getServerUnderTestProvider().getServer(SPEC_NAME, Collections.emptyMap())) { |
| 65 | + JsonMapper jsonMapper = server.getApplicationContext().getBean(JsonMapper.class); |
| 66 | + //when: |
| 67 | + JsonError jsonError = jsonMapper.readValue(JSON_ERROR, JsonError.class); |
| 68 | + //then: |
| 69 | + testResource(jsonError); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private <T extends Resource> void testResource(T resource) { |
| 74 | + assertNotNull(resource); |
| 75 | + assertTrue(resource.getEmbedded().getFirst("errors").isPresent(), "errors should be present"); |
| 76 | + assertTrue(resource.getLinks().getFirst("self").isPresent(), "self link should be present"); |
| 77 | + assertEquals("/resolve", resource.getLinks().getFirst("self").map(Link::getHref).orElse(null)); |
| 78 | + assertFalse(resource.getLinks().getFirst("self").map(Link::isTemplated).orElse(true), "self link should not be templated"); |
| 79 | + } |
| 80 | +} |
0 commit comments