diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 27c7871..95f5b44 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -15,14 +15,17 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - name: Set up JDK 1.8 - uses: actions/setup-java@v1 + - uses: actions/checkout@v4 + - name: Set up JDK 21 + uses: actions/setup-java@v4 with: - java-version: 1.8 + java-version: '21' + distribution: 'temurin' - name: Grant execute permission for gradlew run: chmod +x gradlew + - name: Build with Gradle + uses: gradle/actions/setup-gradle@v3 - name: test run: ./gradlew test env: - API_KEY: ${{secrets.API_KEY}} \ No newline at end of file + SERPAPI_KEY: ${{secrets.SERPAPI_KEY}} \ No newline at end of file diff --git a/LICENSE b/LICENSE index 5d31a9d..fd42aa3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018-2024 SerpApi +Copyright (c) 2018-2025 SerpApi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Makefile b/Makefile index 12175ea..50db6ae 100644 --- a/Makefile +++ b/Makefile @@ -15,10 +15,17 @@ init: test: ./gradlew test --info +# Run test coverage +coverage: + ./gradlew test jacocoTestReport + @echo "Coverage report: build/reports/jacoco/test/html/index.html" + +# Build the project build: clean ./gradlew build publishToMavenLocal -x test @echo "see build/lib" +# Build the demo project oobt: build $(MAKE) -C demo all @@ -26,8 +33,9 @@ oobt: build readme: erb -T '-' README.md.erb > README.md +# Build the javadoc doc: - gradle javadoc --info --warning-mode all + ./gradlew javadoc --info --warning-mode all # Create a release using GitHub release: doc build diff --git a/README.md b/README.md index 4e46957..22a91cc 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ it prints the first 3 location matching Austin (Texas, Texas, Rochester) Let's run a search to get a search_id. ```java Map auth = new HashMap<>(); -auth.put("api_key", System.getenv("API_KEY")); +auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi serpapi = new SerpApi(auth); Map parameter = new HashMap<>(); diff --git a/README.md.erb b/README.md.erb index 2a33cfe..ffea8d7 100644 --- a/README.md.erb +++ b/README.md.erb @@ -135,7 +135,7 @@ it prints the first 3 location matching Austin (Texas, Texas, Rochester) Let's run a search to get a search_id. ```java Map auth = new HashMap<>(); -auth.put("api_key", System.getenv("API_KEY")); +auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi serpapi = new SerpApi(auth); Map parameter = new HashMap<>(); diff --git a/build.gradle b/build.gradle index ec29b8e..c07171f 100644 --- a/build.gradle +++ b/build.gradle @@ -1,20 +1,18 @@ // Java library plugins { id 'java-library' -} - -plugins { - id 'maven-publish' + id 'maven-publish' + id 'jacoco' } // Package default archivesBaseName = 'serpapi' -version = '1.0.0' +version = '1.1.0' group = 'com.github.serpapi' // java version -sourceCompatibility = 1.8 -targetCompatibility = 1.8 +sourceCompatibility = 21 +targetCompatibility = 21 // Load repositories repositories { @@ -65,6 +63,6 @@ publishing { } wrapper { - gradleVersion = "7.3.3" + gradleVersion = "8.5" distributionType = Wrapper.DistributionType.ALL } diff --git a/demo/Makefile b/demo/Makefile index e4c2ab5..a98d3b8 100644 --- a/demo/Makefile +++ b/demo/Makefile @@ -1,9 +1,9 @@ .PHONY: build -ifndef API_KEY -$(error "API_KEY must be defined") +ifndef SERPAPI_KEY +$(error "SERPAPI_KEY must be defined") else -$(info "found API_KEY variable") +$(info "found SERPAPI_KEY variable") endif all: init clean build run diff --git a/demo/build.gradle b/demo/build.gradle index 321c5da..7f4da8e 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -14,6 +14,9 @@ plugins { id 'application' } +sourceCompatibility = 21 +targetCompatibility = 21 + repositories { mavenCentral() diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 669386b..d0d403e 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/serpapi/ParameterStringBuilder.java b/src/main/java/serpapi/ParameterStringBuilder.java index 231563b..905fb07 100644 --- a/src/main/java/serpapi/ParameterStringBuilder.java +++ b/src/main/java/serpapi/ParameterStringBuilder.java @@ -10,6 +10,12 @@ */ public class ParameterStringBuilder { + /** + * Default constructor + */ + public ParameterStringBuilder() { + } + /*** * getParamsString * @param params search parameters diff --git a/src/main/java/serpapi/SerpApi.java b/src/main/java/serpapi/SerpApi.java index 9b2f79c..7a53369 100644 --- a/src/main/java/serpapi/SerpApi.java +++ b/src/main/java/serpapi/SerpApi.java @@ -11,7 +11,7 @@ /** * SerpApi wraps HTTP interaction with the service serpapi.com */ -public class SerpApi extends Exception { +public class SerpApi { /** * client parameters @@ -131,6 +131,8 @@ private JsonObject json(String endpoint, Map parameter) throws S } /*** + * Get the HTTP client used for requests + * * @return http client */ public SerpApiHttp getClient() { diff --git a/src/main/java/serpapi/SerpApiHttp.java b/src/main/java/serpapi/SerpApiHttp.java index 622fd3d..b97605c 100644 --- a/src/main/java/serpapi/SerpApiHttp.java +++ b/src/main/java/serpapi/SerpApiHttp.java @@ -189,6 +189,8 @@ protected void triggerSerpApiException(String content) throws SerpApiException { } /** + * Get the HTTP connection timeout + * * @return current HTTP connection timeout */ public int getHttpConnectionTimeout() { @@ -196,6 +198,8 @@ public int getHttpConnectionTimeout() { } /** + * Set the HTTP connection timeout + * * @param httpConnectionTimeout set HTTP connection timeout */ public void setHttpConnectionTimeout(int httpConnectionTimeout) { @@ -203,6 +207,8 @@ public void setHttpConnectionTimeout(int httpConnectionTimeout) { } /** + * Get the HTTP read timeout + * * @return current HTTP read timeout */ public int getHttpReadTimeout() { @@ -210,6 +216,8 @@ public int getHttpReadTimeout() { } /** + * Set the HTTP read timeout + * * @param httpReadTimeout set HTTP read timeout */ public void setHttpReadTimeout(int httpReadTimeout) { diff --git a/src/test/java/basic_search_result.json b/src/test/java/basic_search_result.json deleted file mode 100644 index d2dd85b..0000000 --- a/src/test/java/basic_search_result.json +++ /dev/null @@ -1,175 +0,0 @@ -{ - "search_metadata": { - "id": "5c91926661a1347a7a2ebe3a", - "status": "Success", - "created_at": "2019-03-20 01:07:50 UTC", - "processed_at": "2019-03-20 01:07:50 UTC", - "google_url": "https://www.google.com/search?q=Coffee&oq=Coffee&uule=w+CAIQICIaQXVzdGluLFRleGFzLFVuaXRlZCBTdGF0ZXM&hl=en&gl=us&num=10&safe=active&start=10&sourceid=chrome&ie=UTF-8", - "total_time_taken": 10.29 - }, - "search_parameters": { - "q": "Coffee", - "location_requested": "Austin, Texas, United States", - "location_used": "Austin,Texas,United States", - "google_domain": "google.com", - "hl": "en", - "gl": "us", - "safe": "active", - "start": "10", - "device": "desktop" - }, - "search_information": { - "total_results": 23620000000, - "time_taken_displayed": 0.6, - "query_displayed": "Coffee" - }, - "organic_results": [ - { - "position": 1, - "title": "What is Coffee? - National Coffee Association", - "link": "http://www.ncausa.org/about-coffee/what-is-coffee", - "displayed_link": "www.ncausa.org › About Coffee › What is Coffee?", - "snippet": "Coffee trees are pruned short to conserve their energy and aid in harvesting, but can grow to more than 30 feet (9 meters) high. Each tree is covered with green, ...", - "cached_page_link": "http://webcache.googleusercontent.com/search?q=cache:eukMDyc_TlwJ:www.ncausa.org/about-coffee/what-is-coffee+&cd=11&hl=en&ct=clnk&gl=us" - }, - { - "position": 2, - "title": "The History of Coffee - National Coffee Association", - "link": "http://www.ncausa.org/about-coffee/history-of-coffee", - "displayed_link": "www.ncausa.org › About Coffee › History of Coffee", - "snippet": "No one knows exactly how or when coffee was discovered, though there are many legends about its origin. ... Coffee grown worldwide can trace its heritage back centuries to the ancient coffee forests on the Ethiopian plateau. There, legend says the goat herder Kaldi first discovered the ...", - "cached_page_link": "http://webcache.googleusercontent.com/search?q=cache:lVL_jlE3B80J:www.ncausa.org/about-coffee/history-of-coffee+&cd=12&hl=en&ct=clnk&gl=us" - }, - { - "position": 3, - "title": "The Coffee House « Austin Improv Comedy Shows, Classes – The ...", - "link": "http://www.hideouttheatre.com/coffeehouse", - "displayed_link": "www.hideouttheatre.com/coffeehouse", - "snippet": "The Hideout is downtown Austin's oldest independent coffee house. We are open morning to night, weekdays and weekends and serve beer, wine, fresh ...", - "cached_page_link": "http://webcache.googleusercontent.com/search?q=cache:VD32HaeikQMJ:www.hideouttheatre.com/coffeehouse+&cd=13&hl=en&ct=clnk&gl=us", - "related_pages_link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=related:www.hideouttheatre.com/coffeehouse+Coffee&tbo=1&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChAfMAJ6BAgJEAU", - "rich_snippet": { - "bottom": { - "extensions": [ - "Thu, Mar 21", - "PGraph", - "Thu, Mar 21", - "Austin Secrets", - "Thu, Mar 21", - "The Free Fringe" - ] - } - } - }, - { - "position": 4, - "title": "Home | The Coffee Bean & Tea Leaf", - "link": "https://www.coffeebean.com/", - "displayed_link": "https://www.coffeebean.com/", - "snippet": "Never run out of your favorite coffees, teas and powders again with our auto-delivery subscription. Select how often your products arrive, pause and cancel ...", - "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:WpQxSYo2c6AJ:https://www.coffeebean.com/+&cd=14&hl=en&ct=clnk&gl=us", - "related_pages_link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=related:https://www.coffeebean.com/+Coffee&tbo=1&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChAfMAN6BAgFEAU" - }, - { - "position": 5, - "title": "Coffee Review - The World's Leading Coffee Guide", - "link": "https://www.coffeereview.com/", - "displayed_link": "https://www.coffeereview.com/", - "snippet": "Coffee reviews, espresso ratings, informative articles, and coffee blogs by Kenneth Davids and other coffee experts.", - "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:c9jncvPT1SsJ:https://www.coffeereview.com/+&cd=15&hl=en&ct=clnk&gl=us" - }, - { - "position": 6, - "title": "Coffee: Benefits, nutrition, and risks - Medical News Today", - "link": "https://www.medicalnewstoday.com/articles/270202.php", - "displayed_link": "https://www.medicalnewstoday.com/articles/270202.php", - "date": "Dec 14, 2017", - "snippet": "Drinking coffee may do much more than simply provide an energy boost when needed. Several scientific studies have identified a number of ...", - "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:FB_36c_qnUAJ:https://www.medicalnewstoday.com/articles/270202.php+&cd=16&hl=en&ct=clnk&gl=us" - }, - { - "position": 7, - "title": "Peet's Coffee: The Original Craft Coffee", - "link": "https://www.peets.com/", - "displayed_link": "https://www.peets.com/", - "snippet": "Since 1966, Peet's Coffee has offered superior coffees and teas by sourcing the best quality coffee beans and tea leaves in the world and adhering to strict ...", - "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:BCjzno6zP6wJ:https://www.peets.com/+&cd=17&hl=en&ct=clnk&gl=us", - "related_pages_link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=related:https://www.peets.com/+Coffee&tbo=1&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChAfMAZ6BAgGEAU" - }, - { - "position": 8, - "title": "The Best Coffee from Starbucks Coffee", - "link": "https://www.starbucks.com/coffee", - "displayed_link": "https://www.starbucks.com/coffee", - "snippet": "Next stop: Asia / Pacific. Journey through the world's coffee regions with our Passport Series—coffee selected to give you a sense of place. Now we're visiting ...", - "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:R7fHYjcarfsJ:https://www.starbucks.com/coffee+&cd=18&hl=en&ct=clnk&gl=us", - "related_pages_link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=related:https://www.starbucks.com/coffee+Coffee&tbo=1&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChAfMAd6BAgHEAU" - }, - { - "position": 9, - "title": "Seventh Flag Coffee - Austin", - "link": "http://www.seventhflagcoffee.com/", - "displayed_link": "www.seventhflagcoffee.com/", - "snippet": "Seventh Flag Coffee Co. is dedicated to creating the best overall coffee experience for every customer that walks through our doors. We are transforming the ...", - "cached_page_link": "http://webcache.googleusercontent.com/search?q=cache:VZCbBEneWWkJ:www.seventhflagcoffee.com/+&cd=19&hl=en&ct=clnk&gl=us", - "related_pages_link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=related:www.seventhflagcoffee.com/+Coffee&tbo=1&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChAfMAh6BAgIEAU" - }, - { - "position": 10, - "title": "Cosmic Coffee + Beer Garden - Austin, Tx", - "link": "https://www.cosmiccoffeebeer.com/", - "displayed_link": "https://www.cosmiccoffeebeer.com/", - "snippet": "specialty coffee, craft cocktails, locally brewed beer, food trailers and live music in a permaculture inspired garden setting in south austin, tx.", - "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:tsOR-R1-gmQJ:https://www.cosmiccoffeebeer.com/+&cd=20&hl=en&ct=clnk&gl=us" - } - ], - "related_searches": [ - { - "query": "mozarts coffee", - "link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=mozarts+coffee&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDVAigAegQIChAB" - }, - { - "query": "bennu coffee", - "link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=bennu+coffee&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDVAigBegQIChAC" - }, - { - "query": "coffee near me", - "link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=coffee+near+me&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDVAigCegQIChAD" - }, - { - "query": "coffee austin", - "link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=coffee+austin&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDVAigDegQIChAE" - }, - { - "query": "best coffee in austin", - "link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=best+coffee+in+austin&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDVAigEegQIChAF" - }, - { - "query": "coffee shops near me", - "link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=coffee+shops+near+me&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDVAigFegQIChAG" - }, - { - "query": "houndstooth coffee", - "link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=houndstooth+coffee&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDVAigGegQIChAH" - }, - { - "query": "epoch coffee", - "link": "https://www.google.com/search?safe=active&gl=us&hl=en&q=epoch+coffee&sa=X&ved=2ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDVAigHegQIChAI" - } - ], - "pagination": { - "current": 2, - "next": "https://www.google.com/search?q=Coffee&safe=active&gl=us&hl=en&ei=b5KRXOC6Bc3EswWc34_YDQ&start=20&sa=N&ved=0ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDw0wMIjAE", - "other_pages": { - "1": "https://www.google.com/search?q=Coffee&safe=active&gl=us&hl=en&ei=b5KRXOC6Bc3EswWc34_YDQ&start=0&sa=N&ved=0ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDy0wMIeQ", - "3": "https://www.google.com/search?q=Coffee&safe=active&gl=us&hl=en&ei=b5KRXOC6Bc3EswWc34_YDQ&start=20&sa=N&ved=0ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDy0wMIfA", - "4": "https://www.google.com/search?q=Coffee&safe=active&gl=us&hl=en&ei=b5KRXOC6Bc3EswWc34_YDQ&start=30&sa=N&ved=0ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDy0wMIfg", - "5": "https://www.google.com/search?q=Coffee&safe=active&gl=us&hl=en&ei=b5KRXOC6Bc3EswWc34_YDQ&start=40&sa=N&ved=0ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDy0wMIgAE", - "6": "https://www.google.com/search?q=Coffee&safe=active&gl=us&hl=en&ei=b5KRXOC6Bc3EswWc34_YDQ&start=50&sa=N&ved=0ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDy0wMIggE", - "7": "https://www.google.com/search?q=Coffee&safe=active&gl=us&hl=en&ei=b5KRXOC6Bc3EswWc34_YDQ&start=60&sa=N&ved=0ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDy0wMIhAE", - "8": "https://www.google.com/search?q=Coffee&safe=active&gl=us&hl=en&ei=b5KRXOC6Bc3EswWc34_YDQ&start=70&sa=N&ved=0ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDy0wMIhgE", - "9": "https://www.google.com/search?q=Coffee&safe=active&gl=us&hl=en&ei=b5KRXOC6Bc3EswWc34_YDQ&start=80&sa=N&ved=0ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDy0wMIiAE", - "10": "https://www.google.com/search?q=Coffee&safe=active&gl=us&hl=en&ei=b5KRXOC6Bc3EswWc34_YDQ&start=90&sa=N&ved=0ahUKEwigrp6FxI_hAhVN4qwKHZzvA9s4ChDy0wMIigE" - } - } -} \ No newline at end of file diff --git a/src/test/java/serpapi/AccountApiTest.java b/src/test/java/serpapi/AccountApiTest.java index cb49d6d..364229d 100644 --- a/src/test/java/serpapi/AccountApiTest.java +++ b/src/test/java/serpapi/AccountApiTest.java @@ -12,16 +12,19 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; +/*** + * Test SerpApi.account() method. + */ public class AccountApiTest { @Test public void account() throws Exception { - if (System.getenv("API_KEY") == null) { - return; + if (System.getenv("SERPAPI_KEY") == null) { + fail("SERPAPI_KEY is not set"); } Map parameter = new HashMap(); - parameter.put("api_key", System.getenv("API_KEY")); + parameter.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(parameter); JsonObject info = client.account(); System.out.println(info.toString()); diff --git a/src/test/java/serpapi/LocationApiTest.java b/src/test/java/serpapi/LocationApiTest.java index 35ca1e9..76fe0c3 100644 --- a/src/test/java/serpapi/LocationApiTest.java +++ b/src/test/java/serpapi/LocationApiTest.java @@ -13,17 +13,21 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; +/** + * Test SerpApi.location() method. + */ public class LocationApiTest { @Test public void location() throws Exception { - if(System.getenv("API_KEY") == null) - return; + if(System.getenv("SERPAPI_KEY") == null) { + fail("SERPAPI_KEY is not set"); + } SerpApi serpapi = new SerpApi(); Map parameter = new HashMap(); - parameter.put("q", "Austin"); + parameter.put("q", "Austin, TX"); parameter.put("limit", "3"); JsonArray location = serpapi.location(parameter); assertEquals("Austin, TX", location.get(0).getAsJsonObject().get("name").getAsString()); diff --git a/src/test/java/serpapi/SearchApiTest.java b/src/test/java/serpapi/SearchApiTest.java new file mode 100644 index 0000000..8f54924 --- /dev/null +++ b/src/test/java/serpapi/SearchApiTest.java @@ -0,0 +1,42 @@ +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.search() method. + */ +public class SearchApiTest { + + @Test + public void search() throws SerpApiException, InterruptedException { + // skip test if no api_key provided + if(System.getenv("SERPAPI_KEY") == null) { + fail("SERPAPI_KEY is not set"); + } + + Map auth = new HashMap<>(); + auth.put("api_key", System.getenv("SERPAPI_KEY")); + SerpApi serpapi = new SerpApi(auth); + + Map parameter = new HashMap<>(); + 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"); + + JsonObject results = serpapi.search(parameter); + assertTrue(results.getAsJsonArray("organic_results").size() > 5); + } + +} diff --git a/src/test/java/serpapi/SerpApiTest.java b/src/test/java/serpapi/SearchArchiveApiTest.java similarity index 58% rename from src/test/java/serpapi/SerpApiTest.java rename to src/test/java/serpapi/SearchArchiveApiTest.java index 4abc9bd..b8dcf6e 100644 --- a/src/test/java/serpapi/SerpApiTest.java +++ b/src/test/java/serpapi/SearchArchiveApiTest.java @@ -10,42 +10,19 @@ import static org.junit.Assert.*; /** - * Test main class + * Test SerpApi.searchArchive() method. */ -public class SerpApiTest { - - @Test - public void search() throws SerpApiException, InterruptedException { - // skip test if no api_key provided - if(System.getenv("API_KEY") == null) - return; - - Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); - SerpApi serpapi = new SerpApi(auth); - - Map parameter = new HashMap<>(); - 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"); - - JsonObject results = serpapi.search(parameter); - assertTrue(results.getAsJsonArray("organic_results").size() > 5); - } +public class SearchArchiveApiTest { @Test public void searchArchive() throws SerpApiException, InterruptedException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) - return; + if(System.getenv("SERPAPI_KEY") == null) { + fail("SERPAPI_KEY is not set"); + } Map parameter = new HashMap<>(); - parameter.put("api_key", System.getenv("API_KEY")); + parameter.put("api_key", System.getenv("SERPAPI_KEY")); parameter.put("q", "Coffee"); parameter.put("location", "Austin, Texas, United States"); parameter.put("hl", "en"); diff --git a/src/test/java/serpapi/AppleAppStoreTest.java b/src/test/java/serpapi/example/AppleAppStoreTest.java similarity index 88% rename from src/test/java/serpapi/AppleAppStoreTest.java rename to src/test/java/serpapi/example/AppleAppStoreTest.java index a8d5708..a1af4f5 100644 --- a/src/test/java/serpapi/AppleAppStoreTest.java +++ b/src/test/java/serpapi/example/AppleAppStoreTest.java @@ -17,12 +17,12 @@ public class AppleAppStoreTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/BaiduTest.java b/src/test/java/serpapi/example/BaiduTest.java similarity index 88% rename from src/test/java/serpapi/BaiduTest.java rename to src/test/java/serpapi/example/BaiduTest.java index 8cbee14..cea68d7 100644 --- a/src/test/java/serpapi/BaiduTest.java +++ b/src/test/java/serpapi/example/BaiduTest.java @@ -17,12 +17,12 @@ public class BaiduTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/BingTest.java b/src/test/java/serpapi/example/BingTest.java similarity index 88% rename from src/test/java/serpapi/BingTest.java rename to src/test/java/serpapi/example/BingTest.java index defc69e..6adacf7 100644 --- a/src/test/java/serpapi/BingTest.java +++ b/src/test/java/serpapi/example/BingTest.java @@ -17,12 +17,12 @@ public class BingTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/DuckduckgoTest.java b/src/test/java/serpapi/example/DuckduckgoTest.java similarity index 88% rename from src/test/java/serpapi/DuckduckgoTest.java rename to src/test/java/serpapi/example/DuckduckgoTest.java index 22d9cee..5a5ba47 100644 --- a/src/test/java/serpapi/DuckduckgoTest.java +++ b/src/test/java/serpapi/example/DuckduckgoTest.java @@ -17,12 +17,12 @@ public class DuckduckgoTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/EbayTest.java b/src/test/java/serpapi/example/EbayTest.java similarity index 88% rename from src/test/java/serpapi/EbayTest.java rename to src/test/java/serpapi/example/EbayTest.java index facdf60..874f48e 100644 --- a/src/test/java/serpapi/EbayTest.java +++ b/src/test/java/serpapi/example/EbayTest.java @@ -17,12 +17,12 @@ public class EbayTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/GoogleAutocompleteTest.java b/src/test/java/serpapi/example/GoogleAutocompleteTest.java similarity index 88% rename from src/test/java/serpapi/GoogleAutocompleteTest.java rename to src/test/java/serpapi/example/GoogleAutocompleteTest.java index b2f9518..a279363 100644 --- a/src/test/java/serpapi/GoogleAutocompleteTest.java +++ b/src/test/java/serpapi/example/GoogleAutocompleteTest.java @@ -17,12 +17,12 @@ public class GoogleAutocompleteTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/GoogleEventsTest.java b/src/test/java/serpapi/example/GoogleEventsTest.java similarity index 88% rename from src/test/java/serpapi/GoogleEventsTest.java rename to src/test/java/serpapi/example/GoogleEventsTest.java index ffddcaf..df46c1a 100644 --- a/src/test/java/serpapi/GoogleEventsTest.java +++ b/src/test/java/serpapi/example/GoogleEventsTest.java @@ -17,12 +17,12 @@ public class GoogleEventsTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/GoogleImagesTest.java b/src/test/java/serpapi/example/GoogleImagesTest.java similarity index 89% rename from src/test/java/serpapi/GoogleImagesTest.java rename to src/test/java/serpapi/example/GoogleImagesTest.java index a2c9932..cf27d53 100644 --- a/src/test/java/serpapi/GoogleImagesTest.java +++ b/src/test/java/serpapi/example/GoogleImagesTest.java @@ -17,12 +17,12 @@ public class GoogleImagesTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/GoogleJobsTest.java b/src/test/java/serpapi/example/GoogleJobsTest.java similarity index 88% rename from src/test/java/serpapi/GoogleJobsTest.java rename to src/test/java/serpapi/example/GoogleJobsTest.java index 9bc5a85..eb274c2 100644 --- a/src/test/java/serpapi/GoogleJobsTest.java +++ b/src/test/java/serpapi/example/GoogleJobsTest.java @@ -17,12 +17,12 @@ public class GoogleJobsTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/GoogleLocalServicesTest.java b/src/test/java/serpapi/example/GoogleLocalServicesTest.java similarity index 89% rename from src/test/java/serpapi/GoogleLocalServicesTest.java rename to src/test/java/serpapi/example/GoogleLocalServicesTest.java index f0cc543..b3574d4 100644 --- a/src/test/java/serpapi/GoogleLocalServicesTest.java +++ b/src/test/java/serpapi/example/GoogleLocalServicesTest.java @@ -17,12 +17,12 @@ public class GoogleLocalServicesTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/GoogleMapsTest.java b/src/test/java/serpapi/example/GoogleMapsTest.java similarity index 89% rename from src/test/java/serpapi/GoogleMapsTest.java rename to src/test/java/serpapi/example/GoogleMapsTest.java index 0e6ac43..37ea934 100644 --- a/src/test/java/serpapi/GoogleMapsTest.java +++ b/src/test/java/serpapi/example/GoogleMapsTest.java @@ -17,12 +17,12 @@ public class GoogleMapsTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/GooglePlayTest.java b/src/test/java/serpapi/example/GooglePlayTest.java similarity index 88% rename from src/test/java/serpapi/GooglePlayTest.java rename to src/test/java/serpapi/example/GooglePlayTest.java index 403eab5..b603697 100644 --- a/src/test/java/serpapi/GooglePlayTest.java +++ b/src/test/java/serpapi/example/GooglePlayTest.java @@ -17,12 +17,12 @@ public class GooglePlayTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/GoogleProductTest.java b/src/test/java/serpapi/example/GoogleProductTest.java similarity index 89% rename from src/test/java/serpapi/GoogleProductTest.java rename to src/test/java/serpapi/example/GoogleProductTest.java index 1ced6d2..65d089f 100644 --- a/src/test/java/serpapi/GoogleProductTest.java +++ b/src/test/java/serpapi/example/GoogleProductTest.java @@ -19,12 +19,12 @@ public class GoogleProductTest { // @Test // public void search() throws SerpApiException { // // skip test if no api_key provided - // if(System.getenv("API_KEY") == null) + // if(System.getenv("SERPAPI_KEY") == null) // return; // // setup serpapi client // Map auth = new HashMap<>(); - // auth.put("api_key", System.getenv("API_KEY")); + // auth.put("api_key", System.getenv("SERPAPI_KEY")); // SerpApi client = new SerpApi(auth); // // run search diff --git a/src/test/java/serpapi/GoogleReverseImageTest.java b/src/test/java/serpapi/example/GoogleReverseImageTest.java similarity index 77% rename from src/test/java/serpapi/GoogleReverseImageTest.java rename to src/test/java/serpapi/example/GoogleReverseImageTest.java index de2abf4..e2f6298 100644 --- a/src/test/java/serpapi/GoogleReverseImageTest.java +++ b/src/test/java/serpapi/example/GoogleReverseImageTest.java @@ -17,12 +17,12 @@ public class GoogleReverseImageTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search @@ -30,7 +30,8 @@ public void search() throws SerpApiException { parameter.put("engine", "google_reverse_image"); parameter.put("image_url", "https://i.imgur.com/5bGzZi7.jpg"); JsonObject results = client.search(parameter); - assertTrue(results.getAsJsonArray("image_sizes").size() > 1); + assertTrue(results.has("image_results")); + assertTrue(results.getAsJsonArray("image_results").size() > 0); } } \ No newline at end of file diff --git a/src/test/java/serpapi/GoogleScholarTest.java b/src/test/java/serpapi/example/GoogleScholarTest.java similarity index 88% rename from src/test/java/serpapi/GoogleScholarTest.java rename to src/test/java/serpapi/example/GoogleScholarTest.java index 8ec41a4..7724439 100644 --- a/src/test/java/serpapi/GoogleScholarTest.java +++ b/src/test/java/serpapi/example/GoogleScholarTest.java @@ -17,12 +17,12 @@ public class GoogleScholarTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/GoogleTest.java b/src/test/java/serpapi/example/GoogleTest.java similarity index 88% rename from src/test/java/serpapi/GoogleTest.java rename to src/test/java/serpapi/example/GoogleTest.java index 0b5acd4..086cdd4 100644 --- a/src/test/java/serpapi/GoogleTest.java +++ b/src/test/java/serpapi/example/GoogleTest.java @@ -17,12 +17,12 @@ public class GoogleTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/HomeDepotTest.java b/src/test/java/serpapi/example/HomeDepotTest.java similarity index 88% rename from src/test/java/serpapi/HomeDepotTest.java rename to src/test/java/serpapi/example/HomeDepotTest.java index 3a4a345..94f4415 100644 --- a/src/test/java/serpapi/HomeDepotTest.java +++ b/src/test/java/serpapi/example/HomeDepotTest.java @@ -17,12 +17,12 @@ public class HomeDepotTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/NaverTest.java b/src/test/java/serpapi/example/NaverTest.java similarity index 88% rename from src/test/java/serpapi/NaverTest.java rename to src/test/java/serpapi/example/NaverTest.java index 98b433e..c83a346 100644 --- a/src/test/java/serpapi/NaverTest.java +++ b/src/test/java/serpapi/example/NaverTest.java @@ -17,12 +17,12 @@ public class NaverTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/WalmartTest.java b/src/test/java/serpapi/example/WalmartTest.java similarity index 88% rename from src/test/java/serpapi/WalmartTest.java rename to src/test/java/serpapi/example/WalmartTest.java index a7d440b..d1b19a9 100644 --- a/src/test/java/serpapi/WalmartTest.java +++ b/src/test/java/serpapi/example/WalmartTest.java @@ -17,12 +17,12 @@ public class WalmartTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/YahooTest.java b/src/test/java/serpapi/example/YahooTest.java similarity index 88% rename from src/test/java/serpapi/YahooTest.java rename to src/test/java/serpapi/example/YahooTest.java index be147f8..4452465 100644 --- a/src/test/java/serpapi/YahooTest.java +++ b/src/test/java/serpapi/example/YahooTest.java @@ -17,12 +17,12 @@ public class YahooTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search diff --git a/src/test/java/serpapi/YoutubeTest.java b/src/test/java/serpapi/example/YoutubeTest.java similarity index 88% rename from src/test/java/serpapi/YoutubeTest.java rename to src/test/java/serpapi/example/YoutubeTest.java index 1d2ce26..4e71a65 100644 --- a/src/test/java/serpapi/YoutubeTest.java +++ b/src/test/java/serpapi/example/YoutubeTest.java @@ -17,12 +17,12 @@ public class YoutubeTest { @Test public void search() throws SerpApiException { // skip test if no api_key provided - if(System.getenv("API_KEY") == null) + if(System.getenv("SERPAPI_KEY") == null) return; // setup serpapi client Map auth = new HashMap<>(); - auth.put("api_key", System.getenv("API_KEY")); + auth.put("api_key", System.getenv("SERPAPI_KEY")); SerpApi client = new SerpApi(auth); // run search