|
| 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.Resource; |
| 19 | +import io.micronaut.http.tck.ServerUnderTest; |
| 20 | +import io.micronaut.http.tck.ServerUnderTestProviderUtils; |
| 21 | +import io.micronaut.json.JsonMapper; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Collections; |
| 26 | + |
| 27 | +import static org.junit.jupiter.api.Assertions.*; |
| 28 | + |
| 29 | +@SuppressWarnings({ |
| 30 | + "java:S5960", // We're allowed assertions, as these are used in tests only |
| 31 | + "checkstyle:MissingJavadocType", |
| 32 | + "checkstyle:DesignForExtension" |
| 33 | +}) |
| 34 | +public class JsonErrorSerdeTest { |
| 35 | + private static final String JSON_ERROR = """ |
| 36 | + {"_links":{"self":[{"href":"/resolve","templated":false}]},"_embedded":{"errors":[{"message":"Internal Server Error: Something bad happened"}]},"message":"Internal Server Error"}"""; |
| 37 | + private static final String SPEC_NAME = "JsonErrorSerdeTest"; |
| 38 | + |
| 39 | + /** |
| 40 | + * @throws IOException Exception thrown while getting the server under test. |
| 41 | + */ |
| 42 | + @Test |
| 43 | + void canDeserializeAJsonErrorAsAGenericResource() throws IOException { |
| 44 | + try (ServerUnderTest server = ServerUnderTestProviderUtils.getServerUnderTestProvider().getServer(SPEC_NAME, Collections.emptyMap())) { |
| 45 | + JsonMapper jsonMapper = server.getApplicationContext().getBean(JsonMapper.class); |
| 46 | + //when: |
| 47 | + Resource resource = jsonMapper.readValue(JSON_ERROR, Resource.class); |
| 48 | + //then: |
| 49 | + assertTrue(resource.getEmbedded().getFirst("errors").isPresent()); |
| 50 | + assertTrue(resource.getLinks().getFirst("self").isPresent()); |
| 51 | + assertEquals("/resolve", resource.getLinks().getFirst("self").get().getHref()); |
| 52 | + assertFalse(resource.getLinks().getFirst("self").get().isTemplated()); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments