Skip to content

Commit

Permalink
Merge pull request #12654 from RakhithaRR/tenant-rkm-notify
Browse files Browse the repository at this point in the history
Add notification event for tenant Resident KM creation
  • Loading branch information
RakhithaRR authored Oct 11, 2024
2 parents f178ce7 + db7b26c commit 76cc333
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
import org.wso2.carbon.apimgt.cleanup.service.OrganizationPurgeDAO;
import org.wso2.carbon.apimgt.impl.APIAdminImpl;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
import org.wso2.carbon.apimgt.impl.dao.GatewayArtifactsMgtDAO;
import org.wso2.carbon.apimgt.impl.dto.EventHubConfigurationDto;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
import org.wso2.carbon.user.api.UserStoreException;
Expand All @@ -53,11 +56,13 @@ public class IdpKmPurgeTest {
private OrganizationPurgeDAO organizationPurgeDAO;
private ApiMgtDAO apiMgtDAO;
private APIAdminImpl amAdmin;
private ServiceReferenceHolder serviceReferenceHolder;

@Before public void init() {
organizationPurgeDAO = Mockito.mock(OrganizationPurgeDAO.class);
apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
amAdmin = Mockito.mock(APIAdminImpl.class);
serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
}

@Test public void testOrganizationRemoval() throws APIManagementException, UserStoreException {
Expand All @@ -79,7 +84,24 @@ public class IdpKmPurgeTest {
Mockito.doReturn(true).when(organizationPurgeDAO).keyManagerOrganizationExist(Mockito.anyString());
Mockito.doNothing().when(amAdmin).deleteIdentityProvider("testOrg", kmConfig);

// Mock eventhub configuration
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito
.mock(APIManagerConfigurationService.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).
thenReturn(apiManagerConfigurationService);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
EventHubConfigurationDto eventHubConfigurationDto = Mockito.mock(EventHubConfigurationDto.class);
Mockito.when(apiManagerConfiguration.getEventHubConfigurationDto()).thenReturn(eventHubConfigurationDto);
Mockito.when(eventHubConfigurationDto.isEnabled()).thenReturn(true);

Mockito.when(APIUtil.isInternalOrganization("testOrg")).thenReturn(true);
Mockito.when(APIUtil.getAndSetDefaultKeyManagerConfiguration(Mockito.any()))
.thenReturn(new KeyManagerConfigurationDTO());
IdpKeyMangerPurge kmPurge = new IdpKeyManagerPurgeWrapper(organizationPurgeDAO);
LinkedHashMap<String, String> subtaskResult = kmPurge.purge("testOrg");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
import org.wso2.carbon.apimgt.impl.dto.TokenHandlingDto;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.keymgt.KeyMgtNotificationSender;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;

import java.util.Arrays;
import java.util.UUID;
Expand Down Expand Up @@ -79,6 +81,11 @@ public static void registerDefaultKeyManager(String organization) throws APIMana
keyManagerConfigurationDTO.addProperty(APIConstants.KeyManager.TOKEN_FORMAT_STRING,
new Gson().toJson(Arrays.asList(tokenHandlingDto)));
instance.addKeyManagerConfiguration(keyManagerConfigurationDTO);
// Populate the Resident Key Manager details and send the KM creation event
KeyManagerConfigurationDTO populatedKMConfigurationDTO = APIUtil.getAndSetDefaultKeyManagerConfiguration(
keyManagerConfigurationDTO);
new KeyMgtNotificationSender()
.notify(populatedKMConfigurationDTO, APIConstants.KeyManager.KeyManagerEvent.ACTION_ADD);
}
}
}
Expand Down

0 comments on commit 76cc333

Please sign in to comment.