Skip to content

Commit c6b93cb

Browse files
committed
feat(calm-models): add CalmArchitecture.parse(Object) overload
1 parent 5079204 commit c6b93cb

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

calm-models/src/main/java/org/finos/calm/model/CalmArchitecture.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ public static CalmArchitecture parse(String json, ObjectMapper mapper) {
4545
}
4646
}
4747

48+
public static CalmArchitecture parse(Object object) {
49+
return parse(object, defaultMapper());
50+
}
51+
52+
public static CalmArchitecture parse(Object object, ObjectMapper mapper) {
53+
try {
54+
CalmArchitectureSchema schema = mapper.convertValue(object, CalmArchitectureSchema.class);
55+
return from(schema, mapper);
56+
} catch (Exception e) {
57+
throw new RuntimeException("Failed to parse CALM architecture from object", e);
58+
}
59+
}
60+
4861
static CalmArchitecture from(CalmArchitectureSchema schema, ObjectMapper mapper) {
4962
List<CalmNode> nodes = schema.getNodes() == null ? List.of() :
5063
schema.getNodes().stream().map(n -> CalmNode.from(n, mapper)).toList();

calm-models/src/test/java/org/finos/calm/model/CalmArchitectureParseTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package org.finos.calm.model;
22

3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.JsonNode;
35
import com.fasterxml.jackson.databind.ObjectMapper;
46
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
57
import org.junit.jupiter.api.Test;
68

79
import java.io.InputStream;
10+
import java.util.Map;
811

912
import static org.assertj.core.api.Assertions.assertThat;
1013

@@ -42,4 +45,22 @@ void getMetadata_returnsEmptyForMissingKey() throws Exception {
4245
CalmArchitecture arch = CalmArchitecture.parse(loadFixture());
4346
assertThat(arch.getMetadata("no-such-key")).isEmpty();
4447
}
48+
49+
@Test
50+
void parse_fromMap_loadsExpectedCounts() throws Exception {
51+
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
52+
Map<String, Object> map = mapper.readValue(loadFixture(), new TypeReference<>() {});
53+
CalmArchitecture arch = CalmArchitecture.parse(map);
54+
assertThat(arch.getNodes()).hasSize(4);
55+
assertThat(arch.getRelationships()).hasSize(3);
56+
}
57+
58+
@Test
59+
void parse_fromJsonNode_loadsExpectedCounts() throws Exception {
60+
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
61+
JsonNode node = mapper.readTree(loadFixture());
62+
CalmArchitecture arch = CalmArchitecture.parse(node);
63+
assertThat(arch.getNodes()).hasSize(4);
64+
assertThat(arch.getRelationships()).hasSize(3);
65+
}
4566
}

0 commit comments

Comments
 (0)