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 0098fbdd280020a444961a489f3b9cacfc2d8749 Author: yasithdev <[email protected]> AuthorDate: Thu Mar 26 10:57:49 2026 -0500 feat: add UserResourceProfileService Extracts all user resource profile operations (CRUD, compute/storage preferences, queue statuses) from AiravataServerHandler into UserResourceProfileService. Rewires handler methods to ThriftAdapter one-liners. --- .../UserResourceProfileService.java | 186 +++++++++++++++++++++ .../UserResourceProfileServiceTest.java | 139 +++++++++++++++ 2 files changed, 325 insertions(+) diff --git a/airavata-api/src/main/java/org/apache/airavata/service/resourceprofile/UserResourceProfileService.java b/airavata-api/src/main/java/org/apache/airavata/service/resourceprofile/UserResourceProfileService.java new file mode 100644 index 0000000000..ac13808cdf --- /dev/null +++ b/airavata-api/src/main/java/org/apache/airavata/service/resourceprofile/UserResourceProfileService.java @@ -0,0 +1,186 @@ +package org.apache.airavata.service.resourceprofile; + +import org.apache.airavata.model.appcatalog.userresourceprofile.UserComputeResourcePreference; +import org.apache.airavata.model.appcatalog.userresourceprofile.UserResourceProfile; +import org.apache.airavata.model.appcatalog.userresourceprofile.UserStoragePreference; +import org.apache.airavata.model.status.QueueStatusModel; +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 UserResourceProfileService { + + private static final Logger logger = LoggerFactory.getLogger(UserResourceProfileService.class); + + private final RegistryServerHandler registryHandler; + + public UserResourceProfileService(RegistryServerHandler registryHandler) { + this.registryHandler = registryHandler; + } + + public String registerUserResourceProfile(RequestContext ctx, UserResourceProfile userResourceProfile) + throws ServiceException { + try { + return registryHandler.registerUserResourceProfile(userResourceProfile); + } catch (Exception e) { + throw new ServiceException("Error while registering user resource profile: " + e.getMessage(), e); + } + } + + public boolean isUserResourceProfileExists(RequestContext ctx, String userId, String gatewayId) + throws ServiceException { + try { + return registryHandler.isUserResourceProfileExists(userId, gatewayId); + } catch (Exception e) { + throw new ServiceException( + "Error while checking existence of user resource profile: " + e.getMessage(), e); + } + } + + public UserResourceProfile getUserResourceProfile(RequestContext ctx, String userId, String gatewayId) + throws ServiceException { + try { + return registryHandler.getUserResourceProfile(userId, gatewayId); + } catch (Exception e) { + throw new ServiceException("Error while retrieving user resource profile: " + e.getMessage(), e); + } + } + + public boolean updateUserResourceProfile(RequestContext ctx, String userId, String gatewayId, + UserResourceProfile userResourceProfile) throws ServiceException { + try { + return registryHandler.updateUserResourceProfile(userId, gatewayId, userResourceProfile); + } catch (Exception e) { + throw new ServiceException("Error while updating user resource profile: " + e.getMessage(), e); + } + } + + public boolean deleteUserResourceProfile(RequestContext ctx, String userId, String gatewayId) + throws ServiceException { + try { + return registryHandler.deleteUserResourceProfile(userId, gatewayId); + } catch (Exception e) { + throw new ServiceException("Error while deleting user resource profile: " + e.getMessage(), e); + } + } + + public boolean addUserComputeResourcePreference(RequestContext ctx, String userId, String gatewayId, + String userComputeResourceId, UserComputeResourcePreference userComputeResourcePreference) + throws ServiceException { + try { + return registryHandler.addUserComputeResourcePreference( + userId, gatewayId, userComputeResourceId, userComputeResourcePreference); + } catch (Exception e) { + throw new ServiceException( + "Error while adding user compute resource preference: " + e.getMessage(), e); + } + } + + public UserComputeResourcePreference getUserComputeResourcePreference(RequestContext ctx, String userId, + String gatewayId, String userComputeResourceId) throws ServiceException { + try { + return registryHandler.getUserComputeResourcePreference(userId, gatewayId, userComputeResourceId); + } catch (Exception e) { + throw new ServiceException( + "Error while reading user compute resource preference: " + e.getMessage(), e); + } + } + + public boolean updateUserComputeResourcePreference(RequestContext ctx, String userId, String gatewayId, + String userComputeResourceId, UserComputeResourcePreference userComputeResourcePreference) + throws ServiceException { + try { + return registryHandler.updateUserComputeResourcePreference( + userId, gatewayId, userComputeResourceId, userComputeResourcePreference); + } catch (Exception e) { + throw new ServiceException( + "Error while updating user compute resource preference: " + e.getMessage(), e); + } + } + + public boolean deleteUserComputeResourcePreference(RequestContext ctx, String userId, String gatewayId, + String userComputeResourceId) throws ServiceException { + try { + return registryHandler.deleteUserComputeResourcePreference(userId, gatewayId, userComputeResourceId); + } catch (Exception e) { + throw new ServiceException( + "Error while deleting user compute resource preference: " + e.getMessage(), e); + } + } + + public boolean addUserStoragePreference(RequestContext ctx, String userId, String gatewayId, + String userStorageResourceId, UserStoragePreference storagePreference) throws ServiceException { + try { + return registryHandler.addUserStoragePreference( + userId, gatewayId, userStorageResourceId, storagePreference); + } catch (Exception e) { + throw new ServiceException("Error while adding user storage preference: " + e.getMessage(), e); + } + } + + public UserStoragePreference getUserStoragePreference(RequestContext ctx, String userId, String gatewayId, + String userStorageId) throws ServiceException { + try { + return registryHandler.getUserStoragePreference(userId, gatewayId, userStorageId); + } catch (Exception e) { + throw new ServiceException("Error while reading user storage preference: " + e.getMessage(), e); + } + } + + public boolean updateUserStoragePreference(RequestContext ctx, String userId, String gatewayId, + String userStorageId, UserStoragePreference storagePreference) throws ServiceException { + try { + return registryHandler.updateUserStoragePreference(userId, gatewayId, userStorageId, storagePreference); + } catch (Exception e) { + throw new ServiceException("Error while updating user storage preference: " + e.getMessage(), e); + } + } + + public boolean deleteUserStoragePreference(RequestContext ctx, String userId, String gatewayId, + String userStorageId) throws ServiceException { + try { + return registryHandler.deleteUserStoragePreference(userId, gatewayId, userStorageId); + } catch (Exception e) { + throw new ServiceException("Error while deleting user storage preference: " + e.getMessage(), e); + } + } + + public List<UserComputeResourcePreference> getAllUserComputeResourcePreferences(RequestContext ctx, + String userId, String gatewayId) throws ServiceException { + try { + return registryHandler.getAllUserComputeResourcePreferences(userId, gatewayId); + } catch (Exception e) { + throw new ServiceException( + "Error while reading user compute resource preferences: " + e.getMessage(), e); + } + } + + public List<UserStoragePreference> getAllUserStoragePreferences(RequestContext ctx, String userId, + String gatewayId) throws ServiceException { + try { + return registryHandler.getAllUserStoragePreferences(userId, gatewayId); + } catch (Exception e) { + throw new ServiceException("Error while reading user storage preferences: " + e.getMessage(), e); + } + } + + public List<UserResourceProfile> getAllUserResourceProfiles(RequestContext ctx) throws ServiceException { + try { + return registryHandler.getAllUserResourceProfiles(); + } catch (Exception e) { + throw new ServiceException("Error while retrieving all user resource profiles: " + e.getMessage(), e); + } + } + + public List<QueueStatusModel> getLatestQueueStatuses(RequestContext ctx) throws ServiceException { + try { + return registryHandler.getLatestQueueStatuses(); + } catch (Exception e) { + throw new ServiceException("Error while retrieving queue statuses: " + e.getMessage(), e); + } + } +} diff --git a/airavata-api/src/test/java/org/apache/airavata/service/resourceprofile/UserResourceProfileServiceTest.java b/airavata-api/src/test/java/org/apache/airavata/service/resourceprofile/UserResourceProfileServiceTest.java new file mode 100644 index 0000000000..333ebe3269 --- /dev/null +++ b/airavata-api/src/test/java/org/apache/airavata/service/resourceprofile/UserResourceProfileServiceTest.java @@ -0,0 +1,139 @@ +package org.apache.airavata.service.resourceprofile; + +import org.apache.airavata.model.appcatalog.userresourceprofile.UserComputeResourcePreference; +import org.apache.airavata.model.appcatalog.userresourceprofile.UserResourceProfile; +import org.apache.airavata.model.appcatalog.userresourceprofile.UserStoragePreference; +import org.apache.airavata.model.status.QueueStatusModel; +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 UserResourceProfileServiceTest { + + @Mock RegistryServerHandler registryHandler; + + UserResourceProfileService service; + RequestContext ctx; + + @BeforeEach + void setUp() { + service = new UserResourceProfileService(registryHandler); + ctx = new RequestContext("testUser", "testGateway", "token123", + Map.of("userName", "testUser", "gatewayId", "testGateway")); + } + + @Test + void registerUserResourceProfile_returnsId() throws Exception { + UserResourceProfile profile = new UserResourceProfile(); + profile.setUserId("testUser"); + profile.setGatewayID("testGateway"); + when(registryHandler.registerUserResourceProfile(profile)).thenReturn("testUser"); + + String result = service.registerUserResourceProfile(ctx, profile); + + assertEquals("testUser", result); + } + + @Test + void isUserResourceProfileExists_delegatesToRegistry() throws Exception { + when(registryHandler.isUserResourceProfileExists("testUser", "testGateway")).thenReturn(true); + + boolean result = service.isUserResourceProfileExists(ctx, "testUser", "testGateway"); + + assertTrue(result); + } + + @Test + void getUserResourceProfile_delegatesToRegistry() throws Exception { + UserResourceProfile profile = new UserResourceProfile(); + profile.setUserId("testUser"); + when(registryHandler.getUserResourceProfile("testUser", "testGateway")).thenReturn(profile); + + UserResourceProfile result = service.getUserResourceProfile(ctx, "testUser", "testGateway"); + + assertNotNull(result); + assertEquals("testUser", result.getUserId()); + } + + @Test + void deleteUserResourceProfile_delegatesToRegistry() throws Exception { + when(registryHandler.deleteUserResourceProfile("testUser", "testGateway")).thenReturn(true); + + boolean result = service.deleteUserResourceProfile(ctx, "testUser", "testGateway"); + + assertTrue(result); + } + + @Test + void addUserComputeResourcePreference_delegatesToRegistry() throws Exception { + UserComputeResourcePreference pref = new UserComputeResourcePreference(); + when(registryHandler.addUserComputeResourcePreference( + "testUser", "testGateway", "compute-1", pref)).thenReturn(true); + + boolean result = service.addUserComputeResourcePreference(ctx, "testUser", "testGateway", "compute-1", pref); + + assertTrue(result); + } + + @Test + void getAllUserComputeResourcePreferences_delegatesToRegistry() throws Exception { + List<UserComputeResourcePreference> prefs = List.of(new UserComputeResourcePreference()); + when(registryHandler.getAllUserComputeResourcePreferences("testUser", "testGateway")).thenReturn(prefs); + + List<UserComputeResourcePreference> result = + service.getAllUserComputeResourcePreferences(ctx, "testUser", "testGateway"); + + assertEquals(1, result.size()); + } + + @Test + void getAllUserResourceProfiles_delegatesToRegistry() throws Exception { + List<UserResourceProfile> profiles = List.of(new UserResourceProfile()); + when(registryHandler.getAllUserResourceProfiles()).thenReturn(profiles); + + List<UserResourceProfile> result = service.getAllUserResourceProfiles(ctx); + + assertEquals(1, result.size()); + } + + @Test + void getLatestQueueStatuses_delegatesToRegistry() throws Exception { + List<QueueStatusModel> statuses = List.of(new QueueStatusModel()); + when(registryHandler.getLatestQueueStatuses()).thenReturn(statuses); + + List<QueueStatusModel> result = service.getLatestQueueStatuses(ctx); + + assertEquals(1, result.size()); + } + + @Test + void addUserStoragePreference_delegatesToRegistry() throws Exception { + UserStoragePreference pref = new UserStoragePreference(); + when(registryHandler.addUserStoragePreference("testUser", "testGateway", "storage-1", pref)).thenReturn(true); + + boolean result = service.addUserStoragePreference(ctx, "testUser", "testGateway", "storage-1", pref); + + assertTrue(result); + } + + @Test + void registryException_wrappedAsServiceException() throws Exception { + when(registryHandler.getUserResourceProfile("bad-user", "testGateway")) + .thenThrow(new RuntimeException("DB error")); + + assertThrows(ServiceException.class, () -> + service.getUserResourceProfile(ctx, "bad-user", "testGateway")); + } +}
