Skip to content

Commit

Permalink
Add minor changes
Browse files Browse the repository at this point in the history
Remove unused methods
Fix API total in GET key manager API usage response
Refactor code
  • Loading branch information
SavinduDimal committed Mar 24, 2024
1 parent 08c3355 commit 3488aae
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,12 @@ Map<String, Object> searchPaginatedApis(String searchQuery, String organization,

/**
* This method used to retrieve global key manager configurations with usage check
* @param organization organization
* @return KeyManagerConfigurationDTO list
* @throws APIManagementException if error occurred
*/
List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations(String organization) throws APIManagementException;
List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations(String organization)
throws APIManagementException;

/**
* This method used to retrieve global key manager with Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,17 +721,6 @@ ResourceFile getWSDL(API api, String environmentName, String environmentType, St
*/
String getRequestedTenant();

/**
* Get consumer key and key manager name by application id and key mapping id
* @param applicationId application id
* @param keyMappingId key mapping id
* @return KeyManagerApplicationInfo
*/
KeyManagerApplicationInfo getKeyManagerNameAndConsumerKeyByAppIDAndKeyMapping(int applicationId,
String keyMappingId)
throws APIManagementException;


/**
*
* @param apiId API UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,7 @@ public KeyManagerConfigurationDTO addKeyManagerConfiguration(
}

public AdminContentSearchResult getAPIUsagesByKeyManagerNameAndOrganization(String org, String keyManagerName,
int offset, int limit)
throws APIManagementException {
int offset, int limit) throws APIManagementException {

APIPersistence apiPersistenceInstance = PersistenceFactory.getAPIPersistenceInstance();
String searchQuery = APIConstants.API_USAGE_BY_KEY_MANAGER_QUERY.replace("$1", keyManagerName);
Expand All @@ -679,12 +678,6 @@ public KeyManagerApplicationUsages getApplicationsOfKeyManager(String keyManager
return keyManagerApplicationUsages;
}

public List<ApplicationInfoKeyManager> getAllApplicationsOfKeyManager(String keyManagerId)
throws APIManagementException {
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
return apiMgtDAO.getAllApplicationsOfKeyManager(keyManagerId);
}

private void validateKeyManagerEndpointConfiguration(KeyManagerConfigurationDTO keyManagerConfigurationDTO)
throws APIManagementException {
if (!APIConstants.KeyManager.DEFAULT_KEY_MANAGER.equals(keyManagerConfigurationDTO.getName())) {
Expand Down Expand Up @@ -984,7 +977,7 @@ public void deleteKeyManagerConfigurationById(String organization, KeyManagerCon
ExceptionCodes.KEY_MANAGER_DELETE_FAILED);
}
} else {
throw new APIManagementException("Key Manager is already used by an API or and Application.",
throw new APIManagementException("Key Manager is already used by an API or an Application.",

Check warning on line 980 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIAdminImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIAdminImpl.java#L980

Added line #L980 was not covered by tests
ExceptionCodes.KEY_MANAGER_DELETE_FAILED);
}
}
Expand Down Expand Up @@ -1748,8 +1741,7 @@ private static IdentityProvider cloneIdentityProvider(IdentityProvider identityP
}

@Override
public List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations()
throws APIManagementException {
public List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations() throws APIManagementException {
List<KeyManagerConfigurationDTO> keyManagerConfigurations = apiMgtDAO.getKeyManagerConfigurationsByOrganization(
APIConstants.GLOBAL_KEY_MANAGER_TENANT_DOMAIN);
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3620,6 +3620,7 @@ public void cleanUpApplicationRegistrationByApplicationIdAndKeyMappingId(int app

public boolean removalKeys(Application application, String keyMappingId, String xWSO2Tenant)
throws APIManagementException {

try {
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(this.username);

Check warning on line 3625 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java#L3625

Added line #L3625 was not covered by tests

Expand All @@ -3637,8 +3638,8 @@ public boolean removalKeys(Application application, String keyMappingId, String

String keyManagerName = APIConstants.KeyManager.DEFAULT_KEY_MANAGER;
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
KeyManagerApplicationInfo KeyManagerApplicationInfo = apiMgtDAO
.getKeyManagerNameAndConsumerKeyByAppIdAndKeyMappingId(application.getId(), keyMappingId);
KeyManagerApplicationInfo KeyManagerApplicationInfo =
apiMgtDAO.getKeyManagerNameAndConsumerKeyByAppIdAndKeyMappingId(application.getId(), keyMappingId);
String keyManagerNameResult = KeyManagerApplicationInfo.getKeyManagerName();

Check warning on line 3643 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java#L3639-L3643

Added lines #L3639 - L3643 were not covered by tests
if (!StringUtils.isEmpty(keyManagerNameResult)) {
keyManagerName = keyManagerNameResult;

Check warning on line 3645 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java#L3645

Added line #L3645 was not covered by tests
Expand All @@ -3658,8 +3659,7 @@ public boolean removalKeys(Application application, String keyMappingId, String
UUID.randomUUID().toString(), System.currentTimeMillis(),
APIConstants.EventType.REMOVE_APPLICATION_KEYMAPPING.name(),
APIUtil.getTenantIdFromTenantDomain(tenantDomain), application.getOrganization(),
application.getId(), application.getUUID(), consumerKey, application.getKeyType(),
keyManagerName);
application.getId(), application.getUUID(), consumerKey, application.getKeyType(), keyManagerName);
APIUtil.sendNotification(removeEntryTrigger, APIConstants.NotifierType.APPLICATION_REGISTRATION.name());
return true;
} catch (APIManagementException e) {
Expand Down Expand Up @@ -3697,14 +3697,6 @@ public APIKey getApplicationKeyByAppIDAndKeyMapping(int applicationId, String ke
return apiKey;
}

@Override
public KeyManagerApplicationInfo getKeyManagerNameAndConsumerKeyByAppIDAndKeyMapping(int applicationId, String keyMappingId)
throws APIManagementException {
return apiMgtDAO
.getKeyManagerNameAndConsumerKeyByAppIdAndKeyMappingId(applicationId, keyMappingId);
}


@Override
public Set<Topic> getTopics(String apiId) throws APIManagementException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9833,25 +9833,6 @@ public KeyManagerApplicationInfo getKeyManagerNameAndConsumerKeyByAppIdAndKeyMap
return null;

Check warning on line 9833 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java#L9831-L9833

Added lines #L9831 - L9833 were not covered by tests
}

public String getKeyManagerNameFromKeyMappingId(String keyMappingId)
throws APIManagementException {

final String query = "SELECT NAME AS KEY_MANAGER_NAME FROM AM_KEY_MANAGER AKM, AM_APPLICATION_KEY_MAPPING " +
"AAKM WHERE AAKM.UUID = ? AND AKM.UUID=AAKM.KEY_MANAGER;";
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, keyMappingId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getString("KEY_MANAGER_NAME");
}
}
} catch (SQLException e) {
throw new APIManagementException("Error while Retrieving Key Mapping ", e);
}
return null;
}

public void deleteApplicationKeyMappingByMappingId(String keyMappingId) throws APIManagementException {

Connection connection = null;
Expand Down Expand Up @@ -20199,8 +20180,8 @@ private Set<String> getAllClonedPolicyIdsForAPI(Connection connection, String ap
return policyIds;
}

public List<ApplicationInfoKeyManager> getAllApplicationsOfKeyManager(String keyManagerId) throws
APIManagementException {
public List<ApplicationInfoKeyManager> getAllApplicationsOfKeyManager(String keyManagerId)
throws APIManagementException {

ArrayList<ApplicationInfoKeyManager> applicationsList = new ArrayList<>();
String sqlQuery = SQLConstants.GET_APPLICATIONS_OF_KEY_MANAGERS_SQL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import java.util.List;
import javax.ws.rs.core.Response;

import static org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;

public class KeyManagersApiServiceImpl implements KeyManagersApiService {

private static final Log log = LogFactory.getLog(KeyManagersApiServiceImpl.class);
Expand Down Expand Up @@ -170,8 +168,8 @@ public Response keyManagersKeyManagerIdApiUsagesGet(String keyManagerId, Integer
} else {
throw new APIManagementException("Requested KeyManager not found", ExceptionCodes.KEY_MANAGER_NOT_FOUND);

Check warning on line 169 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java#L169

Added line #L169 was not covered by tests
}
AdminContentSearchResult result =
apiAdmin.getAPIUsagesByKeyManagerNameAndOrganization(organization, KeyManagerName, offset, limit);
AdminContentSearchResult result = apiAdmin.getAPIUsagesByKeyManagerNameAndOrganization(organization,
KeyManagerName, offset, limit);

Check warning on line 172 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java#L171-L172

Added lines #L171 - L172 were not covered by tests

return Response.ok().entity(KeyManagerMappingUtil.toKeyManagerAPIUsagesDTO(result)).build();

Check warning on line 174 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java#L174

Added line #L174 was not covered by tests
}
Expand All @@ -192,7 +190,7 @@ public Response keyManagersKeyManagerIdAppUsagesGet(String keyManagerId, Integer
throw new APIManagementException("Requested KeyManager not found", ExceptionCodes.KEY_MANAGER_NOT_FOUND);

Check warning on line 190 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java#L190

Added line #L190 was not covered by tests
}
KeyManagerApplicationUsages result = apiAdmin.getApplicationsOfKeyManager(keyManagerId, offset, limit);
return Response.ok().entity(result).build();
return Response.ok().entity(KeyManagerMappingUtil.toKeyManagerAppUsagesDTO(result)).build();

Check warning on line 193 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java#L192-L193

Added lines #L192 - L193 were not covered by tests
}

public Response keyManagersPost(KeyManagerDTO body, MessageContext messageContext) throws APIManagementException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.wso2.carbon.apimgt.api.APIAdmin;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.ExceptionCodes;
import org.wso2.carbon.apimgt.api.model.ApplicationInfoKeyManager;
import org.wso2.carbon.apimgt.api.model.BlockConditionsDTO;
import org.wso2.carbon.apimgt.api.model.policy.Policy;
import org.wso2.carbon.apimgt.impl.APIAdminImpl;
Expand All @@ -41,9 +40,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public static KeyManagerAPIUsagesDTO toKeyManagerAPIUsagesDTO(AdminContentSearch
apis.add(apiInfoKeyManagerDTO);

Check warning on line 398 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/KeyManagerMappingUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/KeyManagerMappingUtil.java#L398

Added line #L398 was not covered by tests
}
}
keyManagerAPIUsagesDTO.setApiCount(result.getApiCount());
keyManagerAPIUsagesDTO.setApiCount(result.getApiTotal());
keyManagerAPIUsagesDTO.setApis(apis);

Check warning on line 402 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/KeyManagerMappingUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/KeyManagerMappingUtil.java#L400-L402

Added lines #L400 - L402 were not covered by tests
}
return keyManagerAPIUsagesDTO;

Check warning on line 404 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/KeyManagerMappingUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/KeyManagerMappingUtil.java#L404

Added line #L404 was not covered by tests
Expand Down Expand Up @@ -444,7 +444,7 @@ public static ApplicationInfoKeyManagerDTO toApplicationInfoKeyManagerDTO(
ApplicationInfoKeyManager applicationInfoKeyManager) {

ApplicationInfoKeyManagerDTO applicationInfoKeyManagerDTO = new ApplicationInfoKeyManagerDTO();
applicationInfoKeyManagerDTO.setName(applicationInfoKeyManager.getUuid());
applicationInfoKeyManagerDTO.setName(applicationInfoKeyManager.getName());
applicationInfoKeyManagerDTO.setUuid(applicationInfoKeyManager.getUuid());
applicationInfoKeyManagerDTO.setOrganization(applicationInfoKeyManager.getOrganization());
applicationInfoKeyManagerDTO.setOrganizationId(applicationInfoKeyManager.getOrganizationId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1202,22 +1202,21 @@ public Response applicationsApplicationIdOauthKeysKeyMappingIdCleanUpPost(String

@Override
public Response applicationsApplicationIdOauthKeysKeyMappingIdDelete(String applicationId, String keyMappingId,
String xWSO2Tenant,
MessageContext messageContext)
throws APIManagementException {
String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {

String username = RestApiCommonUtil.getLoggedInUsername();

Check warning on line 1207 in components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java#L1207

Added line #L1207 was not covered by tests
try {
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
Application application = apiConsumer.getLightweightApplicationByUUID(applicationId);
boolean result = apiConsumer.removalKeys(application, keyMappingId, xWSO2Tenant);

Check warning on line 1211 in components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java#L1209-L1211

Added lines #L1209 - L1211 were not covered by tests
if(result) {
if (result) {
return Response.ok().build();

Check warning on line 1213 in components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java#L1213

Added line #L1213 was not covered by tests
} else {
RestApiUtil.handleResourceNotFoundError(ExceptionCodes.KEYS_DELETE_FAILED.getErrorMessage(),

Check warning on line 1215 in components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java#L1215

Added line #L1215 was not covered by tests
keyMappingId, log);
}
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error occurred while application key cleanup process", e, log);
RestApiUtil.handleInternalServerError("Error occurred while application key delete process", e, log);
}
return null;

Check warning on line 1221 in components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java#L1218-L1221

Added lines #L1218 - L1221 were not covered by tests
}
Expand Down

0 comments on commit 3488aae

Please sign in to comment.