/graylog2-server/src/test/java/org/graylog2/jackson/JsonSubTypePropertyDefaultValueTest.java

https://github.com/Graylog2/graylog2-server · Java · 108 lines · 73 code · 17 blank · 18 comment · 0 complexity · 5708a46fa136a173f6acea47d74be366 MD5 · raw file

  1. /*
  2. * Copyright (C) 2020 Graylog, Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the Server Side Public License, version 1,
  6. * as published by MongoDB, Inc.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * Server Side Public License for more details.
  12. *
  13. * You should have received a copy of the Server Side Public License
  14. * along with this program. If not, see
  15. * <http://www.mongodb.com/licensing/server-side-public-license>.
  16. */
  17. package org.graylog2.jackson;
  18. import com.fasterxml.jackson.annotation.JsonCreator;
  19. import com.fasterxml.jackson.annotation.JsonProperty;
  20. import com.fasterxml.jackson.annotation.JsonSubTypes;
  21. import com.fasterxml.jackson.annotation.JsonTypeInfo;
  22. import com.fasterxml.jackson.databind.ObjectMapper;
  23. import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
  24. import org.junit.jupiter.api.BeforeEach;
  25. import org.junit.jupiter.api.Test;
  26. import static org.assertj.core.api.Assertions.assertThat;
  27. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  28. class JsonSubTypePropertyDefaultValueTest {
  29. static final String VALUE_WITHOUT_TYPE = "{\"test\":{\"a\":\"this is a\"}}";
  30. static final String VALUE_A = "{\"test\":{\"type\":\"a\",\"a\":\"this is a\"}}";
  31. static final String VALUE_B = "{\"test\":{\"type\":\"b\",\"b\":\"this is b\"}}";
  32. ObjectMapper objectMapper;
  33. @BeforeEach
  34. void setUp() {
  35. this.objectMapper = new ObjectMapper();
  36. }
  37. @Test
  38. void deserializeWithoutActiveProblemHandlerModule() throws Exception {
  39. final TestDocument docA = objectMapper.readValue(VALUE_A, TestDocument.class);
  40. final TestDocument docB = objectMapper.readValue(VALUE_B, TestDocument.class);
  41. assertThat(docA.test).isInstanceOf(TestClass.SubTypeA.class);
  42. assertThat(docB.test).isInstanceOf(TestClass.SubTypeB.class);
  43. // The problem handler is not active, so we expect this to throw an exception.
  44. assertThatThrownBy(() -> objectMapper.readValue(VALUE_WITHOUT_TYPE, TestDocument.class))
  45. .isInstanceOf(InvalidTypeIdException.class);
  46. }
  47. @Test
  48. void deserializeWithActiveProblemHandlerModule() throws Exception {
  49. objectMapper.registerModule(new DeserializationProblemHandlerModule());
  50. final TestDocument docA = objectMapper.readValue(VALUE_A, TestDocument.class);
  51. final TestDocument docB = objectMapper.readValue(VALUE_B, TestDocument.class);
  52. // The problem handler is active, so this should not throw an exception
  53. final TestDocument docWithoutType = objectMapper.readValue(VALUE_WITHOUT_TYPE, TestDocument.class);
  54. assertThat(docA.test).isInstanceOf(TestClass.SubTypeA.class);
  55. assertThat(docB.test).isInstanceOf(TestClass.SubTypeB.class);
  56. assertThat(docWithoutType.test).isInstanceOf(TestClass.SubTypeA.class);
  57. }
  58. @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
  59. @JsonSubTypes({
  60. @JsonSubTypes.Type(value = TestClass.SubTypeA.class, name = "a"),
  61. @JsonSubTypes.Type(value = TestClass.SubTypeB.class, name = "b"),
  62. })
  63. @JsonSubTypePropertyDefaultValue("a")
  64. public interface TestClass {
  65. class SubTypeA implements TestClass {
  66. final String type;
  67. final String a;
  68. @JsonCreator
  69. private SubTypeA(@JsonProperty("type") String type, @JsonProperty("a") String a) {
  70. this.type = type;
  71. this.a = a;
  72. }
  73. }
  74. class SubTypeB implements TestClass {
  75. final String type;
  76. final String b;
  77. @JsonCreator
  78. private SubTypeB(@JsonProperty("type") String type, @JsonProperty("b") String b) {
  79. this.type = type;
  80. this.b = b;
  81. }
  82. }
  83. }
  84. public static class TestDocument {
  85. final TestClass test;
  86. @JsonCreator
  87. private TestDocument(@JsonProperty("test") TestClass test) {
  88. this.test = test;
  89. }
  90. }
  91. }