Skip to content

Commit

Permalink
Update based on review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <heemin@amazon.com>
  • Loading branch information
heemin32 committed Apr 20, 2023
1 parent 0962e9a commit b291c23
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,32 @@ private void validateEndpoint(final ActionRequestValidationException errors) {
}

private void validateManifestFile(final URL url, final ActionRequestValidationException errors) {
DatasourceManifest manifest;
try {
manifest = DatasourceManifest.Builder.build(url);
} catch (Exception e) {
log.info("Error occurred while reading a file from {}", url, e);
errors.addValidationError(String.format(Locale.ROOT, "Error occurred while reading a file from %s", url));
return;
}

try {
DatasourceManifest manifest = DatasourceManifest.Builder.build(url);
new URL(manifest.getUrl()).toURI(); // Validate URL complies with RFC-2396
if (manifest.getValidForInDays() <= updateIntervalInDays.days()) {
errors.addValidationError(
String.format(
Locale.ROOT,
"updateInterval %d is should be smaller than %d",
updateIntervalInDays.days(),
manifest.getValidForInDays()
)
);
}
} catch (MalformedURLException | URISyntaxException e) {
log.info("Invalid URL format is provided for url field in the manifest file", e);
errors.addValidationError("Invalid URL format is provided for url field in the manifest file");
} catch (Exception e) {
log.info("Error occurred while reading a file from {}", url, e);
errors.addValidationError(String.format(Locale.ROOT, "Error occurred while reading a file from %s", url));
return;
}

if (manifest.getValidForInDays() <= updateIntervalInDays.days()) {
errors.addValidationError(
String.format(
Locale.ROOT,
"updateInterval %d is should be smaller than %d",
updateIntervalInDays.days(),
manifest.getValidForInDays()
)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,18 @@ public static void getDatasource(final Client client, final String id, final Act
public void onResponse(final GetResponse response) {
if (!response.isExists()) {
actionListener.onResponse(null);
} else {
try {
XContentParser parser = XContentHelper.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
response.getSourceAsBytesRef()
);
actionListener.onResponse(Datasource.PARSER.parse(parser, null));
} catch (IOException e) {
actionListener.onFailure(e);
}
return;
}

try {
XContentParser parser = XContentHelper.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
response.getSourceAsBytesRef()
);
actionListener.onResponse(Datasource.PARSER.parse(parser, null));
} catch (IOException e) {
actionListener.onFailure(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import java.util.ArrayList;
import java.util.Locale;

import org.opensearch.common.Randomness;
import org.opensearch.geospatial.GeospatialTestHelper;
import org.opensearch.geospatial.ip2geo.common.DatasourceManifest;
import org.opensearch.test.OpenSearchTestCase;

public class DatasourceTests extends OpenSearchTestCase {
public void testCurrentIndexName() {
String id = "test";
String id = GeospatialTestHelper.randomLowerCaseString();
Instant now = Instant.now();
Datasource datasource = new Datasource();
datasource.setId(id);
Expand All @@ -33,11 +35,11 @@ public void testCurrentIndexName() {
}

public void testGetIndexNameFor() {
long updatedAt = 123123123l;
long updatedAt = Randomness.get().nextLong();
DatasourceManifest manifest = mock(DatasourceManifest.class);
when(manifest.getUpdatedAt()).thenReturn(updatedAt);

String id = "test";
String id = GeospatialTestHelper.randomLowerCaseString();
Datasource datasource = new Datasource();
datasource.setId(id);
assertEquals(
Expand Down

0 comments on commit b291c23

Please sign in to comment.