This is an automated email from the ASF dual-hosted git repository. yasith pushed a commit to branch feat/airavata-service-layer in repository https://gitbox.apache.org/repos/asf/airavata.git
commit f7a868b144286f670d91b0161152a3bada8feb79 Author: yasithdev <[email protected]> AuthorDate: Thu Mar 26 11:00:15 2026 -0500 feat: add ParserService with ThriftAdapter wiring Extracts getParser, saveParser, listAllParsers, removeParser, getParsingTemplate, getParsingTemplatesForExperiment, saveParsingTemplate, removeParsingTemplate, and listAllParsingTemplates into ParserService. Rewires AiravataServerHandler to delegate via ThriftAdapter. --- .../airavata/service/parser/ParserService.java | 112 ++++++++++++++++++ .../airavata/service/parser/ParserServiceTest.java | 126 +++++++++++++++++++++ 2 files changed, 238 insertions(+) diff --git a/airavata-api/src/main/java/org/apache/airavata/service/parser/ParserService.java b/airavata-api/src/main/java/org/apache/airavata/service/parser/ParserService.java new file mode 100644 index 0000000000..94d9b245c0 --- /dev/null +++ b/airavata-api/src/main/java/org/apache/airavata/service/parser/ParserService.java @@ -0,0 +1,112 @@ +package org.apache.airavata.service.parser; + +import org.apache.airavata.model.appcatalog.parser.Parser; +import org.apache.airavata.model.appcatalog.parser.ParsingTemplate; +import org.apache.airavata.registry.api.service.handler.RegistryServerHandler; +import org.apache.airavata.service.context.RequestContext; +import org.apache.airavata.service.exception.ServiceException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +public class ParserService { + + private static final Logger logger = LoggerFactory.getLogger(ParserService.class); + + private final RegistryServerHandler registryHandler; + + public ParserService(RegistryServerHandler registryHandler) { + this.registryHandler = registryHandler; + } + + public Parser getParser(RequestContext ctx, String parserId, String gatewayId) throws ServiceException { + try { + Parser parser = registryHandler.getParser(parserId, gatewayId); + logger.debug("Retrieved parser {} for gateway {}", parserId, gatewayId); + return parser; + } catch (Exception e) { + throw new ServiceException("Error retrieving parser with id: " + parserId + ": " + e.getMessage(), e); + } + } + + public String saveParser(RequestContext ctx, Parser parser) throws ServiceException { + try { + String parserId = registryHandler.saveParser(parser); + logger.debug("Saved parser {} for gateway {}", parserId, ctx.getGatewayId()); + return parserId; + } catch (Exception e) { + throw new ServiceException("Error saving the parser: " + e.getMessage(), e); + } + } + + public List<Parser> listAllParsers(RequestContext ctx, String gatewayId) throws ServiceException { + try { + List<Parser> parsers = registryHandler.listAllParsers(gatewayId); + logger.debug("Listed {} parsers for gateway {}", parsers.size(), gatewayId); + return parsers; + } catch (Exception e) { + throw new ServiceException("Error listing parsers for gateway " + gatewayId + ": " + e.getMessage(), e); + } + } + + public boolean removeParser(RequestContext ctx, String parserId, String gatewayId) throws ServiceException { + try { + registryHandler.removeParser(parserId, gatewayId); + logger.debug("Removed parser {} from gateway {}", parserId, gatewayId); + return true; + } catch (Exception e) { + throw new ServiceException("Error removing parser " + parserId + " in gateway " + gatewayId + ": " + e.getMessage(), e); + } + } + + public ParsingTemplate getParsingTemplate(RequestContext ctx, String templateId, String gatewayId) throws ServiceException { + try { + ParsingTemplate parsingTemplate = registryHandler.getParsingTemplate(templateId, gatewayId); + logger.debug("Retrieved parsing template {} for gateway {}", templateId, gatewayId); + return parsingTemplate; + } catch (Exception e) { + throw new ServiceException("Error retrieving parsing template with id: " + templateId + ": " + e.getMessage(), e); + } + } + + public List<ParsingTemplate> getParsingTemplatesForExperiment(RequestContext ctx, String experimentId, String gatewayId) throws ServiceException { + try { + List<ParsingTemplate> parsingTemplates = registryHandler.getParsingTemplatesForExperiment(experimentId, gatewayId); + logger.debug("Retrieved {} parsing templates for experiment {}", parsingTemplates.size(), experimentId); + return parsingTemplates; + } catch (Exception e) { + throw new ServiceException("Error retrieving parsing templates for experiment: " + experimentId + ": " + e.getMessage(), e); + } + } + + public String saveParsingTemplate(RequestContext ctx, ParsingTemplate parsingTemplate) throws ServiceException { + try { + String templateId = registryHandler.saveParsingTemplate(parsingTemplate); + logger.debug("Saved parsing template {} for gateway {}", templateId, ctx.getGatewayId()); + return templateId; + } catch (Exception e) { + throw new ServiceException("Error saving the parsing template: " + e.getMessage(), e); + } + } + + public boolean removeParsingTemplate(RequestContext ctx, String templateId, String gatewayId) throws ServiceException { + try { + registryHandler.removeParsingTemplate(templateId, gatewayId); + logger.debug("Removed parsing template {} from gateway {}", templateId, gatewayId); + return true; + } catch (Exception e) { + throw new ServiceException("Error removing parsing template " + templateId + " in gateway " + gatewayId + ": " + e.getMessage(), e); + } + } + + public List<ParsingTemplate> listAllParsingTemplates(RequestContext ctx, String gatewayId) throws ServiceException { + try { + List<ParsingTemplate> templates = registryHandler.listAllParsingTemplates(gatewayId); + logger.debug("Listed {} parsing templates for gateway {}", templates.size(), gatewayId); + return templates; + } catch (Exception e) { + throw new ServiceException("Error listing parsing templates for gateway " + gatewayId + ": " + e.getMessage(), e); + } + } +} diff --git a/airavata-api/src/test/java/org/apache/airavata/service/parser/ParserServiceTest.java b/airavata-api/src/test/java/org/apache/airavata/service/parser/ParserServiceTest.java new file mode 100644 index 0000000000..03e51b3a20 --- /dev/null +++ b/airavata-api/src/test/java/org/apache/airavata/service/parser/ParserServiceTest.java @@ -0,0 +1,126 @@ +package org.apache.airavata.service.parser; + +import org.apache.airavata.model.appcatalog.parser.Parser; +import org.apache.airavata.model.appcatalog.parser.ParsingTemplate; +import org.apache.airavata.registry.api.service.handler.RegistryServerHandler; +import org.apache.airavata.service.context.RequestContext; +import org.apache.airavata.service.exception.ServiceException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class ParserServiceTest { + + @Mock RegistryServerHandler registryHandler; + + ParserService parserService; + RequestContext ctx; + + @BeforeEach + void setUp() { + parserService = new ParserService(registryHandler); + ctx = new RequestContext("testUser", "testGateway", "token123", + Map.of("userName", "testUser", "gatewayId", "testGateway")); + } + + @Test + void getParser_returnsParser() throws Exception { + Parser parser = new Parser(); + parser.setId("parser-1"); + when(registryHandler.getParser("parser-1", "testGateway")).thenReturn(parser); + + Parser result = parserService.getParser(ctx, "parser-1", "testGateway"); + + assertNotNull(result); + assertEquals("parser-1", result.getId()); + } + + @Test + void saveParser_returnsParserId() throws Exception { + Parser parser = new Parser(); + when(registryHandler.saveParser(parser)).thenReturn("parser-saved-id"); + + String result = parserService.saveParser(ctx, parser); + + assertEquals("parser-saved-id", result); + verify(registryHandler).saveParser(parser); + } + + @Test + void listAllParsers_returnsList() throws Exception { + Parser p1 = new Parser(); + Parser p2 = new Parser(); + when(registryHandler.listAllParsers("testGateway")).thenReturn(List.of(p1, p2)); + + List<Parser> result = parserService.listAllParsers(ctx, "testGateway"); + + assertEquals(2, result.size()); + } + + @Test + void removeParser_returnsTrue() throws Exception { + doNothing().when(registryHandler).removeParser("parser-1", "testGateway"); + + boolean result = parserService.removeParser(ctx, "parser-1", "testGateway"); + + assertTrue(result); + verify(registryHandler).removeParser("parser-1", "testGateway"); + } + + @Test + void getParsingTemplate_returnsTemplate() throws Exception { + ParsingTemplate template = new ParsingTemplate(); + template.setId("tpl-1"); + when(registryHandler.getParsingTemplate("tpl-1", "testGateway")).thenReturn(template); + + ParsingTemplate result = parserService.getParsingTemplate(ctx, "tpl-1", "testGateway"); + + assertNotNull(result); + assertEquals("tpl-1", result.getId()); + } + + @Test + void saveParsingTemplate_returnsTemplateId() throws Exception { + ParsingTemplate template = new ParsingTemplate(); + when(registryHandler.saveParsingTemplate(template)).thenReturn("tpl-saved-id"); + + String result = parserService.saveParsingTemplate(ctx, template); + + assertEquals("tpl-saved-id", result); + } + + @Test + void removeParsingTemplate_returnsTrue() throws Exception { + doNothing().when(registryHandler).removeParsingTemplate("tpl-1", "testGateway"); + + boolean result = parserService.removeParsingTemplate(ctx, "tpl-1", "testGateway"); + + assertTrue(result); + } + + @Test + void getParsingTemplatesForExperiment_returnsList() throws Exception { + ParsingTemplate t1 = new ParsingTemplate(); + when(registryHandler.getParsingTemplatesForExperiment("exp-1", "testGateway")).thenReturn(List.of(t1)); + + List<ParsingTemplate> result = parserService.getParsingTemplatesForExperiment(ctx, "exp-1", "testGateway"); + + assertEquals(1, result.size()); + } + + @Test + void getParser_wrapsRegistryException() throws Exception { + when(registryHandler.getParser("bad-parser", "testGateway")).thenThrow(new RuntimeException("DB error")); + + assertThrows(ServiceException.class, () -> parserService.getParser(ctx, "bad-parser", "testGateway")); + } +}
