Search in sources :

Example 1 with TestToRunData

use of com.hp.octane.integrations.executor.TestToRunData in project octane-ci-java-sdk by MicroFocus.

the class TestExecutionServiceImpl method convertLinksToJson.

private String convertLinksToJson(List<Entity> links) throws JsonProcessingException {
    TestToRunDataCollection collection = new TestToRunDataCollection();
    for (Entity link : links) {
        TestToRunData data = new TestToRunData();
        Entity test = (Entity) link.getField("test");
        data.setPackageName(test.getStringValue(EntityConstants.AutomatedTest.PACKAGE_FIELD));
        data.setClassName(test.getStringValue(EntityConstants.AutomatedTest.CLASS_NAME_FIELD));
        data.setTestName(test.getStringValue(EntityConstants.AutomatedTest.NAME_FIELD));
        if (test.containsFieldAndValue("automation_identifier")) {
            data.addParameters(CucumberJVMConverter.FEATURE_FILE_PATH, test.getStringValue("automation_identifier"));
        }
        if (test.containsFieldAndValue("external_test_id")) {
            data.addParameters("external_test_id", test.getStringValue("external_test_id"));
        }
        if (test.containsFieldAndValue("data_table")) {
            data.addParameters(CucumberJVMConverter.FEATURE_FILE_PATH, link.getEntityValue("data_table").getStringValue("relative_path"));
        }
        if (test.containsFieldAndValue(EntityConstants.TestSuiteLinkToTest.EXECUTION_PARAMETERS_FIELD)) {
            String[] parts = link.getStringValue(EntityConstants.TestSuiteLinkToTest.EXECUTION_PARAMETERS_FIELD).split("[\n;]");
            for (String part : parts) {
                String myPart = part.trim();
                int splitterIndex = myPart.indexOf('=');
                if (myPart.isEmpty() || myPart.startsWith("#") || splitterIndex == -1) {
                    continue;
                }
                String name = myPart.substring(0, splitterIndex).trim();
                String value = myPart.substring(splitterIndex + 1).trim();
                data.addParameters(name, value);
            }
        }
        collection.getTestsToRun().add(data);
    }
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return objectMapper.writeValueAsString(collection);
}
Also used : Entity(com.hp.octane.integrations.dto.entities.Entity) TestToRunDataCollection(com.hp.octane.integrations.executor.TestToRunDataCollection) TestToRunData(com.hp.octane.integrations.executor.TestToRunData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with TestToRunData

use of com.hp.octane.integrations.executor.TestToRunData in project octane-ci-java-sdk by MicroFocus.

the class MfUftConverter method convertToMtbxContent.

public String convertToMtbxContent(List<TestToRunData> tests, String workingDir, Map<String, String> globalParameters) {
    tests = tests.stream().filter(testToRunData -> testToRunData.getParameter(TESTING_TOOL_TYPE_PARAMETER) == null).collect(Collectors.toList());
    if (CollectionUtils.isEmpty(tests)) {
        return "";
    }
    boolean addGlobalParameters = isAddGlobalParameters(globalParameters);
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("Mtbx");
        doc.appendChild(rootElement);
        for (TestToRunData test : tests) {
            Element testElement = doc.createElement("Test");
            String packageAndTestName = (SdkStringUtils.isNotEmpty(test.getPackageName()) ? test.getPackageName() + SdkConstants.FileSystem.WINDOWS_PATH_SPLITTER : "") + test.getTestName();
            testElement.setAttribute("name", packageAndTestName);
            String path = workingDir + (SdkStringUtils.isEmpty(test.getPackageName()) ? "" : SdkConstants.FileSystem.WINDOWS_PATH_SPLITTER + test.getPackageName()) + SdkConstants.FileSystem.WINDOWS_PATH_SPLITTER + test.getTestName();
            testElement.setAttribute("path", path);
            // add parameters
            test.getParameters().forEach((paramKey, paramValue) -> {
                if (DATA_TABLE_PARAMETER.equals(paramKey) || ITERATIONS_PARAMETER.equals(paramKey) || MBT_DATA.equals(paramKey)) {
                // skip, will be handled later
                } else {
                    if (INNER_RUN_ID_PARAMETER.equals(paramKey)) {
                        if (!addGlobalParameters) {
                            return;
                        }
                    }
                    addParameterToTestElement(doc, testElement, paramKey, paramValue);
                }
            });
            if (addGlobalParameters) {
                globalParameters.entrySet().stream().filter(p -> !p.getKey().equals(SdkConstants.JobParameters.ADD_GLOBAL_PARAMETERS_TO_TESTS_PARAM)).forEach(entry -> addParameterToTestElement(doc, testElement, entry.getKey(), entry.getValue()));
            }
            // add data table
            String dataTable = test.getParameter(DATA_TABLE_PARAMETER);
            if (SdkStringUtils.isNotEmpty(dataTable)) {
                Element dataTableElement = doc.createElement("DataTable");
                dataTableElement.setAttribute("path", workingDir + SdkConstants.FileSystem.WINDOWS_PATH_SPLITTER + dataTable);
                testElement.appendChild(dataTableElement);
            }
            // add iterations
            String iterations = test.getParameter(ITERATIONS_PARAMETER);
            if (SdkStringUtils.isNotEmpty(iterations)) {
                String[] parts = iterations.split(",");
                Element iterationElement = doc.createElement("Iterations");
                iterationElement.setAttribute("mode", parts[0].trim());
                if (parts.length >= 3) {
                    iterationElement.setAttribute("start", parts[1].trim());
                    iterationElement.setAttribute("end", parts[2].trim());
                }
                testElement.appendChild(iterationElement);
            }
            rootElement.appendChild(testElement);
        }
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.toString();
    } catch (ParserConfigurationException | TransformerException e) {
        String msg = "Failed to build MTBX content : " + e.getMessage();
        logger.error(msg);
        throw new RuntimeException(msg);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) DOMSource(javax.xml.transform.dom.DOMSource) TestsToRunConverter(com.hp.octane.integrations.executor.TestsToRunConverter) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) CollectionUtils(org.apache.commons.collections4.CollectionUtils) OctaneSDK(com.hp.octane.integrations.OctaneSDK) SdkConstants(com.hp.octane.integrations.utils.SdkConstants) Document(org.w3c.dom.Document) Map(java.util.Map) TestToRunData(com.hp.octane.integrations.executor.TestToRunData) OctaneClient(com.hp.octane.integrations.OctaneClient) StringWriter(java.io.StringWriter) OutputKeys(javax.xml.transform.OutputKeys) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SdkStringUtils(com.hp.octane.integrations.utils.SdkStringUtils) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) ConfigurationParameterFactory(com.hp.octane.integrations.services.configurationparameters.factory.ConfigurationParameterFactory) LogManager(org.apache.logging.log4j.LogManager) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) TestToRunData(com.hp.octane.integrations.executor.TestToRunData) Document(org.w3c.dom.Document) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 3 with TestToRunData

