This is an automated email from the ASF dual-hosted git repository.

yasith pushed a commit to branch feat/grpc-armeria-migration
in repository https://gitbox.apache.org/repos/asf/airavata.git

commit be3805ca749e04118850e6e913314330e8a78f29
Author: yasithdev <[email protected]>
AuthorDate: Wed Apr 1 14:17:27 2026 -0400

    fix: repair unit test mocks after module extraction
    
    The compute-service and research-service modules inherit
    airavata-server.properties (with enable.sharing=true) from
    the airavata-api dependency jar. Tests assumed sharing was
    disabled but it was active, causing authorization failures.
    
    Configure SharingRegistryServerHandler mocks in @BeforeEach
    to allow access checks, entity operations, and search calls.
    Update tests that verified sharing-disabled behavior to instead
    verify sharing-enabled behavior with appropriate mock responses.
---
 .../service/GroupResourceProfileServiceTest.java   | 24 +++++++++++--
 .../service/ApplicationCatalogServiceTest.java     | 11 +++++-
 .../research/service/ExperimentServiceTest.java    | 14 +++++++-
 .../research/service/ProjectServiceTest.java       | 41 +++++++++++++++++-----
 4 files changed, 77 insertions(+), 13 deletions(-)

diff --git 
a/airavata-api/compute-service/src/test/java/org/apache/airavata/compute/service/GroupResourceProfileServiceTest.java
 
