### Question:
Artifact { @Nullable public Object getMetadata(String key) { return metadata.get(key); } @Builder(toBuilder = true) private  Artifact(
      String type,
      boolean customKind,
      String name,
      String version,
      String location,
      String reference,
      Map<String, Object> metadata,
      String artifactAccount,
      String provenance,
      String uuid); @Nullable Object getMetadata(String key);  }### Answer:
@Test void putMetadataMapNullValue() throws IOException { Map<String, Object> metadata = new HashMap<>(); metadata.put("test", null); Artifact artifact = Artifact.builder().metadata(metadata).build(); assertThat(artifact.getMetadata("test")).isNull(); }
@Test void deserializeNullMetadataValue() throws IOException { String json = jsonFactory .objectNode() .<ObjectNode>set("metadata", jsonFactory.objectNode().<ObjectNode>set("key", null)) .toString(); Artifact result = objectMapper.readValue(json, Artifact.class); assertThat(result.getMetadata("key")).isNull(); }
@Test void deserializeNullUnknownKey() throws IOException { String json = jsonFactory.objectNode().<ObjectNode>set("key", null).toString(); Artifact result = objectMapper.readValue(json, Artifact.class); assertThat(result.getMetadata("key")).isNull(); }
@Test void unknownKeysInMetadata() throws IOException { String json = jsonFactory.objectNode().put("name", "my-artifact").put("id", "123").toString(); Artifact deserializedArtifact = objectMapper.readValue(json, Artifact.class); assertThat(deserializedArtifact.getName()).isEqualTo("my-artifact"); assertThat(deserializedArtifact.getMetadata("id")).isEqualTo("123"); }
@Test void kindIsIgnored() throws IOException { String json = jsonFactory.objectNode().put("kind", "test").put("name", "my-artifact").toString(); Artifact deserializedArtifact = objectMapper.readValue(json, Artifact.class); assertThat(deserializedArtifact.getMetadata("kind")).isNull(); }
@Test void toBuilderCopiesMetadata() { Artifact originalArtifact = Artifact.builder().putMetadata("abc", "def").putMetadata("mykey", "myval").build(); Artifact newArtifact = originalArtifact.toBuilder().build(); assertThat(newArtifact.getMetadata("abc")).isEqualTo("def"); assertThat(newArtifact.getMetadata("mykey")).isEqualTo("myval"); }
@Test void deserializeComplexMetatdata() throws IOException { String json = jsonFactory .objectNode() .set("test", jsonFactory.objectNode().put("nested", "abc")) .toString(); Artifact artifact = objectMapper.readValue(json, Artifact.class); Object testData = artifact.getMetadata("test"); assertThat(testData).isEqualTo(ImmutableMap.of("nested", "abc")); }
@Test void deserializeNullMetadata() throws IOException { String json = jsonFactory.objectNode().set("metadata", jsonFactory.nullNode()).toString(); Artifact artifact = objectMapper.readValue(json, Artifact.class); assertThat(artifact.getMetadata("abc")).isNull(); }
@Test void immutableMetadata() throws IOException { Map<String, Object> metadata = new HashMap<>(); metadata.put("key1", "before"); Artifact artifact = Artifact.builder().metadata(metadata).build(); assertThat(artifact.getMetadata("key1")).isEqualTo("before"); assertThat(artifact.getMetadata("key2")).isNull(); metadata.put("key1", "after"); metadata.put("key2", "something"); assertThat(artifact.getMetadata("key1")).isEqualTo("before"); assertThat(artifact.getMetadata("key2")).isNull(); }
@Test void putMetadataNullValue() throws IOException { Artifact artifact = Artifact.builder().putMetadata("test", null).build(); assertThat(artifact.getMetadata("test")).isNull(); }