Skip to content

Commit

Permalink
Make some fields in manifest file as mandatory (opensearch-project#289)
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <[email protected]>
  • Loading branch information
heemin32 committed Jul 21, 2023
1 parent 6d5ffa5 commit 82e3430
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private void validateManifestFile(final URL url, final ActionRequestValidationEx
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));
errors.addValidationError(String.format(Locale.ROOT, "Error occurred while reading a file from %s: %s", url, e.getMessage()));
return;
}

Expand All @@ -188,7 +188,7 @@ private void validateManifestFile(final URL url, final ActionRequestValidationEx
return;
}

if (updateInterval.days() >= manifest.getValidForInDays()) {
if (manifest.getValidForInDays() != null && updateInterval.days() >= manifest.getValidForInDays()) {
errors.addValidationError(
String.format(
Locale.ROOT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ public class DatasourceManifest {
}
);
static {
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), URL_FIELD);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), DB_NAME_FIELD);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), SHA256_HASH_FIELD);
PARSER.declareString(ConstructingObjectParser.constructorArg(), URL_FIELD);
PARSER.declareString(ConstructingObjectParser.constructorArg(), DB_NAME_FIELD);
PARSER.declareString(ConstructingObjectParser.constructorArg(), SHA256_HASH_FIELD);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), VALID_FOR_IN_DAYS_FIELD);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), UPDATED_AT_FIELD);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), PROVIDER_FIELD);
PARSER.declareLong(ConstructingObjectParser.constructorArg(), UPDATED_AT_FIELD);
PARSER.declareString(ConstructingObjectParser.constructorArg(), PROVIDER_FIELD);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class PutDatasourceRequestTests extends Ip2GeoTestCase {

public void testValidateWithInvalidUrl() {
public void testValidate_whenInvalidUrl_thenFails() {
String datasourceName = GeospatialTestHelper.randomLowerCaseString();
PutDatasourceRequest request = new PutDatasourceRequest(datasourceName);
request.setEndpoint("invalidUrl");
Expand All @@ -33,29 +33,26 @@ public void testValidateWithInvalidUrl() {
assertEquals("Invalid URL format is provided", exception.validationErrors().get(0));
}

public void testValidateWithInvalidManifestFile() {
public void testValidate_whenInvalidManifestFile_thenFails() {
String datasourceName = GeospatialTestHelper.randomLowerCaseString();
String domain = GeospatialTestHelper.randomLowerCaseString();
PutDatasourceRequest request = new PutDatasourceRequest(datasourceName);
request.setEndpoint(String.format(Locale.ROOT, "https://%s.com", domain));
request.setUpdateInterval(TimeValue.timeValueDays(1));
ActionRequestValidationException exception = request.validate();
assertEquals(1, exception.validationErrors().size());
assertEquals(
String.format(Locale.ROOT, "Error occurred while reading a file from %s", request.getEndpoint()),
exception.validationErrors().get(0)
);
assertTrue(exception.validationErrors().get(0).contains("Error occurred while reading a file"));
}

public void testValidate() throws Exception {
public void testValidate_whenValidInput_thenSucceed() throws Exception {
String datasourceName = GeospatialTestHelper.randomLowerCaseString();
PutDatasourceRequest request = new PutDatasourceRequest(datasourceName);
request.setEndpoint(sampleManifestUrl());
request.setUpdateInterval(TimeValue.timeValueDays(1));
assertNull(request.validate());
}

public void testValidateWithZeroUpdateInterval() throws Exception {
public void testValidate_whenZeroUpdateInterval_thenFails() throws Exception {
String datasourceName = GeospatialTestHelper.randomLowerCaseString();
PutDatasourceRequest request = new PutDatasourceRequest(datasourceName);
request.setEndpoint(sampleManifestUrl());
Expand All @@ -72,7 +69,7 @@ public void testValidateWithZeroUpdateInterval() throws Exception {
);
}

public void testValidateWithLargeUpdateInterval() throws Exception {
public void testValidate_whenLargeUpdateInterval_thenFail() throws Exception {
String datasourceName = GeospatialTestHelper.randomLowerCaseString();
PutDatasourceRequest request = new PutDatasourceRequest(datasourceName);
request.setEndpoint(sampleManifestUrl());
Expand All @@ -86,7 +83,7 @@ public void testValidateWithLargeUpdateInterval() throws Exception {
assertTrue(exception.validationErrors().get(0).contains("should be smaller"));
}

public void testValidateWithInvalidUrlInsideManifest() throws Exception {
public void testValidate_whenInvalidUrlInsideManifest_thenFail() throws Exception {
String datasourceName = GeospatialTestHelper.randomLowerCaseString();
PutDatasourceRequest request = new PutDatasourceRequest(datasourceName);
request.setEndpoint(sampleManifestUrlWithInvalidUrl());
Expand All @@ -100,7 +97,7 @@ public void testValidateWithInvalidUrlInsideManifest() throws Exception {
assertTrue(exception.validationErrors().get(0).contains("Invalid URL format"));
}

public void testValidateDatasourceNames() throws Exception {
public void testValidate_whenInvalidDatasourceNames_thenFails() throws Exception {
String validDatasourceName = GeospatialTestHelper.randomLowerCaseString();
String domain = GeospatialTestHelper.randomLowerCaseString();
PutDatasourceRequest request = new PutDatasourceRequest(validDatasourceName);
Expand Down Expand Up @@ -154,7 +151,7 @@ public void testValidateDatasourceNames() throws Exception {
}
}

public void testStreamInOut() throws Exception {
public void testStreamInOut_whenValidInput_thenSucceed() throws Exception {
String datasourceName = GeospatialTestHelper.randomLowerCaseString();
String domain = GeospatialTestHelper.randomLowerCaseString();
PutDatasourceRequest request = new PutDatasourceRequest(datasourceName);
Expand Down

0 comments on commit 82e3430

Please sign in to comment.