Search in sources :

Example 1 with MultipartBody

use of kong.unirest.MultipartBody in project bacon by project-ncl.

the class DirectKeycloakClientImpl method getCredentialServiceAccount.

@Override
public Credential getCredentialServiceAccount(String keycloakBaseUrl, String realm, String serviceAccountUsername, String secret) throws KeycloakClientException {
    String keycloakEndpoint = keycloakEndpoint(keycloakBaseUrl, realm);
    try {
        log.debug("Getting token via clientServiceAccountUsername / secret");
        MultipartBody body = Unirest.post(keycloakEndpoint).field("grant_type", "client_credentials").field("client_id", serviceAccountUsername).field("client_secret", secret);
        KeycloakResponse response = getKeycloakResponseWithRetries(body);
        Instant now = Instant.now();
        return Credential.builder().keycloakBaseUrl(keycloakBaseUrl).realm(realm).client(serviceAccountUsername).accessToken(response.getAccessToken()).accessTokenExpiresIn(now.plusSeconds(response.getExpiresIn())).refreshToken(response.getRefreshToken()).refreshTokenExpiresIn(now.plusSeconds(response.getRefreshExpiresIn())).build();
    } catch (Exception e) {
        throw new KeycloakClientException(e);
    }
}
Also used : KeycloakResponse(org.jboss.pnc.bacon.auth.model.KeycloakResponse) MultipartBody(kong.unirest.MultipartBody) Instant(java.time.Instant) FatalException(org.jboss.pnc.bacon.common.exception.FatalException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) UnirestException(kong.unirest.UnirestException)

Example 2 with MultipartBody

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

the class SeleniumRobotElementInfoServerConnector method updateElementInfoToServer.

/**
 * compare original checkout to updated data. Send updated data to server
 */
public void updateElementInfoToServer() {
    Map<String, ElementInfo> referenceElementInfos = ElementInfo.getAllStoredElementInfos(true);
    // get reference
    for (Entry<String, ElementInfo> currentElementInfoEntry : ElementInfo.getAllStoredElementInfos(false).entrySet()) {
        ElementInfo currentElementInfo = currentElementInfoEntry.getValue();
        ElementInfo referenceElementInfo = referenceElementInfos.get(currentElementInfoEntry.getKey());
        // if no reference exists, create it on server
        if (referenceElementInfo == null) {
        // TODO
        } else if (!currentElementInfo.getLastUpdate().equals(referenceElementInfo.getLastUpdate())) {
            try {
                MultipartBody request = buildPatchRequest(url + ELEMENT_INFO_API_URL).field("application", applicationId).field("uuid", currentElementInfo.getId()).field("name", currentElementInfo.getName());
                if (currentElementInfo.getCoordX().equals(referenceElementInfo.getCoordX())) {
                    request = request.field("coordX", currentElementInfo.getCoordX().toString());
                }
                if (currentElementInfo.getCoordY().equals(referenceElementInfo.getCoordY())) {
                    request = request.field("coordY", currentElementInfo.getCoordY().toString());
                }
                if (currentElementInfo.getWidth().equals(referenceElementInfo.getWidth())) {
                    request = request.field("width", currentElementInfo.getWidth().toString());
                }
                if (currentElementInfo.getHeight().equals(referenceElementInfo.getHeight())) {
                    request = request.field("height", currentElementInfo.getHeight().toString());
                }
                if (currentElementInfo.getLocator().equals(referenceElementInfo.getLocator())) {
                    request = request.field("locator", currentElementInfo.getLocator());
                }
                if (currentElementInfo.getTagName().equals(referenceElementInfo.getTagName())) {
                    request = request.field("tagName", currentElementInfo.getTagName());
                }
                if (currentElementInfo.getText().equals(referenceElementInfo.getText())) {
                    request = request.field("text", currentElementInfo.getText());
                }
                if (currentElementInfo.getB64Image().equals(referenceElementInfo.getB64Image())) {
                    request = request.field("b64Image", currentElementInfo.getB64Image());
                }
                if (currentElementInfo.getAttributes().equals(referenceElementInfo.getAttributes())) {
                    request = request.field("attributes", new JSONObject(currentElementInfo.getAttributes()).toString());
                }
                if (currentElementInfo.getTotalSearch() != referenceElementInfo.getTotalSearch()) {
                    request = request.field("totalSearch", "+" + Integer.toString(currentElementInfo.getTotalSearch() - referenceElementInfo.getTotalSearch()));
                }
                if (currentElementInfo.getTagStability() != referenceElementInfo.getTagStability()) {
                    request = request.field("tagStability", "+" + Integer.toString(currentElementInfo.getTagStability() - referenceElementInfo.getTagStability()));
                }
                if (currentElementInfo.getTextStability() != referenceElementInfo.getTextStability()) {
                    request = request.field("textStability", "+" + Integer.toString(currentElementInfo.getTextStability() - referenceElementInfo.getTextStability()));
                }
                if (currentElementInfo.getRectangleStability() != referenceElementInfo.getRectangleStability()) {
                    request = request.field("rectangleStability", "+" + Integer.toString(currentElementInfo.getRectangleStability() - referenceElementInfo.getRectangleStability()));
                }
                if (currentElementInfo.getB64ImageStability() != referenceElementInfo.getB64ImageStability()) {
                    request = request.field("b64ImageStability", "+" + Integer.toString(currentElementInfo.getB64ImageStability() - referenceElementInfo.getB64ImageStability()));
                }
                if (currentElementInfo.getAttributesStability() != referenceElementInfo.getAttributesStability()) {
                    Map<String, Object> attributeStabilityDiff = new HashMap<>();
                    for (String attribute : currentElementInfo.getAttributesStability().keySet()) {
                        attributeStabilityDiff.put(attribute, "+" + Integer.toString(currentElementInfo.getAttributesStability().get(attribute) - referenceElementInfo.getAttributesStability().getOrDefault(attribute, 0)));
                    }
                    request = request.field("attributesStability", new JSONObject(attributeStabilityDiff).toString());
                }
                getJSonResponse(request);
            } catch (UnirestException e) {
                logger.warn("cannot update element info: " + e.getMessage());
            }
        }
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) ElementInfo(com.seleniumtests.uipage.htmlelements.ElementInfo) MultipartBody(kong.unirest.MultipartBody) UnirestException(kong.unirest.UnirestException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with MultipartBody

use of kong.unirest.MultipartBody 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 MultipartBody

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

the class ConnectorsTest method createJsonServerMock.

protected OngoingStubbing<JsonNode> createJsonServerMock(String requestType, String apiPath, int statusCode, String... replyData) throws UnirestException {
    @SuppressWarnings("unchecked") HttpResponse<JsonNode> jsonResponse = mock(HttpResponse.class);
    HttpRequest request = mock(HttpRequest.class);
    MultipartBody requestMultipartBody = mock(MultipartBody.class);
    HttpRequestWithBody postRequest = mock(HttpRequestWithBody.class);
    when(request.getUrl()).thenReturn(SERVER_URL);
    when(jsonResponse.getStatus()).thenReturn(statusCode);
    OngoingStubbing<JsonNode> stub = when(jsonResponse.getBody()).thenReturn(new JsonNode(replyData[0]));
    for (String reply : Arrays.asList(replyData).subList(1, replyData.length)) {
        stub = stub.thenReturn(new JsonNode(reply));
    }
    switch(requestType) {
        case "GET":
            GetRequest getRequest = mock(GetRequest.class);
            when(Unirest.get(SERVER_URL + apiPath)).thenReturn(getRequest);
            when(getRequest.header(anyString(), anyString())).thenReturn(getRequest);
            when(getRequest.asJson()).thenReturn(jsonResponse);
            when(getRequest.queryString(anyString(), anyString())).thenReturn(getRequest);
            when(getRequest.queryString(anyString(), anyInt())).thenReturn(getRequest);
            when(getRequest.queryString(anyString(), anyBoolean())).thenReturn(getRequest);
            return stub;
        case "POST":
            when(Unirest.post(SERVER_URL + apiPath)).thenReturn(postRequest);
        case "PATCH":
            when(Unirest.patch(SERVER_URL + apiPath)).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(), any(File.class))).thenReturn(requestMultipartBody);
            when(postRequest.queryString(anyString(), anyString())).thenReturn(postRequest);
            when(postRequest.queryString(anyString(), anyInt())).thenReturn(postRequest);
            when(postRequest.queryString(anyString(), anyBoolean())).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);
            return stub;
    }
    return null;
}
Also used : HttpRequest(kong.unirest.HttpRequest) MultipartBody(kong.unirest.MultipartBody) HttpRequestWithBody(kong.unirest.HttpRequestWithBody) GetRequest(kong.unirest.GetRequest) JsonNode(kong.unirest.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File)

