Skip to content

Commit

Permalink
Tests: Use a local HTTP server to mock Maven Search API.
Browse files Browse the repository at this point in the history
  • Loading branch information
smarkwal committed Sep 24, 2024
1 parent 005625a commit 251e980
Show file tree
Hide file tree
Showing 18 changed files with 795 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@
import org.jarhc.artifacts.Artifact;
import org.jarhc.artifacts.MavenArtifactFinder;
import org.jarhc.artifacts.RepositoryException;
import org.jarhc.it.utils.MavenSearchApiMockServer;
import org.jarhc.test.log.LoggerBuilder;
import org.jarhc.test.log.LoggerUtils;
import org.jarhc.utils.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;

@ExtendWith(MavenSearchApiMockServer.class)
class MavenArtifactFinderTest {

private final Logger logger = LoggerBuilder.collect(MavenArtifactFinder.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package org.jarhc.it;

import org.jarhc.it.utils.MavenSearchApiMockServer;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(MavenSearchApiMockServer.class)
class TestJettyWebAppTest extends AbstractMainTest {

TestJettyWebAppTest() {
Expand Down
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();
}
}

}
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
}
}
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
}
}
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
}
}
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
}
}
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
}
}
Loading

0 comments on commit 251e980

Please sign in to comment.