use of com.hp.octane.integrations.executor.TestToRunData in project octane-ci-java-sdk by MicroFocus.

the class MfMBTConverter method handleMBTModel.

private void handleMBTModel(List<TestToRunData> tests, String checkoutFolder, Map<String, String> globalParameters) {
    // replace test name if required
    for (TestToRunData data : tests) {
        data.setTestName(encodeTestNameIfRequired(data.getTestName()));
    }
    mbtTests = new ArrayList<>();
    int order = 1;
    for (TestToRunData data : tests) {
        String mbtDataRaw = data.getParameter(MBT_DATA);
        MbtData mbtData;
        if (MBT_DATA_NOT_INCLUDED.equals(mbtDataRaw)) {
            throw new RuntimeException("Failed to fetch mbt data for test " + data.getTestName());
        }
        try {
            String raw = new String(Base64.getDecoder().decode(mbtDataRaw), StandardCharsets.UTF_8);
            mbtData = DTOFactory.getInstance().dtoFromJson(raw, MbtData.class);
            if (mbtData.getTestingToolType().equals(TestingToolType.UFT)) {
                // advance package name only for uft tests. it is not relevant for codeless tests
                data.setPackageName("_" + order++);
            }
        } catch (Exception e) {
            String msg = "Failed to decode test action data " + data.getTestName() + " : " + e.getMessage();
            logger.error(msg);
            throw new RuntimeException(msg);
        }
        if (mbtData.getTestingToolType().equals(TestingToolType.UFT)) {
            convertUftTest(checkoutFolder, data, mbtData);
        } else {
            convertCodelessTest(data, mbtData);
        }
    }
}
Also used : MbtData(com.hp.octane.integrations.dto.general.MbtData) TestToRunData(com.hp.octane.integrations.executor.TestToRunData) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 4 with TestToRunData

