All Downloads are FREE. Search and download functionalities are using the official Maven repository.

JavaClientCli.ReferenceTest.mustache Maven / Gradle / Ivy

Go to download

This artefact generates other artefacts such as API client, API server out of API Model

There is a newer version: 1.1.1
Show newest version
{{>licenseInfo}}
package {{invokerPackage}};

import au.org.consumerdatastandards.client.cli.support.ApiClientOptions;
import au.org.consumerdatastandards.conformance.ConformanceError;
import au.org.consumerdatastandards.conformance.ConformanceModel;
import au.org.consumerdatastandards.conformance.Payload;
import au.org.consumerdatastandards.conformance.PayloadValidator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

@ShellComponent
@ShellCommandGroup("Reference Testing")
public class ReferenceTest {
    
    private PayloadValidator payloadValidator = new PayloadValidator();
    
    @Autowired
    ApiClientOptions apiClientOptions;
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ReferenceTest.class);
    
    @ShellMethod("Retrieve current conformance check status")
    public void payloadValidationStatus() {
        LOGGER.info("Automatic payload validation setting: {}", apiClientOptions.isValidationEnabled());
    }
    
    
    @ShellMethod("Validate all Payloads Automatically")
    public void autoValidate(@ShellOption(help = "enable or disable") String enableValidation) throws IOException {
        Boolean validateEnable = Boolean.parseBoolean(enableValidation);
        apiClientOptions.setValidationEnabled(validateEnable);
        payloadValidationStatus();
    }

    @ShellMethod("Validate json payload(s) against CDS")
    public void validatePath(@ShellOption(value = "-f", help = "payload file or folder") String fileOrFolder,
                             @ShellOption(value="-m", help = "model", defaultValue = "") String model) throws IOException {
        
        File file = new File(fileOrFolder);
        if (!file.exists()) {
            LOGGER.error("Unable to find the specified file for validation: {}", fileOrFolder);
        } else if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files == null || files.length == 0) {
                LOGGER.error("No files found in directory: {}", file.getAbsolutePath());
            } else {
                for (File oneFile : files) {
                    validatePath(oneFile.getAbsolutePath(), model);
                }
            }
        } else {
            List payloadErrors = StringUtils.isBlank(model) ?
                payloadValidator.validateFile(file) : payloadValidator.validateFile(file, model);
            if(!payloadErrors.isEmpty()) {
                LOGGER.error("Encountered errors while validating: {}",file.getAbsolutePath());
                for(int i = 0; i < payloadErrors.size(); i++ ) {
                    LOGGER.error("\n{}. {}", i + 1, payloadErrors.get(i).getDescription());
                }
            } else {
                LOGGER.info("Validation of {} successful", file.getAbsolutePath());
            }
        }
    }

    @ShellMethod("List top-level payload models")
    public String listPayloadModels() {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        Set topLevelModels = new TreeSet<>();
        ConformanceModel conformanceModel = payloadValidator.getConformanceModel();
        for (Class payloadModel : conformanceModel.getPayloadModels()) {
            Payload.Type payloadType = conformanceModel.getPayload(payloadModel).getPayloadType();
            if (Payload.Type.RESPONSE_BODY.equals(payloadType)) {
                topLevelModels.add(payloadModel.getSimpleName());
            }
        }
        for (String topLevelModel : topLevelModels) {
            pw.println(topLevelModel);
        }
        return sw.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy