Repository navigation
fix: add getParentEntity to QueryRepository for isOwner() policy evaluation #31992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhang-arvin
wants to merge
3
commits into
open-metadata:main
Choose a base branch
from
zhang-arvin:fix/issue-31717-query-parententity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...service/src/test/java/org/openmetadata/service/jdbi3/QueryRepositoryParentEntityTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * Copyright 2024 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.service.jdbi3; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.MockedStatic; | ||
| import org.openmetadata.schema.EntityInterface; | ||
| import org.openmetadata.schema.entity.data.Query; | ||
| import org.openmetadata.schema.entity.services.DatabaseService; | ||
| import org.openmetadata.schema.type.EntityReference; | ||
| import org.openmetadata.schema.type.Include; | ||
| import org.openmetadata.service.Entity; | ||
|
|
||
| /** | ||
| * Regression coverage for {@link QueryRepository#getParentEntity(Query, String)}. | ||
| * | ||
| * <p>A Query's ownership-policy evaluation uses its parent (the DatabaseService it belongs to) to | ||
| * resolve inherited owners. The override must (1) return {@code null} when the Query carries no | ||
| * service reference, so policy evaluation degrades gracefully instead of throwing an NPE, and (2) | ||
| * resolve the referenced service — with {@link Include#ALL} — when the reference is present. | ||
| */ | ||
| class QueryRepositoryParentEntityTest { | ||
|
|
||
| private static final String FIELDS = "owners"; | ||
|
|
||
| private MockedStatic<Entity> mockedEntity; | ||
| private CollectionDAO collectionDAO; | ||
| private DatabaseService service; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| collectionDAO = mock(CollectionDAO.class); | ||
| when(collectionDAO.queryDAO()).thenReturn(mock(CollectionDAO.QueryDAO.class)); | ||
|
|
||
| service = | ||
| new DatabaseService() | ||
| .withId(UUID.randomUUID()) | ||
| .withName("mysql_prod") | ||
| .withFullyQualifiedName("mysql_prod"); | ||
|
|
||
| mockedEntity = mockStatic(Entity.class); | ||
| mockedEntity.when(Entity::getCollectionDAO).thenReturn(collectionDAO); | ||
| // EntityRepository's constructor derives allowedFields from Entity.getEntityFields(entityClass) | ||
| // and validates the patch/put field strings against it. mockStatic(Entity.class) stubs every | ||
| // static method, so without this stub getEntityFields() returns an empty set and | ||
| // 'new QueryRepository()' throws IllegalArgumentException: Invalid field name queryUsedIn. | ||
| mockedEntity | ||
| .when(() -> Entity.getEntityFields(any())) | ||
| .thenReturn(Set.of("users", "query", "queryUsedIn", "processedLineage")); | ||
| mockedEntity | ||
| .when(() -> Entity.getEntity(any(EntityReference.class), eq(FIELDS), eq(Include.ALL))) | ||
| .thenReturn(service); | ||
| } | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| mockedEntity.close(); | ||
| } | ||
|
|
||
| @Test | ||
| void getParentEntity_returnsNull_whenQueryHasNoService() { | ||
| Query query = new Query().withId(UUID.randomUUID()).withName("q").withQuery("SELECT 1"); | ||
|
|
||
| EntityInterface parent = new QueryRepository().getParentEntity(query, FIELDS); | ||
|
|
||
| assertNull( | ||
| parent, | ||
| "A Query with no service must not resolve a parent, and must not throw during " | ||
| + "ownership-policy evaluation"); | ||
| } | ||
|
|
||
| @Test | ||
| void getParentEntity_resolvesServiceWithIncludeAll_whenServicePresent() { | ||
| EntityReference serviceRef = | ||
| new EntityReference() | ||
| .withId(service.getId()) | ||
| .withType(Entity.DATABASE_SERVICE) | ||
| .withName(service.getName()) | ||
| .withFullyQualifiedName(service.getFullyQualifiedName()); | ||
| Query query = | ||
| new Query() | ||
| .withId(UUID.randomUUID()) | ||
| .withName("q") | ||
| .withQuery("SELECT 1") | ||
| .withService(serviceRef); | ||
|
|
||
| EntityInterface parent = new QueryRepository().getParentEntity(query, FIELDS); | ||
|
|
||
| assertSame( | ||
| service, | ||
| parent, | ||
| "The Query's service reference must be resolved as the parent entity"); | ||
| assertEquals("mysql_prod", parent.getName()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This override changes Query parent resolution for ownership-policy evaluation, but the PR adds no test covering the null-service and resolved-service paths. Without focused coverage, the ownership-policy regression fixed here can return unnoticed.
Context Used: CLAUDE.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!