Example 5 with MultipartBody

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

the class TestSeleniumRobotSnapshotServerConnector method testCreateTestCaseInSessionLongName.

/**
 * Test case in session name is limited to 100 chars by server, check we strip it
 * @throws UnirestException
 */
@Test(groups = { "ut" })
public void testCreateTestCaseInSessionLongName() throws UnirestException {
    SeleniumRobotSnapshotServerConnector connector = spy(configureMockedSnapshotServerConnection());
    MultipartBody request = (MultipartBody) createServerMock("POST", SeleniumRobotSnapshotServerConnector.TESTCASEINSESSION_API_URL, 200, "{'id': '14'}", "body");
    Integer sessionId = connector.createSession("Session1");
    Integer testCaseId = connector.createTestCase("Test 1");
    Integer testCaseInSessionId = connector.createTestCaseInSession(sessionId, testCaseId, "Test 1" + StringUtils.repeat("-", 95));
    verify(request).field("name", ("Test 1" + StringUtils.repeat("-", 94)));
    Assert.assertEquals((int) testCaseInSessionId, 14);
}
Also used : MultipartBody(kong.unirest.MultipartBody) SeleniumRobotSnapshotServerConnector(com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector) Test(org.testng.annotations.Test) ConnectorsTest(com.seleniumtests.ConnectorsTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

MultipartBody (kong.unirest.MultipartBody)11 ConnectorsTest (com.seleniumtests.ConnectorsTest)4 SeleniumRobotSnapshotServerConnector (com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Test (org.testng.annotations.Test)4 Instant (java.time.Instant)3 UnirestException (kong.unirest.UnirestException)3 JSONObject (kong.unirest.json.JSONObject)3 KeycloakResponse (org.jboss.pnc.bacon.auth.model.KeycloakResponse)3 File (java.io.File)2 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2 FatalException (org.jboss.pnc.bacon.common.exception.FatalException)2 ElementInfo (com.seleniumtests.uipage.htmlelements.ElementInfo)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GetRequest (kong.unirest.GetRequest)1 HttpRequest (kong.unirest.HttpRequest)1 HttpRequestWithBody (kong.unirest.HttpRequestWithBody)1 JsonNode (kong.unirest.JsonNode)1 RequestBodyEntity (kong.unirest.RequestBodyEntity)1