Skip to content

Commit d8efc2c

Browse files
committed
Added common code in AppAutomate and Automate in BrowserStackClient
1 parent 13dc89c commit d8efc2c

File tree

7 files changed

+518
-402
lines changed

7 files changed

+518
-402
lines changed
Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,13 @@
11
package com.browserstack.appautomate;
22

33
import java.io.FileNotFoundException;
4-
import java.util.List;
5-
import com.browserstack.automate.Automate.BuildStatus;
64
import com.browserstack.automate.exception.AppAutomateException;
7-
import com.browserstack.automate.exception.BuildNotFound;
85
import com.browserstack.automate.exception.InvalidFileExtensionException;
9-
import com.browserstack.automate.exception.SessionNotFound;
10-
import com.browserstack.automate.model.Build;
11-
import com.browserstack.automate.model.Session;
6+
import com.browserstack.automate.model.AppUploadResponse;
127

138
public interface AppAutomate {
149

15-
public Session getSession(String sessionId) throws SessionNotFound, AppAutomateException;
16-
17-
public String uploadApp(String filePath)
10+
public AppUploadResponse uploadApp(String filePath)
1811
throws AppAutomateException, FileNotFoundException, InvalidFileExtensionException;
19-
20-
List<Build> getBuilds(BuildStatus status, int limit) throws AppAutomateException;
21-
22-
List<Build> getBuilds(int limit) throws AppAutomateException;
23-
24-
List<Build> getBuilds(BuildStatus status) throws AppAutomateException;
25-
26-
List<Build> getBuilds() throws AppAutomateException;
27-
28-
Build getBuild(String buildId) throws BuildNotFound, AppAutomateException;
29-
30-
boolean deleteBuild(String buildId) throws AppAutomateException;
31-
32-
List<Session> getSessions(String buildId, BuildStatus status,
33-
int limit) throws BuildNotFound, AppAutomateException;
34-
35-
List<Session> getSessions(String buildId) throws BuildNotFound, AppAutomateException;
36-
37-
List<Session> getSessions(String buildId, int limit) throws BuildNotFound, AppAutomateException;
38-
39-
List<Session> getSessions(String buildId, BuildStatus status) throws BuildNotFound, AppAutomateException;
40-
4112

4213
}

src/main/java/com/browserstack/appautomate/AppAutomateClient.java

Lines changed: 38 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import java.io.File;
44
import java.io.FileNotFoundException;
5-
import java.util.ArrayList;
6-
import java.util.Arrays;
75
import java.util.List;
86
import com.browserstack.automate.Automate.BuildStatus;
97
import com.browserstack.automate.exception.AppAutomateException;
@@ -12,15 +10,10 @@
1210
import com.browserstack.automate.exception.SessionNotFound;
1311
import com.browserstack.automate.model.AppUploadResponse;
1412
import com.browserstack.automate.model.Build;
15-
import com.browserstack.automate.model.BuildNode;
1613
import com.browserstack.automate.model.Session;
17-
import com.browserstack.automate.model.SessionNode;
1814
import com.browserstack.client.BrowserStackClient;
19-
import com.browserstack.client.BrowserStackRequest;
2015
import com.browserstack.client.exception.BrowserStackException;
21-
import com.browserstack.client.exception.BrowserStackObjectNotFound;
22-
import com.browserstack.client.util.Constants;
23-
import com.fasterxml.jackson.databind.node.ObjectNode;
16+
import com.browserstack.client.util.Tools;
2417
import com.google.api.client.http.FileContent;
2518
import com.google.api.client.http.HttpHeaders;
2619
import com.google.api.client.http.HttpMediaType;
@@ -34,24 +27,32 @@ public AppAutomateClient(String username, String accessKey) {
3427
super(BASE_URL, username, accessKey);
3528
}
3629

30+
/**
31+
* Gets the session associated with the specified identifier.
32+
*
33+
* @param sessionId ID that uniquely identifies a session.
34+
* @return {@link Session} objects containing test session information.
35+
* @throws SessionNotFound
36+
* @throws AppAutomateException
37+
*/
3738
public Session getSession(String sessionId) throws SessionNotFound, AppAutomateException {
3839
try {
39-
SessionNode sessionNode = newRequest(Method.GET, "/sessions/{sessionId}.json")
40-
.routeParam("sessionId", sessionId).asObject(SessionNode.class);
41-
42-
if (sessionNode.getSession() == null) {
43-
throw new SessionNotFound("Session not found: " + sessionId);
44-
}
45-
46-
return sessionNode.getSession().setClient(this);
47-
} catch (BrowserStackObjectNotFound e) {
48-
throw new SessionNotFound("Session not found: " + sessionId);
40+
return super.getSession(sessionId);
4941
} catch (BrowserStackException e) {
5042
throw new AppAutomateException(e);
5143
}
5244
}
5345

