|
2 | 2 |
|
3 | 3 | import java.io.File;
|
4 | 4 | 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; |
5 | 9 | import com.browserstack.automate.exception.AppAutomateException;
|
| 10 | +import com.browserstack.automate.exception.BuildNotFound; |
6 | 11 | import com.browserstack.automate.exception.InvalidFileExtensionException;
|
7 | 12 | 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; |
8 | 16 | import com.browserstack.automate.model.Session;
|
9 | 17 | import com.browserstack.automate.model.SessionNode;
|
10 | 18 | import com.browserstack.client.BrowserStackClient;
|
| 19 | +import com.browserstack.client.BrowserStackRequest; |
11 | 20 | import com.browserstack.client.exception.BrowserStackException;
|
12 | 21 | import com.browserstack.client.exception.BrowserStackObjectNotFound;
|
| 22 | +import com.browserstack.client.util.Constants; |
13 | 23 | import com.fasterxml.jackson.databind.node.ObjectNode;
|
14 | 24 | import com.google.api.client.http.FileContent;
|
15 | 25 | import com.google.api.client.http.HttpHeaders;
|
@@ -64,15 +74,240 @@ public String uploadApp(String filePath)
|
64 | 74 | String.format("form-data; name=\"file\"; filename=\"%s\"", file.getName())));
|
65 | 75 | content.addPart(part);
|
66 | 76 |
|
67 |
| - ObjectNode response = newRequest(Method.POST, "/upload").body(content).asJsonObject(); |
| 77 | + AppUploadResponse appUploadResponse = |
| 78 | + newRequest(Method.POST, "/upload").body(content).asObject(AppUploadResponse.class); |
68 | 79 |
|
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 | + } |
70 | 117 |
|
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); |
72 | 199 | } catch (BrowserStackException e) {
|
73 | 200 | throw new AppAutomateException(e);
|
74 | 201 | }
|
75 | 202 | }
|
76 | 203 |
|
| 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 | + |
77 | 312 | }
|
78 | 313 |
|
0 commit comments