-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSearchArchiveApiTest.java
More file actions
51 lines (42 loc) · 1.59 KB
/
SearchArchiveApiTest.java
File metadata and controls
51 lines (42 loc) · 1.59 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
package serpapi;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
/**
* Test SerpApi.searchArchive() method.
*/
public class SearchArchiveApiTest {
@Test
public void searchArchive() throws SerpApiException, InterruptedException {
// skip test if no api_key provided
if(System.getenv("SERPAPI_KEY") == null) {
fail("SERPAPI_KEY is not set");
}
Map<String, String> parameter = new HashMap<>();
parameter.put("api_key", System.getenv("SERPAPI_KEY"));
parameter.put("q", "Coffee");
parameter.put("location", "Austin, Texas, United States");
parameter.put("hl", "en");
parameter.put("gl", "us");
parameter.put("google_domain", "google.com");
parameter.put("safe", "active");
parameter.put("start", "10");
parameter.put("device", "desktop");
SerpApi serpapi = new SerpApi(parameter);
JsonObject results = serpapi.search(parameter);
assertTrue(results.getAsJsonArray("organic_results").size() > 5);
// now search in the archive
String id = results.getAsJsonObject("search_metadata").getAsJsonPrimitive("id").getAsString();
assertNotNull(id);
System.out.println("id: " + id);
// wait a bit for cache propagation
Thread.sleep(100);
// retrieve search from the archive for free
JsonObject archive = serpapi.searchArchive(id);
// compare id and the content should be the same
assertEquals(id, archive.getAsJsonObject("search_metadata").getAsJsonPrimitive("id").getAsString());
}
}