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
5
import java .util .List ;
8
6
import com .browserstack .automate .Automate .BuildStatus ;
9
7
import com .browserstack .automate .exception .AppAutomateException ;
12
10
import com .browserstack .automate .exception .SessionNotFound ;
13
11
import com .browserstack .automate .model .AppUploadResponse ;
14
12
import com .browserstack .automate .model .Build ;
15
- import com .browserstack .automate .model .BuildNode ;
16
13
import com .browserstack .automate .model .Session ;
17
- import com .browserstack .automate .model .SessionNode ;
18
14
import com .browserstack .client .BrowserStackClient ;
19
- import com .browserstack .client .BrowserStackRequest ;
20
15
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 ;
24
17
import com .google .api .client .http .FileContent ;
25
18
import com .google .api .client .http .HttpHeaders ;
26
19
import com .google .api .client .http .HttpMediaType ;
@@ -34,24 +27,32 @@ public AppAutomateClient(String username, String accessKey) {
34
27
super (BASE_URL , username , accessKey );
35
28
}
36
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
+ */
37
38
public Session getSession (String sessionId ) throws SessionNotFound , AppAutomateException {
38
39
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 );
49
41
} catch (BrowserStackException e ) {
50
42
throw new AppAutomateException (e );
51
43
}
52
44
}
53
45
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 )
55
56
throws AppAutomateException , FileNotFoundException , InvalidFileExtensionException {
56
57
try {
57
58
File file = new File (filePath );
@@ -77,13 +78,13 @@ public String uploadApp(String filePath)
77
78
AppUploadResponse appUploadResponse =
78
79
newRequest (Method .POST , "/upload" ).body (content ).asObject (AppUploadResponse .class );
79
80
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 );
82
83
}
84
+ return appUploadResponse ;
83
85
} catch (BrowserStackException e ) {
84
86
throw new AppAutomateException (e );
85
87
}
86
- return null ;
87
88
}
88
89
89
90
/**
@@ -98,38 +99,13 @@ public String uploadApp(String filePath)
98
99
* @return List of {@link Build} objects.
99
100
* @throws AppAutomateException
100
101
*/
101
- public final List <Build > getBuilds (final BuildStatus status , final int limit )
102
+ public List <Build > getBuilds (final BuildStatus status , final int limit )
102
103
throws AppAutomateException {
103
- BrowserStackRequest httpRequest ;
104
104
try {
105
- httpRequest = newRequest ( Method . GET , "/builds.json" );
105
+ return super . getBuilds ( status , limit );
106
106
} catch (BrowserStackException e ) {
107
107
throw new AppAutomateException (e );
108
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
- }
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 ;
133
109
}
134
110
135
111
/**
@@ -142,7 +118,7 @@ public final List<Build> getBuilds(final BuildStatus status, final int limit)
142
118
* @return List of {@link Build} objects.
143
119
* @throws AppAutomateException
144
120
*/
145
- public final List <Build > getBuilds () throws AppAutomateException {
121
+ public List <Build > getBuilds () throws AppAutomateException {
146
122
return getBuilds (null , 0 );
147
123
}
148
124
@@ -157,7 +133,7 @@ public final List<Build> getBuilds() throws AppAutomateException {
157
133
* @return List of {@link Build} objects.
158
134
* @throws AppAutomateException
159
135
*/
160
- public final List <Build > getBuilds (final int limit ) throws AppAutomateException {
136
+ public List <Build > getBuilds (final int limit ) throws AppAutomateException {
161
137
return getBuilds (null , limit );
162
138
}
163
139
@@ -172,7 +148,7 @@ public final List<Build> getBuilds(final int limit) throws AppAutomateException
172
148
* @return List of {@link Build} objects.
173
149
* @throws AppAutomateException
174
150
*/
175
- public final List <Build > getBuilds (final BuildStatus status ) throws AppAutomateException {
151
+ public List <Build > getBuilds (final BuildStatus status ) throws AppAutomateException {
176
152
return getBuilds (status , 0 );
177
153
}
178
154
@@ -184,18 +160,9 @@ public final List<Build> getBuilds(final BuildStatus status) throws AppAutomateE
184
160
* @throws BuildNotFound
185
161
* @throws AppAutomateException
186
162
*/
187
- public final Build getBuild (final String buildId ) throws BuildNotFound , AppAutomateException {
163
+ public Build getBuild (final String buildId ) throws BuildNotFound , AppAutomateException {
188
164
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 );
199
166
} catch (BrowserStackException e ) {
200
167
throw new AppAutomateException (e );
201
168
}
@@ -208,13 +175,9 @@ public final Build getBuild(final String buildId) throws BuildNotFound, AppAutom
208
175
* @return true or false based on successful deletion of the build.
209
176
* @throws AppAutomateException
210
177
*/
211
- public final boolean deleteBuild (final String buildId ) throws AppAutomateException {
178
+ public boolean deleteBuild (final String buildId ) throws AppAutomateException {
212
179
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 );
218
181
} catch (BrowserStackException e ) {
219
182
throw new AppAutomateException (e );
220
183
}
@@ -230,42 +193,13 @@ public final boolean deleteBuild(final String buildId) throws AppAutomateExcepti
230
193
* @throws BuildNotFound
231
194
* @throws AppAutomateException
232
195
*/
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 {
253
198
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 );
257
200
} catch (BrowserStackException e ) {
258
201
throw new AppAutomateException (e );
259
202
}
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
203
}
270
204
271
205
/**
@@ -276,7 +210,7 @@ public final List<Session> getSessions(final String buildId, final BuildStatus s
276
210
* @throws BuildNotFound
277
211
* @throws AppAutomateException
278
212
*/
279
- public final List <Session > getSessions (final String buildId )
213
+ public List <Session > getSessions (final String buildId )
280
214
throws BuildNotFound , AppAutomateException {
281
215
return getSessions (buildId , null , 0 );
282
216
}
@@ -290,7 +224,7 @@ public final List<Session> getSessions(final String buildId)
290
224
* @throws BuildNotFound
291
225
* @throws AppAutomateException
292
226
*/
293
- public final List <Session > getSessions (final String buildId , final int limit )
227
+ public List <Session > getSessions (final String buildId , final int limit )
294
228
throws BuildNotFound , AppAutomateException {
295
229
return getSessions (buildId , null , limit );
296
230
}
@@ -304,7 +238,7 @@ public final List<Session> getSessions(final String buildId, final int limit)
304
238
* @throws BuildNotFound
305
239
* @throws AppAutomateException
306
240
*/
307
- public final List <Session > getSessions (final String buildId , final BuildStatus status )
241
+ public List <Session > getSessions (final String buildId , final BuildStatus status )
308
242
throws BuildNotFound , AppAutomateException {
309
243
return getSessions (buildId , status , 0 );
310
244
}
0 commit comments