-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test for Ip2GeoProcessor
Signed-off-by: Heemin Kim <[email protected]>
- Loading branch information
Showing
15 changed files
with
445 additions
and
13 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/org/opensearch/geospatial/exceptions/ResourceInUseException.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,35 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.exceptions; | ||
|
||
import java.io.IOException; | ||
|
||
import org.opensearch.OpenSearchException; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.rest.RestStatus; | ||
|
||
/** | ||
* Generic ResourceInUseException corresponding to the {@link RestStatus#BAD_REQUEST} status code | ||
*/ | ||
public class ResourceInUseException extends OpenSearchException { | ||
|
||
public ResourceInUseException(String msg, Object... args) { | ||
super(msg, args); | ||
} | ||
|
||
public ResourceInUseException(String msg, Throwable cause, Object... args) { | ||
super(msg, cause, args); | ||
} | ||
|
||
public ResourceInUseException(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
@Override | ||
public final RestStatus status() { | ||
return RestStatus.BAD_REQUEST; | ||
} | ||
} |
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
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
120 changes: 120 additions & 0 deletions
120
src/test/java/org/opensearch/geospatial/ip2geo/Ip2GeoDataServer.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,120 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.ip2geo; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import lombok.SneakyThrows; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
import org.opensearch.common.SuppressForbidden; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpServer; | ||
|
||
/** | ||
* Simple http server to serve static files under test/java/resources/ip2geo/server for integration testing | ||
*/ | ||
@Log4j2 | ||
@SuppressForbidden(reason = "used only for testing") | ||
public class Ip2GeoDataServer { | ||
private static final String SYS_PROPERTY_KEY_CLUSTER_ENDPOINT = "tests.rest.cluster"; | ||
private static final String LOCAL_CLUSTER_ENDPOINT = "127.0.0.1"; | ||
private static final String ROOT = "ip2geo/server"; | ||
private static final int PORT = 8001; | ||
|
||
private static HttpServer server; | ||
private static volatile int counter = 0; | ||
private static String endpointPrefix = "http://localhost:" + PORT; | ||
private static String cityFilePath = endpointPrefix + "/city/manifest_local.json"; | ||
private static String countryFilePath = endpointPrefix + "/country/manifest_local.json"; | ||
|
||
/** | ||
* Return an endpoint to a manifest file for a sample city data | ||
* The sample data should contain three lines as follows | ||
* | ||
* cidr,city,country | ||
* 10.0.0.0/8,Seattle,USA | ||
* 127.0.0.0/12,Vancouver,Canada | ||
* fd12:2345:6789:1::/64,Bengaluru,India | ||
* | ||
*/ | ||
public static String getEndpointCity() { | ||
return cityFilePath; | ||
} | ||
|
||
/** | ||
* Return an endpoint to a manifest file for a sample country data | ||
* The sample data should contain three lines as follows | ||
* | ||
* cidr,country | ||
* 10.0.0.0/8,USA | ||
* 127.0.0.0/12,Canada | ||
* fd12:2345:6789:1::/64,India | ||
* | ||
*/ | ||
public static String getEndpointCountry() { | ||
return countryFilePath; | ||
} | ||
|
||
@SneakyThrows | ||
synchronized public static void start() { | ||
// If it is remote cluster test, use external endpoint and do not launch local server | ||
if (System.getProperty(SYS_PROPERTY_KEY_CLUSTER_ENDPOINT).contains(LOCAL_CLUSTER_ENDPOINT) == false) { | ||
String externalEndpointPrefix = "https://github.com/opensearch-project/geospatial/blob/main/src/test/resources/ip2geo/server"; | ||
cityFilePath = externalEndpointPrefix + "/city/manifest.json"; | ||
countryFilePath = externalEndpointPrefix + "/country/manifest.json"; | ||
return; | ||
} | ||
|
||
counter++; | ||
if (server != null) { | ||
return; | ||
} | ||
server = HttpServer.create(new InetSocketAddress("localhost", PORT), 0); | ||
server.createContext("/", new Ip2GeoHttpHandler()); | ||
server.start(); | ||
log.info("Local file server started on port {}", PORT); | ||
} | ||
|
||
synchronized public static void stop() { | ||
if (server == null) { | ||
return; | ||
} | ||
counter--; | ||
if (counter > 0) { | ||
return; | ||
} | ||
|
||
server.stop(0); | ||
server = null; | ||
log.info("Server stopped"); | ||
} | ||
|
||
@SuppressForbidden(reason = "used only for testing") | ||
private static class Ip2GeoHttpHandler implements HttpHandler { | ||
@Override | ||
public void handle(final HttpExchange exchange) throws IOException { | ||
try { | ||
byte[] data = Files.readAllBytes( | ||
Paths.get(this.getClass().getClassLoader().getResource(ROOT + exchange.getRequestURI().getPath()).toURI()) | ||
); | ||
exchange.sendResponseHeaders(200, data.length); | ||
OutputStream outputStream = exchange.getResponseBody(); | ||
outputStream.write(data); | ||
outputStream.flush(); | ||
outputStream.close(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.