use of com.hp.octane.integrations.executor.TestToRunData in project octane-ci-java-sdk by MicroFocus.

the class MfMBTConverter method handleMbtDataRetrieval.

private void handleMbtDataRetrieval(List<TestToRunData> tests, Map<String, String> globalParameters) {
    if (shouldRetrieveMbtData(tests)) {
        OctaneClient octaneClient = OctaneSDK.getClientByInstanceId(globalParameters.get(OCTANE_CONFIG_ID_PARAMETER_NAME));
        OctaneConfiguration octaneConfig = octaneClient.getConfigurationService().getConfiguration();
        String url = octaneConfig.getUrl() + "/api" + "/shared_spaces/" + octaneConfig.getSharedSpace() + "/workspaces/" + globalParameters.get(OCTANE_WORKSPACE_PARAMETER_NAME) + "/suite_runs/" + globalParameters.get(SUITE_RUN_ID_PARAMETER_NAME) + "/get_suite_data";
        Map<String, String> headers = new HashMap<>();
        headers.put(ACCEPT_HEADER, ContentType.APPLICATION_JSON.getMimeType());
        headers.put(OctaneRestClient.CLIENT_TYPE_HEADER, OctaneRestClient.CLIENT_TYPE_VALUE);
        OctaneRequest request = DTOFactory.getInstance().newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setHeaders(headers).setUrl(url);
        try {
            OctaneResponse octaneResponse = octaneClient.getRestService().obtainOctaneRestClient().execute(request);
            if (octaneResponse != null && octaneResponse.getStatus() == HttpStatus.SC_OK) {
                Map<String, String> parsedResponse = parseSuiteRunDataJson(octaneResponse.getBody());
                if (parsedResponse != null) {
                    for (TestToRunData test : tests) {
                        String runID = test.getParameter(INNER_RUN_ID_PARAMETER);
                        test.addParameters(MBT_DATA, parsedResponse.get(runID));
                    }
                }
            } else {
                logger.error("Failed to get response {}", (octaneResponse != null ? octaneResponse.getStatus() : "(null)"));
                return;
            }
        } catch (IOException e) {
            logger.error("Failed to get response ", e);
            return;
        }
    }
}
Also used : OctaneClient(com.hp.octane.integrations.OctaneClient) OctaneConfiguration(com.hp.octane.integrations.OctaneConfiguration) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) TestToRunData(com.hp.octane.integrations.executor.TestToRunData)

Example 5 with TestToRunData

use of com.hp.octane.integrations.executor.TestToRunData in project octane-ci-java-sdk by MicroFocus.

the class CucumberJVMConverter method convertInternal.

@Override
protected String convertInternal(List<TestToRunData> data, String executionDirectory, Map<String, String> globalParameters) {
    StringBuilder sb = new StringBuilder();
    String classJoiner = "";
    for (TestToRunData testData : data) {
        String featureFilePath = getFeatureFilePath(testData);
        if (featureFilePath != null && !featureFilePath.isEmpty()) {
            sb.append(classJoiner);
            sb.append("'");
            sb.append(featureFilePath);
            sb.append("'");
            classJoiner = " ";
        }
    }
    return sb.toString();
}
Also used : TestToRunData(com.hp.octane.integrations.executor.TestToRunData)

Aggregations

TestToRunData (com.hp.octane.integrations.executor.TestToRunData)5 OctaneClient (com.hp.octane.integrations.OctaneClient)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 OctaneConfiguration (com.hp.octane.integrations.OctaneConfiguration)1 OctaneSDK (com.hp.octane.integrations.OctaneSDK)1 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)1 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)1 Entity (com.hp.octane.integrations.dto.entities.Entity)1 MbtData (com.hp.octane.integrations.dto.general.MbtData)1 TestToRunDataCollection (com.hp.octane.integrations.executor.TestToRunDataCollection)1 TestsToRunConverter (com.hp.octane.integrations.executor.TestsToRunConverter)1 ConfigurationParameterFactory (com.hp.octane.integrations.services.configurationparameters.factory.ConfigurationParameterFactory)1 SdkConstants (com.hp.octane.integrations.utils.SdkConstants)1 SdkStringUtils (com.hp.octane.integrations.utils.SdkStringUtils)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1