Search in sources :

Example 1 with RequestBodyEntity

use of kong.unirest.RequestBodyEntity in project dependency-track-maven-plugin by pmckeown.

the class BomClient method uploadBom.

/**
 * Upload a BOM to the Dependency-Track server.  The BOM is processed asynchronously after the upload is completed
 * and the response returned.  The response contains a token that can be used later to query if the bom that the
 * token relates to has been completely processed.
 *
 * @param bom the request object containing the project details and the Base64 encoded bom.xml
 * @return a response containing a token to later determine if processing the supplied BOM is completed
 */
Response<UploadBomResponse> uploadBom(UploadBomRequest bom) {
    RequestBodyEntity requestBodyEntity = Unirest.put(commonConfig.getDependencyTrackBaseUrl() + V1_BOM).header(CONTENT_TYPE, "application/json").header("X-Api-Key", commonConfig.getApiKey()).body(bom);
    HttpResponse<UploadBomResponse> httpResponse = requestBodyEntity.asObject(new GenericType<UploadBomResponse>() {
    });
    Optional<UploadBomResponse> body;
    if (httpResponse.isSuccess()) {
        body = Optional.of(httpResponse.getBody());
    } else {
        body = Optional.empty();
    }
    return new Response<>(httpResponse.getStatus(), httpResponse.getStatusText(), httpResponse.isSuccess(), body);
}
Also used : HttpResponse(kong.unirest.HttpResponse) Response(io.github.pmckeown.dependencytrack.Response) RequestBodyEntity(kong.unirest.RequestBodyEntity)

Example 2 with RequestBodyEntity

use of kong.unirest.RequestBodyEntity in project jbang-catalog by quintesse.

the class Util method apiPost.

HttpResponse<JsonNode> apiPost(String api, Object body) {
    String url = getApiUrl(api);
    logApiRequest("POST", api, body);
    RequestBodyEntity req = Unirest.post(url).body(body);
    if (activeServer.user != null && activeServer.password != null) {
        req.basicAuth(activeServer.user, activeServer.password);
    }
    HttpResponse<JsonNode> res = req.asJson();
    logApiResult(res);
    return res;
}
Also used : RequestBodyEntity(kong.unirest.RequestBodyEntity) JsonNode(kong.unirest.JsonNode)

Example 3 with RequestBodyEntity

use of kong.unirest.RequestBodyEntity in project seleniumRobot by bhecquet.

the class ConnectorsTest method preparePostRequest.