54-
public String uploadApp(String filePath)
46+
/**
47+
* Gets the filePath of app to be uploaded.
48+
*
49+
* @param filePath absolute path of app to be uploaded.
50+
* @return AppUploadResponse object containing app upload response details.
51+
* @throws AppAutomateException
52+
* @throws FileNotFoundException
53+
* @throws InvalidFileExtensionException
54+
*/
55+
public AppUploadResponse uploadApp(String filePath)
5556
throws AppAutomateException, FileNotFoundException, InvalidFileExtensionException {
5657
try {
5758
File file = new File(filePath);
@@ -77,13 +78,13 @@ public String uploadApp(String filePath)
7778
AppUploadResponse appUploadResponse =
7879
newRequest(Method.POST, "/upload").body(content).asObject(AppUploadResponse.class);
7980

80-
if (appUploadResponse.getAppUrl() != null) {
81-
return appUploadResponse.getAppUrl();
81+
if (appUploadResponse == null || Tools.isStringEmpty(appUploadResponse.getAppUrl())) {
82+
throw new AppAutomateException("App upload failed!", 0);
8283
}
84+
return appUploadResponse;
8385
} catch (BrowserStackException e) {
8486
throw new AppAutomateException(e);
8587
}
86-
return null;
8788
}
8889

8990
/**
@@ -98,38 +99,13 @@ public String uploadApp(String filePath)
9899
* @return List of {@link Build} objects.
99100
* @throws AppAutomateException
100101
*/
101-
public final List<Build> getBuilds(final BuildStatus status, final int limit)
102+
public List<Build> getBuilds(final BuildStatus status, final int limit)
102103
throws AppAutomateException {
103-
BrowserStackRequest httpRequest;
104104
try {
105-
httpRequest = newRequest(Method.GET, "/builds.json");
105+
return super.getBuilds(status, limit);
106106
} catch (BrowserStackException e) {
107107
throw new AppAutomateException(e);
108108
}
109-
110-
if (limit > 0) {
111-
httpRequest.queryString(Constants.Filter.LIMIT, limit);
112-
}
113-
114-
if (status != null) {
115-
httpRequest.queryString(Constants.Filter.FILTER, status.name().toLowerCase());
116-
}
117-
118-
List<BuildNode> buildNodes;
119-
try {
120-
buildNodes = Arrays.asList(httpRequest.asObject(BuildNode[].class));
121-
} catch (BrowserStackException e) {
122-
throw new AppAutomateException(e);
123-
}
124-
125-
final List<Build> builds = new ArrayList<Build>();
126-
for (BuildNode buildNode : buildNodes) {
127-
if (buildNode != null && buildNode.getBuild() != null) {
128-
builds.add(buildNode.getBuild().<Build>setClient(this));
129-
}
130-
}
131-
132-
return builds;
133109
}
134110

135111
/**
@@ -142,7 +118,7 @@ public final List<Build> getBuilds(final BuildStatus status, final int limit)
142118
* @return List of {@link Build} objects.
143119
* @throws AppAutomateException
144120
*/
145-
public final List<Build> getBuilds() throws AppAutomateException {
121+
public List<Build> getBuilds() throws AppAutomateException {
146122
return getBuilds(null, 0);
147123
}
148124

@@ -157,7 +133,7 @@ public final List<Build> getBuilds() throws AppAutomateException {
157133
* @return List of {@link Build} objects.
158134
* @throws AppAutomateException
159135
*/
160-
public final List<Build> getBuilds(final int limit) throws AppAutomateException {
136+
public List<Build> getBuilds(final int limit) throws AppAutomateException {
161137
return getBuilds(null, limit);
162138
}
163139

@@ -172,7 +148,7 @@ public final List<Build> getBuilds(final int limit) throws AppAutomateException
172148
* @return List of {@link Build} objects.
173149
* @throws AppAutomateException
174150
*/
175-
public final List<Build> getBuilds(final BuildStatus status) throws AppAutomateException {
151+
public List<Build> getBuilds(final BuildStatus status) throws AppAutomateException {
176152
return getBuilds(status, 0);
177153
}
178154

@@ -184,18 +160,9 @@ public final List<Build> getBuilds(final BuildStatus status) throws AppAutomateE
184160
* @throws BuildNotFound
185161
* @throws AppAutomateException
186162
*/
187-
public final Build getBuild(final String buildId) throws BuildNotFound, AppAutomateException {
163+
public Build getBuild(final String buildId) throws BuildNotFound, AppAutomateException {
188164
try {
189-
BuildNode buildNode = newRequest(Method.GET, "/builds/{buildId}.json")
190-
.routeParam("buildId", buildId).asObject(BuildNode.class);
191-
192-
if (buildNode == null) {
193-
throw new BuildNotFound("Build not found: " + buildId);
194-
}
195-
196-
return buildNode.getBuild().setClient(this);
197-
} catch (BrowserStackObjectNotFound e) {
198-
throw new BuildNotFound("Build not found: " + buildId);
165+
return super.getBuild(buildId);
199166
} catch (BrowserStackException e) {
200167
throw new AppAutomateException(e);
201168
}
@@ -208,13 +175,9 @@ public final Build getBuild(final String buildId) throws BuildNotFound, AppAutom
208175
* @return true or false based on successful deletion of the build.
209176
* @throws AppAutomateException
210177
*/
211-
public final boolean deleteBuild(final String buildId) throws AppAutomateException {
178+
public boolean deleteBuild(final String buildId) throws AppAutomateException {
212179
try {
213-
ObjectNode result = newRequest(BrowserStackClient.Method.DELETE, "/builds/{buildId}.json")
214-
.routeParam("buildId", buildId).asJsonObject();
215-
216-
String status = (result != null) ? result.path("status").asText() : null;
217-
return (status != null && status.equals("ok"));
180+
return super.deleteBuild(buildId);
218181
} catch (BrowserStackException e) {
219182
throw new AppAutomateException(e);
220183
}
@@ -230,42 +193,13 @@ public final boolean deleteBuild(final String buildId) throws AppAutomateExcepti
230193
* @throws BuildNotFound
231194
* @throws AppAutomateException
232195
*/
233-
public final List<Session> getSessions(final String buildId, final BuildStatus status,
234-
final int limit) throws BuildNotFound, AppAutomateException {
235-
236-
BrowserStackRequest httpRequest = null;
237-
try {
238-
httpRequest =
239-
newRequest(Method.GET, "/builds/{buildId}/sessions.json").routeParam("buildId", buildId);
240-
} catch (BrowserStackException e) {
241-
throw new AppAutomateException(e);
242-
}
243-
244-
if (limit > 0) {
245-
httpRequest.queryString(Constants.Filter.LIMIT, limit);
246-
}
247-
248-
if (status != null) {
249-
httpRequest.queryString(Constants.Filter.FILTER, status);
250-
}
251-
252-
List<SessionNode> sessionNodes;
196+
public List<Session> getSessions(final String buildId, final BuildStatus status, final int limit)
197+
throws BuildNotFound, AppAutomateException {
253198
try {
254-
sessionNodes = Arrays.asList(httpRequest.asObject(SessionNode[].class));
255-
} catch (BrowserStackObjectNotFound e) {
256-
throw new BuildNotFound("Build not found: " + buildId);
199+
return super.getSessions(buildId, status, limit);
257200
} catch (BrowserStackException e) {
258201
throw new AppAutomateException(e);
259202
}
260-
261-
List<Session> sessions = new ArrayList<Session>();
262-
for (SessionNode sessionNode : sessionNodes) {
263-
if (sessionNode != null && sessionNode.getSession() != null) {
264-
sessions.add(sessionNode.getSession().<Session>setClient(this));
265-
}
266-
}
267-
268-
return sessions;
269203
}
270204

271205
/**
@@ -276,7 +210,7 @@ public final List<Session> getSessions(final String buildId, final BuildStatus s
276210
* @throws BuildNotFound
277211
* @throws AppAutomateException
278212
*/
279-
public final List<Session> getSessions(final String buildId)
213+
public List<Session> getSessions(final String buildId)
280214
throws BuildNotFound, AppAutomateException {
281215
return getSessions(buildId, null, 0);
282216
}
@@ -290,7 +224,7 @@ public final List<Session> getSessions(final String buildId)
290224
* @throws BuildNotFound
291225
* @throws AppAutomateException
292226
*/
293-
public final List<Session> getSessions(final String buildId, final int limit)
227+
public List<Session> getSessions(final String buildId, final int limit)
294228
throws BuildNotFound, AppAutomateException {
295229
return getSessions(buildId, null, limit);
296230
}
@@ -304,7 +238,7 @@ public final List<Session> getSessions(final String buildId, final int limit)
304238
* @throws BuildNotFound
305239
* @throws AppAutomateException
306240
*/
307-
public final List<Session> getSessions(final String buildId, final BuildStatus status)
241+
public List<Session> getSessions(final String buildId, final BuildStatus status)
308242
throws BuildNotFound, AppAutomateException {
309243
return getSessions(buildId, status, 0);
310244
}

src/main/java/com/browserstack/automate/Automate.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,6 @@ public interface Automate {
2727

2828
boolean deleteProject(int projectId) throws AutomateException;
2929

30-
List<Build> getBuilds(BuildStatus status, int limit) throws AutomateException;
31-
32-
List<Build> getBuilds(int limit) throws AutomateException;
33-
34-
List<Build> getBuilds(BuildStatus status) throws AutomateException;
35-
36-
List<Build> getBuilds() throws AutomateException;
37-
38-
Build getBuild(String buildId) throws BuildNotFound, AutomateException;
39-
40-
boolean deleteBuild(String buildId) throws AutomateException;
41-
42-
List<Session> getSessions(String buildId, BuildStatus status,
43-
int limit) throws BuildNotFound, AutomateException;
44-
45-
List<Session> getSessions(String buildId) throws BuildNotFound, AutomateException;
46-
47-
List<Session> getSessions(String buildId, int limit) throws BuildNotFound, AutomateException;
48-
49-
List<Session> getSessions(String buildId, BuildStatus status) throws BuildNotFound, AutomateException;
50-
51-
Session getSession(String sessionId) throws SessionNotFound, AutomateException;
52-
5330
Session updateSessionStatus(String sessionId, Map<String, Object> data) throws AutomateException;
5431

5532
Session updateSessionStatus(String sessionId,

0 commit comments

Comments
 (0)