Skip to content

Commit

Permalink
Merge "REST: Use referenced-resource-not-found for the non-existent…
Browse files Browse the repository at this point in the history
… property"
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Sep 17, 2024
2 parents d833f79 + f45c0a0 commit 5a3c6b8
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 4 deletions.
3 changes: 2 additions & 1 deletion repo/rest-api/specs/global/responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@
"invalid-value": { "$ref": "./examples.json#/InvalidValueExample" },
"missing-field": { "$ref": "./examples.json#/MissingFieldExample" },
"value-too-long": { "$ref": "./examples.json#/ValueTooLongExample" },
"resource-too-large": { "$ref": "./examples.json#/ResourceTooLargeExample" }
"resource-too-large": { "$ref": "./examples.json#/ResourceTooLargeExample" },
"referenced-resource-not-found": { "$ref": "./examples.json#/ReferencedResourceNotFoundExample" }
}
}
},
Expand Down
5 changes: 4 additions & 1 deletion repo/rest-api/specs/resources/statements/responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"cannot-modify-read-only-value": {
"$ref": "../../global/examples.json#/CannotModifyReadOnlyValue"
},
"resource-too-large": { "$ref": "../../global/examples.json#/ResourceTooLargeExample" }
"resource-too-large": { "$ref": "../../global/examples.json#/ResourceTooLargeExample" },
"referenced-resource-not-found": { "$ref": "../../global/examples.json#/ReferencedResourceNotFoundExample" }
}
}
},
Expand Down Expand Up @@ -199,6 +200,7 @@
"cannot-modify-read-only-value": {
"$ref": "../../global/examples.json#/CannotModifyReadOnlyValue"
},
"referenced-resource-not-found": { "$ref": "../../global/examples.json#/ReferencedResourceNotFoundExample" },
"resource-too-large": { "$ref": "../../global/examples.json#/ResourceTooLargeExample" }
}
}
Expand Down Expand Up @@ -391,6 +393,7 @@
"cannot-modify-read-only-value": {
"$ref": "../../global/examples.json#/CannotModifyReadOnlyValue"
},
"referenced-resource-not-found": { "$ref": "../../global/examples.json#/ReferencedResourceNotFoundExample" },
"resource-too-large": { "$ref": "../../global/examples.json#/ResourceTooLargeExample" }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ private function handleStatementsValidationErrors( ValidationError $validationEr
case StatementValidator::CODE_INVALID_FIELD:
throw UseCaseError::newInvalidValue( $context[StatementValidator::CONTEXT_PATH] );
case StatementValidator::CODE_PROPERTY_NOT_FOUND:
throw UseCaseError::newReferencedResourceNotFound( $context[StatementValidator::CONTEXT_PATH] );
case StatementValidator::CODE_INVALID_FIELD_TYPE:
throw UseCaseError::newInvalidValue( $context[StatementValidator::CONTEXT_PATH] );
case StatementValidator::CODE_MISSING_FIELD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function validateAndDeserialize( StatementSerializationRequest $request )
$context = $validationError->getContext();
switch ( $validationError->getCode() ) {
case StatementValidator::CODE_PROPERTY_NOT_FOUND:
throw UseCaseError::newReferencedResourceNotFound( $context[StatementValidator::CONTEXT_PATH] );
case StatementValidator::CODE_INVALID_FIELD:
throw UseCaseError::newInvalidValue( $context[StatementValidator::CONTEXT_PATH] );
case StatementValidator::CODE_MISSING_FIELD:
Expand Down
12 changes: 12 additions & 0 deletions repo/rest-api/src/RouteHandlers/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,9 @@
"cannot-modify-read-only-value": {
"$ref": "#/components/examples/CannotModifyReadOnlyValue"
},
"referenced-resource-not-found": {
"$ref": "#/components/examples/ReferencedResourceNotFoundExample"
},
"resource-too-large": {
"$ref": "#/components/examples/ResourceTooLargeExample"
}
Expand Down Expand Up @@ -3110,6 +3113,9 @@
"cannot-modify-read-only-value": {
"$ref": "#/components/examples/CannotModifyReadOnlyValue"
},
"referenced-resource-not-found": {
"$ref": "#/components/examples/ReferencedResourceNotFoundExample"
},
"resource-too-large": {
"$ref": "#/components/examples/ResourceTooLargeExample"
}
Expand Down Expand Up @@ -3419,6 +3425,9 @@
},
"resource-too-large": {
"$ref": "#/components/examples/ResourceTooLargeExample"
},
"referenced-resource-not-found": {
"$ref": "#/components/examples/ReferencedResourceNotFoundExample"
}
}
}
Expand Down Expand Up @@ -4628,6 +4637,9 @@
},
"resource-too-large": {
"$ref": "#/components/examples/ResourceTooLargeExample"
},
"referenced-resource-not-found": {
"$ref": "#/components/examples/ReferencedResourceNotFoundExample"
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions repo/rest-api/tests/mocha/api-testing/AddItemStatementTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,65 @@ describe( newAddItemStatementRequestBuilder().getRouteDescription(), () => {

assertValidError( response, 400, 'invalid-value', { path: '/statement' } );
} );

it( 'non-existent statement property id', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue( nonExistentProperty );

const response = await newAddItemStatementRequestBuilder( testItemId, statement )
.assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: '/statement/property/id' }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );

it( 'qualifier with non-existent property', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue(
( await entityHelper.createUniqueStringProperty() ).entity.id
);
statement.qualifiers = [
{ property: { id: nonExistentProperty }, value: { type: 'novalue' } }
];

const response = await newAddItemStatementRequestBuilder( testItemId, statement )
.assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: '/statement/qualifiers/0/property/id' }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );

it( 'reference with non-existent property', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue(
( await entityHelper.createUniqueStringProperty() ).entity.id
);
statement.references = [];
statement.references[ 0 ] = {
parts: [ { property: { id: nonExistentProperty }, value: { type: 'novalue' } } ]
};

const response = await newAddItemStatementRequestBuilder( testItemId, statement )
.assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: '/statement/references/0/parts/0/property/id' }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );
} );

describe( '404 error response', () => {
Expand Down
59 changes: 59 additions & 0 deletions repo/rest-api/tests/mocha/api-testing/AddPropertyStatementTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,65 @@ describe( newAddPropertyStatementRequestBuilder().getRouteDescription(), () => {

assertValidError( response, 400, 'invalid-value', { path: '/statement' } );
} );

it( 'non-existent statement property id', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue( nonExistentProperty );

const response = await newAddPropertyStatementRequestBuilder( testPropertyId, statement )
.assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: '/statement/property/id' }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );

it( 'qualifier with non-existent property', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue(
( await entityHelper.createUniqueStringProperty() ).entity.id
);
statement.qualifiers = [
{ property: { id: nonExistentProperty }, value: { type: 'novalue' } }
];

const response = await newAddPropertyStatementRequestBuilder( testPropertyId, statement )
.assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: '/statement/qualifiers/0/property/id' }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );

it( 'reference with non-existent property', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue(
( await entityHelper.createUniqueStringProperty() ).entity.id
);
statement.references = [];
statement.references[ 0 ] = {
parts: [ { property: { id: nonExistentProperty }, value: { type: 'novalue' } } ]
};

const response = await newAddPropertyStatementRequestBuilder( testPropertyId, statement )
.assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: '/statement/references/0/parts/0/property/id' }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );
} );

describe( '404 error response', () => {
Expand Down
62 changes: 62 additions & 0 deletions repo/rest-api/tests/mocha/api-testing/CreateItemTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,68 @@ describe( newCreateItemRequestBuilder().getRouteDescription(), () => {
assert.equal( response.body.message, "Statement's Property ID does not match the statement group key" );
} );

it( 'non-existent statement property id', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue( nonExistentProperty );

const response = await newCreateItemRequestBuilder( {
statements: { [ nonExistentProperty ]: [ statement ] }
} ).assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: `/item/statements/${nonExistentProperty}/0/property/id` }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );

it( 'statement qualifier with non-existent property', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue(
predicatePropertyId
);
statement.qualifiers = [
{ property: { id: nonExistentProperty }, value: { type: 'novalue' } }
];

const response = await newCreateItemRequestBuilder( {
statements: { [ predicatePropertyId ]: [ statement ] }
} ).assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: `/item/statements/${predicatePropertyId}/0/qualifiers/0/property/id` }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );

it( 'statement reference with non-existent property', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue(
predicatePropertyId
);
statement.references = [];
statement.references[ 0 ] = {
parts: [ { property: { id: nonExistentProperty }, value: { type: 'novalue' } } ]
};

const response = await newCreateItemRequestBuilder( {
statements: { [ predicatePropertyId ]: [ statement ] }
} ).assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: `/item/statements/${predicatePropertyId}/0/references/0/parts/0/property/id` }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );

it( 'invalid site ID', async () => {
const invalidSiteId = 'not-a-valid-site-id';
const response = await newCreateItemRequestBuilder( {
Expand Down
38 changes: 38 additions & 0 deletions repo/rest-api/tests/mocha/api-testing/ReplaceItemStatementTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,44 @@ describe( 'PUT statement tests', () => {
assert.strictEqual( response.body.message, 'Required field missing' );
} );

it( 'qualifier with non-existent property', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue( predicatePropertyId );
statement.qualifiers = [
{ property: { id: nonExistentProperty }, value: { type: 'novalue' } }
];

const response = await newReplaceRequestBuilder( testItemId, testStatementId, statement )
.assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: '/statement/qualifiers/0/property/id' }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );

it( 'reference with non-existent property', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue( predicatePropertyId );
statement.references = [];
statement.references[ 0 ] = {
parts: [ { property: { id: nonExistentProperty }, value: { type: 'novalue' } } ]
};

const response = await newReplaceRequestBuilder( testItemId, testStatementId, statement )
.assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: '/statement/references/0/parts/0/property/id' }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );
} );

describe( '404 error response', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,44 @@ describe( 'PUT statement tests', () => {
assert.strictEqual( response.body.message, 'Required field missing' );
} );

it( 'qualifier with non-existent property', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue( testStatementPropertyId );
statement.qualifiers = [
{ property: { id: nonExistentProperty }, value: { type: 'novalue' } }
];

const response = await newReplaceRequestBuilder( testPropertyId, testStatementId, statement )
.assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: '/statement/qualifiers/0/property/id' }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );

it( 'reference with non-existent property', async () => {
const nonExistentProperty = 'P9999999';
const statement = entityHelper.newStatementWithRandomStringValue( testStatementPropertyId );
statement.references = [];
statement.references[ 0 ] = {
parts: [ { property: { id: nonExistentProperty }, value: { type: 'novalue' } } ]
};

const response = await newReplaceRequestBuilder( testPropertyId, testStatementId, statement )
.assertValidRequest().makeRequest();

assertValidError(
response,
400,
'referenced-resource-not-found',
{ path: '/statement/references/0/parts/0/property/id' }
);
assert.strictEqual( response.body.message, 'The referenced resource does not exist' );
} );
} );

describe( '404 error response', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public function itemStatementsValidationErrorProvider(): Generator {
StatementValidator::CONTEXT_VALUE => 'non-existing-property-id',
]
),
UseCaseError::newInvalidValue( '/some-path-to-non-existing-property' ),
UseCaseError::newReferencedResourceNotFound( '/some-path-to-non-existing-property' ),
];
}

Expand Down
Loading

0 comments on commit 5a3c6b8

Please sign in to comment.