forked from task032015/searchAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPISearchTest.java
More file actions
79 lines (65 loc) · 2.05 KB
/
APISearchTest.java
File metadata and controls
79 lines (65 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.test.itunes;
import junit.framework.TestCase;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Before;
import org.junit.Test;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class SearchAPITest extends TestCase {
Client client = null;
ClientResponse response = null;
JSONParser parser = new JSONParser();
@Before
public void setUp() throws Exception {
client = Client.create();
}
@Test
public void testGet() {
WebResource webResource = client
.resource("https://itunes.apple.com/search").queryParam("term", "Test");
response = webResource.accept("application/json")
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
JSONObject jsonObject = null;
try {
Object obj = parser.parse(output);
jsonObject = (JSONObject) obj;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Long name = (Long) jsonObject.get("resultCount");
assertNotNull(output);
assertEquals(50,name.longValue());
}
@Test
public void testGetEmpty() {
WebResource webResource = client
.resource("https://itunes.apple.com/search");
response = webResource.accept("application/json")
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
JSONObject jsonObject = null;
try {
Object obj = parser.parse(output);
jsonObject = (JSONObject) obj;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Long name = (Long) jsonObject.get("resultCount");
assertNotNull(output);
assertEquals(0,name.longValue());
}
}