Skip to content

Commit

Permalink
Merge pull request #191 from REGnosys/add-ingest-transform-type
Browse files Browse the repository at this point in the history
Test pack creator - support input xml deserialisation
  • Loading branch information
hugohills-regnosys authored Nov 18, 2024
2 parents 3be9c8f + e248d27 commit bb05dc6
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@

public class FunctionNameHelper {

public String getInputType(Class<? extends RosettaFunction> function) {
public Class<?> getInputClass(Class<? extends RosettaFunction> function) {
Method functionMethod = getFuncMethod(function);
return functionMethod.getParameterTypes()[0].getName();
return functionMethod.getParameterTypes()[0];
}

public String getInputType(Class<? extends RosettaFunction> function) {
return getInputClass(function).getName();
}

public String getOutputType(Class<? extends RosettaFunction> function) {
Expand Down Expand Up @@ -76,6 +80,7 @@ protected String readableId(Class<? extends RosettaFunction> function) {
.orElse(function.getSimpleName());

String sanitise = simpleName
.replace("Ingest_", "")
.replace("Report_", "")
.replace("Function", "")
.replace("Enrich_", "")
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/regnosys/testing/pipeline/PipelineFilter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package com.regnosys.testing.pipeline;

/*-
* ===============
* Rune Testing
* ===============
* Copyright (C) 2022 - 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 java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* 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.
Expand Down Expand Up @@ -41,16 +41,12 @@ public class PipelineFunctionRunner {

public Result run(PipelineModel pipelineModel, ImmutableMap<Class<?>, String> outputSchemaMap, Path inputPath) {
TestPackFunctionRunner functionRunner = getFunctionRunner(pipelineModel, outputSchemaMap);
Pair<String, TestPackModel.SampleModel.Assertions> run = functionRunner.run(inputPath);
return new Result(run.left(), run.right());
Pair<String, TestPackModel.SampleModel.Assertions> result = functionRunner.run(inputPath);
return new Result(result.left(), result.right());
}

private TestPackFunctionRunner getFunctionRunner(PipelineModel pipelineModel, ImmutableMap<Class<?>, String> outputSchemaMap) {
if (pipelineModel.getOutputSerialisation() != null) {
return provider.create(pipelineModel.getTransform(), pipelineModel.getOutputSerialisation(), outputSchemaMap, injector);
} else {
return provider.create(pipelineModel.getTransform(), injector);
}
private TestPackFunctionRunner getFunctionRunner(PipelineModel pipelineModel, ImmutableMap<Class<?>, String> schemaMap) {
return provider.create(pipelineModel.getTransform(), pipelineModel.getInputSerialisation(), pipelineModel.getOutputSerialisation(), schemaMap, injector);
}

public static class Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* 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.
Expand Down Expand Up @@ -42,23 +42,29 @@ public List<PipelineModel> createPipelineModels(PipelineTree pipelineTree) {
}

protected PipelineModel build(PipelineNode modelBuilder, PipelineTreeConfig config) {

String inputType = helper.getInputType(modelBuilder.getFunction());
String outputType = helper.getOutputType(modelBuilder.getFunction());
String inputSerialisationConfigPath = config.getXmlConfigMap().get(helper.getInputClass(modelBuilder.getFunction()));
String outputSerialisationConfigPath = config.getXmlConfigMap().get(helper.getFuncMethod(modelBuilder.getFunction()).getReturnType());
String name = helper.getName(modelBuilder.getFunction());

// assume XML for now.
PipelineModel.Serialisation outputSerialisation = outputSerialisationConfigPath == null ? null :
new PipelineModel.Serialisation(PipelineModel.Serialisation.Format.XML, outputSerialisationConfigPath);
PipelineModel.Serialisation inputSerialisation = getSerialisation(inputSerialisationConfigPath);
PipelineModel.Serialisation outputSerialisation = getSerialisation(outputSerialisationConfigPath);

String pipelineId = modelBuilder.id(config.isStrictUniqueIds());
String upstreamPipelineId = modelBuilder.upstreamId(config.isStrictUniqueIds());

return new PipelineModel(pipelineId,
name,
new PipelineModel.Transform(modelBuilder.getTransformType(), modelBuilder.getFunction().getName(), inputType, outputType),
upstreamPipelineId, outputSerialisation);
upstreamPipelineId,
inputSerialisation,
outputSerialisation);
}

private PipelineModel.Serialisation getSerialisation(String xmlConfigPath) {
return xmlConfigPath == null ? null :
new PipelineModel.Serialisation(PipelineModel.Serialisation.Format.XML, xmlConfigPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void writeTestPacks(PipelineTreeConfig config) throws IOException {
Path outputPath = resourcesPath.resolve(pipelineNode.getOutputPath(config.isStrictUniqueIds()));
LOGGER.info("Output path {} ", outputPath);

List<Path> inputSamples = findAllJsonSamples(inputPath);
List<Path> inputSamples = findAllSamples(inputPath);

Map<String, List<Path>> testPackToSamples =
filterAndGroupingByTestPackId(resourcesPath, inputPath, inputSamples, config.getTestPackIdFilter());
Expand All @@ -108,14 +108,13 @@ public void writeTestPacks(PipelineTreeConfig config) throws IOException {
}
}

private List<Path> findAllJsonSamples(Path inputDir) throws IOException {
private List<Path> findAllSamples(Path inputDir) throws IOException {
if (!Files.exists(inputDir)) {
return List.of();
}
try (Stream<Path> paths = Files.walk(inputDir)) {
return paths.filter(Files::isRegularFile)
.filter(Files::exists)
.filter(x -> x.getFileName().toString().endsWith(".json"))
.collect(Collectors.toList());
}
}
Expand All @@ -135,8 +134,8 @@ private TestPackModel writeTestPackSamples(Path resourcesPath, Path inputPath, P
Path outputSample = resourcesPath.relativize(outputDir.resolve(resourcesPath.relativize(inputPath).relativize(inputSample)));
outputSample = outputSample.getParent().resolve(Path.of(updateFileExtensionBasedOnOutputFormat(pipeline, outputSample.toFile().getName())));

PipelineFunctionRunner.Result run = pipelineFunctionRunner.run(pipeline, config.getXmlSchemaMap(), resourcesPath.resolve(inputSample));
TestPackModel.SampleModel.Assertions assertions = run.getAssertions();
PipelineFunctionRunner.Result result = pipelineFunctionRunner.run(pipeline, config.getXmlSchemaMap(), resourcesPath.resolve(inputSample));
TestPackModel.SampleModel.Assertions assertions = result.getAssertions();

String baseFileName = getBaseFileName(inputSample.toUri().toURL());
String displayName = baseFileName.replace("-", " ");
Expand All @@ -145,7 +144,7 @@ private TestPackModel writeTestPackSamples(Path resourcesPath, Path inputPath, P
sampleModels.add(sampleModel);

Files.createDirectories(resourcesPath.resolve(outputSample).getParent());
Files.write(resourcesPath.resolve(outputSample), run.getSerialisedOutput().getBytes());
Files.write(resourcesPath.resolve(outputSample), result.getSerialisedOutput().getBytes());
}

List<TestPackModel.SampleModel> sortedSamples = sampleModels
Expand All @@ -160,11 +159,12 @@ private TestPackModel writeTestPackSamples(Path resourcesPath, Path inputPath, P
}

private String updateFileExtensionBasedOnOutputFormat(PipelineModel pipelineModel, String fileName) {
if (pipelineModel.getOutputSerialisation() != null) {
String outputFormat = pipelineModel.getOutputSerialisation().getFormat().toString().toLowerCase();
return fileName.substring(0, fileName.lastIndexOf(".")) + "." + outputFormat;
}
return fileName;
String outputFormat = Optional.ofNullable(pipelineModel.getOutputSerialisation())
.map(PipelineModel.Serialisation::getFormat)
.map(PipelineModel.Serialisation.Format::toString)
.orElse("json")
.toLowerCase();
return fileName.substring(0, fileName.lastIndexOf(".")) + "." + outputFormat;
}

private Map<String, List<Path>> filterAndGroupingByTestPackId(Path resourcesPath, Path inputPath, List<Path> inputSamples, Predicate<String> testPackIdFilter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.regnosys.rosetta.common.hashing.ReferenceConfig;
import com.regnosys.rosetta.common.hashing.ReferenceResolverProcessStep;
Expand All @@ -46,7 +47,7 @@

import static com.regnosys.rosetta.common.transform.TestPackModel.SampleModel.Assertions;
import static com.regnosys.rosetta.common.transform.TestPackUtils.readFile;
import static com.regnosys.testing.testpack.TestPackFunctionRunnerProviderImpl.JSON_OBJECT_MAPPER;
import static com.regnosys.testing.transform.TransformTestExtension.ERROR_OUTPUT;

public class TestPackFunctionRunnerImpl<IN extends RosettaModelObject> implements TestPackFunctionRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(TestPackFunctionRunnerImpl.class);
Expand All @@ -56,6 +57,7 @@ public class TestPackFunctionRunnerImpl<IN extends RosettaModelObject> implement
private final Class<IN> inputType;
private final RosettaTypeValidator typeValidator;
private final ReferenceConfig referenceConfig;
private final ObjectMapper inputObjectMapper;
private final ObjectWriter outputObjectWriter;
private final Validator xsdValidator;

Expand All @@ -64,12 +66,14 @@ public TestPackFunctionRunnerImpl(Function<IN, RosettaModelObject> function,
Class<IN> inputType,
RosettaTypeValidator typeValidator,
ReferenceConfig referenceConfig,
ObjectMapper inputObjectMapper,
ObjectWriter outputObjectWriter,
Validator xsdValidator) {
this.function = function;
this.inputType = inputType;
this.typeValidator = typeValidator;
this.referenceConfig = referenceConfig;
this.inputObjectMapper = inputObjectMapper;
this.outputObjectWriter = outputObjectWriter;
this.xsdValidator = xsdValidator;
}
Expand All @@ -81,14 +85,14 @@ public Pair<String, Assertions> run(Path inputPath) {
// TODO - fix this hack.
Path inputPathFromRepositoryRoot = inputPath.isAbsolute() ? inputPath : ROSETTA_SOURCE_PATH.resolve(inputPath);
URL inputFileUrl = inputPathFromRepositoryRoot.toUri().toURL();
IN input = readFile(inputFileUrl, JSON_OBJECT_MAPPER, inputType);
IN input = readFile(inputFileUrl, inputObjectMapper, inputType);
output = function.apply(resolveReferences(input));
} catch (MalformedURLException e) {
LOGGER.error("Failed to load input path {}", inputPath, e);
return Pair.of(null, new Assertions(null, null, true));
return Pair.of(ERROR_OUTPUT, new Assertions(null, null, true));
} catch (Exception e) {
LOGGER.error("Exception occurred running sample creation", e);
return Pair.of(null, new Assertions(null, null, true));
return Pair.of(ERROR_OUTPUT, new Assertions(null, null, true));
}

String serialisedOutput;
Expand All @@ -108,6 +112,7 @@ public Pair<String, Assertions> run(Path inputPath) {
return Pair.of(serialisedOutput, assertions);
}

@SuppressWarnings("unchecked")
private <T extends RosettaModelObject> T resolveReferences(T o) {
RosettaModelObjectBuilder builder = o.toBuilder();
new ReferenceResolverProcessStep(referenceConfig).runProcessStep(o.getType(), builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
@ImplementedBy(TestPackFunctionRunnerProviderImpl.class)
public interface TestPackFunctionRunnerProvider {

TestPackFunctionRunner create(Transform transform, Injector injector);

TestPackFunctionRunner create(Transform transform, Serialisation outputSerialisation, ImmutableMap<Class<?>, String> outputSchemaMap, Injector injector);
TestPackFunctionRunner create(Transform transform,
Serialisation inputSerialisation,
Serialisation outputSerialisation,
ImmutableMap<Class<?>, String> outputSchemaMap,
Injector injector);
}
Loading

0 comments on commit bb05dc6

Please sign in to comment.