From 5dd4002af0ca6b402017fd58ad4ed7acbe4ec7e2 Mon Sep 17 00:00:00 2001 From: asha15 <165079T@uom.lk> Date: Sun, 10 Nov 2024 21:25:53 +0530 Subject: [PATCH] Add unit test for discoverable shared applications --- .../OrgApplicationManagerImplTest.java | 38 +++++++++++++++++++ .../FragmentApplicationMgtListenerTest.java | 19 +++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImplTest.java b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImplTest.java index 7ca83a0cd..f07b85245 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImplTest.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImplTest.java @@ -24,6 +24,7 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo; import org.wso2.carbon.identity.application.common.model.ServiceProvider; import org.wso2.carbon.identity.application.common.model.ServiceProviderProperty; import org.wso2.carbon.identity.application.mgt.ApplicationManagementService; @@ -32,8 +33,10 @@ import org.wso2.carbon.identity.organization.management.application.model.MainApplicationDO; import org.wso2.carbon.identity.organization.management.application.model.SharedApplicationDO; import org.wso2.carbon.identity.organization.management.service.OrganizationManager; +import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -45,6 +48,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.IS_FRAGMENT_APP; /** @@ -272,4 +276,38 @@ public void testGetChildAppIdsWithInvalidParentAppId() throws Exception { Assert.assertEquals(resolvedChildAppIdMap, Collections.emptyMap()); } + + @Test + public void testGetDiscoverableSharedApplicationBasicInfo() throws OrganizationManagementException { + + String tenantDomain = "abc.com"; + String rootOrgId = "root-org-id"; + + ApplicationBasicInfo app1 = new ApplicationBasicInfo(); + app1.setApplicationName("App1"); + app1.setDescription("Description1"); + ApplicationBasicInfo app2 = new ApplicationBasicInfo(); + app2.setApplicationName("App2"); + app2.setDescription("Description2"); + List expectedApps = Arrays.asList(app1, app2); + when(organizationManager.getPrimaryOrganizationId(tenantDomain)).thenReturn(rootOrgId); + when(orgApplicationMgtDAO.getDiscoverableSharedApplicationBasicInfo(10, 0, "*", + "ASC", "dummyName", tenantDomain, rootOrgId)).thenReturn(expectedApps); + List actualApps = orgApplicationManager.getDiscoverableSharedApplicationBasicInfo( + 10, 0, "*", "ASC", "dummyName", tenantDomain); + assertEquals(expectedApps, actualApps); + } + + @Test + public void testGetCountOfDiscoverableSharedApplications() throws OrganizationManagementException { + + int expectedCount = 5; + + when(organizationManager.getPrimaryOrganizationId("dummyTenant")).thenReturn("rootOrg123"); + when(orgApplicationMgtDAO.getCountOfDiscoverableSharedApplications("*", "dummyTenant", + "rootOrg123")).thenReturn(5); + + int actualCount = orgApplicationManager.getCountOfDiscoverableSharedApplications("*", "dummyTenant"); + assertEquals(expectedCount, actualCount); + } } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/listener/FragmentApplicationMgtListenerTest.java b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/listener/FragmentApplicationMgtListenerTest.java index 932aeba39..596fb0f90 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/listener/FragmentApplicationMgtListenerTest.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/listener/FragmentApplicationMgtListenerTest.java @@ -44,10 +44,12 @@ import org.wso2.carbon.identity.organization.management.service.OrganizationManager; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; +import java.lang.reflect.Method; import java.util.Optional; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.testng.Assert.assertTrue; import static org.testng.AssertJUnit.assertEquals; @@ -90,6 +92,8 @@ public void setUp() { OrgApplicationMgtDataHolder.getInstance().setOrganizationManager(organizationManager); OrgApplicationMgtDataHolder.getInstance().setOrgApplicationMgtDAO(orgApplicationMgtDAO); OrgApplicationMgtDataHolder.getInstance().setApplicationManagementService(applicationManagementService); + mockedUtilities = Mockito.mockStatic(IdentityTenantUtil.class, Mockito.withSettings() + .defaultAnswer(Mockito.CALLS_REAL_METHODS)); } @DataProvider(name = "subOrganizationMetaData") @@ -214,10 +218,21 @@ private ServiceProviderProperty mockServiceProviderProperty(String name, String return property; } + @Test + public void testInheritDiscoverabilityProperty() throws Exception { + + ServiceProvider mainApplication = mock(ServiceProvider.class); + when(mainApplication.isDiscoverable()).thenReturn(true); + when(serviceProvider.isDiscoverable()).thenReturn(false); + Method method = fragmentApplicationMgtListener.getClass() + .getDeclaredMethod("inheritDiscoverabilityProperty", ServiceProvider.class, ServiceProvider.class); + method.setAccessible(true); + method.invoke(fragmentApplicationMgtListener, mainApplication, serviceProvider); + verify(serviceProvider).setDiscoverable(true); + } + private void mockUtils(String requestInitiatedDomain) { - mockedUtilities = Mockito.mockStatic(IdentityTenantUtil.class, Mockito.withSettings() - .defaultAnswer(Mockito.CALLS_REAL_METHODS)); mockedUtilities.when(() -> IdentityTenantUtil.getTenantDomainFromContext()).thenReturn(requestInitiatedDomain); }