private HttpRequest preparePostRequest(String serverUrl, String responseType, HttpRequestWithBody postRequest, HttpResponse<String> response, HttpResponse<JsonNode> jsonResponse) {
    RequestBodyEntity requestBodyEntity = mock(RequestBodyEntity.class);
    MultipartBody requestMultipartBody = mock(MultipartBody.class);
    when(postRequest.socketTimeout(anyInt())).thenReturn(postRequest);
    when(postRequest.field(anyString(), anyString())).thenReturn(requestMultipartBody);
    when(postRequest.field(anyString(), anyInt())).thenReturn(requestMultipartBody);
    when(postRequest.field(anyString(), anyLong())).thenReturn(requestMultipartBody);
    when(postRequest.field(anyString(), anyDouble())).thenReturn(requestMultipartBody);
    when(postRequest.field(anyString(), any(File.class))).thenReturn(requestMultipartBody);
    when(postRequest.basicAuth(anyString(), anyString())).thenReturn(postRequest);
    when(postRequest.headerReplace(anyString(), anyString())).thenReturn(postRequest);
    when(postRequest.queryString(anyString(), anyString())).thenReturn(postRequest);
    when(postRequest.queryString(anyString(), anyInt())).thenReturn(postRequest);
    when(postRequest.queryString(anyString(), anyBoolean())).thenReturn(postRequest);
    when(postRequest.queryString(anyString(), any(SessionId.class))).thenReturn(postRequest);
    when(postRequest.header(anyString(), anyString())).thenReturn(postRequest);
    when(requestMultipartBody.field(anyString(), anyString())).thenReturn(requestMultipartBody);
    when(requestMultipartBody.field(anyString(), any(File.class))).thenReturn(requestMultipartBody);
    when(requestMultipartBody.asString()).thenReturn(response);
    doReturn(response).when(postRequest).asString();
    when(postRequest.getUrl()).thenReturn(serverUrl);
    when(postRequest.body(any(JSONObject.class))).thenReturn(requestBodyEntity);
    when(postRequest.body(any(byte[].class))).thenReturn(requestBodyEntity);
    when(postRequest.asJson()).thenReturn(jsonResponse);
    when(requestBodyEntity.asJson()).thenReturn(jsonResponse);
    when(requestBodyEntity.asString()).thenReturn(response);
    when(requestMultipartBody.getUrl()).thenReturn(serverUrl);
    when(requestMultipartBody.asJson()).thenReturn(jsonResponse);
    if ("request".equals(responseType)) {
        return postRequest;
    } else if ("body".equals(responseType)) {
        return requestMultipartBody;
    } else if ("requestBodyEntity".equals(responseType)) {
        return requestBodyEntity;
    } else {
        return null;
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) RequestBodyEntity(kong.unirest.RequestBodyEntity) MultipartBody(kong.unirest.MultipartBody) File(java.io.File) SessionId(org.openqa.selenium.remote.SessionId)

Example 4 with RequestBodyEntity

use of kong.unirest.RequestBodyEntity in project seleniumRobot by bhecquet.

the class TestCampaign method testCreateCampaignWithError.

@Test(groups = { "ut" }, expectedExceptions = ScenarioException.class)
public void testCreateCampaignWithError() {
    RequestBodyEntity postRequest = (RequestBodyEntity) createServerMock("POST", "/campaigns", 200, "{}", "requestBodyEntity");
    when(postRequest.asJson()).thenThrow(UnirestException.class);
    CampaignFolder campaignFolder = new CampaignFolder("http://localhost:8080/api/rest/latest/campaign-folders/7", 7, "folder", project, null);
    Campaign.create(project, "myCampaign", campaignFolder);
}
Also used : RequestBodyEntity(kong.unirest.RequestBodyEntity) CampaignFolder(com.seleniumtests.connectors.tms.squash.entities.CampaignFolder) Test(org.testng.annotations.Test) ConnectorsTest(com.seleniumtests.ConnectorsTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with RequestBodyEntity

use of kong.unirest.RequestBodyEntity in project seleniumRobot by bhecquet.

the class TestIteration method testCreateIterationWithError.

@Test(groups = { "ut" }, expectedExceptions = ScenarioException.class)
public void testCreateIterationWithError() {
    RequestBodyEntity postRequest = (RequestBodyEntity) createServerMock("POST", "/campaigns/2/iterations", 200, "{}", "requestBodyEntity");
    when(postRequest.asJson()).thenThrow(UnirestException.class);
    Campaign campaign = new Campaign("http://localhost:8080/api/rest/latest/campaigns/2", 2, "campaign");
    Iteration.create(campaign, "new iteration");
}
Also used : Campaign(com.seleniumtests.connectors.tms.squash.entities.Campaign) RequestBodyEntity(kong.unirest.RequestBodyEntity) Test(org.testng.annotations.Test) ConnectorsTest(com.seleniumtests.ConnectorsTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

RequestBodyEntity (kong.unirest.RequestBodyEntity)9 ConnectorsTest (com.seleniumtests.ConnectorsTest)5 Test (org.testng.annotations.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 JsonNode (kong.unirest.JsonNode)2 Campaign (com.seleniumtests.connectors.tms.squash.entities.Campaign)1 CampaignFolder (com.seleniumtests.connectors.tms.squash.entities.CampaignFolder)1 Iteration (com.seleniumtests.connectors.tms.squash.entities.Iteration)1 TestCase (com.seleniumtests.connectors.tms.squash.entities.TestCase)1 TestPlanItemExecution (com.seleniumtests.connectors.tms.squash.entities.TestPlanItemExecution)1 Response (io.github.pmckeown.dependencytrack.Response)1 File (java.io.File)1 HttpResponse (kong.unirest.HttpResponse)1 MultipartBody (kong.unirest.MultipartBody)1 JSONObject (kong.unirest.json.JSONObject)1 SessionId (org.openqa.selenium.remote.SessionId)1