Skip to content

Commit 8a134fe

Browse files
committed
Added methods for fetching builds and session in AppAutomateClient
1 parent 3ec1190 commit 8a134fe

File tree

2 files changed

+264
-3
lines changed

2 files changed

+264
-3
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +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;
46
import com.browserstack.automate.exception.AppAutomateException;
7+
import com.browserstack.automate.exception.BuildNotFound;
58
import com.browserstack.automate.exception.InvalidFileExtensionException;
69
import com.browserstack.automate.exception.SessionNotFound;
10+
import com.browserstack.automate.model.Build;
711
import com.browserstack.automate.model.Session;
812

913
public interface AppAutomate {
@@ -12,5 +16,27 @@ public interface AppAutomate {
1216

1317
public String uploadApp(String filePath)
1418
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+
1541

1642
}

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

Lines changed: 238 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
import java.io.File;
44
import java.io.FileNotFoundException;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import com.browserstack.automate.Automate.BuildStatus;
59
import com.browserstack.automate.exception.AppAutomateException;
10+
import com.browserstack.automate.exception.BuildNotFound;
611
import com.browserstack.automate.exception.InvalidFileExtensionException;
712
import com.browserstack.automate.exception.SessionNotFound;
13+
import com.browserstack.automate.model.AppUploadResponse;
14+
import com.browserstack.automate.model.Build;
15+
import com.browserstack.automate.model.BuildNode;
816
import com.browserstack.automate.model.Session;
917
import com.browserstack.automate.model.SessionNode;
1018
import com.browserstack.client.BrowserStackClient;
19+
import com.browserstack.client.BrowserStackRequest;
1120
import com.browserstack.client.exception.BrowserStackException;
1221
import com.browserstack.client.exception.BrowserStackObjectNotFound;
22+
import com.browserstack.client.util.Constants;
1323
import com.fasterxml.jackson.databind.node.ObjectNode;
1424
import com.google.api.client.http.FileContent;
1525
import com.google.api.client.http.HttpHeaders;
@@ -64,15 +74,240 @@ public String uploadApp(String filePath)
6474
String.format("form-data; name=\"file\"; filename=\"%s\"", file.getName())));
6575
content.addPart(part);
6676

67-
ObjectNode response = newRequest(Method.POST, "/upload").body(content).asJsonObject();
77+
AppUploadResponse appUploadResponse =
78+
newRequest(Method.POST, "/upload").body(content).asObject(AppUploadResponse.class);
6879

69-
String appId = (response != null) ? response.path("app_url").asText() : null;
80+
if (appUploadResponse.getAppUrl() != null) {
81+
return appUploadResponse.getAppUrl();
82+
}
83+
} catch (BrowserStackException e) {
84+
throw new AppAutomateException(e);
85+
}
86+
return null;
87+
}
88+
89+
/**
90+
* Gets the list of builds.
91+
*
92+
* <p>
93+
* A build is an organizational structure for tests.
94+
* </p>
95+
*
96+
* @param status Return only builds that match the specified build status.
97+
* @param limit Limit results to the specified count.
98+
* @return List of {@link Build} objects.
99+
* @throws AppAutomateException
100+
*/
101+
public final List<Build> getBuilds(final BuildStatus status, final int limit)
102+
throws AppAutomateException {
103+
BrowserStackRequest httpRequest;
104+
try {
105+
httpRequest = newRequest(Method.GET, "/builds.json");
106+
} catch (BrowserStackException e) {
107+
throw new AppAutomateException(e);
108+
}
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+
}
70117

