|
| 1 | +package com.browserstack.appautomate; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.util.List; |
| 6 | +import com.browserstack.automate.Automate.BuildStatus; |
| 7 | +import com.browserstack.automate.exception.AppAutomateException; |
| 8 | +import com.browserstack.automate.exception.BuildNotFound; |
| 9 | +import com.browserstack.automate.exception.InvalidFileExtensionException; |
| 10 | +import com.browserstack.automate.exception.SessionNotFound; |
| 11 | +import com.browserstack.automate.model.AppUploadResponse; |
| 12 | +import com.browserstack.automate.model.Build; |
| 13 | +import com.browserstack.automate.model.Session; |
| 14 | +import com.browserstack.client.BrowserStackClient; |
| 15 | +import com.browserstack.client.exception.BrowserStackException; |
| 16 | +import com.browserstack.client.util.Tools; |
| 17 | +import com.google.api.client.http.FileContent; |
| 18 | +import com.google.api.client.http.HttpHeaders; |
| 19 | +import com.google.api.client.http.HttpMediaType; |
| 20 | +import com.google.api.client.http.MultipartContent; |
| 21 | + |
| 22 | +public class AppAutomateClient extends BrowserStackClient implements AppAutomate { |
| 23 | + |
| 24 | + private static final String BASE_URL = "https://api-cloud.browserstack.com/app-automate"; |
| 25 | + |
| 26 | + public AppAutomateClient(String username, String accessKey) { |
| 27 | + super(BASE_URL, username, accessKey); |
| 28 | + } |
| 29 | + |
| 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 | + */ |
| 38 | + public Session getSession(String sessionId) throws SessionNotFound, AppAutomateException { |
| 39 | + try { |
| 40 | + return super.getSession(sessionId); |
| 41 | + } catch (BrowserStackException e) { |
| 42 | + throw new AppAutomateException(e); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 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) |
| 56 | + throws AppAutomateException, FileNotFoundException, InvalidFileExtensionException { |
| 57 | + try { |
| 58 | + File file = new File(filePath); |
| 59 | + |
| 60 | + if (!file.exists()) { |
| 61 | + throw new FileNotFoundException("File not found at " + filePath); |
| 62 | + } |
| 63 | + |
| 64 | + if (!filePath.endsWith(".apk") && !filePath.endsWith(".ipa")) { |
| 65 | + throw new InvalidFileExtensionException("File extension should be only .apk or .ipa."); |
| 66 | + } |
| 67 | + |
| 68 | + MultipartContent content = new MultipartContent().setMediaType( |
| 69 | + new HttpMediaType("multipart/form-data").setParameter("boundary", "__END_OF_PART__")); |
| 70 | + |
| 71 | + FileContent fileContent = new FileContent("multipart/form-data", file); |
| 72 | + |
| 73 | + MultipartContent.Part part = new MultipartContent.Part(fileContent); |
| 74 | + part.setHeaders(new HttpHeaders().set("Content-Disposition", |
| 75 | + String.format("form-data; name=\"file\"; filename=\"%s\"", file.getName()))); |
| 76 | + content.addPart(part); |
| 77 | + |
| 78 | + AppUploadResponse appUploadResponse = |
| 79 | + newRequest(Method.POST, "/upload").body(content).asObject(AppUploadResponse.class); |
| 80 | + |
| 81 | + if (appUploadResponse == null || Tools.isStringEmpty(appUploadResponse.getAppUrl())) { |
| 82 | + throw new AppAutomateException("App upload failed!", 0); |
| 83 | + } |
| 84 | + return appUploadResponse; |
| 85 | + } catch (BrowserStackException e) { |
| 86 | + throw new AppAutomateException(e); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets the list of builds. |
| 92 | + * |
| 93 | + * <p> |
| 94 | + * A build is an organizational structure for tests. |
| 95 | + * </p> |
| 96 | + * |
| 97 | + * @param status Return only builds that match the specified build status. |
| 98 | + * @param limit Limit results to the specified count. |
| 99 | + * @return List of {@link Build} objects. |
| 100 | + * @throws AppAutomateException |
| 101 | + */ |
| 102 | + public List<Build> getBuilds(final BuildStatus status, final int limit) |
| 103 | + throws AppAutomateException { |
| 104 | + try { |
| 105 | + return super.getBuilds(status, limit); |
| 106 | + } catch (BrowserStackException e) { |
| 107 | + throw new AppAutomateException(e); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Gets the list of builds. |
| 113 | + * |
| 114 | + * <p> |
| 115 | + * A build is an organizational structure for tests. |
| 116 | + * </p> |
| 117 | + * |
| 118 | + * @return List of {@link Build} objects. |
| 119 | + * @throws AppAutomateException |
| 120 | + */ |
| 121 | + public List<Build> getBuilds() throws AppAutomateException { |
| 122 | + return getBuilds(null, 0); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Gets the list of builds. |
| 127 | + * |
| 128 | + * <p> |
| 129 | + * A build is an organizational structure for tests. |
| 130 | + * </p> |
| 131 | + * |
| 132 | + * @param limit Limit results to the specified count. |
| 133 | + * @return List of {@link Build} objects. |
| 134 | + * @throws AppAutomateException |
| 135 | + */ |
| 136 | + public List<Build> getBuilds(final int limit) throws AppAutomateException { |
| 137 | + return getBuilds(null, limit); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Gets the list of builds. |
| 142 | + * |
| 143 | + * <p> |
| 144 | + * A build is an organizational structure for tests. |
| 145 | + * </p> |
| 146 | + * |
| 147 | + * @param status Include only builds that match the specified build status. |
| 148 | + * @return List of {@link Build} objects. |
| 149 | + * @throws AppAutomateException |
| 150 | + */ |
| 151 | + public List<Build> getBuilds(final BuildStatus status) throws AppAutomateException { |
| 152 | + return getBuilds(status, 0); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Gets the build identified by the build identifier. |
| 157 | + * |
| 158 | + * @param buildId ID that uniquely identifies a build. |
| 159 | + * @return List of {@link Build} objects. |
| 160 | + * @throws BuildNotFound |
| 161 | + * @throws AppAutomateException |
| 162 | + */ |
| 163 | + public Build getBuild(final String buildId) throws BuildNotFound, AppAutomateException { |
| 164 | + try { |
| 165 | + return super.getBuild(buildId); |
| 166 | + } catch (BrowserStackException e) { |
| 167 | + throw new AppAutomateException(e); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Delete the build identified by the build identifier. |
| 173 | + * |
| 174 | + * @param buildId ID that uniquely identifies a build. |
| 175 | + * @return true or false based on successful deletion of the build. |
| 176 | + * @throws AppAutomateException |
| 177 | + */ |
| 178 | + public boolean deleteBuild(final String buildId) throws AppAutomateException { |
| 179 | + try { |
| 180 | + return super.deleteBuild(buildId); |
| 181 | + } catch (BrowserStackException e) { |
| 182 | + throw new AppAutomateException(e); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Retrieves the list of sessions existing under a specific build. |
| 188 | + * |
| 189 | + * @param buildId ID that uniquely identifies a build. |
| 190 | + * @param status Include only builds that match the specified build status. |
| 191 | + * @param limit Limit results to the specified count. |
| 192 | + * @return List of {@link Session} objects containing test session information. |
| 193 | + * @throws BuildNotFound |
| 194 | + * @throws AppAutomateException |
| 195 | + */ |
| 196 | + public List<Session> getSessions(final String buildId, final BuildStatus status, final int limit) |
| 197 | + throws BuildNotFound, AppAutomateException { |
| 198 | + try { |
| 199 | + return super.getSessions(buildId, status, limit); |
| 200 | + } catch (BrowserStackException e) { |
| 201 | + throw new AppAutomateException(e); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Retrieves the list of sessions existing under a specific build. |
| 207 | + * |
| 208 | + * @param buildId ID that uniquely identifies a build. |
| 209 | + * @return List of {@link Session} objects containing test session information. |
| 210 | + * @throws BuildNotFound |
| 211 | + * @throws AppAutomateException |
| 212 | + */ |
| 213 | + public List<Session> getSessions(final String buildId) |
| 214 | + throws BuildNotFound, AppAutomateException { |
| 215 | + return getSessions(buildId, null, 0); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Retrieves the list of sessions existing under a specific build. |
| 220 | + * |
| 221 | + * @param buildId ID that uniquely identifies a build. |
| 222 | + * @param limit Limit results to the specified count. |
| 223 | + * @return List of {@link Session} objects containing test session information. |
| 224 | + * @throws BuildNotFound |
| 225 | + * @throws AppAutomateException |
| 226 | + */ |
| 227 | + public List<Session> getSessions(final String buildId, final int limit) |
| 228 | + throws BuildNotFound, AppAutomateException { |
| 229 | + return getSessions(buildId, null, limit); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Retrieves the list of sessions existing under a specific build. |
| 234 | + * |
| 235 | + * @param buildId ID that uniquely identifies a build. |
| 236 | + * @param status Include only builds that match the specified build status. |
| 237 | + * @return List of {@link Session} objects containing test session information. |
| 238 | + * @throws BuildNotFound |
| 239 | + * @throws AppAutomateException |
| 240 | + */ |
| 241 | + public List<Session> getSessions(final String buildId, final BuildStatus status) |
| 242 | + throws BuildNotFound, AppAutomateException { |
| 243 | + return getSessions(buildId, status, 0); |
| 244 | + } |
| 245 | + |
| 246 | +} |
| 247 | + |
0 commit comments