b/airavata-api/compute-service/src/test/java/org/apache/airavata/compute/service/GroupResourceProfileServiceTest.java
index e4ed57893f..7a7a54bf48 100644
--- 
a/airavata-api/compute-service/src/test/java/org/apache/airavata/compute/service/GroupResourceProfileServiceTest.java
+++ 
b/airavata-api/compute-service/src/test/java/org/apache/airavata/compute/service/GroupResourceProfileServiceTest.java
@@ -37,8 +37,11 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
 
 @ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
 class GroupResourceProfileServiceTest {
 
     @Mock
@@ -51,7 +54,14 @@ class GroupResourceProfileServiceTest {
     RequestContext ctx;
 
     @BeforeEach
-    void setUp() {
+    void setUp() throws Exception {
+        // Sharing is enabled via airavata-server.properties on the classpath.
+        // Configure the sharing mock to allow all access checks and entity 
operations.
+        when(sharingHandler.userHasAccess(anyString(), anyString(), 
anyString(), anyString()))
+                .thenReturn(true);
+        when(sharingHandler.searchEntities(anyString(), anyString(), 
anyList(), anyInt(), anyInt()))
+                .thenReturn(List.of());
+
         service = new GroupResourceProfileService(registryHandler, 
sharingHandler);
         ctx = new RequestContext(
                 "testUser", "testGateway", "token123", Map.of("userName", 
"testUser", "gatewayId", "testGateway"));
@@ -63,7 +73,17 @@ class GroupResourceProfileServiceTest {
         profile = 
profile.toBuilder().setGroupResourceProfileName("test-profile").build();
         
when(registryHandler.createGroupResourceProfile(profile)).thenReturn("grp-profile-1");
 
-        // Sharing disabled (ServerSettings.isEnableSharing() returns false in 
tests)
+        // Sharing enabled: mock entity creation and gateway groups for 
sharing registration
+        when(sharingHandler.createEntity(any())).thenReturn("grp-profile-1");
+        
when(registryHandler.isGatewayGroupsExists("testGateway")).thenReturn(true);
+        GatewayGroups groups = GatewayGroups.newBuilder()
+                .setGatewayId("testGateway")
+                .setAdminsGroupId("admins")
+                .setReadOnlyAdminsGroupId("readOnlyAdmins")
+                .build();
+        
when(registryHandler.getGatewayGroups("testGateway")).thenReturn(groups);
+        when(sharingHandler.isPermissionExists("testGateway", 
"testGateway:MANAGE_SHARING")).thenReturn(true);
+
         String result = service.createGroupResourceProfile(ctx, profile);
 
         assertEquals("grp-profile-1", result);
diff --git 
a/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ApplicationCatalogServiceTest.java
 
b/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ApplicationCatalogServiceTest.java
index df5e5e049a..53648e58a3 100644
--- 
a/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ApplicationCatalogServiceTest.java
+++ 
b/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ApplicationCatalogServiceTest.java
@@ -20,6 +20,7 @@
 package org.apache.airavata.research.service;
 
 import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.*;
 import static org.mockito.Mockito.*;
 
 import java.util.List;
@@ -39,8 +40,11 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
 
 @ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
 class ApplicationCatalogServiceTest {
 
     @Mock
@@ -56,7 +60,12 @@ class ApplicationCatalogServiceTest {
     RequestContext ctx;
 
     @BeforeEach
-    void setUp() {
+    void setUp() throws Exception {
+        // Sharing is enabled via airavata-server.properties on the classpath.
+        // Configure the sharing mock to allow all access checks.
+        when(sharingHandler.userHasAccess(anyString(), anyString(), 
anyString(), anyString()))
+                .thenReturn(true);
+
         service = new ApplicationCatalogService(registryHandler, 
sharingHandler, credentialHandler);
         ctx = new RequestContext(
                 "testUser", "testGateway", "token123", Map.of("userName", 
"testUser", "gatewayId", "testGateway"));
diff --git 
a/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ExperimentServiceTest.java
 
b/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ExperimentServiceTest.java
index b34441c499..f0d2e39c09 100644
--- 
a/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ExperimentServiceTest.java
+++ 
b/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ExperimentServiceTest.java
@@ -23,6 +23,7 @@ import org.apache.airavata.execution.service.ServiceException;
 import org.apache.airavata.execution.service.ServiceAuthorizationException;
 
 import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.*;
 import static org.mockito.Mockito.*;
 import static org.mockito.Mockito.doNothing;
 
@@ -44,13 +45,17 @@ import org.apache.airavata.model.status.proto.ProcessStatus;
 import org.apache.airavata.model.task.proto.TaskModel;
 import org.apache.airavata.model.task.proto.TaskTypes;
 import org.apache.airavata.sharing.handler.SharingRegistryServerHandler;
+import org.apache.airavata.sharing.model.EntityEntity;
 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 org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
 
 @ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
 class ExperimentServiceTest {
 
     @Mock
@@ -66,7 +71,14 @@ class ExperimentServiceTest {
     RequestContext ctx;
 
     @BeforeEach
-    void setUp() {
+    void setUp() throws Exception {
+        // Sharing is enabled via airavata-server.properties on the classpath.
+        // Configure the sharing mock to allow all access checks and entity 
operations.
+        when(sharingHandler.userHasAccess(anyString(), anyString(), 
anyString(), anyString()))
+                .thenReturn(true);
+        when(sharingHandler.getEntity(anyString(), anyString()))
+                .thenReturn(new EntityEntity());
+
         experimentService = new ExperimentService(registryHandler, 
sharingHandler, eventPublisher);
         ctx = new RequestContext(
                 "testUser", "testGateway", "token123", Map.of("userName", 
"testUser", "gatewayId", "testGateway"));
diff --git 
a/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ProjectServiceTest.java
 
b/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ProjectServiceTest.java
index 7a6a043d36..188a672fe7 100644
--- 
a/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ProjectServiceTest.java
+++ 
b/airavata-api/research-service/src/test/java/org/apache/airavata/research/service/ProjectServiceTest.java
@@ -23,6 +23,7 @@ import org.apache.airavata.execution.service.ServiceException;
 import org.apache.airavata.execution.service.ServiceAuthorizationException;
 
 import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.*;
 import static org.mockito.Mockito.*;
 
 import java.util.List;
@@ -30,13 +31,17 @@ import java.util.Map;
 import org.apache.airavata.execution.handler.RegistryServerHandler;
 import org.apache.airavata.model.workspace.proto.Project;
 import org.apache.airavata.sharing.handler.SharingRegistryServerHandler;
+import org.apache.airavata.sharing.model.EntityEntity;
 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 org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
 
 @ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
 class ProjectServiceTest {
 
     @Mock
@@ -49,7 +54,14 @@ class ProjectServiceTest {
     RequestContext ctx;
 
     @BeforeEach
-    void setUp() {
+    void setUp() throws Exception {
+        // Sharing is enabled via airavata-server.properties on the classpath.
+        // Configure the sharing mock to allow all access checks.
+        when(sharingHandler.userHasAccess(anyString(), anyString(), 
anyString(), anyString()))
+                .thenReturn(true);
+        when(sharingHandler.searchEntities(anyString(), anyString(), 
anyList(), anyInt(), anyInt()))
+                .thenReturn(List.of());
+
         projectService = new ProjectService(registryHandler, sharingHandler);
         ctx = new RequestContext(
                 "testUser", "testGateway", "token123", Map.of("userName", 
"testUser", "gatewayId", "testGateway"));
@@ -83,17 +95,17 @@ class ProjectServiceTest {
     }
 
     @Test
-    void getProject_nonOwnerRejectedWhenSharingDisabled() throws Exception {
+    void getProject_nonOwnerRejectedWhenSharingEnabled() throws Exception {
         Project project = Project.newBuilder().setOwner("otherUser").build();
         project = project.toBuilder().setGatewayId("testGateway").build();
 
         when(registryHandler.getProject("proj-123")).thenReturn(project);
 
-        // sharing disabled (ServerSettings.isEnableSharing() returns false by 
default in tests)
-        Project result = projectService.getProject(ctx, "proj-123");
+        // Sharing enabled: non-owner without READ permission is rejected
+        when(sharingHandler.userHasAccess("testGateway", 
"testUser@testGateway", "proj-123", "testGateway:READ"))
+                .thenReturn(false);
 
-        assertNull(result);
-        verifyNoInteractions(sharingHandler);
+        assertThrows(ServiceAuthorizationException.class, () -> 
projectService.getProject(ctx, "proj-123"));
     }
 
     @Test
@@ -103,7 +115,10 @@ class ProjectServiceTest {
 
         when(registryHandler.getProject("proj-123")).thenReturn(project);
 
-        // sharing disabled — non-owner should be rejected
+        // Sharing enabled: non-owner without WRITE permission is rejected
+        when(sharingHandler.userHasAccess("testGateway", 
"testUser@testGateway", "proj-123", "testGateway:WRITE"))
+                .thenReturn(false);
+
         assertThrows(ServiceAuthorizationException.class, () -> 
projectService.deleteProject(ctx, "proj-123"));
     }
 
@@ -123,13 +138,21 @@ class ProjectServiceTest {
 
     @Test
     void getUserProjects_delegatesToRegistry() throws Exception {
+        // With sharing enabled, getUserProjects uses searchEntities + 
searchProjects path
+        EntityEntity proj1 = new EntityEntity();
+        proj1.setEntityId("proj-1");
+        EntityEntity proj2 = new EntityEntity();
+        proj2.setEntityId("proj-2");
+        when(sharingHandler.searchEntities(eq("testGateway"), 
eq("testUser@testGateway"), anyList(), eq(0), eq(-1)))
+                .thenReturn(List.of(proj1, proj2));
+
         List<Project> projects = List.of(Project.getDefaultInstance(), 
Project.getDefaultInstance());
-        when(registryHandler.getUserProjects("testGateway", "testUser", 10, 
0)).thenReturn(projects);
+        when(registryHandler.searchProjects(eq("testGateway"), eq("testUser"), 
anyList(), anyMap(), eq(10), eq(0)))
+                .thenReturn(projects);
 
         List<Project> result = projectService.getUserProjects(ctx, 
"testGateway", "testUser", 10, 0);
 
         assertEquals(2, result.size());
-        verify(registryHandler).getUserProjects("testGateway", "testUser", 10, 
0);
     }
 
     @Test

Reply via email to