com.regnosys.rosetta.common.testing.FunctionRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rosetta-common Show documentation
Show all versions of rosetta-common Show documentation
Rune Common is a java library that is utilised by Rosetta Code Generators and models expressed in the Rosetta DSL.
package com.regnosys.rosetta.common.testing;
/*-
* ==============
* Rune Common
* ==============
* Copyright (C) 2018 - 2024 REGnosys
* ==============
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ==============
*/
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Iterables;
import com.regnosys.rosetta.common.hashing.ReferenceConfig;
import com.regnosys.rosetta.common.hashing.ReferenceResolverProcessStep;
import com.regnosys.rosetta.common.util.ClassPathUtils;
import com.rosetta.model.lib.RosettaModelObject;
import com.rosetta.model.lib.RosettaModelObjectBuilder;
import com.rosetta.model.lib.functions.RosettaFunction;
import com.rosetta.model.lib.process.PostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class FunctionRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(FunctionRunner.class);
private static final String ROSETTA_FUNC_EVAL_METHOD_NAME = "evaluate";
private final ExecutionDescriptor executionDescriptor;
private final InstanceLoader instanceLoader;
private final ClassLoader classLoader;
private final ObjectMapper objectMapper;
public FunctionRunner(ExecutionDescriptor executionDescriptor,
InstanceLoader instanceLoader,
ClassLoader classLoader,
ObjectMapper objectMapper) {
this.executionDescriptor = executionDescriptor;
this.instanceLoader = instanceLoader;
this.classLoader = classLoader;
this.objectMapper = objectMapper;
}
public FunctionRunnerResult run() throws ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException {
LOGGER.info("Executing " + executionDescriptor.getGroup() + ":" + executionDescriptor.getName());
String inputFile = executionDescriptor.getInputFile();
String expectedOutputFile = executionDescriptor.getExpectedOutputFile();
LOGGER.info("Output File: " + expectedOutputFile);
if (executionDescriptor.isNativeFunction()) {
JsonNode jsonNode = objectMapper.readTree(loadURL(inputFile));
Object actualOutput = postProcess(runNativeFunction(jsonNode, executionDescriptor.getExecutableFunctionClass()));
String jsonActual = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(actualOutput);
if (expectedOutputFile == null) {
return new FunctionRunnerResult(jsonNode, null, actualOutput, jsonActual, null);
}
Object expectedOutput;
String jsonExpected;
try {
expectedOutput = objectMapper.readValue(loadURL(expectedOutputFile), getType(actualOutput));
jsonExpected = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(expectedOutput);
} catch (Exception e) {
LOGGER.error("Error getting expected output " + executionDescriptor.getGroup() + ":" + executionDescriptor.getName(), e);
expectedOutput = null;
jsonExpected = "";
}
return new FunctionRunnerResult(jsonNode, expectedOutput, actualOutput, jsonActual, jsonExpected);
} else {
Class> functionClass = loadExecutableFunctionClass(executionDescriptor.getExecutableFunctionClass());
ExecutableFunction instance = instanceLoader.createInstance(functionClass);
INPUT input = objectMapper.readValue(loadURL(inputFile), instance.getInputType());
OUTPUT actualOutput = postProcess(instance.execute(input));
String jsonActual = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(actualOutput);
if (expectedOutputFile == null) {
return new FunctionRunnerResult<>(input, null, actualOutput, jsonActual, null);
}
try {
OUTPUT expectedOutput = objectMapper.readValue(loadURL(expectedOutputFile), instance.getOutputType());
String jsonExpected = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(expectedOutput);
return new FunctionRunnerResult<>(input, expectedOutput, actualOutput, jsonActual, jsonExpected);
} catch (IOException e) {
LOGGER.warn("Unable to deserialise expected json file, proceeding without it.");
// TODO: load the url into a string and print it here
return new FunctionRunnerResult<>(input, null, actualOutput, jsonActual, "");
}
}
}
private Class> getType(Object actualOutput) {
return RosettaModelObject.class.isInstance(actualOutput) ?
((RosettaModelObject) actualOutput).getType() :
actualOutput.getClass();
}
private