-
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 UpdateDatasource API (#307)
Signed-off-by: Heemin Kim <[email protected]>
- Loading branch information
Showing
11 changed files
with
316 additions
and
29 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/org/opensearch/geospatial/exceptions/IncompatibleDatasourceException.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,38 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* IncompatibleDatasourceException corresponding to the {@link RestStatus#BAD_REQUEST} status code | ||
* | ||
* The exception is thrown when a user tries to update datasource with new endpoint which is not compatible | ||
* with current datasource | ||
*/ | ||
public class IncompatibleDatasourceException extends OpenSearchException { | ||
|
||
public IncompatibleDatasourceException(String msg, Object... args) { | ||
super(msg, args); | ||
} | ||
|
||
public IncompatibleDatasourceException(String msg, Throwable cause, Object... args) { | ||
super(msg, cause, args); | ||
} | ||
|
||
public IncompatibleDatasourceException(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
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
44 changes: 44 additions & 0 deletions
44
src/test/java/org/opensearch/geospatial/exceptions/IncompatibleDatasourceExceptionTests.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,44 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.exceptions; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
import org.opensearch.common.io.stream.BytesStreamInput; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
import org.opensearch.rest.RestStatus; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
public class IncompatibleDatasourceExceptionTests extends OpenSearchTestCase { | ||
public void testConstructor_whenCreated_thenSucceed() { | ||
IncompatibleDatasourceException exception = new IncompatibleDatasourceException( | ||
"New datasource is not compatible with existing datasource" | ||
); | ||
assertEquals(RestStatus.BAD_REQUEST, exception.status()); | ||
} | ||
|
||
public void testConstructor_whenCreatedWithRootCause_thenSucceed() { | ||
IncompatibleDatasourceException exception = new IncompatibleDatasourceException( | ||
"New datasource is not compatible with existing datasource", | ||
new RuntimeException() | ||
); | ||
assertEquals(RestStatus.BAD_REQUEST, exception.status()); | ||
} | ||
|
||
@SneakyThrows | ||
public void testConstructor_whenCreatedWithStream_thenSucceed() { | ||
IncompatibleDatasourceException exception = new IncompatibleDatasourceException( | ||
"New datasource is not compatible with existing datasource" | ||
); | ||
|
||
BytesStreamOutput output = new BytesStreamOutput(); | ||
exception.writeTo(output); | ||
BytesStreamInput input = new BytesStreamInput(output.bytes().toBytesRef().bytes); | ||
IncompatibleDatasourceException copiedException = new IncompatibleDatasourceException(input); | ||
assertEquals(exception.getMessage(), copiedException.getMessage()); | ||
assertEquals(exception.status(), copiedException.status()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/test/java/org/opensearch/geospatial/exceptions/ResourceInUseExceptionTests.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,37 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.exceptions; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
import org.opensearch.common.io.stream.BytesStreamInput; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
import org.opensearch.rest.RestStatus; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
public class ResourceInUseExceptionTests extends OpenSearchTestCase { | ||
public void testConstructor_whenCreated_thenSucceed() { | ||
ResourceInUseException exception = new ResourceInUseException("Resource is in use"); | ||
assertEquals(RestStatus.BAD_REQUEST, exception.status()); | ||
} | ||
|
||
public void testConstructor_whenCreatedWithRootCause_thenSucceed() { | ||
ResourceInUseException exception = new ResourceInUseException("Resource is in use", new RuntimeException()); | ||
assertEquals(RestStatus.BAD_REQUEST, exception.status()); | ||
} | ||
|
||
@SneakyThrows | ||
public void testConstructor_whenCreatedWithStream_thenSucceed() { | ||
ResourceInUseException exception = new ResourceInUseException("New datasource is not compatible with existing datasource"); | ||
|
||
BytesStreamOutput output = new BytesStreamOutput(); | ||
exception.writeTo(output); | ||
BytesStreamInput input = new BytesStreamInput(output.bytes().toBytesRef().bytes); | ||
ResourceInUseException copiedException = new ResourceInUseException(input); | ||
assertEquals(exception.getMessage(), copiedException.getMessage()); | ||
assertEquals(exception.status(), copiedException.status()); | ||
} | ||
} |
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
104 changes: 104 additions & 0 deletions
104
src/test/java/org/opensearch/geospatial/ip2geo/action/UpdateDatasourceIT.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,104 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.ip2geo.action; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.opensearch.client.ResponseException; | ||
import org.opensearch.geospatial.GeospatialRestTestCase; | ||
import org.opensearch.geospatial.GeospatialTestHelper; | ||
import org.opensearch.geospatial.ip2geo.Ip2GeoDataServer; | ||
import org.opensearch.rest.RestStatus; | ||
|
||
public class UpdateDatasourceIT extends GeospatialRestTestCase { | ||
// Use this value in resource name to avoid name conflict among tests | ||
private static final String PREFIX = UpdateDatasourceIT.class.getSimpleName().toLowerCase(Locale.ROOT); | ||
|
||
@BeforeClass | ||
public static void start() { | ||
Ip2GeoDataServer.start(); | ||
} | ||
|
||
@AfterClass | ||
public static void stop() { | ||
Ip2GeoDataServer.stop(); | ||
} | ||
|
||
@SneakyThrows | ||
public void testUpdateDatasource_whenValidInput_thenUpdated() { | ||
boolean isDatasourceCreated = false; | ||
String datasourceName = PREFIX + GeospatialTestHelper.randomLowerCaseString(); | ||
try { | ||
Map<String, Object> datasourceProperties = Map.of( | ||
PutDatasourceRequest.ENDPOINT_FIELD.getPreferredName(), | ||
Ip2GeoDataServer.getEndpointCountry() | ||
); | ||
|
||
// Create datasource and wait for it to be available | ||
createDatasource(datasourceName, datasourceProperties); | ||
isDatasourceCreated = true; | ||
waitForDatasourceToBeAvailable(datasourceName, Duration.ofSeconds(10)); | ||
|
||
int updateIntervalInDays = 1; | ||
updateDatasourceEndpoint(datasourceName, Ip2GeoDataServer.getEndpointCity(), updateIntervalInDays); | ||
List<Map<String, Object>> datasources = (List<Map<String, Object>>) getDatasource(datasourceName).get("datasources"); | ||
|
||
assertEquals(Ip2GeoDataServer.getEndpointCity(), datasources.get(0).get("endpoint")); | ||
assertEquals(updateIntervalInDays, datasources.get(0).get("update_interval_in_days")); | ||
} finally { | ||
if (isDatasourceCreated) { | ||
deleteDatasource(datasourceName, 3); | ||
} | ||
} | ||
} | ||
|
||
@SneakyThrows | ||
public void testUpdateDatasource_whenIncompatibleFields_thenFails() { | ||
boolean isDatasourceCreated = false; | ||
String datasourceName = PREFIX + GeospatialTestHelper.randomLowerCaseString(); | ||
try { | ||
Map<String, Object> datasourceProperties = Map.of( | ||
PutDatasourceRequest.ENDPOINT_FIELD.getPreferredName(), | ||
Ip2GeoDataServer.getEndpointCity() | ||
); | ||
|
||
// Create datasource and wait for it to be available | ||
createDatasource(datasourceName, datasourceProperties); | ||
isDatasourceCreated = true; | ||
waitForDatasourceToBeAvailable(datasourceName, Duration.ofSeconds(10)); | ||
|
||
// Update should fail as country data does not have every fields that city data has | ||
int updateIntervalInDays = 1; | ||
ResponseException exception = expectThrows( | ||
ResponseException.class, | ||
() -> updateDatasourceEndpoint(datasourceName, Ip2GeoDataServer.getEndpointCountry(), updateIntervalInDays) | ||
); | ||
assertEquals(RestStatus.BAD_REQUEST.getStatus(), exception.getResponse().getStatusLine().getStatusCode()); | ||
} finally { | ||
if (isDatasourceCreated) { | ||
deleteDatasource(datasourceName, 3); | ||
} | ||
} | ||
} | ||
|
||
private void updateDatasourceEndpoint(final String datasourceName, final String endpoint, final int updateInterval) throws IOException { | ||
Map<String, Object> properties = Map.of( | ||
UpdateDatasourceRequest.ENDPOINT_FIELD.getPreferredName(), | ||
endpoint, | ||
UpdateDatasourceRequest.UPDATE_INTERVAL_IN_DAYS_FIELD.getPreferredName(), | ||
updateInterval | ||
); | ||
updateDatasource(datasourceName, properties); | ||
} | ||
} |
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.