71-
return appId;
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;
133+
}
134+
135+
/**
136+
* Gets the list of builds.
137+
*
138+
* <p>
139+
* A build is an organizational structure for tests.
140+
* </p>
141+
*
142+
* @return List of {@link Build} objects.
143+
* @throws AppAutomateException
144+
*/
145+
public final List<Build> getBuilds() throws AppAutomateException {
146+
return getBuilds(null, 0);
147+
}
148+
149+
/**
150+
* Gets the list of builds.
151+
*
152+
* <p>
153+
* A build is an organizational structure for tests.
154+
* </p>
155+
*
156+
* @param limit Limit results to the specified count.
157+
* @return List of {@link Build} objects.
158+
* @throws AppAutomateException
159+
*/
160+
public final List<Build> getBuilds(final int limit) throws AppAutomateException {
161+
return getBuilds(null, limit);
162+
}
163+
164+
/**
165+
* Gets the list of builds.
166+
*
167+
* <p>
168+
* A build is an organizational structure for tests.
169+
* </p>
170+
*
171+
* @param status Include only builds that match the specified build status.
172+
* @return List of {@link Build} objects.
173+
* @throws AppAutomateException
174+
*/
175+
public final List<Build> getBuilds(final BuildStatus status) throws AppAutomateException {
176+
return getBuilds(status, 0);
177+
}
178+
179+
/**
180+
* Gets the build identified by the build identifier.
181+
*
182+
* @param buildId ID that uniquely identifies a build.
183+
* @return List of {@link Build} objects.
184+
* @throws BuildNotFound
185+
* @throws AppAutomateException
186+
*/
187+
public final Build getBuild(final String buildId) throws BuildNotFound, AppAutomateException {
188+
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);
72199
} catch (BrowserStackException e) {
73200
throw new AppAutomateException(e);
74201
}
75202
}
76203

204+
/**
205+
* Delete the build identified by the build identifier.
206+
*
207+
* @param buildId ID that uniquely identifies a build.
208+
* @return true or false based on successful deletion of the build.
209+
* @throws AppAutomateException
210+
*/
211+
public final boolean deleteBuild(final String buildId) throws AppAutomateException {
212+
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"));
218+
} catch (BrowserStackException e) {
219+
throw new AppAutomateException(e);
220+
}
221+
}
222+
223+
/**
224+
* Retrieves the list of sessions existing under a specific build.
225+
*
226+
* @param buildId ID that uniquely identifies a build.
227+
* @param status Include only builds that match the specified build status.
228+
* @param limit Limit results to the specified count.
229+
* @return List of {@link Session} objects containing test session information.
230+
* @throws BuildNotFound
231+
* @throws AppAutomateException
232+
*/
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;
253+
try {
254+
sessionNodes = Arrays.asList(httpRequest.asObject(SessionNode[].class));
255+
} catch (BrowserStackObjectNotFound e) {
256+
throw new BuildNotFound("Build not found: " + buildId);
257+
} catch (BrowserStackException e) {
258+
throw new AppAutomateException(e);
259+
}
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;
269+
}
270+
271+
/**
272+
* Retrieves the list of sessions existing under a specific build.
273+
*
274+
* @param buildId ID that uniquely identifies a build.
275+
* @return List of {@link Session} objects containing test session information.
276+
* @throws BuildNotFound
277+
* @throws AppAutomateException
278+
*/
279+
public final List<Session> getSessions(final String buildId)
280+
throws BuildNotFound, AppAutomateException {
281+
return getSessions(buildId, null, 0);
282+
}
283+
284+
/**
285+
* Retrieves the list of sessions existing under a specific build.
286+
*
287+
* @param buildId ID that uniquely identifies a build.
288+
* @param limit Limit results to the specified count.
289+
* @return List of {@link Session} objects containing test session information.
290+
* @throws BuildNotFound
291+
* @throws AppAutomateException
292+
*/
293+
public final List<Session> getSessions(final String buildId, final int limit)
294+
throws BuildNotFound, AppAutomateException {
295+
return getSessions(buildId, null, limit);
296+
}
297+
298+
/**
299+
* Retrieves the list of sessions existing under a specific build.
300+
*
301+
* @param buildId ID that uniquely identifies a build.
302+
* @param status Include only builds that match the specified build status.
303+
* @return List of {@link Session} objects containing test session information.
304+
* @throws BuildNotFound
305+
* @throws AppAutomateException
306+
*/
307+
public final List<Session> getSessions(final String buildId, final BuildStatus status)
308+
throws BuildNotFound, AppAutomateException {
309+
return getSessions(buildId, status, 0);
310+
}
311+
77312
}
78313

0 commit comments

Comments
 (0)