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

org.catools.zapi.rest.execution.CZApiExecutionClient Maven / Gradle / Ivy

There is a newer version: 0.1.25
Show newest version
package org.catools.zapi.rest.execution;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.catools.common.collections.CList;
import org.catools.common.collections.CSet;
import org.catools.common.testng.model.CExecutionStatus;
import org.catools.zapi.configs.CZApiConfigs;
import org.catools.zapi.exception.CZApiException;
import org.catools.zapi.model.CZApiCycle;
import org.catools.zapi.model.CZApiExecutions;
import org.catools.zapi.rest.CZApiRestClient;
import org.codehaus.jettison.json.JSONObject;

import javax.ws.rs.core.UriBuilder;
import java.net.URI;

public class CZApiExecutionClient extends CZApiRestClient {
    public CZApiExecutionClient() {
        super();
    }

    public void addTestsToCycle(CSet issueKeys, CZApiCycle cycle, int partitionSize) {
        addTestsToCycle(cycle.getProject().getId(), cycle.getVersion().getId(), cycle.getId(), issueKeys, partitionSize);
    }

    public void addTestsToCycle(Long projectId, Long versionId, Long cycleId, CSet issueKeys, int partitionSize) {
        for (CList keys : issueKeys.partition(partitionSize)) {
            JSONObject entity;
            try {
                entity = new JSONObject().put("issues", keys)
                        .put("method", "1")
                        .put("cycleId", cycleId)
                        .put("projectId", projectId)
                        .put("versionId", versionId);
            } catch (Throwable t) {
                throw new CZApiException("Failed to build JSONObject", t);
            }

            URI cycleUri = UriBuilder.fromUri(CZApiConfigs.ZApi.getZApiUri()).path("/execution/addTestsToCycle").build();
            RequestSpecification specification = RestAssured.given().baseUri(cycleUri.toString()).body(entity.toString());
            post(specification);
        }
    }

    public void updateBulkStatus(CZApiExecutions exections, CExecutionStatus status) {
        updateBulkStatus(exections.mapToSet(e -> e.getId()), status);
    }

    public void updateBulkStatus(CSet executionIds, CExecutionStatus status) {
        updateBulkStatus(executionIds, status, 50);
    }

    public void updateBulkStatus(CSet executionIds, CExecutionStatus status, int partitionSize) {
        URI cycleUri = UriBuilder.fromUri(CZApiConfigs.ZApi.getZApiUri()).path("/execution/updateBulkStatus").build();
        try {
            for (CList ids : executionIds.partition(partitionSize)) {
                JSONObject entity = new JSONObject().put("executions", ids).put("status", CZApiConfigs.ZApi.getStatusMap().get(status.name()));
                RequestSpecification specification = RestAssured.given().baseUri(cycleUri.toString()).body(entity.toString());
                Response response = put(specification);
                logger.debug("response code:" + response.statusCode() + ", message:" + response.body().asString());
            }
        } catch (Throwable t) {
            throw new CZApiException("Could not update execution statuses", t);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy