From 0347f17ab780f4111a9edf942433475f44bebd3d Mon Sep 17 00:00:00 2001 From: dhaura Date: Thu, 20 Jun 2024 20:00:44 +0530 Subject: [PATCH 01/15] Add methods to retrieve parent app id and child app ids when a shared app is given. --- .../application/OrgApplicationManager.java | 46 ++++++++++-- .../OrgApplicationManagerImpl.java | 75 +++++++++++++++++++ .../application/constant/SQLConstants.java | 7 +- .../application/dao/OrgApplicationMgtDAO.java | 18 ++++- .../dao/impl/OrgApplicationMgtDAOImpl.java | 22 +++++- 5 files changed, 158 insertions(+), 10 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java index 58df464a3..e2cd2d440 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java @@ -20,10 +20,12 @@ import org.wso2.carbon.identity.application.common.model.ServiceProvider; import org.wso2.carbon.identity.organization.management.application.model.SharedApplication; +import org.wso2.carbon.identity.organization.management.service.exception.NotImplementedException; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; import org.wso2.carbon.identity.organization.management.service.model.BasicOrganization; import java.util.List; +import java.util.Map; /** * Interface for Organization Application Management. @@ -33,14 +35,14 @@ public interface OrgApplicationManager { /** * Share the application to all the child organizations or to a list of child organizations based on the user input. * - * @param ownerOrgId Identifier of the organization owning the application. - * @param mainAppId Identifier of the main application. - * @param shareWithAllChildren Attribute indicating if the application is shared with all sub-organizations. - * @param sharedOrgs Optional list of identifiers of child organization to share the application. + * @param ownerOrgId Identifier of the organization owning the application. + * @param mainAppId Identifier of the main application. + * @param shareWithAllChildren Attribute indicating if the application is shared with all sub-organizations. + * @param sharedOrgs Optional list of identifiers of child organization to share the application. * @throws OrganizationManagementException on errors when sharing the application. */ void shareOrganizationApplication(String ownerOrgId, String mainAppId, boolean shareWithAllChildren, - List sharedOrgs) throws OrganizationManagementException; + List sharedOrgs) throws OrganizationManagementException; /** * Remove the shared (fragment) application for given organization to stop sharing the business application. @@ -68,7 +70,7 @@ List getApplicationSharedOrganizations(String ownerOrgId, Str * Returns the shared applications list of a given primary application, along with their organizations. * * @param ownerOrgId ID of the organization owning the primary application. - * @param mainAppId UUID of the primary application. + * @param mainAppId UUID of the primary application. * @return A list of shared applications details. * @throws OrganizationManagementException on errors occurred while retrieving the list of shared applications. */ @@ -143,5 +145,35 @@ default String getMainApplicationIdForGivenSharedApp(String sharedAppId, String * @throws OrganizationManagementException on errors when sharing the application. */ void shareApplication(String ownerOrgId, String sharedOrgId, ServiceProvider mainApplication, - boolean shareWithAllChildren) throws OrganizationManagementException; + boolean shareWithAllChildren) throws OrganizationManagementException; + + /** + * Get the shared parent application ID for the given chile application ID and parent organization ID. + * + * @param childAppId The unique ID of the shared child application. + * @param parentOrgId The organization ID of the parent. + * @param childOrgId The organization ID of the child. + * @return The unique identifier of the shared parent application. + * @throws OrganizationManagementException If errors occurred when retrieving the parent application ID. + */ + default String getParentAppId(String childAppId, String childOrgId, String parentOrgId) + throws OrganizationManagementException { + + throw new NotImplementedException("getParentAppId method is not implemented in " + this.getClass().getName()); + } + + /** + * Get shared child application IDs of the given parent application. + * + * @param parentAppId The unique ID of the shared parent application. + * @param parentOrgId The organization ID of the parent. + * @param childOrgIds The organization ID list of the children. + * @return The map containing organization ID and application ID of the shared child applications. + * @throws OrganizationManagementException If errors occurred when retrieving the child app IDs. + */ + default Map getChildAppIds(String parentAppId, String parentOrgId, List childOrgIds) + throws OrganizationManagementException { + + throw new NotImplementedException("getParentAppId method is not implemented in " + this.getClass().getName()); + } } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java index c16c75577..0a594c004 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java @@ -75,6 +75,7 @@ import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -655,6 +656,62 @@ public void shareApplication(String ownerOrgId, String sharedOrgId, ServiceProvi } } + @Override + public String getParentAppId(String childAppId, String childOrgId, String parentOrgId) + throws OrganizationManagementException { + + Optional mainApplicationDO = + getOrgApplicationMgtDAO().getMainApplication(childAppId, childOrgId); + if (mainApplicationDO.isPresent()) { + String rootOrgId = mainApplicationDO.get().getOrganizationId(); + if (rootOrgId.equals(parentOrgId)) { + return mainApplicationDO.get().getMainApplicationId(); + } + return getOrgApplicationMgtDAO().getParentAppId(mainApplicationDO.get().getMainApplicationId(), rootOrgId, + parentOrgId); + } + return null; + } + + @Override + public Map getChildAppIds(String parentAppId, String parentOrgId, List childOrgIds) + throws OrganizationManagementException { + + if (childOrgIds == null || childOrgIds.isEmpty()) { + return Collections.emptyMap(); + } + + OrganizationManager organizationManager = OrgApplicationMgtDataHolder.getInstance().getOrganizationManager(); + String parentTenantDomain = organizationManager.resolveTenantDomain(parentOrgId); + + ApplicationManagementService applicationManagementService = + OrgApplicationMgtDataHolder.getInstance().getApplicationManagementService(); + ServiceProvider serviceProvider; + try { + serviceProvider = applicationManagementService.getApplicationByResourceId(parentAppId, parentTenantDomain); + } catch (IdentityApplicationManagementException e) { + throw handleServerException(ERROR_CODE_ERROR_RETRIEVING_APPLICATION, e, parentAppId); + } + + if (serviceProvider != null) { + boolean isFragmentApp = Arrays.stream(serviceProvider.getSpProperties()) + .anyMatch(property -> IS_FRAGMENT_APP.equals(property.getName()) && + Boolean.parseBoolean(property.getValue())); + if (!isFragmentApp) { + // Parent app is the main application + return filterSharedApplications(parentAppId, parentOrgId, childOrgIds); + } + } + + Optional mainApplicationDO = + getOrgApplicationMgtDAO().getMainApplication(parentAppId, parentOrgId); + if (mainApplicationDO.isPresent()) { + return filterSharedApplications(mainApplicationDO.get().getMainApplicationId(), + mainApplicationDO.get().getOrganizationId(), childOrgIds); + } + return Collections.emptyMap(); + } + /** * This method checks whether the exception is thrown due to the OAuth client already existing. * @param e The IdentityException thrown upon OAuth app creation failure. @@ -902,6 +959,24 @@ private boolean shouldUpdateShareWithAllChildren(boolean shareWithAllChildren, S return false; } + /** + * Filter and return shared applications of the given main application for the given child organization IDs. + * + * @param mainAppId Application ID of the main application. + * @param mainOrgId Organization ID of the main application. + * @return The map containing organization ID and application ID of the filtered shared applications. + */ + private Map filterSharedApplications(String mainAppId, String mainOrgId, List childOrgIds) + throws OrganizationManagementException { + + List sharedApplications = + getOrgApplicationMgtDAO().getSharedApplications(mainOrgId, mainAppId); + return sharedApplications.stream() + .filter(app -> childOrgIds.contains(app.getOrganizationId())) + .collect(Collectors.toMap(SharedApplicationDO::getOrganizationId, + SharedApplicationDO::getFragmentApplicationId)); + } + private OAuthAdminServiceImpl getOAuthAdminService() { return OrgApplicationMgtDataHolder.getInstance().getOAuthAdminService(); diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java index 6a858524b..5da1a25de 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com). + * Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -67,6 +67,11 @@ public class SQLConstants { SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_MAIN_APP_ID + "; AND OWNER_ORG_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_OWNER_ORG_ID + ";"; + public static final String GET_PARENT_APP_ID = "SELECT SHARED_APP_ID FROM SP_SHARED_APP WHERE MAIN_APP_ID = " + + ":" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_MAIN_APP_ID + "AND OWNER_ORG_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_OWNER_ORG_ID + " AND SHARED_ORG_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_SHARED_ORG_ID + ";"; + private SQLConstants() { } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java index 8ab06d49d..46b52c87e 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com). + * Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -20,6 +20,7 @@ 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.exception.NotImplementedException; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; import java.util.List; @@ -118,4 +119,19 @@ Optional getSharedApplicationResourceId(String mainAppId, String ownerOr */ void updateShareWithAllChildren(String mainApplicationId, String ownerOrganizationId, boolean shareWithAllChildren) throws OrganizationManagementException; + + /** + * Returns the unique identifier of the shared parent application. + * + * @param mainAppId The app ID of the main application. + * @param ownerOrgId The organization ID of the owner. + * @param parentOrgId The organization ID of the parent. + * @throws OrganizationManagementException The server exception is thrown in a failure + * when retrieving the parent app ID. + */ + default String getParentAppId(String mainAppId, String ownerOrgId, String parentOrgId) + throws OrganizationManagementException { + + throw new NotImplementedException("getParentAppId method is not implemented in " + this.getClass().getName()); + } } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java index c4f72b0ca..eb0670fb5 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com). + * Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -33,6 +33,7 @@ import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.IS_FRAGMENT_APP; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_MAIN_APPLICATION; +import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_PARENT_APP_ID; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_SHARED_APPLICATION; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_SHARED_APPLICATIONS; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_SHARED_APP_ID; @@ -53,6 +54,7 @@ import static org.wso2.carbon.identity.organization.management.application.util.OrgApplicationManagerUtil.getNewTemplate; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_CHECKING_APPLICATION_HAS_FRAGMENTS; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_CHECKING_APPLICATION_IS_A_FRAGMENT; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_GETTING_PARENT_APP_ID; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_LINK_APPLICATIONS; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_RESOLVING_MAIN_APPLICATION; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_RESOLVING_SHARED_APPLICATION; @@ -224,6 +226,24 @@ public void updateShareWithAllChildren(String mainApplicationId, String ownerOrg } } + @Override + public String getParentAppId(String mainAppId, String ownerOrgId, String parentOrgId) + throws OrganizationManagementException { + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + return namedJdbcTemplate.fetchSingleRecord(GET_PARENT_APP_ID, + (resultSet, rowNumber) -> resultSet.getString(DB_SCHEMA_COLUMN_NAME_SHARED_APP_ID), + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_MAIN_APP_ID, mainAppId); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_OWNER_ORG_ID, ownerOrgId); + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_SHARED_ORG_ID, parentOrgId); + }); + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_ERROR_GETTING_PARENT_APP_ID, e, mainAppId, parentOrgId); + } + } + /** * Get the value for the shareWithAllChildren column according to the db type. * From 5d68424edd5b8677374dd4d327d575c49bba90e9 Mon Sep 17 00:00:00 2001 From: dhaura Date: Thu, 20 Jun 2024 20:01:24 +0530 Subject: [PATCH 02/15] Update License header. --- .../management/application/OrgApplicationManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java index e2cd2d440..7ebbbfae5 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com). + * Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except From 078e10ccf0fad1bf0975a17ec3b1e0d9643e54e5 Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 25 Jun 2024 12:33:21 +0530 Subject: [PATCH 03/15] Add unit tests for parent app id retrieval and child app ids retrieval methods. --- .../application/constant/SQLConstants.java | 6 +- .../OrgApplicationManagerImplTest.java | 220 ++++++++++++++++++ .../impl/OrgApplicationMgtDAOImplTest.java | 188 +++++++++++++++ .../src/test/resources/dbscripts/h2.sql | 42 ++++ .../src/test/resources/testng.xml | 31 +++ 5 files changed, 484 insertions(+), 3 deletions(-) create mode 100644 components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImplTest.java create mode 100644 components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java create mode 100644 components/org.wso2.carbon.identity.organization.management.application/src/test/resources/dbscripts/h2.sql create mode 100644 components/org.wso2.carbon.identity.organization.management.application/src/test/resources/testng.xml diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java index 5da1a25de..1b6a860f2 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java @@ -67,9 +67,9 @@ public class SQLConstants { SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_MAIN_APP_ID + "; AND OWNER_ORG_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_OWNER_ORG_ID + ";"; - public static final String GET_PARENT_APP_ID = "SELECT SHARED_APP_ID FROM SP_SHARED_APP WHERE MAIN_APP_ID = " + - ":" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_MAIN_APP_ID + "AND OWNER_ORG_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_OWNER_ORG_ID + " AND SHARED_ORG_ID = :" + + public static final String GET_PARENT_APP_ID = "SELECT SHARED_APP_ID FROM SP_SHARED_APP WHERE MAIN_APP_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_MAIN_APP_ID + "; AND OWNER_ORG_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_OWNER_ORG_ID + "; AND SHARED_ORG_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_SHARED_ORG_ID + ";"; private SQLConstants() { 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 new file mode 100644 index 000000000..a854fc5b7 --- /dev/null +++ b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImplTest.java @@ -0,0 +1,220 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. 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.wso2.carbon.identity.organization.management.application; + +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +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; +import org.wso2.carbon.identity.organization.management.application.dao.OrgApplicationMgtDAO; +import org.wso2.carbon.identity.organization.management.application.internal.OrgApplicationMgtDataHolder; +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 java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.IS_FRAGMENT_APP; + +public class OrgApplicationManagerImplTest { + + @Mock + private OrgApplicationMgtDAO orgApplicationMgtDAO; + + @Mock + private OrganizationManager organizationManager; + + @Mock + private ApplicationManagementService applicationManagementService; + + private static final Map childAppIdMap = new HashMap() {{ + put("30b701c6-e309-4241-b047-0c299c45d1a0", "42ef1d92-add6-449b-8a3c-fc308d2a4eac"); + put("5b65d2f9-1b0c-4e66-9f2c-3aa57dcd1ef7", "ede1f19f-c8b4-4f3c-a19d-18aab939ad3f"); + put("8o65d2l6-1u0c-4e76-9f2c-4aa67trt1ef8", "udt1f16f-c8y4-4r3c-a20d-58hhg969ad6p"); + }}; + private static final String CHILD_APP_ID = "42ef1d92-add6-449b-8a3c-fc308d2a4eac"; + private static final String CHILD_ORG_ID = "30b701c6-e309-4241-b047-0c299c45d1a0"; + private static final String PARENT_APP_ID = "1e2ef3df-e670-4339-9833-9df41dda7c96"; + private static final String PARENT_ORG_ID = "93d996f9-a5ba-4275-a52b-adaad9eba869"; + private static final String PARENT_TENANT_DOMAIN = "test-organization"; + private static final String ROOT_APP_ID = "fa9b9ac5-a429-49e2-9c51-4259c7ebe45e"; + private static final String ROOT_ORG_ID = "72b81cba-51c7-4dc1-91be-b267e177c17a"; + private static final String ROOT_TENANT_DOMAIN = "root-organization"; + + @BeforeMethod + public void setUp() { + + MockitoAnnotations.openMocks(this); + OrgApplicationMgtDataHolder.getInstance().setOrgApplicationMgtDAO(orgApplicationMgtDAO); + OrgApplicationMgtDataHolder.getInstance().setOrganizationManager(organizationManager); + OrgApplicationMgtDataHolder.getInstance().setApplicationManagementService(applicationManagementService); + } + + @DataProvider(name = "parentAppIdRetrievalTestData") + public Object[][] getParentAppIdRetrievalTestData() { + + return new Object[][]{ + // Generic case. + {CHILD_APP_ID, CHILD_ORG_ID, PARENT_APP_ID, PARENT_ORG_ID, ROOT_APP_ID, ROOT_ORG_ID, true, + PARENT_APP_ID}, + // Parent app is the root app. + {CHILD_APP_ID, CHILD_ORG_ID, ROOT_APP_ID, ROOT_ORG_ID, ROOT_APP_ID, ROOT_ORG_ID, true, ROOT_APP_ID}, + // App is not shared with the parent app. + {CHILD_APP_ID, CHILD_ORG_ID, null, PARENT_ORG_ID, ROOT_APP_ID, ROOT_ORG_ID, false, null}, + // Root app is passed as the child app. + {ROOT_APP_ID, ROOT_ORG_ID, null, ROOT_ORG_ID, ROOT_APP_ID, ROOT_ORG_ID, true, null} + }; + } + + @Test(dataProvider = "parentAppIdRetrievalTestData") + public void testGetParentAppId(String childAppId, String childOrgId, String parentAppId, + String parentOrgId, String rootAppId, String rootOrgId, + boolean isAppSharedWithParent, String expectedParentAppId) + throws Exception { + + MainApplicationDO mainApp = new MainApplicationDO(rootOrgId, rootAppId); + if (parentAppId != null) { + when(orgApplicationMgtDAO.getMainApplication(childAppId, childOrgId)).thenReturn(Optional.of(mainApp)); + when(orgApplicationMgtDAO.getParentAppId(rootAppId, rootOrgId, parentOrgId)).thenReturn(parentAppId); + } else if (isAppSharedWithParent) { + when(orgApplicationMgtDAO.getMainApplication(childAppId, childOrgId)).thenReturn(Optional.empty()); + } else { + when(orgApplicationMgtDAO.getMainApplication(childAppId, childOrgId)).thenReturn(Optional.of(mainApp)); + when(orgApplicationMgtDAO.getParentAppId(rootAppId, rootOrgId, parentOrgId)).thenReturn(null); + } + + OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); + String resolvedParentAppId = orgApplicationManager.getParentAppId(childAppId, childOrgId, parentOrgId); + + Assert.assertEquals(resolvedParentAppId, expectedParentAppId); + } + + @Test + public void testGetParentAppIdWithInvalidChildAppIds() throws Exception { + + String invalidChildAppId = "invalid-child-app-id"; + when(orgApplicationMgtDAO.getMainApplication(invalidChildAppId, CHILD_ORG_ID)).thenReturn(Optional.empty()); + + OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); + String resolvedParentAppId = orgApplicationManager.getParentAppId(invalidChildAppId, CHILD_ORG_ID, + PARENT_ORG_ID); + + Assert.assertNull(resolvedParentAppId); + } + + @DataProvider(name = "childAppIdsRetrievalTestData") + public Object[][] getChildAppIdsRetrievalTestData() { + + List childOrgIds = new ArrayList<>(childAppIdMap.keySet()); + + return new Object[][]{ + // Generic case. + {PARENT_APP_ID, PARENT_ORG_ID, PARENT_TENANT_DOMAIN, ROOT_APP_ID, ROOT_ORG_ID, true, childOrgIds, + childAppIdMap, childAppIdMap}, + // App is not shared with child organizations. + {PARENT_APP_ID, PARENT_ORG_ID, PARENT_TENANT_DOMAIN, ROOT_APP_ID, ROOT_ORG_ID, true, childOrgIds, + Collections.emptyMap(), Collections.emptyMap()}, + // Passing an empty list of child organizations. + {PARENT_APP_ID, PARENT_ORG_ID, PARENT_TENANT_DOMAIN, ROOT_APP_ID, ROOT_ORG_ID, true, + Collections.emptyList(), Collections.emptyMap(), Collections.emptyMap()}, + // Passing a null list of child organizations. + {PARENT_APP_ID, PARENT_ORG_ID, PARENT_TENANT_DOMAIN, ROOT_APP_ID, ROOT_ORG_ID, true, null, + Collections.emptyMap(), Collections.emptyMap()}, + // Parent app is the root app. + {ROOT_APP_ID, ROOT_ORG_ID, ROOT_TENANT_DOMAIN, ROOT_APP_ID, ROOT_ORG_ID, false, childOrgIds, + childAppIdMap, childAppIdMap}, + // Parent app is the root app but app is not shared with child organizations. + {ROOT_APP_ID, ROOT_ORG_ID, ROOT_TENANT_DOMAIN, ROOT_APP_ID, ROOT_ORG_ID, false, childOrgIds, + Collections.emptyMap(), Collections.emptyMap()}, + // Parent app is the root app and passing an empty list of child organizations. + {ROOT_APP_ID, ROOT_ORG_ID, ROOT_TENANT_DOMAIN, ROOT_APP_ID, ROOT_ORG_ID, false, Collections.emptyList(), + Collections.emptyMap(), Collections.emptyMap()}, + // Parent app is the root app and passing a null list of child organizations. + {ROOT_APP_ID, ROOT_ORG_ID, ROOT_TENANT_DOMAIN, ROOT_APP_ID, ROOT_ORG_ID, false, null, + Collections.emptyMap(), Collections.emptyMap()}, + }; + } + + @Test(dataProvider = "childAppIdsRetrievalTestData") + public void testGetChildAppIds(String parentAppId, String parentOrgId, String parentTenantDomain, String rootAppId, + String rootOrgId, boolean isFragmentApp, List childOrgIds, + Map childAppIdMap, Map expectedChildAppIdMap) + throws Exception { + + when(organizationManager.resolveTenantDomain(parentOrgId)).thenReturn(parentTenantDomain); + + ServiceProvider parentApp = mock(ServiceProvider.class); + ServiceProviderProperty isFragmentAppProperty = new ServiceProviderProperty(); + isFragmentAppProperty.setName(IS_FRAGMENT_APP); + isFragmentAppProperty.setValue(String.valueOf(isFragmentApp)); + when(parentApp.getSpProperties()).thenReturn(new ServiceProviderProperty[]{isFragmentAppProperty}); + when(applicationManagementService.getApplicationByResourceId(parentAppId, parentTenantDomain)).thenReturn( + parentApp); + + List sharedApplications = childAppIdMap.entrySet().stream() + .map(app -> { + return new SharedApplicationDO(app.getKey(), app.getValue()); + }) + .collect(Collectors.toList()); + + if (isFragmentApp) { + MainApplicationDO mainApp = new MainApplicationDO(rootOrgId, rootAppId); + when(orgApplicationMgtDAO.getMainApplication(parentAppId, parentOrgId)).thenReturn(Optional.of(mainApp)); + when(orgApplicationMgtDAO.getSharedApplications(rootOrgId, rootAppId)).thenReturn(sharedApplications); + } else { + when(orgApplicationMgtDAO.getSharedApplications(parentOrgId, parentAppId)).thenReturn(sharedApplications); + } + + OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); + Map resolvedChildAppIdMap = + orgApplicationManager.getChildAppIds(parentAppId, parentOrgId, childOrgIds); + + Assert.assertEquals(resolvedChildAppIdMap, expectedChildAppIdMap); + } + + @Test + public void testGetChildAppIdsWithInvalidChildAppIds() throws Exception { + + String invalidParentAppId = "invalid-parent-app-id"; + when(organizationManager.resolveTenantDomain(PARENT_ORG_ID)).thenReturn(PARENT_TENANT_DOMAIN); + when(applicationManagementService.getApplicationByResourceId(invalidParentAppId, + PARENT_TENANT_DOMAIN)).thenReturn(null); + when(orgApplicationMgtDAO.getMainApplication(invalidParentAppId, PARENT_ORG_ID)).thenReturn(Optional.empty()); + + OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); + Map resolvedChildAppIdMap = + orgApplicationManager.getChildAppIds(invalidParentAppId, PARENT_ORG_ID, + new ArrayList<>(childAppIdMap.keySet())); + + Assert.assertEquals(resolvedChildAppIdMap, Collections.emptyMap()); + } +} diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java new file mode 100644 index 000000000..13a1c4f10 --- /dev/null +++ b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. 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.wso2.carbon.identity.organization.management.application.dao.impl; + +import org.apache.commons.dbcp.BasicDataSource; +import org.apache.commons.lang.StringUtils; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; + +import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import javax.sql.DataSource; + +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + +public class OrgApplicationMgtDAOImplTest { + + private static final String DB_NAME = "testOrgAppMgtDB"; + private static final String H2_SCRIPT_NAME = "h2.sql"; + private static Map dataSourceMap = new HashMap<>(); + + private static final String ROOT_APP_ID = "fa9b9ac5-a429-49e2-9c51-4259c7ebe45e"; + private static final String ROOT_APP_NAME = "test-app"; + private static final String ROOT_ORG_ID = "72b81cba-51c7-4dc1-91be-b267e177c17a"; + private static final int ROOT_TENANT_ID = 1; + private static final String PRIMARY_USERSTORE = "PRIMARY"; + private static final String ADMIN_USERNAME = "admin"; + private static final String AUTH_TYPE = "default"; + + private static final String CREATE_APPLICATION_SQL_STATEMENT = "INSERT INTO SP_APP " + + "(TENANT_ID, APP_NAME, USER_STORE, USERNAME, AUTH_TYPE, UUID) VALUES (?, ?, ?, ?, ?, ?);"; + private static final String SHARE_APPLICATION_SQL_STATEMENT = "INSERT INTO SP_SHARED_APP " + + "(MAIN_APP_ID, OWNER_ORG_ID, SHARED_APP_ID, SHARED_ORG_ID, SHARE_WITH_ALL_CHILDREN) " + + "VALUES (?, ?, ?, ?, ?);"; + + @BeforeClass + public void setUp() throws Exception { + + initiateH2Base(); + mockDataSource(); + createApplication(ROOT_TENANT_ID, ROOT_APP_NAME, ROOT_APP_ID, PRIMARY_USERSTORE, ADMIN_USERNAME, AUTH_TYPE); + } + + @AfterClass + public void tearDown() throws Exception { + + closeH2Base(); + } + + @DataProvider(name = "parentAppIdRetrievalTestData") + public Object[][] getParentAppIdRetrievalTestData() throws Exception { + + String parentAppId = "1e2ef3df-e670-4339-9833-9df41dda7c96"; + String parentOrgId = "93d996f9-a5ba-4275-a52b-adaad9eba869"; + int parentTenantId = 2; + String invalidParentOrgId = "6t9ef3df-f966-5839-9123-8ki61dda7c88"; + + createApplication(parentTenantId, ROOT_APP_NAME, parentAppId, PRIMARY_USERSTORE, ADMIN_USERNAME, AUTH_TYPE); + shareApplication(ROOT_APP_ID, ROOT_ORG_ID, parentAppId, parentOrgId); + + return new Object[][]{ + // App is shared with the given parent org. + {parentOrgId, parentAppId}, + // Since root org does not have a parent, null should be returned. + {ROOT_ORG_ID, null}, + // App is not shared with the given parent org or + // the parent org does not exist, null should be returned. + {invalidParentOrgId, null}, + // When parent org id is empty, null should be returned. + {"", null}, + // When parent org id is null, null should be returned. + {null, null} + }; + } + + @Test(dataProvider = "parentAppIdRetrievalTestData") + public void testGetParentAppId(String parentOrgId, String expectedParentId) throws Exception { + + OrgApplicationMgtDAOImpl orgApplicationMgtDAO = new OrgApplicationMgtDAOImpl(); + String parentAppId = orgApplicationMgtDAO.getParentAppId(ROOT_APP_ID, ROOT_ORG_ID, parentOrgId); + + Assert.assertEquals(parentAppId, expectedParentId); + } + + private void initiateH2Base() throws Exception { + + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName("org.h2.Driver"); + dataSource.setUsername("username"); + dataSource.setPassword("password"); + dataSource.setUrl("jdbc:h2:mem:test" + DB_NAME); + dataSource.setTestOnBorrow(true); + dataSource.setValidationQuery("select 1"); + try (Connection connection = dataSource.getConnection()) { + connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + getFilePath(H2_SCRIPT_NAME) + "'"); + } + dataSourceMap.put(DB_NAME, dataSource); + } + + private String getFilePath(String fileName) { + + if (StringUtils.isNotBlank(fileName)) { + return Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "dbscripts", + fileName).toString(); + } + throw new IllegalArgumentException("DB Script file name cannot be empty."); + } + + private void mockDataSource() { + + DataSource dataSource = dataSourceMap.get(DB_NAME); + mockStatic(IdentityDatabaseUtil.class); + when(IdentityDatabaseUtil.getDataSource()).thenReturn(dataSource); + } + + private Connection getConnection() throws SQLException { + + if (dataSourceMap.get(DB_NAME) != null) { + return dataSourceMap.get(DB_NAME).getConnection(); + } + throw new RuntimeException("No datasource initiated for database: " + DB_NAME); + } + + public void closeH2Base() throws Exception { + + BasicDataSource dataSource = dataSourceMap.remove(DB_NAME); + if (dataSource != null) { + dataSource.close(); + } + } + + private void createApplication(int tenantId, String appName, String appId, String userstore, String username, + String authType) throws Exception { + + try (Connection connection = getConnection()) { + PreparedStatement statement = connection.prepareStatement(CREATE_APPLICATION_SQL_STATEMENT); + statement.setInt(1, tenantId); + statement.setString(2, appName); + statement.setString(3, userstore); + statement.setString(4, username); + statement.setString(5, authType); + statement.setString(6, appId); + statement.execute(); + } + } + + private void shareApplication(String rootAppId, String rootOrgId, String sharedAppId, String sharedOrgId) { + + try (Connection connection = getConnection()) { + PreparedStatement statement = connection.prepareStatement(SHARE_APPLICATION_SQL_STATEMENT); + statement.setString(1, rootAppId); + statement.setString(2, rootOrgId); + statement.setString(3, sharedAppId); + statement.setString(4, sharedOrgId); + statement.setBoolean(5, true); + statement.execute(); + } catch (SQLException e) { + throw new RuntimeException("Error while sharing application: " + rootAppId + " with organization: " + + sharedOrgId, e); + } + } +} diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/test/resources/dbscripts/h2.sql b/components/org.wso2.carbon.identity.organization.management.application/src/test/resources/dbscripts/h2.sql new file mode 100644 index 000000000..40b3d0298 --- /dev/null +++ b/components/org.wso2.carbon.identity.organization.management.application/src/test/resources/dbscripts/h2.sql @@ -0,0 +1,42 @@ +CREATE TABLE IF NOT EXISTS SP_APP ( + ID INTEGER NOT NULL AUTO_INCREMENT, + TENANT_ID INTEGER NOT NULL, + APP_NAME VARCHAR (255) NOT NULL , + USER_STORE VARCHAR (255) NOT NULL, + USERNAME VARCHAR (255) NOT NULL , + DESCRIPTION VARCHAR (1024), + ROLE_CLAIM VARCHAR (512), + AUTH_TYPE VARCHAR (255) NOT NULL, + PROVISIONING_USERSTORE_DOMAIN VARCHAR (512), + IS_LOCAL_CLAIM_DIALECT CHAR(1) DEFAULT '1', + IS_SEND_LOCAL_SUBJECT_ID CHAR(1) DEFAULT '0', + IS_SEND_AUTH_LIST_OF_IDPS CHAR(1) DEFAULT '0', + IS_USE_TENANT_DOMAIN_SUBJECT CHAR(1) DEFAULT '1', + IS_USE_USER_DOMAIN_SUBJECT CHAR(1) DEFAULT '1', + ENABLE_AUTHORIZATION CHAR(1) DEFAULT '0', + SUBJECT_CLAIM_URI VARCHAR (512), + IS_SAAS_APP CHAR(1) DEFAULT '0', + IS_DUMB_MODE CHAR(1) DEFAULT '0', + UUID CHAR(36), + IMAGE_URL VARCHAR(1024), + ACCESS_URL VARCHAR(1024), + IS_DISCOVERABLE CHAR(1) DEFAULT '0', + + PRIMARY KEY (ID)); + +ALTER TABLE SP_APP ADD CONSTRAINT APPLICATION_NAME_CONSTRAINT UNIQUE(APP_NAME, TENANT_ID); +ALTER TABLE SP_APP ADD CONSTRAINT APPLICATION_UUID_CONSTRAINT UNIQUE(UUID); + +CREATE TABLE IF NOT EXISTS SP_SHARED_APP ( + ID INTEGER NOT NULL AUTO_INCREMENT, + MAIN_APP_ID CHAR(36) NOT NULL, + OWNER_ORG_ID CHAR(36) NOT NULL, + SHARED_APP_ID CHAR(36) NOT NULL, + SHARED_ORG_ID CHAR(36) NOT NULL, + SHARE_WITH_ALL_CHILDREN BOOLEAN DEFAULT FALSE, + PRIMARY KEY (ID), + FOREIGN KEY (MAIN_APP_ID) REFERENCES SP_APP(UUID) ON DELETE CASCADE, + FOREIGN KEY (SHARED_APP_ID) REFERENCES SP_APP(UUID) ON DELETE CASCADE, + UNIQUE (MAIN_APP_ID, OWNER_ORG_ID, SHARED_ORG_ID), + UNIQUE (SHARED_APP_ID) +); diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/test/resources/testng.xml b/components/org.wso2.carbon.identity.organization.management.application/src/test/resources/testng.xml new file mode 100644 index 000000000..32771a1b0 --- /dev/null +++ b/components/org.wso2.carbon.identity.organization.management.application/src/test/resources/testng.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + From e33c063a55315f15cdadee4bf91f4829da245a95 Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 25 Jun 2024 12:35:54 +0530 Subject: [PATCH 04/15] Rename unit test method name. --- .../management/application/OrgApplicationManagerImplTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a854fc5b7..ed4eb7dcf 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 @@ -202,7 +202,7 @@ public void testGetChildAppIds(String parentAppId, String parentOrgId, String pa } @Test - public void testGetChildAppIdsWithInvalidChildAppIds() throws Exception { + public void testGetChildAppIdsWithInvalidParentAppId() throws Exception { String invalidParentAppId = "invalid-parent-app-id"; when(organizationManager.resolveTenantDomain(PARENT_ORG_ID)).thenReturn(PARENT_TENANT_DOMAIN); From ed9d2d17f3952a7ffe4a90712400ed18e307983f Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 25 Jun 2024 13:17:42 +0530 Subject: [PATCH 05/15] Improve unit tests. --- .../application/OrgApplicationManagerImplTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 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 ed4eb7dcf..e674649fd 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 @@ -119,9 +119,9 @@ public void testGetParentAppId(String childAppId, String childOrgId, String pare } @Test - public void testGetParentAppIdWithInvalidChildAppIds() throws Exception { + public void testGetParentAppIdWithInvalidChildAppId() throws Exception { - String invalidChildAppId = "invalid-child-app-id"; + String invalidChildAppId = "6t9ef3df-f966-5839-9123-8ki61dda7c88"; when(orgApplicationMgtDAO.getMainApplication(invalidChildAppId, CHILD_ORG_ID)).thenReturn(Optional.empty()); OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); @@ -204,7 +204,7 @@ public void testGetChildAppIds(String parentAppId, String parentOrgId, String pa @Test public void testGetChildAppIdsWithInvalidParentAppId() throws Exception { - String invalidParentAppId = "invalid-parent-app-id"; + String invalidParentAppId = "5y9ef3df-f966-5839-9693-8ki61dda7c77"; when(organizationManager.resolveTenantDomain(PARENT_ORG_ID)).thenReturn(PARENT_TENANT_DOMAIN); when(applicationManagementService.getApplicationByResourceId(invalidParentAppId, PARENT_TENANT_DOMAIN)).thenReturn(null); From a0d22c6595e189a9a4fe20de265589b3f5b89e8c Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 25 Jun 2024 16:50:14 +0530 Subject: [PATCH 06/15] Refactor testGetChildAppIds unit test. --- .../application/OrgApplicationManagerImplTest.java | 10 +++------- 1 file changed, 3 insertions(+), 7 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 e674649fd..34368cc28 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 @@ -186,13 +186,9 @@ public void testGetChildAppIds(String parentAppId, String parentOrgId, String pa }) .collect(Collectors.toList()); - if (isFragmentApp) { - MainApplicationDO mainApp = new MainApplicationDO(rootOrgId, rootAppId); - when(orgApplicationMgtDAO.getMainApplication(parentAppId, parentOrgId)).thenReturn(Optional.of(mainApp)); - when(orgApplicationMgtDAO.getSharedApplications(rootOrgId, rootAppId)).thenReturn(sharedApplications); - } else { - when(orgApplicationMgtDAO.getSharedApplications(parentOrgId, parentAppId)).thenReturn(sharedApplications); - } + MainApplicationDO mainApp = new MainApplicationDO(rootOrgId, rootAppId); + when(orgApplicationMgtDAO.getMainApplication(parentAppId, parentOrgId)).thenReturn(Optional.of(mainApp)); + when(orgApplicationMgtDAO.getSharedApplications(rootOrgId, rootAppId)).thenReturn(sharedApplications); OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); Map resolvedChildAppIdMap = From fa1e5e5dd3e32ce65c82bf4a1bdeba10a9b36ced Mon Sep 17 00:00:00 2001 From: dhaura Date: Fri, 28 Jun 2024 10:02:53 +0530 Subject: [PATCH 07/15] Refactor getParentAppId method. --- .../application/OrgApplicationManager.java | 5 ++--- .../application/OrgApplicationManagerImpl.java | 12 +++++++++--- .../application/OrgApplicationManagerImplTest.java | 13 ++++++++++--- .../dao/impl/OrgApplicationMgtDAOImplTest.java | 3 +++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java index 7ebbbfae5..610d30002 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java @@ -148,15 +148,14 @@ void shareApplication(String ownerOrgId, String sharedOrgId, ServiceProvider mai boolean shareWithAllChildren) throws OrganizationManagementException; /** - * Get the shared parent application ID for the given chile application ID and parent organization ID. + * Get the shared parent application ID for the given child application ID of the given child organization. * * @param childAppId The unique ID of the shared child application. - * @param parentOrgId The organization ID of the parent. * @param childOrgId The organization ID of the child. * @return The unique identifier of the shared parent application. * @throws OrganizationManagementException If errors occurred when retrieving the parent application ID. */ - default String getParentAppId(String childAppId, String childOrgId, String parentOrgId) + default String getParentAppId(String childAppId, String childOrgId) throws OrganizationManagementException { throw new NotImplementedException("getParentAppId method is not implemented in " + this.getClass().getName()); diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java index 0a594c004..b3bed171d 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java @@ -657,14 +657,20 @@ public void shareApplication(String ownerOrgId, String sharedOrgId, ServiceProvi } @Override - public String getParentAppId(String childAppId, String childOrgId, String parentOrgId) + public String getParentAppId(String childAppId, String childOrgId) throws OrganizationManagementException { Optional mainApplicationDO = getOrgApplicationMgtDAO().getMainApplication(childAppId, childOrgId); - if (mainApplicationDO.isPresent()) { + if (!mainApplicationDO.isPresent()) { + return null; + } + + List ancestorOrganizationIds = getOrganizationManager().getAncestorOrganizationIds(childOrgId); + if (!ancestorOrganizationIds.isEmpty() && ancestorOrganizationIds.size() > 1) { + String parentOrgId = ancestorOrganizationIds.get(1); String rootOrgId = mainApplicationDO.get().getOrganizationId(); - if (rootOrgId.equals(parentOrgId)) { + if (parentOrgId.equals(rootOrgId)) { return mainApplicationDO.get().getMainApplicationId(); } return getOrgApplicationMgtDAO().getParentAppId(mainApplicationDO.get().getMainApplicationId(), rootOrgId, 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 34368cc28..2a6be6f74 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 @@ -45,6 +45,9 @@ import static org.mockito.Mockito.when; import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.IS_FRAGMENT_APP; +/** + * Unit tests for OrgApplicationManagerImpl.. + */ public class OrgApplicationManagerImplTest { @Mock @@ -101,6 +104,11 @@ public void testGetParentAppId(String childAppId, String childOrgId, String pare boolean isAppSharedWithParent, String expectedParentAppId) throws Exception { + List ancestorOrganizationIds = new ArrayList<>(); + ancestorOrganizationIds.add(childOrgId); + ancestorOrganizationIds.add(parentOrgId); + when(organizationManager.getAncestorOrganizationIds(childOrgId)).thenReturn(ancestorOrganizationIds); + MainApplicationDO mainApp = new MainApplicationDO(rootOrgId, rootAppId); if (parentAppId != null) { when(orgApplicationMgtDAO.getMainApplication(childAppId, childOrgId)).thenReturn(Optional.of(mainApp)); @@ -113,7 +121,7 @@ public void testGetParentAppId(String childAppId, String childOrgId, String pare } OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); - String resolvedParentAppId = orgApplicationManager.getParentAppId(childAppId, childOrgId, parentOrgId); + String resolvedParentAppId = orgApplicationManager.getParentAppId(childAppId, childOrgId); Assert.assertEquals(resolvedParentAppId, expectedParentAppId); } @@ -125,8 +133,7 @@ public void testGetParentAppIdWithInvalidChildAppId() throws Exception { when(orgApplicationMgtDAO.getMainApplication(invalidChildAppId, CHILD_ORG_ID)).thenReturn(Optional.empty()); OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); - String resolvedParentAppId = orgApplicationManager.getParentAppId(invalidChildAppId, CHILD_ORG_ID, - PARENT_ORG_ID); + String resolvedParentAppId = orgApplicationManager.getParentAppId(invalidChildAppId, CHILD_ORG_ID); Assert.assertNull(resolvedParentAppId); } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java index 13a1c4f10..cdae13441 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java @@ -39,6 +39,9 @@ import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.when; +/** + * Unit tests for OrgApplicationMgtDAOImpl.. + */ public class OrgApplicationMgtDAOImplTest { private static final String DB_NAME = "testOrgAppMgtDB"; From a4e9ff277f5aca4f3a07937cb4bb14b0bc5a4ebd Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 2 Jul 2024 10:01:50 +0530 Subject: [PATCH 08/15] Introduce getAncestorAppIds method instead of retrieving parent app id. --- .../pom.xml | 4 + .../application/OrgApplicationManager.java | 13 +- .../OrgApplicationManagerImpl.java | 43 +++---- .../application/constant/SQLConstants.java | 12 +- .../application/dao/OrgApplicationMgtDAO.java | 17 +-- .../dao/impl/OrgApplicationMgtDAOImpl.java | 34 ++++-- .../OrgApplicationManagerImplTest.java | 114 ++++++++++++------ .../impl/OrgApplicationMgtDAOImplTest.java | 107 ++++++++++++---- pom.xml | 6 + 9 files changed, 240 insertions(+), 110 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/pom.xml b/components/org.wso2.carbon.identity.organization.management.application/pom.xml index e7ba7f116..9a84e5acd 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/pom.xml +++ b/components/org.wso2.carbon.identity.organization.management.application/pom.xml @@ -92,6 +92,10 @@ org.wso2.carbon.identity.framework org.wso2.carbon.identity.role.v2.mgt.core + + commons-collections.wso2 + commons-collections + org.testng diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java index 610d30002..0d2517d5d 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java @@ -148,17 +148,18 @@ void shareApplication(String ownerOrgId, String sharedOrgId, ServiceProvider mai boolean shareWithAllChildren) throws OrganizationManagementException; /** - * Get the shared parent application ID for the given child application ID of the given child organization. + * Get the shared ancestor application IDs for the given child application ID of the given child organization. * - * @param childAppId The unique ID of the shared child application. - * @param childOrgId The organization ID of the child. - * @return The unique identifier of the shared parent application. + * @param childAppId The unique ID of the shared child application. + * @param childOrgId The organization ID of the child. + * @return Map containing shared ancestor application IDs and their organization IDs. * @throws OrganizationManagementException If errors occurred when retrieving the parent application ID. */ - default String getParentAppId(String childAppId, String childOrgId) + default Map getAncestorAppIds(String childAppId, String childOrgId) throws OrganizationManagementException { - throw new NotImplementedException("getParentAppId method is not implemented in " + this.getClass().getName()); + throw new NotImplementedException( + "getAncestorAppIds method is not implemented in " + this.getClass().getName()); } /** diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java index b3bed171d..6ca818293 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java @@ -657,33 +657,35 @@ public void shareApplication(String ownerOrgId, String sharedOrgId, ServiceProvi } @Override - public String getParentAppId(String childAppId, String childOrgId) + public Map getAncestorAppIds(String childAppId, String childOrgId) throws OrganizationManagementException { Optional mainApplicationDO = getOrgApplicationMgtDAO().getMainApplication(childAppId, childOrgId); if (!mainApplicationDO.isPresent()) { - return null; + return Collections.emptyMap(); } + String rootOrgId = mainApplicationDO.get().getOrganizationId(); + String rootAppId = mainApplicationDO.get().getMainApplicationId(); List ancestorOrganizationIds = getOrganizationManager().getAncestorOrganizationIds(childOrgId); - if (!ancestorOrganizationIds.isEmpty() && ancestorOrganizationIds.size() > 1) { - String parentOrgId = ancestorOrganizationIds.get(1); - String rootOrgId = mainApplicationDO.get().getOrganizationId(); - if (parentOrgId.equals(rootOrgId)) { - return mainApplicationDO.get().getMainApplicationId(); - } - return getOrgApplicationMgtDAO().getParentAppId(mainApplicationDO.get().getMainApplicationId(), rootOrgId, - parentOrgId); + Map ancestorAppIds = new HashMap<>(); + // Add root app to the map. + ancestorAppIds.put(rootOrgId, rootAppId); + if (!ancestorOrganizationIds.isEmpty() && ancestorOrganizationIds.size() > 2) { + getOrgApplicationMgtDAO().getSharedApplications(rootAppId, rootOrgId, + ancestorOrganizationIds.subList(1, ancestorOrganizationIds.size() - 1)).forEach( + sharedApplication -> ancestorAppIds.put(sharedApplication.getOrganizationId(), + sharedApplication.getFragmentApplicationId())); } - return null; + return ancestorAppIds; } @Override public Map getChildAppIds(String parentAppId, String parentOrgId, List childOrgIds) throws OrganizationManagementException { - if (childOrgIds == null || childOrgIds.isEmpty()) { + if (CollectionUtils.isEmpty(childOrgIds)) { return Collections.emptyMap(); } @@ -705,14 +707,14 @@ public Map getChildAppIds(String parentAppId, String parentOrgId Boolean.parseBoolean(property.getValue())); if (!isFragmentApp) { // Parent app is the main application - return filterSharedApplications(parentAppId, parentOrgId, childOrgIds); + return getFilteredChildApplications(parentAppId, parentOrgId, childOrgIds); } } Optional mainApplicationDO = getOrgApplicationMgtDAO().getMainApplication(parentAppId, parentOrgId); if (mainApplicationDO.isPresent()) { - return filterSharedApplications(mainApplicationDO.get().getMainApplicationId(), + return getFilteredChildApplications(mainApplicationDO.get().getMainApplicationId(), mainApplicationDO.get().getOrganizationId(), childOrgIds); } return Collections.emptyMap(); @@ -966,21 +968,20 @@ private boolean shouldUpdateShareWithAllChildren(boolean shareWithAllChildren, S } /** - * Filter and return shared applications of the given main application for the given child organization IDs. + * Filter and return shared child applications of the given main application for the given child organization IDs. * * @param mainAppId Application ID of the main application. * @param mainOrgId Organization ID of the main application. * @return The map containing organization ID and application ID of the filtered shared applications. */ - private Map filterSharedApplications(String mainAppId, String mainOrgId, List childOrgIds) + private Map getFilteredChildApplications(String mainAppId, String mainOrgId, + List childOrgIds) throws OrganizationManagementException { List sharedApplications = - getOrgApplicationMgtDAO().getSharedApplications(mainOrgId, mainAppId); - return sharedApplications.stream() - .filter(app -> childOrgIds.contains(app.getOrganizationId())) - .collect(Collectors.toMap(SharedApplicationDO::getOrganizationId, - SharedApplicationDO::getFragmentApplicationId)); + getOrgApplicationMgtDAO().getSharedApplications(mainAppId, mainOrgId, childOrgIds); + return sharedApplications.stream().collect(Collectors.toMap(SharedApplicationDO::getOrganizationId, + SharedApplicationDO::getFragmentApplicationId)); } private OAuthAdminServiceImpl getOAuthAdminService() { diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java index 1b6a860f2..f2f3fd2f5 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java @@ -67,10 +67,11 @@ public class SQLConstants { SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_MAIN_APP_ID + "; AND OWNER_ORG_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_OWNER_ORG_ID + ";"; - public static final String GET_PARENT_APP_ID = "SELECT SHARED_APP_ID FROM SP_SHARED_APP WHERE MAIN_APP_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_MAIN_APP_ID + "; AND OWNER_ORG_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_OWNER_ORG_ID + "; AND SHARED_ORG_ID = :" + - SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_SHARED_ORG_ID + ";"; + public static final String GET_FILTERED_SHARED_APPLICATIONS = + "SELECT SHARED_ORG_ID, SHARED_APP_ID FROM SP_SHARED_APP WHERE MAIN_APP_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_MAIN_APP_ID + "; AND OWNER_ORG_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_OWNER_ORG_ID + "; AND SHARED_ORG_ID IN (" + + SQLPlaceholders.SHARED_ORG_ID_LIST_PLACEHOLDER + ")"; private SQLConstants() { @@ -91,6 +92,9 @@ public static final class SQLPlaceholders { public static final String DB_SCHEMA_COLUMN_NAME_METADATA_VALUE = "METADATA_VALUE"; public static final String DB_SCHEMA_COLUMN_NAME_SP_APP_ID = "SP_APP_ID"; + public static final String SHARED_ORG_ID_LIST_PLACEHOLDER = "_SHARED_ORG_ID_LIST_"; + public static final String SHARED_ORG_ID_PLACEHOLDER_PREFIX = "SHARED_ORG_ID_"; + private SQLPlaceholders() { } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java index 46b52c87e..9ac54051b 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java @@ -121,17 +121,20 @@ void updateShareWithAllChildren(String mainApplicationId, String ownerOrganizati throws OrganizationManagementException; /** - * Returns the unique identifier of the shared parent application. + * Returns the unique identifiers of shared applications associated with the given main application + * within the given shared organizations. * - * @param mainAppId The app ID of the main application. - * @param ownerOrgId The organization ID of the owner. - * @param parentOrgId The organization ID of the parent. + * @param mainAppId The app ID of the main application. + * @param ownerOrgId The organization ID of the owner. + * @param sharedOrgIds The list of app shared organization IDs. * @throws OrganizationManagementException The server exception is thrown in a failure - * when retrieving the parent app ID. + * when retrieving the shared apps. */ - default String getParentAppId(String mainAppId, String ownerOrgId, String parentOrgId) + default List getSharedApplications(String mainAppId, String ownerOrgId, + List sharedOrgIds) throws OrganizationManagementException { - throw new NotImplementedException("getParentAppId method is not implemented in " + this.getClass().getName()); + throw new NotImplementedException( + "getSharedApplications method is not implemented in " + this.getClass().getName()); } } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java index eb0670fb5..2a7c7b5d5 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java @@ -18,6 +18,7 @@ package org.wso2.carbon.identity.organization.management.application.dao.impl; +import org.apache.commons.collections.CollectionUtils; import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; import org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException; @@ -28,12 +29,15 @@ import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementServerException; +import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.IS_FRAGMENT_APP; +import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_FILTERED_SHARED_APPLICATIONS; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_MAIN_APPLICATION; -import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_PARENT_APP_ID; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_SHARED_APPLICATION; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_SHARED_APPLICATIONS; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_SHARED_APP_ID; @@ -50,11 +54,12 @@ import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_SHARE_WITH_ALL_CHILDREN; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_SP_APP_ID; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_SP_ID; +import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.SQLPlaceholders.SHARED_ORG_ID_LIST_PLACEHOLDER; +import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.SQLPlaceholders.SHARED_ORG_ID_PLACEHOLDER_PREFIX; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.UPDATE_SHARE_WITH_ALL_CHILDREN; import static org.wso2.carbon.identity.organization.management.application.util.OrgApplicationManagerUtil.getNewTemplate; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_CHECKING_APPLICATION_HAS_FRAGMENTS; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_CHECKING_APPLICATION_IS_A_FRAGMENT; -import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_GETTING_PARENT_APP_ID; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_LINK_APPLICATIONS; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_RESOLVING_MAIN_APPLICATION; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_RESOLVING_SHARED_APPLICATION; @@ -227,20 +232,35 @@ public void updateShareWithAllChildren(String mainApplicationId, String ownerOrg } @Override - public String getParentAppId(String mainAppId, String ownerOrgId, String parentOrgId) + public List getSharedApplications(String mainAppId, String ownerOrgId, + List sharedOrgIds) throws OrganizationManagementException { + if (CollectionUtils.isEmpty(sharedOrgIds)) { + return Collections.emptyList(); + } + + String placeholders = IntStream.range(0, sharedOrgIds.size()) + .mapToObj(i -> ":" + SHARED_ORG_ID_PLACEHOLDER_PREFIX + i + ";") + .collect(Collectors.joining(", ")); + String sqlStmt = GET_FILTERED_SHARED_APPLICATIONS.replace(SHARED_ORG_ID_LIST_PLACEHOLDER, placeholders); + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); try { - return namedJdbcTemplate.fetchSingleRecord(GET_PARENT_APP_ID, - (resultSet, rowNumber) -> resultSet.getString(DB_SCHEMA_COLUMN_NAME_SHARED_APP_ID), + return namedJdbcTemplate.executeQuery(sqlStmt, + (resultSet, rowNumber) -> new SharedApplicationDO( + resultSet.getString(DB_SCHEMA_COLUMN_NAME_SHARED_ORG_ID), + resultSet.getString(DB_SCHEMA_COLUMN_NAME_SHARED_APP_ID)), namedPreparedStatement -> { namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_MAIN_APP_ID, mainAppId); namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_OWNER_ORG_ID, ownerOrgId); - namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_SHARED_ORG_ID, parentOrgId); + for (int i = 0; i < sharedOrgIds.size(); i++) { + namedPreparedStatement.setString(SHARED_ORG_ID_PLACEHOLDER_PREFIX + i, sharedOrgIds.get(i)); + } }); } catch (DataAccessException e) { - throw handleServerException(ERROR_CODE_ERROR_GETTING_PARENT_APP_ID, e, mainAppId, parentOrgId); + throw handleServerException( + ERROR_CODE_ERROR_RESOLVING_SHARED_APPLICATION, e, mainAppId, ownerOrgId); } } 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 2a6be6f74..1466d5289 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 @@ -41,6 +41,8 @@ import java.util.Optional; import java.util.stream.Collectors; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.IS_FRAGMENT_APP; @@ -52,15 +54,15 @@ public class OrgApplicationManagerImplTest { @Mock private OrgApplicationMgtDAO orgApplicationMgtDAO; - @Mock private OrganizationManager organizationManager; - @Mock private ApplicationManagementService applicationManagementService; + private OrgApplicationManager orgApplicationManager; + private static final Map childAppIdMap = new HashMap() {{ - put("30b701c6-e309-4241-b047-0c299c45d1a0", "42ef1d92-add6-449b-8a3c-fc308d2a4eac"); + put("99b701c6-e309-4241-b047-0c299c45d1a0", "56ef1d92-add6-449b-8a3c-fc308d2a4eac"); put("5b65d2f9-1b0c-4e66-9f2c-3aa57dcd1ef7", "ede1f19f-c8b4-4f3c-a19d-18aab939ad3f"); put("8o65d2l6-1u0c-4e76-9f2c-4aa67trt1ef8", "udt1f16f-c8y4-4r3c-a20d-58hhg969ad6p"); }}; @@ -72,6 +74,8 @@ public class OrgApplicationManagerImplTest { private static final String ROOT_APP_ID = "fa9b9ac5-a429-49e2-9c51-4259c7ebe45e"; private static final String ROOT_ORG_ID = "72b81cba-51c7-4dc1-91be-b267e177c17a"; private static final String ROOT_TENANT_DOMAIN = "root-organization"; + private static final String INVALID_CHILD_APP_ID = "6t9ef3df-f966-5839-9123-8ki61dda7c88"; + private static final String INVALID_PARENT_APP_ID = "5y9ef3df-f966-5839-9693-8ki61dda7c77"; @BeforeMethod public void setUp() { @@ -80,6 +84,8 @@ public void setUp() { OrgApplicationMgtDataHolder.getInstance().setOrgApplicationMgtDAO(orgApplicationMgtDAO); OrgApplicationMgtDataHolder.getInstance().setOrganizationManager(organizationManager); OrgApplicationMgtDataHolder.getInstance().setApplicationManagementService(applicationManagementService); + + orgApplicationManager = new OrgApplicationManagerImpl(); } @DataProvider(name = "parentAppIdRetrievalTestData") @@ -98,44 +104,75 @@ public Object[][] getParentAppIdRetrievalTestData() { }; } - @Test(dataProvider = "parentAppIdRetrievalTestData") - public void testGetParentAppId(String childAppId, String childOrgId, String parentAppId, - String parentOrgId, String rootAppId, String rootOrgId, - boolean isAppSharedWithParent, String expectedParentAppId) - throws Exception { + @Test + public void testGetAncestorAppIdsOfChildApp() throws Exception { List ancestorOrganizationIds = new ArrayList<>(); - ancestorOrganizationIds.add(childOrgId); - ancestorOrganizationIds.add(parentOrgId); - when(organizationManager.getAncestorOrganizationIds(childOrgId)).thenReturn(ancestorOrganizationIds); + ancestorOrganizationIds.add(CHILD_ORG_ID); + ancestorOrganizationIds.add(PARENT_ORG_ID); + ancestorOrganizationIds.add(ROOT_ORG_ID); + when(organizationManager.getAncestorOrganizationIds(CHILD_ORG_ID)).thenReturn(ancestorOrganizationIds); + + List ancestorApplications = new ArrayList<>(); + SharedApplicationDO parentApplicationDO = new SharedApplicationDO(PARENT_ORG_ID, PARENT_APP_ID); + ancestorApplications.add(parentApplicationDO); + + MainApplicationDO mainApp = new MainApplicationDO(ROOT_ORG_ID, ROOT_APP_ID); + when(orgApplicationMgtDAO.getMainApplication(CHILD_APP_ID, CHILD_ORG_ID)).thenReturn(Optional.of(mainApp)); + when(orgApplicationMgtDAO.getSharedApplications(eq(ROOT_APP_ID), eq(ROOT_ORG_ID), anyList())) + .thenReturn(ancestorApplications); + + Map resolvedAncestorAppIds = + orgApplicationManager.getAncestorAppIds(CHILD_APP_ID, CHILD_ORG_ID); + + Assert.assertNotNull(resolvedAncestorAppIds); + Assert.assertEquals(resolvedAncestorAppIds.size(), 2); + Assert.assertEquals(resolvedAncestorAppIds.get(PARENT_ORG_ID), PARENT_APP_ID); + Assert.assertEquals(resolvedAncestorAppIds.get(ROOT_ORG_ID), ROOT_APP_ID); + } - MainApplicationDO mainApp = new MainApplicationDO(rootOrgId, rootAppId); - if (parentAppId != null) { - when(orgApplicationMgtDAO.getMainApplication(childAppId, childOrgId)).thenReturn(Optional.of(mainApp)); - when(orgApplicationMgtDAO.getParentAppId(rootAppId, rootOrgId, parentOrgId)).thenReturn(parentAppId); - } else if (isAppSharedWithParent) { - when(orgApplicationMgtDAO.getMainApplication(childAppId, childOrgId)).thenReturn(Optional.empty()); - } else { - when(orgApplicationMgtDAO.getMainApplication(childAppId, childOrgId)).thenReturn(Optional.of(mainApp)); - when(orgApplicationMgtDAO.getParentAppId(rootAppId, rootOrgId, parentOrgId)).thenReturn(null); - } - - OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); - String resolvedParentAppId = orgApplicationManager.getParentAppId(childAppId, childOrgId); - - Assert.assertEquals(resolvedParentAppId, expectedParentAppId); + @Test + public void testGetAncestorAppIdsOfParentApp() throws Exception { + + List ancestorOrganizationIds = new ArrayList<>(); + ancestorOrganizationIds.add(PARENT_ORG_ID); + ancestorOrganizationIds.add(ROOT_ORG_ID); + when(organizationManager.getAncestorOrganizationIds(PARENT_ORG_ID)).thenReturn(ancestorOrganizationIds); + + MainApplicationDO mainApp = new MainApplicationDO(ROOT_ORG_ID, ROOT_APP_ID); + when(orgApplicationMgtDAO.getMainApplication(PARENT_APP_ID, PARENT_ORG_ID)).thenReturn(Optional.of(mainApp)); + when(orgApplicationMgtDAO.getSharedApplications(eq(ROOT_APP_ID), eq(ROOT_ORG_ID), anyList())) + .thenReturn(Collections.emptyList()); + + Map resolvedAncestorAppIds = + orgApplicationManager.getAncestorAppIds(PARENT_APP_ID, PARENT_ORG_ID); + + Assert.assertNotNull(resolvedAncestorAppIds); + Assert.assertEquals(resolvedAncestorAppIds.size(), 1); + Assert.assertEquals(resolvedAncestorAppIds.get(ROOT_ORG_ID), ROOT_APP_ID); + } + + @Test + public void testGetAncestorAppIdsOfRootApp() throws Exception { + + when(orgApplicationMgtDAO.getMainApplication(ROOT_APP_ID, ROOT_ORG_ID)).thenReturn(Optional.empty()); + + Map resolvedAncestorAppIds = + orgApplicationManager.getAncestorAppIds(ROOT_APP_ID, ROOT_ORG_ID); + + Assert.assertNotNull(resolvedAncestorAppIds); + Assert.assertEquals(resolvedAncestorAppIds.size(), 0); } @Test - public void testGetParentAppIdWithInvalidChildAppId() throws Exception { + public void testGetAncestorAppIdsWithInvalidChildAppId() throws Exception { - String invalidChildAppId = "6t9ef3df-f966-5839-9123-8ki61dda7c88"; - when(orgApplicationMgtDAO.getMainApplication(invalidChildAppId, CHILD_ORG_ID)).thenReturn(Optional.empty()); + when(orgApplicationMgtDAO.getMainApplication(INVALID_CHILD_APP_ID, CHILD_ORG_ID)).thenReturn(Optional.empty()); - OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); - String resolvedParentAppId = orgApplicationManager.getParentAppId(invalidChildAppId, CHILD_ORG_ID); + Map resolvedAncestorAppIds = + orgApplicationManager.getAncestorAppIds(INVALID_CHILD_APP_ID, CHILD_ORG_ID); - Assert.assertNull(resolvedParentAppId); + Assert.assertEquals(resolvedAncestorAppIds, Collections.emptyMap()); } @DataProvider(name = "childAppIdsRetrievalTestData") @@ -195,9 +232,9 @@ public void testGetChildAppIds(String parentAppId, String parentOrgId, String pa MainApplicationDO mainApp = new MainApplicationDO(rootOrgId, rootAppId); when(orgApplicationMgtDAO.getMainApplication(parentAppId, parentOrgId)).thenReturn(Optional.of(mainApp)); - when(orgApplicationMgtDAO.getSharedApplications(rootOrgId, rootAppId)).thenReturn(sharedApplications); + when(orgApplicationMgtDAO.getSharedApplications(rootAppId, rootOrgId, childOrgIds)) + .thenReturn(sharedApplications); - OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); Map resolvedChildAppIdMap = orgApplicationManager.getChildAppIds(parentAppId, parentOrgId, childOrgIds); @@ -207,15 +244,14 @@ public void testGetChildAppIds(String parentAppId, String parentOrgId, String pa @Test public void testGetChildAppIdsWithInvalidParentAppId() throws Exception { - String invalidParentAppId = "5y9ef3df-f966-5839-9693-8ki61dda7c77"; when(organizationManager.resolveTenantDomain(PARENT_ORG_ID)).thenReturn(PARENT_TENANT_DOMAIN); - when(applicationManagementService.getApplicationByResourceId(invalidParentAppId, + when(applicationManagementService.getApplicationByResourceId(INVALID_PARENT_APP_ID, PARENT_TENANT_DOMAIN)).thenReturn(null); - when(orgApplicationMgtDAO.getMainApplication(invalidParentAppId, PARENT_ORG_ID)).thenReturn(Optional.empty()); + when(orgApplicationMgtDAO.getMainApplication(INVALID_PARENT_APP_ID, PARENT_ORG_ID)).thenReturn( + Optional.empty()); - OrgApplicationManager orgApplicationManager = new OrgApplicationManagerImpl(); Map resolvedChildAppIdMap = - orgApplicationManager.getChildAppIds(invalidParentAppId, PARENT_ORG_ID, + orgApplicationManager.getChildAppIds(INVALID_PARENT_APP_ID, PARENT_ORG_ID, new ArrayList<>(childAppIdMap.keySet())); Assert.assertEquals(resolvedChildAppIdMap, Collections.emptyMap()); diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java index cdae13441..011c1e111 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java @@ -26,12 +26,16 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; +import org.wso2.carbon.identity.organization.management.application.model.SharedApplicationDO; import java.nio.file.Paths; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.sql.DataSource; @@ -48,10 +52,19 @@ public class OrgApplicationMgtDAOImplTest { private static final String H2_SCRIPT_NAME = "h2.sql"; private static Map dataSourceMap = new HashMap<>(); + private OrgApplicationMgtDAOImpl orgApplicationMgtDAO; + private static final String ROOT_APP_ID = "fa9b9ac5-a429-49e2-9c51-4259c7ebe45e"; private static final String ROOT_APP_NAME = "test-app"; private static final String ROOT_ORG_ID = "72b81cba-51c7-4dc1-91be-b267e177c17a"; private static final int ROOT_TENANT_ID = 1; + private static final String SHARED_APP_ID_1 = "42ef1d92-add6-449b-8a3c-fc308d2a4eac"; + private static final String SHARED_ORG_ID_1 = "30b701c6-e309-4241-b047-0c299c45d1a0"; + private static final int SHARED_TENANT_ID_1 = 2; + private static final String SHARED_APP_ID_2 = "1e2ef3df-e670-4339-9833-9df41dda7c96"; + private static final String SHARED_ORG_ID_2 = "93d996f9-a5ba-4275-a52b-adaad9eba869"; + private static final int SHARED_TENANT_ID_2 = 3; + private static final String UNSHARED_ORG_ID = "89d996f9-a5ba-4275-a52b-adaad9eba869"; private static final String PRIMARY_USERSTORE = "PRIMARY"; private static final String ADMIN_USERNAME = "admin"; private static final String AUTH_TYPE = "default"; @@ -61,54 +74,72 @@ public class OrgApplicationMgtDAOImplTest { private static final String SHARE_APPLICATION_SQL_STATEMENT = "INSERT INTO SP_SHARED_APP " + "(MAIN_APP_ID, OWNER_ORG_ID, SHARED_APP_ID, SHARED_ORG_ID, SHARE_WITH_ALL_CHILDREN) " + "VALUES (?, ?, ?, ?, ?);"; + private static final String DELETE_APPLICATION_SQL_STATEMENT = "DELETE FROM SP_APP " + + "WHERE UUID = ?;"; + private static final String DELETE_SHARED_APPLICATION_SQL_STATEMENT = "DELETE FROM SP_SHARED_APP " + + "WHERE SHARED_APP_ID = ?;"; @BeforeClass public void setUp() throws Exception { initiateH2Base(); mockDataSource(); + + // Create root application. createApplication(ROOT_TENANT_ID, ROOT_APP_NAME, ROOT_APP_ID, PRIMARY_USERSTORE, ADMIN_USERNAME, AUTH_TYPE); + + orgApplicationMgtDAO = new OrgApplicationMgtDAOImpl(); } @AfterClass public void tearDown() throws Exception { + // Delete root application. + deleteApplication(ROOT_APP_ID); closeH2Base(); } - @DataProvider(name = "parentAppIdRetrievalTestData") - public Object[][] getParentAppIdRetrievalTestData() throws Exception { - - String parentAppId = "1e2ef3df-e670-4339-9833-9df41dda7c96"; - String parentOrgId = "93d996f9-a5ba-4275-a52b-adaad9eba869"; - int parentTenantId = 2; - String invalidParentOrgId = "6t9ef3df-f966-5839-9123-8ki61dda7c88"; - - createApplication(parentTenantId, ROOT_APP_NAME, parentAppId, PRIMARY_USERSTORE, ADMIN_USERNAME, AUTH_TYPE); - shareApplication(ROOT_APP_ID, ROOT_ORG_ID, parentAppId, parentOrgId); + @DataProvider(name = "filteredSharedApplicationsTestData") + public Object[][] getFilteredSharedApplicationsTestData() { return new Object[][]{ - // App is shared with the given parent org. - {parentOrgId, parentAppId}, - // Since root org does not have a parent, null should be returned. - {ROOT_ORG_ID, null}, - // App is not shared with the given parent org or - // the parent org does not exist, null should be returned. - {invalidParentOrgId, null}, - // When parent org id is empty, null should be returned. - {"", null}, - // When parent org id is null, null should be returned. - {null, null} + // Passing org ids of both shared apps only. + {Arrays.asList(SHARED_ORG_ID_1, SHARED_ORG_ID_2), 2}, + // Passing org ids of both shared apps and an unshared org id. + {Arrays.asList(SHARED_ORG_ID_1, SHARED_ORG_ID_2, UNSHARED_ORG_ID), 2}, + // Passing org id of one shared app only. + {Collections.singletonList(SHARED_ORG_ID_1), 1}, + // Passing org id of shared app and an unshared org id. + {Arrays.asList(SHARED_ORG_ID_1, UNSHARED_ORG_ID), 1}, + // Passing an unshared org id only. + {Collections.singletonList(UNSHARED_ORG_ID), 0}, + // Passing an empty list + {Collections.emptyList(), 0}, }; } - @Test(dataProvider = "parentAppIdRetrievalTestData") - public void testGetParentAppId(String parentOrgId, String expectedParentId) throws Exception { + @Test(dataProvider = "filteredSharedApplicationsTestData") + public void testGetFilteredSharedApplications(List sharedOrgIds, int expectedNumOfApps) throws Exception { + + // Create shared application 1. + createApplication(SHARED_TENANT_ID_1, ROOT_APP_NAME, SHARED_APP_ID_1, PRIMARY_USERSTORE, ADMIN_USERNAME, + AUTH_TYPE); + shareApplication(ROOT_APP_ID, ROOT_ORG_ID, SHARED_APP_ID_1, SHARED_ORG_ID_1); - OrgApplicationMgtDAOImpl orgApplicationMgtDAO = new OrgApplicationMgtDAOImpl(); - String parentAppId = orgApplicationMgtDAO.getParentAppId(ROOT_APP_ID, ROOT_ORG_ID, parentOrgId); + // Create shared application 2. + createApplication(SHARED_TENANT_ID_2, ROOT_APP_NAME, SHARED_APP_ID_2, PRIMARY_USERSTORE, ADMIN_USERNAME, + AUTH_TYPE); + shareApplication(ROOT_APP_ID, ROOT_ORG_ID, SHARED_APP_ID_2, SHARED_ORG_ID_2); - Assert.assertEquals(parentAppId, expectedParentId); + List sharedApplications = + orgApplicationMgtDAO.getSharedApplications(ROOT_APP_ID, ROOT_ORG_ID, sharedOrgIds); + + Assert.assertNotNull(sharedApplications); + Assert.assertEquals(sharedApplications.size(), expectedNumOfApps); + + // Delete shared applications. + deleteSharedApplication(SHARED_APP_ID_1); + deleteSharedApplication(SHARED_APP_ID_2); } private void initiateH2Base() throws Exception { @@ -188,4 +219,28 @@ private void shareApplication(String rootAppId, String rootOrgId, String sharedA sharedOrgId, e); } } + + private void deleteApplication(String appId) { + + try (Connection connection = getConnection()) { + PreparedStatement statement = connection.prepareStatement(DELETE_APPLICATION_SQL_STATEMENT); + statement.setString(1, appId); + statement.execute(); + } catch (SQLException e) { + throw new RuntimeException("Error while deleting application: " + appId, e); + } + } + + private void deleteSharedApplication(String appId) { + + try (Connection connection = getConnection()) { + PreparedStatement statement = connection.prepareStatement(DELETE_SHARED_APPLICATION_SQL_STATEMENT); + statement.setString(1, appId); + statement.execute(); + } catch (SQLException e) { + throw new RuntimeException("Error while deleting shared application: " + appId, e); + } + + deleteApplication(appId); + } } diff --git a/pom.xml b/pom.xml index a6d237435..81a488a56 100644 --- a/pom.xml +++ b/pom.xml @@ -263,6 +263,11 @@ ${findbugs.annotations.version} provided + + commons-collections.wso2 + commons-collections + ${apache.common.collection.version} + @@ -543,6 +548,7 @@ [1.4.0,2.0.0) 9.0.31.wso2v1 + 3.2.0.wso2v1 [2.6,3) [1.2,2) [3.2.0,4.0.0) From 33f8832d92a2c8ae080a418610965e62c5611e1a Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 2 Jul 2024 10:34:06 +0530 Subject: [PATCH 09/15] Bump organization management core version. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 81a488a56..29376fc2a 100644 --- a/pom.xml +++ b/pom.xml @@ -517,7 +517,7 @@ [1.0.0,2.0.0) - 1.1.2 + 1.1.9 [1.0.0,2.0.0) From 89684f6d14f517dd21d4bd7e3eec325ad92324ec Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 2 Jul 2024 11:08:58 +0530 Subject: [PATCH 10/15] Refactor getAncestorAppIds and getFilteredChildApplications methods. --- .../application/OrgApplicationManagerImpl.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java index 6ca818293..be81c783b 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java @@ -673,10 +673,11 @@ public Map getAncestorAppIds(String childAppId, String childOrgI // Add root app to the map. ancestorAppIds.put(rootOrgId, rootAppId); if (!ancestorOrganizationIds.isEmpty() && ancestorOrganizationIds.size() > 2) { - getOrgApplicationMgtDAO().getSharedApplications(rootAppId, rootOrgId, - ancestorOrganizationIds.subList(1, ancestorOrganizationIds.size() - 1)).forEach( - sharedApplication -> ancestorAppIds.put(sharedApplication.getOrganizationId(), - sharedApplication.getFragmentApplicationId())); + List ancestorApplications = + getOrgApplicationMgtDAO().getSharedApplications(rootAppId, rootOrgId, + ancestorOrganizationIds.subList(1, ancestorOrganizationIds.size() - 1)); + ancestorApplications.forEach(ancestorApplication -> ancestorAppIds.put( + ancestorApplication.getOrganizationId(), ancestorApplication.getFragmentApplicationId())); } return ancestorAppIds; } @@ -978,10 +979,10 @@ private Map getFilteredChildApplications(String mainAppId, Strin List childOrgIds) throws OrganizationManagementException { - List sharedApplications = + List childApplications = getOrgApplicationMgtDAO().getSharedApplications(mainAppId, mainOrgId, childOrgIds); - return sharedApplications.stream().collect(Collectors.toMap(SharedApplicationDO::getOrganizationId, - SharedApplicationDO::getFragmentApplicationId)); + return childApplications.stream().collect(Collectors.toMap( + SharedApplicationDO::getOrganizationId, SharedApplicationDO::getFragmentApplicationId)); } private OAuthAdminServiceImpl getOAuthAdminService() { From 646273cadc7868903f5c8835a7e1572e391d770a Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 2 Jul 2024 11:52:16 +0530 Subject: [PATCH 11/15] Use CollectionUtils isNotEmpty method to check if ancestorOrganizationIds list is empty. --- .../management/application/OrgApplicationManagerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java index be81c783b..0ce23e3a8 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java @@ -672,7 +672,7 @@ public Map getAncestorAppIds(String childAppId, String childOrgI Map ancestorAppIds = new HashMap<>(); // Add root app to the map. ancestorAppIds.put(rootOrgId, rootAppId); - if (!ancestorOrganizationIds.isEmpty() && ancestorOrganizationIds.size() > 2) { + if (CollectionUtils.isNotEmpty(ancestorOrganizationIds) && ancestorOrganizationIds.size() > 2) { List ancestorApplications = getOrgApplicationMgtDAO().getSharedApplications(rootAppId, rootOrgId, ancestorOrganizationIds.subList(1, ancestorOrganizationIds.size() - 1)); From aa5dd2b2611166505025b45e7b29d979bda437d8 Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 2 Jul 2024 13:15:06 +0530 Subject: [PATCH 12/15] Refactor getAncestorAppIds method. --- .../application/OrgApplicationManagerImpl.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java index 0ce23e3a8..41f654a54 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java @@ -666,15 +666,15 @@ public Map getAncestorAppIds(String childAppId, String childOrgI return Collections.emptyMap(); } - String rootOrgId = mainApplicationDO.get().getOrganizationId(); - String rootAppId = mainApplicationDO.get().getMainApplicationId(); + String ownerOrgId = mainApplicationDO.get().getOrganizationId(); + String mainAppId = mainApplicationDO.get().getMainApplicationId(); List ancestorOrganizationIds = getOrganizationManager().getAncestorOrganizationIds(childOrgId); Map ancestorAppIds = new HashMap<>(); - // Add root app to the map. - ancestorAppIds.put(rootOrgId, rootAppId); + // Add main app to the map. + ancestorAppIds.put(ownerOrgId, mainAppId); if (CollectionUtils.isNotEmpty(ancestorOrganizationIds) && ancestorOrganizationIds.size() > 2) { List ancestorApplications = - getOrgApplicationMgtDAO().getSharedApplications(rootAppId, rootOrgId, + getOrgApplicationMgtDAO().getSharedApplications(mainAppId, ownerOrgId, ancestorOrganizationIds.subList(1, ancestorOrganizationIds.size() - 1)); ancestorApplications.forEach(ancestorApplication -> ancestorAppIds.put( ancestorApplication.getOrganizationId(), ancestorApplication.getFragmentApplicationId())); @@ -707,7 +707,7 @@ public Map getChildAppIds(String parentAppId, String parentOrgId .anyMatch(property -> IS_FRAGMENT_APP.equals(property.getName()) && Boolean.parseBoolean(property.getValue())); if (!isFragmentApp) { - // Parent app is the main application + // Parent app is the main application. return getFilteredChildApplications(parentAppId, parentOrgId, childOrgIds); } } From 6bc8b6f5e9181faffb2c20befe3986561c7bce75 Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 2 Jul 2024 14:36:54 +0530 Subject: [PATCH 13/15] Improve comments. --- .../management/application/OrgApplicationManagerImpl.java | 1 + .../management/application/dao/OrgApplicationMgtDAO.java | 1 + .../application/dao/impl/OrgApplicationMgtDAOImplTest.java | 6 +++--- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java index 41f654a54..5e306d14c 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java @@ -974,6 +974,7 @@ private boolean shouldUpdateShareWithAllChildren(boolean shareWithAllChildren, S * @param mainAppId Application ID of the main application. * @param mainOrgId Organization ID of the main application. * @return The map containing organization ID and application ID of the filtered shared applications. + * @throws OrganizationManagementException If an error occurs while retrieving child applications. */ private Map getFilteredChildApplications(String mainAppId, String mainOrgId, List childOrgIds) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java index 9ac54051b..1f5ffb9a2 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java @@ -127,6 +127,7 @@ void updateShareWithAllChildren(String mainApplicationId, String ownerOrganizati * @param mainAppId The app ID of the main application. * @param ownerOrgId The organization ID of the owner. * @param sharedOrgIds The list of app shared organization IDs. + * @return The list of shared application IDs within the given shared organizations. * @throws OrganizationManagementException The server exception is thrown in a failure * when retrieving the shared apps. */ diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java index 011c1e111..d95756988 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/test/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImplTest.java @@ -44,7 +44,7 @@ import static org.mockito.Mockito.when; /** - * Unit tests for OrgApplicationMgtDAOImpl.. + * Unit tests for OrgApplicationMgtDAOImpl. */ public class OrgApplicationMgtDAOImplTest { @@ -74,9 +74,9 @@ public class OrgApplicationMgtDAOImplTest { private static final String SHARE_APPLICATION_SQL_STATEMENT = "INSERT INTO SP_SHARED_APP " + "(MAIN_APP_ID, OWNER_ORG_ID, SHARED_APP_ID, SHARED_ORG_ID, SHARE_WITH_ALL_CHILDREN) " + "VALUES (?, ?, ?, ?, ?);"; - private static final String DELETE_APPLICATION_SQL_STATEMENT = "DELETE FROM SP_APP " + + private static final String DELETE_APPLICATION_SQL_STATEMENT = "DELETE FROM SP_APP " + "WHERE UUID = ?;"; - private static final String DELETE_SHARED_APPLICATION_SQL_STATEMENT = "DELETE FROM SP_SHARED_APP " + + private static final String DELETE_SHARED_APPLICATION_SQL_STATEMENT = "DELETE FROM SP_SHARED_APP " + "WHERE SHARED_APP_ID = ?;"; @BeforeClass From d38bcc8e69df843dbfc90c47ac5d69547245ae24 Mon Sep 17 00:00:00 2001 From: dhaura Date: Wed, 3 Jul 2024 13:10:52 +0530 Subject: [PATCH 14/15] Modify getAncestorAppIds method to return the ancestor apps map including the given app. --- .../OrgApplicationManagerImpl.java | 54 +++++++++++++------ .../OrgApplicationManagerImplTest.java | 26 +++++++-- 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java index 5e306d14c..ce83fed21 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java @@ -663,6 +663,10 @@ public Map getAncestorAppIds(String childAppId, String childOrgI Optional mainApplicationDO = getOrgApplicationMgtDAO().getMainApplication(childAppId, childOrgId); if (!mainApplicationDO.isPresent()) { + // Check if the child app is a main application. + if (isMainApp(childAppId, childOrgId)) { + return Collections.singletonMap(childOrgId, childAppId); + } return Collections.emptyMap(); } @@ -670,12 +674,12 @@ public Map getAncestorAppIds(String childAppId, String childOrgI String mainAppId = mainApplicationDO.get().getMainApplicationId(); List ancestorOrganizationIds = getOrganizationManager().getAncestorOrganizationIds(childOrgId); Map ancestorAppIds = new HashMap<>(); - // Add main app to the map. + // Add main app to the map. ancestorAppIds.put(ownerOrgId, mainAppId); - if (CollectionUtils.isNotEmpty(ancestorOrganizationIds) && ancestorOrganizationIds.size() > 2) { + if (CollectionUtils.isNotEmpty(ancestorOrganizationIds) && ancestorOrganizationIds.size() > 1) { List ancestorApplications = getOrgApplicationMgtDAO().getSharedApplications(mainAppId, ownerOrgId, - ancestorOrganizationIds.subList(1, ancestorOrganizationIds.size() - 1)); + ancestorOrganizationIds.subList(0, ancestorOrganizationIds.size() - 1)); ancestorApplications.forEach(ancestorApplication -> ancestorAppIds.put( ancestorApplication.getOrganizationId(), ancestorApplication.getFragmentApplicationId())); } @@ -690,16 +694,41 @@ public Map getChildAppIds(String parentAppId, String parentOrgId return Collections.emptyMap(); } + // Check if the parent application is a main application. + if (isMainApp(parentAppId, parentOrgId)) { + return getFilteredChildApplications(parentAppId, parentOrgId, childOrgIds); + } + + Optional mainApplicationDO = + getOrgApplicationMgtDAO().getMainApplication(parentAppId, parentOrgId); + if (mainApplicationDO.isPresent()) { + return getFilteredChildApplications(mainApplicationDO.get().getMainApplicationId(), + mainApplicationDO.get().getOrganizationId(), childOrgIds); + } + return Collections.emptyMap(); + } + + /** + * Returns whether the given application is a main application. + * + * @param appId The unique ID of the application. + * @param orgId The organization ID of the given application. + * @return Returns true if the given application is a main application. + * @throws OrganizationManagementException If an error occurs while retrieving the application. + */ + private boolean isMainApp(String appId, String orgId) + throws OrganizationManagementException { + OrganizationManager organizationManager = OrgApplicationMgtDataHolder.getInstance().getOrganizationManager(); - String parentTenantDomain = organizationManager.resolveTenantDomain(parentOrgId); + String parentTenantDomain = organizationManager.resolveTenantDomain(orgId); ApplicationManagementService applicationManagementService = OrgApplicationMgtDataHolder.getInstance().getApplicationManagementService(); ServiceProvider serviceProvider; try { - serviceProvider = applicationManagementService.getApplicationByResourceId(parentAppId, parentTenantDomain); + serviceProvider = applicationManagementService.getApplicationByResourceId(appId, parentTenantDomain); } catch (IdentityApplicationManagementException e) { - throw handleServerException(ERROR_CODE_ERROR_RETRIEVING_APPLICATION, e, parentAppId); + throw handleServerException(ERROR_CODE_ERROR_RETRIEVING_APPLICATION, e, appId); } if (serviceProvider != null) { @@ -707,18 +736,11 @@ public Map getChildAppIds(String parentAppId, String parentOrgId .anyMatch(property -> IS_FRAGMENT_APP.equals(property.getName()) && Boolean.parseBoolean(property.getValue())); if (!isFragmentApp) { - // Parent app is the main application. - return getFilteredChildApplications(parentAppId, parentOrgId, childOrgIds); + // Given app is a main application. + return true; } } - - Optional mainApplicationDO = - getOrgApplicationMgtDAO().getMainApplication(parentAppId, parentOrgId); - if (mainApplicationDO.isPresent()) { - return getFilteredChildApplications(mainApplicationDO.get().getMainApplicationId(), - mainApplicationDO.get().getOrganizationId(), childOrgIds); - } - return Collections.emptyMap(); + return false; } /** 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 1466d5289..dbf68daa7 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 @@ -114,7 +114,9 @@ public void testGetAncestorAppIdsOfChildApp() throws Exception { when(organizationManager.getAncestorOrganizationIds(CHILD_ORG_ID)).thenReturn(ancestorOrganizationIds); List ancestorApplications = new ArrayList<>(); + SharedApplicationDO childApplicationDO = new SharedApplicationDO(CHILD_ORG_ID, CHILD_APP_ID); SharedApplicationDO parentApplicationDO = new SharedApplicationDO(PARENT_ORG_ID, PARENT_APP_ID); + ancestorApplications.add(childApplicationDO); ancestorApplications.add(parentApplicationDO); MainApplicationDO mainApp = new MainApplicationDO(ROOT_ORG_ID, ROOT_APP_ID); @@ -126,7 +128,8 @@ public void testGetAncestorAppIdsOfChildApp() throws Exception { orgApplicationManager.getAncestorAppIds(CHILD_APP_ID, CHILD_ORG_ID); Assert.assertNotNull(resolvedAncestorAppIds); - Assert.assertEquals(resolvedAncestorAppIds.size(), 2); + Assert.assertEquals(resolvedAncestorAppIds.size(), 3); + Assert.assertEquals(resolvedAncestorAppIds.get(CHILD_ORG_ID), CHILD_APP_ID); Assert.assertEquals(resolvedAncestorAppIds.get(PARENT_ORG_ID), PARENT_APP_ID); Assert.assertEquals(resolvedAncestorAppIds.get(ROOT_ORG_ID), ROOT_APP_ID); } @@ -139,16 +142,21 @@ public void testGetAncestorAppIdsOfParentApp() throws Exception { ancestorOrganizationIds.add(ROOT_ORG_ID); when(organizationManager.getAncestorOrganizationIds(PARENT_ORG_ID)).thenReturn(ancestorOrganizationIds); + List ancestorApplications = new ArrayList<>(); + SharedApplicationDO parentApplicationDO = new SharedApplicationDO(PARENT_ORG_ID, PARENT_APP_ID); + ancestorApplications.add(parentApplicationDO); + MainApplicationDO mainApp = new MainApplicationDO(ROOT_ORG_ID, ROOT_APP_ID); when(orgApplicationMgtDAO.getMainApplication(PARENT_APP_ID, PARENT_ORG_ID)).thenReturn(Optional.of(mainApp)); when(orgApplicationMgtDAO.getSharedApplications(eq(ROOT_APP_ID), eq(ROOT_ORG_ID), anyList())) - .thenReturn(Collections.emptyList()); + .thenReturn(ancestorApplications); Map resolvedAncestorAppIds = orgApplicationManager.getAncestorAppIds(PARENT_APP_ID, PARENT_ORG_ID); Assert.assertNotNull(resolvedAncestorAppIds); - Assert.assertEquals(resolvedAncestorAppIds.size(), 1); + Assert.assertEquals(resolvedAncestorAppIds.size(), 2); + Assert.assertEquals(resolvedAncestorAppIds.get(PARENT_ORG_ID), PARENT_APP_ID); Assert.assertEquals(resolvedAncestorAppIds.get(ROOT_ORG_ID), ROOT_APP_ID); } @@ -157,11 +165,21 @@ public void testGetAncestorAppIdsOfRootApp() throws Exception { when(orgApplicationMgtDAO.getMainApplication(ROOT_APP_ID, ROOT_ORG_ID)).thenReturn(Optional.empty()); + when(organizationManager.resolveTenantDomain(ROOT_ORG_ID)).thenReturn(ROOT_TENANT_DOMAIN); + + ServiceProvider rootApp = mock(ServiceProvider.class); + ServiceProviderProperty isFragmentAppProperty = new ServiceProviderProperty(); + isFragmentAppProperty.setName(IS_FRAGMENT_APP); + isFragmentAppProperty.setValue("false"); + when(rootApp.getSpProperties()).thenReturn(new ServiceProviderProperty[]{isFragmentAppProperty}); + when(applicationManagementService.getApplicationByResourceId(ROOT_APP_ID, ROOT_TENANT_DOMAIN)).thenReturn( + rootApp); + Map resolvedAncestorAppIds = orgApplicationManager.getAncestorAppIds(ROOT_APP_ID, ROOT_ORG_ID); Assert.assertNotNull(resolvedAncestorAppIds); - Assert.assertEquals(resolvedAncestorAppIds.size(), 0); + Assert.assertEquals(resolvedAncestorAppIds.size(), 1); } @Test From 4a880cda919340753b6c7bfbad302ced4aef7fcd Mon Sep 17 00:00:00 2001 From: dhaura Date: Thu, 4 Jul 2024 09:51:17 +0530 Subject: [PATCH 15/15] Improve comments and refactor code. --- .../management/application/OrgApplicationManager.java | 6 +++--- .../management/application/OrgApplicationManagerImpl.java | 7 +++---- .../application/OrgApplicationManagerImplTest.java | 6 ++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java index 0d2517d5d..d475623ff 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java @@ -153,7 +153,7 @@ void shareApplication(String ownerOrgId, String sharedOrgId, ServiceProvider mai * @param childAppId The unique ID of the shared child application. * @param childOrgId The organization ID of the child. * @return Map containing shared ancestor application IDs and their organization IDs. - * @throws OrganizationManagementException If errors occurred when retrieving the parent application ID. + * @throws OrganizationManagementException If errors occurred when retrieving the shared ancestor application IDs */ default Map getAncestorAppIds(String childAppId, String childOrgId) throws OrganizationManagementException { @@ -165,7 +165,7 @@ default Map getAncestorAppIds(String childAppId, String childOrg /** * Get shared child application IDs of the given parent application. * - * @param parentAppId The unique ID of the shared parent application. + * @param parentAppId The unique ID of the root app / shared app in parent org. * @param parentOrgId The organization ID of the parent. * @param childOrgIds The organization ID list of the children. * @return The map containing organization ID and application ID of the shared child applications. @@ -174,6 +174,6 @@ default Map getAncestorAppIds(String childAppId, String childOrg default Map getChildAppIds(String parentAppId, String parentOrgId, List childOrgIds) throws OrganizationManagementException { - throw new NotImplementedException("getParentAppId method is not implemented in " + this.getClass().getName()); + throw new NotImplementedException("getChildAppIds method is not implemented in " + this.getClass().getName()); } } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java index ce83fed21..537de4e56 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java @@ -716,17 +716,16 @@ public Map getChildAppIds(String parentAppId, String parentOrgId * @return Returns true if the given application is a main application. * @throws OrganizationManagementException If an error occurs while retrieving the application. */ - private boolean isMainApp(String appId, String orgId) - throws OrganizationManagementException { + private boolean isMainApp(String appId, String orgId) throws OrganizationManagementException { OrganizationManager organizationManager = OrgApplicationMgtDataHolder.getInstance().getOrganizationManager(); - String parentTenantDomain = organizationManager.resolveTenantDomain(orgId); + String tenantDomain = organizationManager.resolveTenantDomain(orgId); ApplicationManagementService applicationManagementService = OrgApplicationMgtDataHolder.getInstance().getApplicationManagementService(); ServiceProvider serviceProvider; try { - serviceProvider = applicationManagementService.getApplicationByResourceId(appId, parentTenantDomain); + serviceProvider = applicationManagementService.getApplicationByResourceId(appId, tenantDomain); } catch (IdentityApplicationManagementException e) { throw handleServerException(ERROR_CODE_ERROR_RETRIEVING_APPLICATION, e, appId); } 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 dbf68daa7..7ca83a0cd 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 @@ -48,7 +48,7 @@ import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.IS_FRAGMENT_APP; /** - * Unit tests for OrgApplicationManagerImpl.. + * Unit tests for OrgApplicationManagerImpl. */ public class OrgApplicationManagerImplTest { @@ -243,9 +243,7 @@ public void testGetChildAppIds(String parentAppId, String parentOrgId, String pa parentApp); List sharedApplications = childAppIdMap.entrySet().stream() - .map(app -> { - return new SharedApplicationDO(app.getKey(), app.getValue()); - }) + .map(app -> new SharedApplicationDO(app.getKey(), app.getValue())) .collect(Collectors.toList()); MainApplicationDO mainApp = new MainApplicationDO(rootOrgId, rootAppId);