Search in sources :

Example 1 with CompareByOrder

use of com.sequenceiq.it.cloudbreak.context.CompareByOrder in project cloudbreak by hortonworks.

the class TestInvocationListener method afterInvocation.

/**
 * Collects all the available test resources to a JSON file based on the:
 * - Related test DTO: provides a resource name type (eg.: environmentName),
 * - Test Context: provides the resource's unique name (eg.: aws-test-039df4d8aec04a61965).
 *
 * Resource files can be saved based on the test cases (eg.: resource_names_testCreateNewEnvironmentWithNewNetworkAndNoInternet);
 * because of each test has it's own Test Context, that has been built by the Given test steps for that thread of test execution.
 */
@Override
public void afterInvocation(IInvokedMethod invokedMethod, ITestResult testResult) {
    TestContext testContext;
    JSONObject jsonObject = new JSONObject();
    Object[] parameters = testResult.getParameters();
    if (parameters == null || parameters.length == 0) {
        LOGGER.warn("Test context could not be found because parameters array is empty in test result.");
        return;
    }
    try {
        testContext = (TestContext) parameters[0];
    } catch (ClassCastException e) {
        LOGGER.warn("Test context could not be casted from test result parameters.");
        return;
    }
    List<CloudbreakTestDto> testDtos = new ArrayList<>(testContext.getResourceNames().values());
    List<CloudbreakTestDto> orderedTestDtos = testDtos.stream().sorted(new CompareByOrder()).collect(Collectors.toList());
    for (CloudbreakTestDto testDto : orderedTestDtos) {
        try {
            String resourceNameType = Optional.ofNullable(testDto.getResourceNameType()).orElse("");
            if (!resourceNameType.trim().isEmpty()) {
                if (jsonObject.has(resourceNameType)) {
                    List<String> resourceNames = new ArrayList<>();
                    try {
                        JSONArray resources = jsonObject.getJSONArray(resourceNameType);
                        for (int i = 0; i < resources.length(); i++) {
                            String resource = resources.getString(i);
                            resourceNames.add(resource);
                        }
                    } catch (JSONException e) {
                        String resource = jsonObject.getString(resourceNameType);
                        resourceNames.add(resource);
                    }
                    resourceNames.add(testDto.getName());
                    LOGGER.info("Created resource name array: '{}'.", resourceNames);
                    JSONArray resourceNameArray = new JSONArray(resourceNames);
                    jsonObject.put(resourceNameType, resourceNameArray);
                } else {
                    jsonObject.put(resourceNameType, testDto.getName());
                }
                LOGGER.info("Put Resource Name: '{}' to JSON Object.", testDto.getName());
            }
        } catch (Exception e) {
            LOGGER.info("Appending JSON object is failing, because of: {}", e.getMessage(), e);
        }
    }
    if (jsonObject.length() != 0) {
        String fileName = "resource_names_" + testContext.getTestMethodName().orElseGet(this::getDefaultFileNameTag) + ".json";
        try {
            Files.writeString(Paths.get(fileName), jsonObject.toString());
            LOGGER.info("Resource file have been created with name: {} and content: {}.", fileName, jsonObject);
        } catch (IOException e) {
            LOGGER.info("Creating/Appending resource file throws exception: {}", e.getMessage(), e);
        } catch (Exception e) {
            LOGGER.info("Creating/Appending resource file is failing, because of: {}", e.getMessage(), e);
        }
    } else {
        LOGGER.info("No resources found, no output file needs to be created.");
    }
}
Also used : TestContext(com.sequenceiq.it.cloudbreak.context.TestContext) CompareByOrder(com.sequenceiq.it.cloudbreak.context.CompareByOrder) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) IOException(java.io.IOException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) CloudbreakTestDto(com.sequenceiq.it.cloudbreak.dto.CloudbreakTestDto) JSONObject(org.json.JSONObject)

Aggregations

CompareByOrder (com.sequenceiq.it.cloudbreak.context.CompareByOrder)1 TestContext (com.sequenceiq.it.cloudbreak.context.TestContext)1 CloudbreakTestDto (com.sequenceiq.it.cloudbreak.dto.CloudbreakTestDto)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1