|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.java.dynamicclient; |
| 7 | + |
| 8 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 9 | +import static org.hamcrest.Matchers.endsWith; |
| 10 | + |
| 11 | +import java.util.Map; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import software.amazon.smithy.java.core.schema.Schema; |
| 14 | +import software.amazon.smithy.java.core.serde.document.Document; |
| 15 | +import software.amazon.smithy.java.dynamicschemas.StructDocument; |
| 16 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 17 | +import software.amazon.smithy.model.traits.ErrorTrait; |
| 18 | + |
| 19 | +public class DocumentExceptionTest { |
| 20 | + |
| 21 | + private static final Schema SCHEMA = Schema.structureBuilder(ShapeId.from("foo#Error"), new ErrorTrait("client")) |
| 22 | + .putMember("message", Schema.createString(ShapeId.from("foo#Str"))) |
| 23 | + .build(); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void addsMessageWhenFound() { |
| 27 | + var ex = new DocumentException(SCHEMA, |
| 28 | + "hello: oh my!", |
| 29 | + StructDocument.of(SCHEMA, |
| 30 | + Document.of(Map.of( |
| 31 | + "message", |
| 32 | + Document.of("oh my!"))))); |
| 33 | + |
| 34 | + assertThat(ex.toString(), endsWith("hello: oh my!")); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void doesNotAddMessageTwice() { |
| 39 | + var ex = new DocumentException(SCHEMA, |
| 40 | + "hello: oh my!", |
| 41 | + StructDocument.of(SCHEMA, |
| 42 | + Document.of(Map.of( |
| 43 | + "message", |
| 44 | + Document.of("oh my!"))))); |
| 45 | + |
| 46 | + assertThat(ex.toString(), endsWith("hello: oh my!")); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void addsNothingWhenNoMessage() { |
| 51 | + var ex = new DocumentException(SCHEMA, "hello", StructDocument.of(SCHEMA, Document.of(Map.of()))); |
| 52 | + |
| 53 | + assertThat(ex.toString(), endsWith("hello")); |
| 54 | + } |
| 55 | +} |
0 commit comments