Skip to content

Commit

Permalink
Merge pull request #66 from healenium/IS-65
Browse files Browse the repository at this point in the history
Is 65
  • Loading branch information
Alex-Reif authored Oct 6, 2020
2 parents b95df48 + 7864ea2 commit 5795d75
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
43 changes: 23 additions & 20 deletions src/main/java/com/epam/healenium/client/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -19,6 +19,7 @@
import com.epam.healenium.model.RequestDto;
import com.epam.healenium.treecomparing.Node;
import com.epam.healenium.treecomparing.Scored;
import com.epam.healenium.utils.SystemUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -59,10 +60,10 @@ public RestClient(Config config) {

private OkHttpClient okHttpClient() {
return new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
}

private ObjectMapper initMapper() {
Expand All @@ -79,16 +80,16 @@ private ObjectMapper initMapper() {
* Store info in backend
* @param by
*/
public void selectorRequest(By by, StackTraceElement element, List<Node> nodePath){
public void selectorRequest(By by, StackTraceElement element, List<Node> nodePath) {
RequestDto requestDto = mapper.buildDto(by, element, nodePath);
try {
RequestBody body = RequestBody.create(JSON, objectMapper.writeValueAsString(requestDto));
Request request = new Request.Builder()
.url(baseUrl)
.post(body)
.build();
.url(baseUrl)
.post(body)
.build();
okHttpClient().newCall(request).execute();
} catch (Exception e){
} catch (Exception e) {
log.warn("Failed to make response");
}
}
Expand All @@ -100,7 +101,7 @@ public void selectorRequest(By by, StackTraceElement element, List<Node> nodePat
* @param page
* @return
*/
public void healRequest(By locator, StackTraceElement element, String page, List<Scored<By>> choices, Scored<By> healed, byte[] screenshot){
public void healRequest(By locator, StackTraceElement element, String page, List<Scored<By>> choices, Scored<By> healed, byte[] screenshot) {
RequestDto requestDto = mapper.buildDto(locator, element, page, choices, healed, screenshot);
try {
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
Expand All @@ -109,13 +110,15 @@ public void healRequest(By locator, StackTraceElement element, String page, List
.build();

Request request = new Request.Builder()
.addHeader("sessionKey", sessionKey)
.url(baseUrl + "/healing")
.post(requestBody)
.build();
.addHeader("sessionKey", sessionKey)
.addHeader("instance", SystemUtils.getHostIpAddress())
.addHeader("hostProject", SystemUtils.getHostProjectName())
.url(baseUrl + "/healing")
.post(requestBody)
.build();
okHttpClient().newCall(request).execute();
} catch (Exception e){
log.warn("Failed to make response",e);
} catch (Exception e) {
log.warn("Failed to make response", e);
}
}

Expand All @@ -125,7 +128,7 @@ public void healRequest(By locator, StackTraceElement element, String page, List
* @param element
* @return
*/
public Optional<List<Node>> getLastValidPath(By locator, StackTraceElement element){
public Optional<List<Node>> getLastValidPath(By locator, StackTraceElement element) {
List<Node> nodes = null;
RequestDto requestDto = mapper.buildDto(locator, element);
try {
Expand All @@ -145,7 +148,7 @@ public Optional<List<Node>> getLastValidPath(By locator, StackTraceElement eleme
nodes = objectMapper.readValue(result, new TypeReference<List<Node>>() {
});
}
} catch (Exception ex){
} catch (Exception ex) {
log.warn("Failed to make response", ex);
}
return Optional.ofNullable(nodes);
Expand All @@ -155,7 +158,7 @@ public Optional<List<Node>> getLastValidPath(By locator, StackTraceElement eleme
*
* @return
*/
private String buildScreenshotName(){
private String buildScreenshotName() {
return "screenshot_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd-MMM-yyyy-hh-mm-ss").withLocale(Locale.US)) + ".png";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public String create(Node node) {
.stream()
.filter(entry -> StringUtils.isNoneBlank(entry.getKey(), entry.getValue()))
.filter(entry -> !SKIPPED_ATTRIBUTES.contains(entry.getKey()))
.map(entry -> String.format("[%s=\"%s\"]", entry.getKey().trim(), entry.getValue().trim()))
.map(entry -> String.format("[%s='%s']", entry.getKey().trim(), entry.getValue().trim()))
.collect(Collectors.joining());
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/epam/healenium/utils/SystemUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Healenium-web Copyright (C) 2019 EPAM
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.epam.healenium.utils;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.net.Inet4Address;
import java.net.UnknownHostException;

@Slf4j
@UtilityClass
public class SystemUtils {

public String getHostProjectName() {
String projectPath = System.getProperty("user.dir").replace("\\", "/");
return projectPath.substring(projectPath.lastIndexOf("/") + 1);
}

public String getHostIpAddress() {
try {
return Inet4Address.getLocalHost().getHostAddress();
} catch (UnknownHostException ex) {
log.warn("Failed to get host address", ex);
}
return StringUtils.EMPTY;
}
}

0 comments on commit 5795d75

Please sign in to comment.