Commit 7f9980c7 authored by Matt Wise's avatar Matt Wise
Browse files

Better error handling/response messages. Fix schema name validation

commit 933d041e
Author: Matt Wise <wsmatth@amazon.com>
Date: Fri Oct 23 2020 10:47:13 GMT-0500 (Central Daylight Time)

    better error response on get records call

commit d88f7c08
Author: Matt Wise <wsmatth@amazon.com>
Date: Fri Oct 23 2020 10:41:48 GMT-0500 (Central Daylight Time)

    add sample request/response

commit 436cd126
Author: Matt Wise <wsmatth@amazon.com>
Date: Fri Oct 23 2020 10:40:35 GMT-0500 (Central Daylight Time)

    better validation

commit a472fb7f
Author: Matt Wise <wsmatth@amazon.com>
Date: Thu Oct 22 2020 17:24:29 GMT-0500 (Central Daylight Time)

    validate schema kind is of dataset  registry
parent 01d848df
......@@ -20,6 +20,7 @@ public class DatasetRegistryValidationDoc {
// private constructor
}
public static final String INVALID_DATASET_REGISTRY_SCHEMA_KIND = "Invalid schema kind for dataset registry. Kind must be: '%s'";
public static final String MISSING_DATASET_PROPERTIES_VALIDATION = "DatasetProperties cannot be null";
public static final String MISSING_DATASET_REGISTRIES_ARRAY = "datasetRegistries cannot be empty";
public static final String MAX_DATASET_REGISTRIES_EXCEEDED = "Only 20 Dataset Registries can be ingested at a time";
......
package org.opengroup.osdu.datasetregistry.model;
import lombok.Data;
@Data
public class StorageExceptionResponse {
private Integer code;
private String reason;
private String message;
}
......@@ -20,6 +20,7 @@ import java.util.Map;
import javax.inject.Inject;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.http.HttpException;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.storage.MultiRecordIds;
......@@ -27,6 +28,7 @@ import org.opengroup.osdu.core.common.model.storage.Record;
import org.opengroup.osdu.core.common.model.storage.Schema;
import org.opengroup.osdu.core.common.model.storage.SchemaItem;
import org.opengroup.osdu.datasetregistry.model.DatasetRegistryValidationDoc;
import org.opengroup.osdu.datasetregistry.model.StorageExceptionResponse;
import org.opengroup.osdu.datasetregistry.response.CreateUpdateDatasetRegistryResponse;
import org.opengroup.osdu.datasetregistry.storage.CreateUpdateRecordsResponse;
import org.opengroup.osdu.datasetregistry.storage.GetRecordsResponse;
......@@ -66,8 +68,8 @@ public class DatasetRegistryServiceImpl implements DatasetRegistryService {
try {
storageResponse = storageService.createOrUpdateRecords(datasetRegistries);
} catch (StorageException e) {
throw new AppException(e.getHttpResponse().getResponseCode(),
HttpStatus.valueOf(e.getHttpResponse().getResponseCode()).getReasonPhrase(), e.getMessage());
StorageExceptionResponse body = e.getHttpResponse().parseBody(StorageExceptionResponse.class);
throw new AppException(body.getCode(), "Storage Service: " + body.getReason(), body.getMessage());
}
List<String> recordIds = storageResponse.getRecordIds();
......@@ -79,8 +81,8 @@ public class DatasetRegistryServiceImpl implements DatasetRegistryService {
getRecordsResponse = storageService.getRecords(multiRecordIds);
}
catch (StorageException e) {
throw new AppException(e.getHttpResponse().getResponseCode(),
HttpStatus.valueOf(e.getHttpResponse().getResponseCode()).getReasonPhrase(), e.getMessage());
StorageExceptionResponse body = e.getHttpResponse().parseBody(StorageExceptionResponse.class);
throw new AppException(body.getCode(), "Storage Service: " + body.getReason(), body.getMessage());
}
CreateUpdateDatasetRegistryResponse response = new CreateUpdateDatasetRegistryResponse(getRecordsResponse.getRecords());
......@@ -128,7 +130,10 @@ public class DatasetRegistryServiceImpl implements DatasetRegistryService {
private boolean validateDatasetRegistrySchema(Record datasetRegistry, Schema datasetRegistrySchema) {
//kind matches expected schema kind
datasetRegistry.getKind().equals(datasetRegistrySchema.getKind());
if (!datasetRegistry.getKind().equals(datasetRegistrySchema.getKind())) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(),
String.format(DatasetRegistryValidationDoc.INVALID_DATASET_REGISTRY_SCHEMA_KIND, datasetRegistrySchema.getKind()));
}
Map<String, Object> datasetRegistryData = datasetRegistry.getData();
......@@ -136,7 +141,7 @@ public class DatasetRegistryServiceImpl implements DatasetRegistryService {
for (SchemaItem schemaItem : datasetRegistrySchema.getSchema()) {
if (!datasetRegistryData.containsKey(schemaItem.getPath())) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(),
String.format(DatasetRegistryValidationDoc.DATASET_REGISTRY_MISSING_PROPERTY_VALIDATION_FORMAT, schemaItem.getPath()));
String.format(DatasetRegistryValidationDoc.DATASET_REGISTRY_MISSING_PROPERTY_VALIDATION_FORMAT, schemaItem.getPath()));
}
//TODO: Validate type of value matches
......
{
"datasetRegistries": [{
"kind": "opendes:osdu:dataset-registry:0.0.1",
"type": "dataset-registry",
"data": {
"ResourceID": "srn:osdu:file:dc556e0e3a554105a80cfcb19372a62d:",
"ResourceTypeID": "srn:type:file/json:",
"ResourceSecurityClassification": "srn:reference-data/ResourceSecurityClassification:RESTRICTED:",
"ResourceSource": "Some Company App",
"ResourceName": "trajectories - 1000.json",
"DatasetProperties": {
"FileSourceInfo": {
"FileSource": "",
"PreLoadFilePath": "s3://default_bucket/r1/data/provided/trajectories/1000.json"
}
}
},
"namespace": "opendes:osdu",
"legal": {
"legaltags": [
"opendes-public-usa-dataset-1"
],
"otherRelevantDataCountries": [
"US"
],
"status": "compliant"
},
"acl": {
"viewers": [
"data.default.viewers@opendes.testing.com"
],
"owners": [
"data.default.owners@opendes.testing.com"
]
}
}]
}
\ No newline at end of file
{
"datasetRegistries": [
{
"data": {
"ResourceID": "srn:osdu:file:dc556e0e3a554105a80cfcb19372a62d:",
"ResourceTypeID": "srn:type:file/json:",
"ResourceSecurityClassification": "srn:reference-data/ResourceSecurityClassification:RESTRICTED:",
"ResourceSource": "Some Company App",
"ResourceName": "trajectories - 1000.json",
"DatasetProperties": {
"FileSourceInfo": {
"FileSource": "",
"PreLoadFilePath": "s3://default_bucket/r1/data/provided/trajectories/1000.json"
}
}
},
"meta": [],
"id": "int-test-storage:doc:b27b263e9d404551804f5ac58a982ed6",
"version": 1602696602124745,
"kind": "int-test-storage:osdu:dataset-registry:0.0.1",
"acl": {
"viewers": [
"data.default.viewers@int-test-storage.testing.com"
],
"owners": [
"data.default.owners@int-test-storage.testing.com"
]
},
"legal": {
"legaltags": [
"opendes-1601488471382"
],
"otherRelevantDataCountries": [
"US"
],
"status": "compliant"
},
"createUser": "mattadmin@example.com",
"createTime": "2020-10-14T17:30:02.183Z"
}
]
}
\ No newline at end of file
{
"kind": "opendes:osdu:dataset-registry:0.0.1",
"schema": [
{
"path": "ResourceTypeID",
"kind": "string",
"ext": {}
},
{
"path": "ResourceID",
"kind": "string",
"ext": {}
},
{
"path": "ResourceSecurityClassification",
"kind": "string",
"ext": {}
},
{
"path": "ResourceName",
"kind": "string",
"ext": {}
},
{
"path": "ResourceDescription",
"kind": "string",
"ext": {}
},
{
"path": "ResourceSource",
"kind": "string",
"ext": {}
}
]
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment