com.chutneytesting.design.infra.storage.scenario.compose.RawImplementationMapper Maven / Gradle / Ivy
package com.chutneytesting.design.infra.storage.scenario.compose;
import com.chutneytesting.execution.domain.compiler.ScenarioConversionException;
import com.chutneytesting.execution.domain.scenario.composed.StepImplementation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.springframework.stereotype.Component;
@Component
public class RawImplementationMapper {
private final ObjectMapper objectMapper;
public RawImplementationMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
public StepImplementation deserialize(String rawImplementation) {
JsonNode implementation;
try {
implementation = objectMapper.readTree(rawImplementation);
} catch (IOException e) {
throw new ScenarioConversionException(e);
}
return new StepImplementation(
type(implementation),
target(implementation),
inputs(implementation),
outputs(implementation),
validations(implementation)
);
}
private String type(JsonNode implementation) {
if (implementation.hasNonNull("identifier")) {
return implementation.get("identifier").textValue();
}
return null;
}
private String target(JsonNode implementation) {
return Optional.ofNullable(implementation.get("target")).orElse(TextNode.valueOf("")).textValue();
}
private Map outputs(JsonNode implementation) {
Map outputs = new LinkedHashMap<>();
if (implementation.hasNonNull("outputs")) {
final JsonNode outputsNode = implementation.get("outputs");
outputsNode.forEach(in -> {
String name = in.get("key").asText();
outputs.put(name, in.get("value").asText());
});
}
return outputs;
}
private Map inputs(JsonNode implementation) {
Map inputs = new LinkedHashMap<>();
// Simple inputs
if (implementation.hasNonNull("inputs")) {
final JsonNode simpleInputs = implementation.get("inputs");
simpleInputs.forEach(in -> {
String inputName = in.get("name").asText();
inputs.put(inputName, transformSimpleInputValue(in));
});
}
// List inputs
if (implementation.hasNonNull("listInputs")) {
final JsonNode listInputs = implementation.get("listInputs");
listInputs.forEach(in -> {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy