Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2025 Collate
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openmetadata.it.tests;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.openmetadata.it.util.SdkClients;
import org.openmetadata.sdk.exceptions.OpenMetadataException;
import org.openmetadata.sdk.network.HttpMethod;
import org.openmetadata.sdk.network.RequestOptions;

/**
* {@code GET /v1/queryCostRecord/{id}} resolves the record and then dereferences it to scope its
* authorization check at {@code costRecord.getQueryReference().getId()}. Because
* {@code EntityTimeSeriesRepository.getById} answers a missing row with {@code null}, an id with no
* row used to raise a NullPointerException that the exception mapper rendered as a 500.
*/
@Execution(ExecutionMode.CONCURRENT)
public class QueryCostRecordResourceIT {

@Test
void byIdEndpointReturnsNotFoundForUnknownId() {
OpenMetadataException error =
assertThrows(
OpenMetadataException.class,
() ->
SdkClients.adminClient()
.getHttpClient()
.executeForString(
HttpMethod.GET,
"/v1/queryCostRecord/" + UUID.randomUUID(),
null,
RequestOptions.builder().build()));
assertEquals(
404, error.getStatusCode(), "an id with no row must be a 404, not a NullPointerException");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package org.openmetadata.it.tests;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.databind.DeserializationFeature;
Expand Down Expand Up @@ -46,6 +48,7 @@
import org.openmetadata.schema.type.Column;
import org.openmetadata.schema.type.ColumnDataType;
import org.openmetadata.sdk.client.OpenMetadataClient;
import org.openmetadata.sdk.exceptions.OpenMetadataException;
import org.openmetadata.sdk.models.ListParams;
import org.openmetadata.sdk.network.HttpMethod;
import org.openmetadata.sdk.network.RequestOptions;
Expand All @@ -59,6 +62,10 @@
* missing made {@code GET /dataQuality/testCases/testCaseIncidentStatus/stateId/{stateId}} fail with
* {@code EntityRelationshipNotFoundException}, and deleting the rows left both the relationship and
* the stale {@code incidentId} on the test case behind.
*
* <p>The by-id sibling has the same exposure from the other direction: an id with no row at all —
* from a stale {@code incidentId}, or from a caller passing a stateId where a record id belongs —
* must answer 404 rather than dereferencing a null record into a 500.
*/
@Execution(ExecutionMode.CONCURRENT)
@ExtendWith(TestNamespaceExtension.class)
Expand Down Expand Up @@ -179,6 +186,85 @@ void deleteByIdIgnoresUnknownId() {
resolutionStatusRepository().deleteById(UUID.randomUUID(), true);
}

@Test
void byIdEndpointReturnsNotFoundForUnknownId() {
OpenMetadataException error =
assertThrows(
OpenMetadataException.class,
() -> SdkClients.adminClient().testCaseResolutionStatuses().get(UUID.randomUUID()));
assertEquals(
404, error.getStatusCode(), "an id with no row must be a 404, not a NullPointerException");
}

/**
* The production trigger: {@code TestCase.incidentId} carries the incident's stateId, so a client
* that feeds it to the by-id endpoint always misses the record table.
*/
@Test
void byIdEndpointReturnsNotFoundForStateId(TestNamespace ns) throws Exception {
Table table = createTable(ns, "byIdState");
TestCase testCase = createTestCase(table, "byIdStateCase_" + ns.uniqueShortId());
TestCaseResolutionStatus incident = createIncidentRecord(testCase);

assertNotEquals(
incident.getId(), incident.getStateId(), "stateId and record id must be distinct ids");
OpenMetadataException error =
assertThrows(
OpenMetadataException.class,
() -> SdkClients.adminClient().testCaseResolutionStatuses().get(incident.getStateId()));
assertEquals(404, error.getStatusCode(), "a stateId is not a record id, so it must 404");
}

/** The not-found guard must not swallow a record that does exist. */
@Test
void byIdEndpointReturnsHealthyRecord(TestNamespace ns) throws Exception {
Table table = createTable(ns, "byIdOk");
TestCase testCase = createTestCase(table, "byIdOkCase_" + ns.uniqueShortId());
TestCaseResolutionStatus incident = createIncidentRecord(testCase);

TestCaseResolutionStatus fetched =
SdkClients.adminClient().testCaseResolutionStatuses().get(incident.getId());

assertEquals(incident.getId(), fetched.getId());
assertNotNull(fetched.getTestCaseReference(), "testCaseReference must be resolved");
assertEquals(testCase.getId(), fetched.getTestCaseReference().getId());
}

/**
* The write path shares the same guard, and regressing it would 500 rather than 404 exactly as the
* read path did. The record is resolved before the patch is applied, so an empty patch is enough.
*/
@Test
void patchByIdReturnsNotFoundForUnknownId() {
OpenMetadataException error =
assertThrows(
OpenMetadataException.class,
() ->
SdkClients.adminClient()
.testCaseResolutionStatuses()
.patch(UUID.randomUUID(), MAPPER.createArrayNode()));
assertEquals(
404, error.getStatusCode(), "an id with no row must be a 404, not a NullPointerException");
}

@Test
void patchByIdReturnsNotFoundForStateId(TestNamespace ns) throws Exception {
Table table = createTable(ns, "patchState");
TestCase testCase = createTestCase(table, "patchStateCase_" + ns.uniqueShortId());
TestCaseResolutionStatus incident = createIncidentRecord(testCase);

assertNotEquals(
incident.getId(), incident.getStateId(), "stateId and record id must be distinct ids");
OpenMetadataException error =
assertThrows(
OpenMetadataException.class,
() ->
SdkClients.adminClient()
.testCaseResolutionStatuses()
.patch(incident.getStateId(), MAPPER.createArrayNode()));
assertEquals(404, error.getStatusCode(), "a stateId is not a record id, so it must 404");
}

private JsonNode readData(String response) throws Exception {
assertNotNull(response, "endpoint must not fail");
JsonNode data = MAPPER.readTree(response).get("data");
Expand All @@ -188,6 +274,11 @@ private JsonNode readData(String response) throws Exception {

/** Creates a real incident by ingesting a failing result, and returns its stateId. */
private UUID createIncident(TestCase testCase) {
return createIncidentRecord(testCase).getStateId();
}

/** Creates a real incident by ingesting a failing result, and returns the opened record. */
private TestCaseResolutionStatus createIncidentRecord(TestCase testCase) {
OpenMetadataClient client = SdkClients.adminClient();
CreateTestCaseResult result =
new CreateTestCaseResult()
Expand All @@ -199,8 +290,9 @@ private UUID createIncident(TestCase testCase) {
TestCaseResolutionStatus latest =
resolutionStatusRepository().getLatestRecord(testCase.getFullyQualifiedName());
assertNotNull(latest, "a failing result should have opened an incident");
assertNotNull(latest.getId());
assertNotNull(latest.getStateId());
return latest.getStateId();
return latest;
}

/** Simulates the production corruption: rows survive but lose their parentOf relationship. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.openmetadata.schema.utils.JsonUtils;
import org.openmetadata.schema.utils.ResultList;
import org.openmetadata.service.Entity;
import org.openmetadata.service.exception.CatalogExceptionMessage;
import org.openmetadata.service.exception.EntityNotFoundException;
import org.openmetadata.service.search.SearchAggregation;
import org.openmetadata.service.search.SearchAggregationNode;
Expand Down Expand Up @@ -385,6 +386,21 @@ public T getLatestRecord(String recordFQN, String extension) {
return entityRecord;
}

/**
* {@link #getById} answers a missing row with {@code null}, which a resource that has to
* dereference the record — to authorize against the entity it belongs to, say — turns into a
* NullPointerException and a 500. Callers that want the read to fail rather than degrade should
* use this instead, so the miss surfaces as a 404.
*/
public T getByIdOrNotFound(UUID id) {
T entityRecord = getById(id);
if (entityRecord == null) {
throw EntityNotFoundException.byMessage(
CatalogExceptionMessage.entityNotFound(entityType, id));
}
return entityRecord;
}

public T getById(UUID id) {
String jsonRecord = timeSeriesDao.getById(id);
if (jsonRecord == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,18 @@ public ResultList<TestCaseResolutionStatus> listForStateId(
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = TestCaseResolutionStatus.class)))
schema = @Schema(implementation = TestCaseResolutionStatus.class))),
@ApiResponse(
responseCode = "404",
description = "Test case failure status for instance {id} is not found")
})
public TestCaseResolutionStatus get(
@Context SecurityContext securityContext,
@Parameter(description = "Test Case Failure Status ID", schema = @Schema(type = "UUID"))
@PathParam("id")
UUID testCaseResolutionStatusId) {
TestCaseResolutionStatus testCaseResolutionStatus =
repository.getById(testCaseResolutionStatusId);
repository.getByIdOrNotFound(testCaseResolutionStatusId);
TestCase testCase =
Entity.getEntityByName(
Entity.TEST_CASE,
Expand Down Expand Up @@ -615,7 +618,7 @@ public Response patch(
}))
JsonPatch patch) {

TestCaseResolutionStatus testCaseResolutionStatus = repository.getById(id);
TestCaseResolutionStatus testCaseResolutionStatus = repository.getByIdOrNotFound(id);
TestCase testCase =
Entity.getEntityByName(
Entity.TEST_CASE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ public QueryCostResource(Authorizer authorizer) {
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = QueryCostRecord.class)))
schema = @Schema(implementation = QueryCostRecord.class))),
@ApiResponse(
responseCode = "404",
description = "Query cost record for instance {id} is not found")
})
public QueryCostRecord get(
@Context SecurityContext securityContext,
@Parameter(description = "Get query cost record by id", schema = @Schema(type = "UUID"))
@PathParam("id")
UUID testCaseResolutionStatusId) {
QueryCostRecord costRecord = repository.getById(testCaseResolutionStatusId);
UUID id) {
QueryCostRecord costRecord = repository.getByIdOrNotFound(id);
OperationContext queryCostOperationContext =
new OperationContext(Entity.QUERY, MetadataOperation.VIEW_ALL);
ResourceContextInterface queryResourceContext =
Expand Down
Loading