mchades commented on code in PR #12625: URL: https://github.com/apache/gravitino/pull/12625#discussion_r4120084787
########## server/src/test/java/org/apache/gravitino/server/web/rest/TestSemanticModelOperations.java: ########## @@ -0,0 +1,495 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.gravitino.server.web.rest; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.time.Instant; +import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.catalog.SemanticModelDispatcher; +import org.apache.gravitino.dto.requests.SemanticModelCreateRequest; +import org.apache.gravitino.dto.responses.ErrorConstants; +import org.apache.gravitino.dto.responses.ErrorResponse; +import org.apache.gravitino.dto.responses.SemanticModelResponse; +import org.apache.gravitino.dto.semantic.DatasetDTO; +import org.apache.gravitino.dto.semantic.SemanticModelDefinitionDTO; +import org.apache.gravitino.exceptions.ConnectionFailedException; +import org.apache.gravitino.exceptions.ForbiddenException; +import org.apache.gravitino.exceptions.IllegalSemanticModelException; +import org.apache.gravitino.exceptions.MetalakeNotInUseException; +import org.apache.gravitino.exceptions.NoSuchSchemaException; +import org.apache.gravitino.exceptions.NoSuchSemanticModelException; +import org.apache.gravitino.exceptions.SemanticModelAlreadyExistsException; +import org.apache.gravitino.meta.AuditInfo; +import org.apache.gravitino.meta.SemanticModelEntity; +import org.apache.gravitino.rest.RESTUtils; +import org.apache.gravitino.semantic.AIContext; +import org.apache.gravitino.semantic.CustomExtension; +import org.apache.gravitino.semantic.DataType; +import org.apache.gravitino.semantic.Dataset; +import org.apache.gravitino.semantic.DialectExpression; +import org.apache.gravitino.semantic.Dialects; +import org.apache.gravitino.semantic.Expression; +import org.apache.gravitino.semantic.Field; +import org.apache.gravitino.semantic.Metric; +import org.apache.gravitino.semantic.SemanticModel; +import org.apache.gravitino.semantic.SemanticModelDefinition; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; +import org.glassfish.jersey.internal.inject.AbstractBinder; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.TestProperties; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +/** Tests the Semantic Model create/load REST resource and its error mappings. */ +public class TestSemanticModelOperations extends BaseOperationsTest { + + private static final String VND_V1_JSON = "application/vnd.gravitino.v1+json"; + + private static class MockServletRequestFactory extends ServletRequestFactoryBase { + @Override + public HttpServletRequest get() { + return mock(HttpServletRequest.class); + } + } + + private final SemanticModelDispatcher dispatcher = mock(SemanticModelDispatcher.class); + private final String metalake = "semantic_model_metalake"; + private final String catalog = "semantic_model_catalog"; + private final String schema = "semantic_model_schema"; + private final Namespace namespace = NamespaceUtil.ofSemanticModel(metalake, catalog, schema); + + /** {@inheritDoc} */ + @Override + protected Application configure() { + try { + forceSet( + TestProperties.CONTAINER_PORT, String.valueOf(RESTUtils.findAvailablePort(2000, 3000))); + } catch (IOException e) { + throw new RuntimeException(e); + } + + ResourceConfig resourceConfig = new ResourceConfig(); + resourceConfig.register(SemanticModelOperations.class); + resourceConfig.register( + new AbstractBinder() { + @Override + protected void configure() { + bind(dispatcher).to(SemanticModelDispatcher.class).ranked(2); + bindFactory(MockServletRequestFactory.class).to(HttpServletRequest.class); + } + }); + return resourceConfig; + } + + @BeforeEach + void resetDispatcher() { + reset(dispatcher); + } + + @Test + void testLoadSemanticModelReturnsCompleteDefinitionWithoutWrites() { + NameIdentifier ident = semanticModelIdentifier("sales"); + SemanticModel semanticModel = semanticModel("sales", "Sales definitions"); + when(dispatcher.loadSemanticModel(ident)).thenReturn(semanticModel); + + Response response = get(semanticModelPath() + "/sales"); + + Assertions.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + SemanticModelResponse body = response.readEntity(SemanticModelResponse.class); + body.validate(); + Assertions.assertEquals("sales", body.getSemanticModel().name()); + Assertions.assertEquals("Sales definitions", body.getSemanticModel().comment()); + Assertions.assertEquals(semanticModel.definition(), body.getSemanticModel().definition()); + Assertions.assertEquals("orders", body.getSemanticModel().definition().datasets()[0].name()); + Assertions.assertEquals( + "order_total", body.getSemanticModel().definition().metrics()[0].name()); + Assertions.assertEquals( + "acme", body.getSemanticModel().definition().customExtensions()[0].vendorName()); + Assertions.assertEquals(Map.of("domain", "sales"), body.getSemanticModel().properties()); + Assertions.assertEquals("tester", body.getSemanticModel().auditInfo().creator()); + verify(dispatcher).loadSemanticModel(ident); + verifyNoMoreInteractions(dispatcher); + } + + @Test + void testLoadSemanticModelNotFound() { + NameIdentifier ident = semanticModelIdentifier("sales"); + doThrow(new NoSuchSemanticModelException("sales does not exist")) + .when(dispatcher) + .loadSemanticModel(ident); + + assertError( + get(semanticModelPath() + "/sales"), + Response.Status.NOT_FOUND, + ErrorConstants.NOT_FOUND_CODE, + NoSuchSemanticModelException.class.getSimpleName(), + "sales does not exist"); + } + + @Test + void testLoadSemanticModelNotInUse() { + NameIdentifier ident = semanticModelIdentifier("sales"); + doThrow(new MetalakeNotInUseException("metalake is not in use")) + .when(dispatcher) + .loadSemanticModel(ident); + + assertError( + get(semanticModelPath() + "/sales"), + Response.Status.CONFLICT, + ErrorConstants.NOT_IN_USE_CODE, + MetalakeNotInUseException.class.getSimpleName(), + "metalake is not in use"); + } Review Comment: The mapping is implemented in the shared `SemanticModelExceptionHandler`, and the existing load test exercises the exact `NotInUseException` branch with a concrete subclass. The create error cases already verify that create exceptions reach the same handler. Since the dispatcher is mocked here, adding a create-side `CatalogNotInUseException` case would not exercise the Core catalog/schema path and would only repeat the same mapping assertion, so I’ll keep the focused regression test as-is. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
