Skip to content

Commit

Permalink
Merge pull request #472 from szprutamich/master
Browse files Browse the repository at this point in the history
Version 3.15
  • Loading branch information
szprutamich authored Mar 7, 2023
2 parents 449a8db + 67334a4 commit d31d1a6
Show file tree
Hide file tree
Showing 49 changed files with 145 additions and 156 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.testdroid</groupId>
<artifactId>testdroid-api</artifactId>
<version>3.14</version>
<version>3.15</version>
<packaging>jar</packaging>
<name>Bitbar API v2</name>
<url>https://github.com/bitbar/testdroid-api</url>
Expand All @@ -12,7 +12,7 @@
<jacoco-maven-plugin.skip>true</jacoco-maven-plugin.skip>
<!-- disable default deployment -->
<maven.deploy.skip>true</maven.deploy.skip>
<org.springframework.boot.spring-boot-dependencies>2.7.8</org.springframework.boot.spring-boot-dependencies>
<org.springframework.boot.spring-boot-dependencies>2.7.9</org.springframework.boot.spring-boot-dependencies>
<org.apache.commons.collections4.version>4.4</org.apache.commons.collections4.version>
<org.apache.commons.io.version>2.11.0</org.apache.commons.io.version>
<commons-text.version>1.10.0</commons-text.version>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/testdroid/api/APIException.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
*/
public class APIException extends Exception {

private Integer status;
private final Integer status;

public APIException() {
super();
this.status = null;
}

public APIException(String message) {
Expand All @@ -22,6 +23,7 @@ public APIException(Integer status, String message) {

public APIException(Throwable t) {
super(t);
this.status = null;
}

public APIException(String message, Throwable t) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/testdroid/api/APIListResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public class APIListResource<T extends APIEntity> {

private final String resourceURI;

private APIList<T> entity;

public APIListResource(APIClient client, String resourceURI, Class<T> type) {
this.client = client;
this.resourceURI = resourceURI;
Expand All @@ -29,7 +27,7 @@ public APIListResource(APIClient client, String resourceURI, Context<T> context)
}

public APIList<T> getEntity() throws APIException {
entity = client.get(resourceURI, context);
APIList<T> entity = client.get(resourceURI, context);
for (APIEntity item : entity.getData()) {
item.client = this.client;
item.selfURI = APIEntity.createUri(this.resourceURI, String.format("/%s", item.id));
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/testdroid/api/AbstractAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ protected <T extends APIEntity> T postOnce(
MultipartFormDataContent.Part filePart = new MultipartFormDataContent.Part("file", fileContent);
multipartContent.addPart(filePart);

for (String name : fileExtraParams.keySet()) {
MultipartFormDataContent.Part part = new MultipartFormDataContent.Part(name,
new ByteArrayContent(null, fileExtraParams.get(name).getBytes()));
for (Map.Entry<String, String> entry : fileExtraParams.entrySet()) {
MultipartFormDataContent.Part part = new MultipartFormDataContent.Part(entry.getKey(),
new ByteArrayContent(null, entry.getValue().getBytes()));
part.setHeaders(new HttpHeaders().set(
"Content-Disposition", String.format("form-data; name=\"%s\"", name)));
"Content-Disposition", String.format("form-data; name=\"%s\"", entry.getKey())));
multipartContent.addPart(part);
}
content = multipartContent;
Expand Down Expand Up @@ -360,8 +360,8 @@ protected <T extends APIEntity> String buildUrl(String url, Context<T> context)
}

protected Map<String, Object> fixMapParameters(Map<String, Object> map) {
return map.entrySet().stream().collect(toMap(Map.Entry::getKey, p -> p.getValue() == null ? EMPTY :
p.getValue() instanceof Enum<?> ? p.getValue().toString() : p.getValue()));
return map.entrySet().stream().collect(toMap(Map.Entry::getKey, p -> Optional.ofNullable(p.getValue())
.map(v -> v instanceof Enum<?> ? v.toString() : v).orElse(EMPTY)));
}

protected APIException getAPIException(HttpResponseException ex) {
Expand Down
36 changes: 23 additions & 13 deletions src/main/java/com/testdroid/api/DefaultAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,26 @@ public class DefaultAPIClient extends AbstractAPIClient {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultAPIClient.class);

private static final String ACCESS_TOKEN = "access_token";

public static final int HTTP_CONNECT_TIMEOUT = 60000;

public static final int HTTP_READ_TIMEOUT = 60000;

public static final String BITBAR_API_OAUTH2_CLIENT_ID = "testdroid-cloud-api";

private static final String EXPIRES_IN = "expires_in";

private static final String FAILED_TO_ACQUIRE_ACCESS_TOKEN = "Failed to acquire access token";

private static final String FAILED_TO_ACQUIRE_ACCESS_TOKEN_REASON = "Failed to acquire access token. Reason: %s";

private static final String FAILED_TO_REFRESH_ACCESS_TOKEN = "Failed to refresh access token";

private static final String FAILED_TO_REFRESH_ACCESS_TOKEN_REASON = "Failed to refresh access token. Reason: %s";

private static final String REFRESH_TOKEN = "refresh_token";

protected String accessToken;

protected long accessTokenExpireTime = 0;
Expand Down Expand Up @@ -118,10 +128,10 @@ private void initializeDefaultAPIClient(String cloudURL, String username, String

@Override
protected HttpRequestFactory getRequestFactory() throws APIException {
String accessToken = getAccessToken();
String token = getAccessToken();
final Credential credential = new Credential.Builder(BearerToken.queryParameterAccessMethod()).build();
if (StringUtils.isNotBlank(accessToken)) {
credential.setAccessToken(accessToken);
if (StringUtils.isNotBlank(token)) {
credential.setAccessToken(token);
}
return httpTransport.createRequestFactory(credential);
}
Expand Down Expand Up @@ -167,9 +177,9 @@ protected String acquireAccessToken() throws APIException {

String responseJson = StringUtils.join(IOUtils.readLines(response.getContent(), UTF_8), "\n");
Map<String, String> json = fromJson(responseJson, TypeReferenceFactory.getMapTypeReference());
accessTokenExpireTime = System.currentTimeMillis() + (Long.parseLong(json.get("expires_in")) * 1000);
refreshToken = json.get("refresh_token");
return json.get("access_token");
accessTokenExpireTime = System.currentTimeMillis() + (Long.parseLong(json.get(EXPIRES_IN)) * 1000);
refreshToken = json.get(REFRESH_TOKEN);
return json.get(ACCESS_TOKEN);
} catch (HttpResponseException ex) {
String message;
if (StringUtils.isNotBlank(ex.getContent())) {
Expand Down Expand Up @@ -199,8 +209,8 @@ protected String refreshAccessToken() throws APIException {
GenericUrl url = new GenericUrl(String.format("%s/oauth/token", cloudURL));
HashMap<String, Object> data = new HashMap<>();
data.put("client_id", BITBAR_API_OAUTH2_CLIENT_ID);
data.put("grant_type", "refresh_token");
data.put("refresh_token", refreshToken);
data.put("grant_type", REFRESH_TOKEN);
data.put(REFRESH_TOKEN, refreshToken);
HttpContent content = new UrlEncodedContent(data);

HttpRequest request = httpTransport.createRequestFactory().buildPostRequest(url, content);
Expand All @@ -210,16 +220,16 @@ protected String refreshAccessToken() throws APIException {
response = request.execute();

if (response.getStatusCode() != 200) {
throw new APIException(response.getStatusCode(), "Failed to refresh access token");
throw new APIException(response.getStatusCode(), FAILED_TO_REFRESH_ACCESS_TOKEN);
}

String jsonContent = StringUtils.join(IOUtils.readLines(response.getContent(), UTF_8), "\n");
Map<String, String> json = fromJson(jsonContent, TypeReferenceFactory.getMapTypeReference());
accessTokenExpireTime = System.currentTimeMillis() + (Long.parseLong(json.get("expires_in")) * 1000);
refreshToken = json.get("refresh_token");
return json.get("access_token");
accessTokenExpireTime = System.currentTimeMillis() + (Long.parseLong(json.get(EXPIRES_IN)) * 1000);
refreshToken = json.get(REFRESH_TOKEN);
return json.get(ACCESS_TOKEN);
} catch (IOException ex) {
throw new APIException(String.format("Failed to refresh access token. Reason: %s", ex.getMessage()), ex);
throw new APIException(String.format(FAILED_TO_REFRESH_ACCESS_TOKEN_REASON, ex.getMessage()), ex);
} finally {
disconnectQuietly(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
public class MultipartFormDataContent extends AbstractHttpContent {

private static final String BOUNDARY = "boundary";

private static final String NEWLINE = "\r\n";

private static final String TWO_DASHES = "--";
Expand All @@ -24,15 +26,14 @@ public class MultipartFormDataContent extends AbstractHttpContent {
private ArrayList<Part> parts = new ArrayList<>();

public MultipartFormDataContent() {
super(new HttpMediaType("multipart/form-data").setParameter("boundary", "__END_OF_PART__"));
super(new HttpMediaType("multipart/form-data").setParameter(BOUNDARY, "__END_OF_PART__"));
}

public void writeTo(OutputStream out) throws IOException {
Writer writer = new OutputStreamWriter(out, getCharset());
String boundary = getBoundary();

HttpHeaders headers;
String contentDisposition;
HttpContent content;
StreamingContent streamingContent;
for (Part part : parts) {
Expand All @@ -49,7 +50,7 @@ public void writeTo(OutputStream out) throws IOException {

// Write disposition
if (part.getName() != null) {
contentDisposition = String.format("form-data; name=\"%s\"", part.name);
String contentDisposition = String.format("form-data; name=\"%s\"", part.name);
// Do we have a filename?
// Then add to the content dispos
if (part.filename != null) {
Expand Down Expand Up @@ -148,7 +149,7 @@ public MultipartFormDataContent addPart(Part part) {
* Returns the boundary string to use.
*/
public final String getBoundary() {
return getMediaType().getParameter("boundary");
return getMediaType().getParameter(BOUNDARY);
}

/**
Expand All @@ -164,7 +165,7 @@ public final String getBoundary() {
* </p>
*/
public MultipartFormDataContent setBoundary(String boundary) {
getMediaType().setParameter("boundary", Preconditions.checkNotNull(boundary));
getMediaType().setParameter(BOUNDARY, Preconditions.checkNotNull(boundary));
return this;
}

Expand Down Expand Up @@ -287,4 +288,4 @@ public Part setEncoding(HttpEncoding encoding) {
return this;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public enum DeactivateReason {
public APIAccountService() {
}

@SuppressWarnings("squid:S107")
public APIAccountService(
Long accountId, Long activatedById, String activatedByName, boolean active, boolean autoRenew,
String braintreeId, LocalDateTime createTime, Long deactivatedById, String deactivatedByName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class APIAccountServicePayment extends APIEntity {
public APIAccountServicePayment() {
}

@SuppressWarnings("squid:S107")
public APIAccountServicePayment(
Long id, Long accountId, LocalDateTime startBillingPeriod, LocalDateTime endBillingPeriod, String name,
Long totalPrice, Long includedTime, Long additionalTime, Long usedTime) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/testdroid/api/model/APIActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum HttpMethod {
public APIActivity() {
}

@SuppressWarnings("squid:S107")
public APIActivity(
Long id, LocalDateTime createTime, HttpMethod httpMethod, String userAgent, String parameters, String uri,
String body, String accept, Long userId, String userEmail, boolean deprecatedResource) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/testdroid/api/model/APIAdminDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public APIAdminDevice(Long id) {
super(id);
}

@SuppressWarnings("squid:S107")
public APIAdminDevice(
Long id, String name, String manufacturer, boolean enabled, String serialId, String fingerprint,
String unlockGesture, String releaseVersion, Integer apiLevel, Long deviceModelId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public APIAdminDeviceModel() {

}

@SuppressWarnings("squid:S107")
public APIAdminDeviceModel(
Long id, String name, APIDevice.OsType osType, APIDevice.Platform platform, String location,
Boolean dedicated, String releaseVersion, Boolean enabled, Integer online, Integer total, Long running,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ public APIAdminDeviceSession() {

}

@SuppressWarnings("squid:S107")
public APIAdminDeviceSession(
Long id, LocalDateTime createTime, LocalDateTime startTime, LocalDateTime endTime,
String startedByDisplayName, Long projectId,
String projectName, Long testRunId, String testRunName, APIDeviceSession.State state, Integer priority,
Boolean billable, Long deviceTime, APIDeviceSessionStep.Type currentStepType, String retriedFailReason) {
String startedByDisplayName, Long projectId, String projectName, Long testRunId, String testRunName,
APIDeviceSession.State state, Integer priority, Boolean billable, Long deviceTime,
APIDeviceSessionStep.Type currentStepType, String retriedFailReason) {
super(id);
this.createTime = TimeConverter.toDate(createTime);
this.startTime = TimeConverter.toDate(startTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class APIAdminFrameworkStatistics extends APIEntity {
public APIAdminFrameworkStatistics() {
}

@SuppressWarnings("squid:S107")
public APIAdminFrameworkStatistics(
Date day, Long frameworkId, APIDevice.OsType osType, String releaseVersion, String frameworkName,
APIDeviceSession.State state, Long count, APIDeviceSession.Type type, Long userId, String userEmail,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class APIAdminInteractiveDeviceSession extends APIEntity {
public APIAdminInteractiveDeviceSession() {
}

@SuppressWarnings("squid:S107")
public APIAdminInteractiveDeviceSession(
Long id, LocalDateTime createTime, LocalDateTime startTime, LocalDateTime endTime, String userEmail,
Long userId, Long duration, String deviceModelName, Long deviceModelId, String deviceName, Long deviceId,
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/testdroid/api/model/APIAdminTestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class APIAdminTestRun extends APIEntity {
public APIAdminTestRun() {
}

@SuppressWarnings("squid:S107")
public APIAdminTestRun(
Long id, LocalDateTime createTime, LocalDateTime startTime, LocalDateTime endTime, APITestRun.State state,
Long startedById, String userName, String projectName, String testRunName, Float successRatio,
Expand All @@ -67,6 +68,7 @@ public APIAdminTestRun(
this.frameworkName = frameworkName;
}

@SuppressWarnings("squid:S107")
public APIAdminTestRun(
Long id, LocalDateTime createTime, LocalDateTime startTime, LocalDateTime endTime, APITestRun.State state,
Long startedById, String userName, String projectName, String testRunName, Float successRatio,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class APIBillingPeriod extends APIEntity {
public APIBillingPeriod() {
}

@SuppressWarnings("squid:S107")
public APIBillingPeriod(
Long billingPeriodId, Long accountServiceId, Long userId, String mail, String plan,
LocalDateTime startBillingPeriod, LocalDateTime endBillingPeriod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static Optional<Target> fromString(String name) {
}

public APIClientSideTestConfig() {
// need for serialization/deserialization
}

public String getHookURL() {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/testdroid/api/model/APICluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public enum Type {
public APICluster() {
}

@SuppressWarnings("squid:S107")
public APICluster(
Long id, String name, String url, String jenkinsUrl, String pluginVersion, State state,
LocalDateTime stateTime, LocalDateTime stateChangeTime, Boolean enabled, Type type, String ipAddress,
Expand Down
14 changes: 1 addition & 13 deletions src/main/java/com/testdroid/api/model/APIConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public class APIConnection extends APIEntity implements Serializable {

private String type;

@Deprecated
private String url;

private Integer port;

private String path;
Expand All @@ -40,6 +37,7 @@ public class APIConnection extends APIEntity implements Serializable {
public APIConnection() {
}

@SuppressWarnings("squid:S107")
public APIConnection(
Long id, LocalDateTime createTime, LocalDateTime endTime, Long deviceSessionId, String password,
String type, String urlSchema, String host, Integer port, String path, String externalId) {
Expand All @@ -53,7 +51,6 @@ public APIConnection(
this.path = path;
this.urlSchema = urlSchema;
this.host = host;
this.url = String.format("%s:%d", host, port);
this.externalId = externalId;
}

Expand Down Expand Up @@ -97,14 +94,6 @@ public void setType(String type) {
this.type = type;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getUrlSchema() {
return urlSchema;
}
Expand Down Expand Up @@ -153,7 +142,6 @@ protected <T extends APIEntity> void clone(T from) {
this.endTime = apiConnection.endTime;
this.password = apiConnection.password;
this.type = apiConnection.type;
this.url = apiConnection.url;
this.port = apiConnection.port;
this.path = apiConnection.path;
this.urlSchema = apiConnection.urlSchema;
Expand Down
Loading

0 comments on commit d31d1a6

Please sign in to comment.