-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests: Use a local HTTP server to mock Maven Search API.
- Loading branch information
Showing
18 changed files
with
795 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
jarhc/src/integrationTest/java/org/jarhc/it/utils/MavenSearchApiMockServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2024 Stephan Markwalder | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jarhc.it.utils; | ||
|
||
import com.sun.net.httpserver.Headers; | ||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpServer; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import org.jarhc.TestUtils; | ||
import org.junit.jupiter.api.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class MavenSearchApiMockServer implements BeforeAllCallback, AfterAllCallback { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(MavenSearchApiMockServer.class); | ||
|
||
private HttpServer httpServer; | ||
private String originalSearchUrl; | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext context) throws Exception { | ||
if (httpServer != null) { | ||
throw new IllegalStateException("Server is already running."); | ||
} | ||
|
||
// start HTTP server on random port | ||
httpServer = HttpServer.create(new InetSocketAddress("localhost", 0), 0); | ||
httpServer.createContext("/checksum", new RequestHandler()); | ||
httpServer.setExecutor(null); // use default executor | ||
httpServer.start(); | ||
|
||
// get selected port | ||
int port = httpServer.getAddress().getPort(); | ||
|
||
// update search URL in Java System Properties | ||
originalSearchUrl = System.getProperty("jarhc.search.url"); | ||
String searchUrl = "http://localhost:" + port + "/checksum?q=%s"; | ||
System.setProperty("jarhc.search.url", searchUrl); | ||
} | ||
|
||
@Override | ||
public void afterAll(ExtensionContext context) { | ||
|
||
// stop HTTP server | ||
if (httpServer != null) { | ||
httpServer.stop(0); | ||
httpServer = null; | ||
} | ||
|
||
// restore original search URL in Java System Properties | ||
if (originalSearchUrl != null) { | ||
System.setProperty("jarhc.search.url", originalSearchUrl); | ||
} else { | ||
System.clearProperty("jarhc.search.url"); | ||
} | ||
} | ||
|
||
private static class RequestHandler implements HttpHandler { | ||
|
||
public void handle(HttpExchange exchange) throws IOException { | ||
|
||
// validate query string | ||
String query = exchange.getRequestURI().getQuery(); | ||
if (query == null || !query.startsWith("q=")) { | ||
LOGGER.error("Invalid query: '{}'", query); | ||
exchange.sendResponseHeaders(400, 0); | ||
exchange.getResponseBody().close(); | ||
return; | ||
} | ||
|
||
// get checksum from query string | ||
String checksum = query.substring(2); | ||
|
||
LOGGER.debug("Mock Maven Search API response for checksum: '{}'", checksum); | ||
|
||
// load response from test resources | ||
String response = TestUtils.getResourceAsString("/maven-search-api-responses/" + checksum + ".json", "UTF-8"); | ||
|
||
// send response headers | ||
Headers headers = exchange.getResponseHeaders(); | ||
headers.set("Content-Type", "application/json"); | ||
exchange.sendResponseHeaders(200, response.length()); | ||
|
||
// send response body | ||
OutputStream stream = exchange.getResponseBody(); | ||
stream.write(response.getBytes()); | ||
stream.close(); | ||
} | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...onTest/resources/maven-search-api-responses/0693496adda5acaea4078d92c4a766c8f2d0d5c5.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"response": { | ||
"docs": [{ | ||
"p": "jar", | ||
"a": "jetty-servlets", | ||
"v": "9.4.20.v20190813", | ||
"g": "org.eclipse.jetty", | ||
"id": "org.eclipse.jetty:jetty-servlets:9.4.20.v20190813", | ||
"ec": [ | ||
"-javadoc.jar", | ||
"-sources.jar", | ||
"-config.jar", | ||
".jar", | ||
".pom" | ||
], | ||
"timestamp": 1565732393000, | ||
"tags": [ | ||
"servlets", | ||
"utility", | ||
"from", | ||
"jetty" | ||
] | ||
}], | ||
"numFound": 1, | ||
"start": 0 | ||
}, | ||
"responseHeader": { | ||
"QTime": 1, | ||
"params": { | ||
"q": "1:\"0693496adda5acaea4078d92c4a766c8f2d0d5c5\"", | ||
"core": "", | ||
"indent": "off", | ||
"fl": "id,g,a,v,p,ec,timestamp,tags", | ||
"start": "", | ||
"sort": "score desc,timestamp desc,g asc,a asc,v desc", | ||
"rows": "20", | ||
"wt": "json", | ||
"version": "2.2" | ||
}, | ||
"status": 0 | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...onTest/resources/maven-search-api-responses/093ee1760aba62d6896d578bd7d247d0fa52f0e7.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"response": { | ||
"docs": [{ | ||
"p": "jar", | ||
"a": "commons-codec", | ||
"v": "1.11", | ||
"g": "commons-codec", | ||
"id": "commons-codec:commons-codec:1.11", | ||
"ec": [ | ||
"-sources.jar", | ||
"-javadoc.jar", | ||
"-test-sources.jar", | ||
".jar", | ||
"-tests.jar", | ||
"-src.zip", | ||
"-src.tar.gz", | ||
".pom", | ||
"-bin.zip", | ||
"-bin.tar.gz" | ||
], | ||
"timestamp": 1508252070000, | ||
"tags": [ | ||
"encoders", | ||
"decoders", | ||
"phonetic", | ||
"such", | ||
"simple", | ||
"package", | ||
"apache", | ||
"codec", | ||
"contains", | ||
"collection", | ||
"used", | ||
"addition", | ||
"formats", | ||
"base64", | ||
"hexadecimal", | ||
"widely", | ||
"commons", | ||
"utilities", | ||
"maintains", | ||
"encoding", | ||
"various", | ||
"also", | ||
"encoder", | ||
"these" | ||
] | ||
}], | ||
"numFound": 1, | ||
"start": 0 | ||
}, | ||
"responseHeader": { | ||
"QTime": 3, | ||
"params": { | ||
"q": "1:\"093ee1760aba62d6896d578bd7d247d0fa52f0e7\"", | ||
"core": "", | ||
"indent": "off", | ||
"fl": "id,g,a,v,p,ec,timestamp,tags", | ||
"start": "", | ||
"sort": "score desc,timestamp desc,g asc,a asc,v desc", | ||
"rows": "20", | ||
"wt": "json", | ||
"version": "2.2" | ||
}, | ||
"status": 0 | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...onTest/resources/maven-search-api-responses/1234567890123456789012345678901234567890.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"response": { | ||
"docs": [], | ||
"numFound": 0, | ||
"start": 0 | ||
}, | ||
"responseHeader": { | ||
"QTime": 2, | ||
"params": { | ||
"q": "1:\"1234567890123456789012345678901234567890\"", | ||
"core": "", | ||
"indent": "off", | ||
"fl": "id,g,a,v,p,ec,timestamp,tags", | ||
"start": "", | ||
"sort": "score desc,timestamp desc,g asc,a asc,v desc", | ||
"rows": "20", | ||
"wt": "json", | ||
"version": "2.2" | ||
}, | ||
"status": 0 | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...onTest/resources/maven-search-api-responses/2cd9b264f76e3d087ee21bfc99305928e1bdb443.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ | ||
"response": { | ||
"docs": [ | ||
{ | ||
"p": "jar", | ||
"a": "slf4j-api", | ||
"v": "RELEASE113", | ||
"g": "org.netbeans.external", | ||
"id": "org.netbeans.external:slf4j-api:RELEASE113", | ||
"ec": [ | ||
"-sources.jar", | ||
".jar", | ||
".nbm", | ||
".pom" | ||
], | ||
"timestamp": 1582302753000, | ||
"tags": [ | ||
"integrated", | ||
"platform", | ||
"application", | ||
"environment", | ||
"development", | ||
"apache", | ||
"tooling", | ||
"netbeans", | ||
"framework" | ||
] | ||
}, | ||
{ | ||
"p": "jar", | ||
"a": "slf4j-api", | ||
"v": "1.7.28", | ||
"g": "org.slf4j", | ||
"id": "org.slf4j:slf4j-api:1.7.28", | ||
"ec": [ | ||
"-sources.jar", | ||
"-javadoc.jar", | ||
".jar", | ||
"-tests.jar", | ||
".pom" | ||
], | ||
"timestamp": 1565425641000, | ||
"tags": ["slf4j"] | ||
} | ||
], | ||
"numFound": 2, | ||
"start": 0 | ||
}, | ||
"responseHeader": { | ||
"QTime": 2, | ||
"params": { | ||
"q": "1:\"2cd9b264f76e3d087ee21bfc99305928e1bdb443\"", | ||
"core": "", | ||
"indent": "off", | ||
"fl": "id,g,a,v,p,ec,timestamp,tags", | ||
"start": "", | ||
"sort": "score desc,timestamp desc,g asc,a asc,v desc", | ||
"rows": "20", | ||
"wt": "json", | ||
"version": "2.2" | ||
}, | ||
"status": 0 | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...onTest/resources/maven-search-api-responses/3cd63d075497751784b2fa84be59432f4905bf7c.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"response": { | ||
"docs": [{ | ||
"p": "jar", | ||
"a": "javax.servlet-api", | ||
"v": "3.1.0", | ||
"g": "javax.servlet", | ||
"id": "javax.servlet:javax.servlet-api:3.1.0", | ||
"ec": [ | ||
"-javadoc.jar", | ||
"-sources.jar", | ||
".jar", | ||
".pom" | ||
], | ||
"timestamp": 1366933959000, | ||
"tags": [ | ||
"technology", | ||
"collaboration", | ||
"source", | ||
"java" | ||
] | ||
}], | ||
"numFound": 1, | ||
"start": 0 | ||
}, | ||
"responseHeader": { | ||
"QTime": 1, | ||
"params": { | ||
"q": "1:\"3cd63d075497751784b2fa84be59432f4905bf7c\"", | ||
"core": "", | ||
"indent": "off", | ||
"fl": "id,g,a,v,p,ec,timestamp,tags", | ||
"start": "", | ||
"sort": "score desc,timestamp desc,g asc,a asc,v desc", | ||
"rows": "20", | ||
"wt": "json", | ||
"version": "2.2" | ||
}, | ||
"status": 0 | ||
} | ||
} |
Oops, something went wrong.