Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify implementation to allow an API and a Common policies with same and version #12660

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1543,8 +1543,10 @@ EnvironmentPropertiesDTO getEnvironmentSpecificAPIProperties(String apiUuid, Str
void setOperationPoliciesToURITemplates(String apiId, Set<URITemplate> uriTemplates) throws APIManagementException;

/**
* Import an operation policy from the API CTL project. This will either create a new API specific policy,
* update existing API specific policy or return the policyID of existing policy if policy content is not changed.
* Import an operation policy from the API CTL project which is exported from a product version prior to the update
* level which introduced to have an API and a Common policy with identical names and versions.
* This will either create a new API specific policy, update existing API specific policy or return the
* policyID of existing policy if policy content is not changed.
*
* @param operationPolicyData Operation Policy Data
* @param organization Organization name
Expand All @@ -1554,6 +1556,21 @@ EnvironmentPropertiesDTO getEnvironmentSpecificAPIProperties(String apiUuid, Str
String importOperationPolicy(OperationPolicyData operationPolicyData, String organization)
throws APIManagementException;

/**
* Import an operation policy of a given policy type, from the API CTL project.
* This will either create a new API specific policy, update existing API specific policy or return the
* policyID of existing policy if policy content is not changed.
*
* @param operationPolicyData Operation Policy Data
* @param organization Organization name
* @param policyType Policy Type
* @return UUID of the imported operation policy
* @throws APIManagementException
*/
String importOperationPolicyOfGivenType(OperationPolicyData operationPolicyData, String policyType,
String organization) throws APIManagementException;

RusJaI marked this conversation as resolved.
Show resolved Hide resolved

/**
* Add an API specific operation policy
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

private String policyName = "";
private String policyVersion = "v1";
private String policyType = null;

Check warning on line 28 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java#L28

Added line #L28 was not covered by tests
private String direction = null;
private Map<String, Object> parameters = null;
private String policyId = null;
Expand All @@ -50,6 +51,16 @@
this.policyVersion = policyVersion;
}

public String getPolicyType() {

return policyType;

Check warning on line 56 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java#L56

Added line #L56 was not covered by tests
}

public void setPolicyType(String policyType) {

this.policyType = policyType;
}

Check warning on line 62 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java#L61-L62

Added lines #L61 - L62 were not covered by tests

public Map<String, Object> getParameters() {

return parameters;
Expand Down Expand Up @@ -98,7 +109,8 @@
if (o instanceof OperationPolicy) {
OperationPolicy policyObj = (OperationPolicy) o;
return Objects.equals(policyName, policyObj.policyName) && Objects.equals(policyVersion,
policyObj.policyVersion) && Objects.equals(direction, policyObj.direction) && Objects.equals(
policyObj.policyVersion) && Objects.equals(direction, policyObj.direction)
&& policyType.equals(policyObj.policyType) && Objects.equals(
parameters, policyObj.parameters) && Objects.equals(policyId, policyObj.policyId);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
import org.wso2.carbon.apimgt.impl.importexport.APIImportExportException;
import org.wso2.carbon.apimgt.impl.importexport.ExportFormat;
import org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI;
import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.lifecycle.CheckListItem;
import org.wso2.carbon.apimgt.impl.lifecycle.LCManagerFactory;
Expand Down Expand Up @@ -5280,6 +5281,84 @@
}
}

public String getPolicyType(OperationPolicy policy, Map<String, String> apiOperationPolicyIdToClonedPolicyIdMap)
throws APIManagementException {
String policyType = null;

Check warning on line 5286 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5286

Added line #L5286 was not covered by tests
if (policy.getPolicyId() == null) {
policyType = ImportExportConstants.POLICY_TYPE_API;

Check warning on line 5288 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5288

Added line #L5288 was not covered by tests
} else {
// In an api product resource update scenario, when existing policy has been removed from an api,
// there's no entry attached to api policy id and apiId in AM_API_OPERATION_POLICY table
if (!apiOperationPolicyIdToClonedPolicyIdMap.containsKey(policy.getPolicyId())) {
return null;

Check warning on line 5293 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5293

Added line #L5293 was not covered by tests
}
// check if cloned policy id is null
if (apiOperationPolicyIdToClonedPolicyIdMap.get(policy.getPolicyId()) == null) {
policyType = ImportExportConstants.POLICY_TYPE_API;

Check warning on line 5297 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5297

Added line #L5297 was not covered by tests
} else {
policyType = ImportExportConstants.POLICY_TYPE_COMMON;

Check warning on line 5299 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5299

Added line #L5299 was not covered by tests
}
}
return policyType;

Check warning on line 5302 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5302

Added line #L5302 was not covered by tests
}

public String getProductPolicyType(OperationPolicy policy, String apiUUID,
Map<String, String> apiProductOperationPolicyIdToClonedPolicyIdMap)
throws APIManagementException {

String originatedPolicyId = apiProductOperationPolicyIdToClonedPolicyIdMap.get(policy.getPolicyId());
Map<String, String> apiOperationPolicyIdToClonedPolicyIdMap =
getClonedAPISpecificOperationPolicyIdsList(apiUUID);
policy.setPolicyId(originatedPolicyId);
return getPolicyType(policy, apiOperationPolicyIdToClonedPolicyIdMap);

Check warning on line 5313 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5309-L5313

Added lines #L5309 - L5313 were not covered by tests
}

public void populatePolicyTypeInAPI(API api) throws APIManagementException {

Map<String, String> apiOperationPolicyIdToClonedPolicyIdMap = getClonedAPISpecificOperationPolicyIdsList(api.getUuid());
Set<URITemplate> uriTemplates = api.getUriTemplates();

Check warning on line 5319 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5318-L5319

Added lines #L5318 - L5319 were not covered by tests
for (URITemplate uriTemplate : uriTemplates) {
List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();

Check warning on line 5321 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5321

Added line #L5321 was not covered by tests
if (!operationPolicies.isEmpty()) {
for (OperationPolicy operationPolicy : operationPolicies) {
String policyType = getPolicyType(operationPolicy, apiOperationPolicyIdToClonedPolicyIdMap);
operationPolicy.setPolicyType(policyType);
}

Check warning on line 5326 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5324-L5326

Added lines #L5324 - L5326 were not covered by tests
}
}
api.setUriTemplates(uriTemplates);

Check warning on line 5329 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5328-L5329

Added lines #L5328 - L5329 were not covered by tests

List<OperationPolicy> apiPolicies = api.getApiPolicies();

Check warning on line 5331 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5331

Added line #L5331 was not covered by tests
if (apiPolicies != null && !apiPolicies.isEmpty()) {
for (OperationPolicy policy : apiPolicies) {
String policyType = getPolicyType(policy, apiOperationPolicyIdToClonedPolicyIdMap);
policy.setPolicyType(policyType);
}

Check warning on line 5336 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5334-L5336

Added lines #L5334 - L5336 were not covered by tests
}
api.setApiPolicies(apiPolicies);
}

Check warning on line 5339 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5338-L5339

Added lines #L5338 - L5339 were not covered by tests

public void populatePolicyTypeInApiProduct(APIProduct product) throws APIManagementException {

Map<String, String> apiProductOperationPolicyIdToClonedPolicyIdMap =
getClonedAPISpecificOperationPolicyIdsList(product.getUuid());
List<APIProductResource> productResources = product.getProductResources();

Check warning on line 5345 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5343-L5345

Added lines #L5343 - L5345 were not covered by tests
for (APIProductResource resource : productResources) {
URITemplate uriTemplate = resource.getUriTemplate();
List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();

Check warning on line 5348 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5347-L5348

Added lines #L5347 - L5348 were not covered by tests
if (!operationPolicies.isEmpty()) {
for (OperationPolicy operationPolicy : operationPolicies) {
String policyType = getProductPolicyType(operationPolicy, resource.getApiId(),

Check warning on line 5351 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5351

Added line #L5351 was not covered by tests
apiProductOperationPolicyIdToClonedPolicyIdMap);
operationPolicy.setPolicyType(policyType);
}

Check warning on line 5354 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5353-L5354

Added lines #L5353 - L5354 were not covered by tests
}
uriTemplate.setOperationPolicies(operationPolicies);
resource.setUriTemplate(uriTemplate);
}
product.setProductResources(productResources);
}

Check warning on line 5360 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5356-L5360

Added lines #L5356 - L5360 were not covered by tests

@Override
public boolean isSubscriptionValidationDisabled(String uuid) throws APIManagementException {
String status = apiMgtDAO.getSubscriptionValidationStatus(uuid);
Expand Down Expand Up @@ -5322,6 +5401,7 @@
populateApiInfo(api);
populateSubtypeConfiguration(api);
populateDefaultVersion(api);
populatePolicyTypeInAPI(api);

Check warning on line 5404 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5404

Added line #L5404 was not covered by tests
return api;
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
Expand Down Expand Up @@ -5501,6 +5581,7 @@
if (migrationEnabled == null) {
populateDefaultVersion(product);
}
populatePolicyTypeInApiProduct(product);

Check warning on line 5584 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L5584

Added line #L5584 was not covered by tests
return product;
} else {
String msg = "Failed to get API Product. API Product artifact corresponding to artifactId " + uuid
Expand Down Expand Up @@ -7093,6 +7174,87 @@
return policyId;
}

@Override
RusJaI marked this conversation as resolved.
Show resolved Hide resolved
public String importOperationPolicyOfGivenType(OperationPolicyData importedPolicyData, String policyType,
String organization) throws APIManagementException {

OperationPolicySpecification importedSpec = importedPolicyData.getSpecification();

Check warning on line 7181 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7181

Added line #L7181 was not covered by tests
OperationPolicyData existingOperationPolicy;

String policyId = null;

Check warning on line 7184 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7184

Added line #L7184 was not covered by tests
if (policyType == null) {
/*To handle scenarios where api is exported from a previous U2 version. API and Common policies with
same name and same version is not supported there
*/
policyId = importOperationPolicy(importedPolicyData, organization);

Check warning on line 7189 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7189

Added line #L7189 was not covered by tests
} else if (policyType.equalsIgnoreCase(ImportExportConstants.POLICY_TYPE_COMMON)) {
existingOperationPolicy = getCommonOperationPolicyByPolicyName(importedSpec.getName(),
importedSpec.getVersion(),organization, false);

Check warning on line 7192 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7191-L7192

Added lines #L7191 - L7192 were not covered by tests

if (existingOperationPolicy != null) {
if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
if (log.isDebugEnabled()) {
log.debug("Matching common policy found for imported policy and Md5 hashes match.");

Check warning on line 7197 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7197

Added line #L7197 was not covered by tests
}
policyId = existingOperationPolicy.getPolicyId();

Check warning on line 7199 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7199

Added line #L7199 was not covered by tests
} else {
importedSpec.setName(importedSpec.getName() + "_imported");
importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported");
importedPolicyData.setSpecification(importedSpec);
importedPolicyData.setMd5Hash(APIUtil.getHashOfOperationPolicy(importedPolicyData));
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData,

Check warning on line 7205 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7201-L7205

Added lines #L7201 - L7205 were not covered by tests
organization);
if (log.isDebugEnabled()) {
log.debug("Even though existing common policy name match with imported policy, "
+ "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId()

Check warning on line 7209 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7208-L7209

Added lines #L7208 - L7209 were not covered by tests
+ ". A new policy created with ID " + policyId);
}
}
} else {
importedSpec.setName(importedSpec.getName() + "_imported");
importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported");
importedPolicyData.setSpecification(importedSpec);
importedPolicyData.setMd5Hash(APIUtil.getHashOfOperationPolicy(importedPolicyData));
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData,

Check warning on line 7218 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7214-L7218

Added lines #L7214 - L7218 were not covered by tests
organization);
if (log.isDebugEnabled()) {
log.debug("There is no common policy currently available for the imported policy. " +

Check warning on line 7221 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7221

Added line #L7221 was not covered by tests
"A new policy created with ID " + policyId);
}
}
} else { //api level policy by default
existingOperationPolicy =
getAPISpecificOperationPolicyByPolicyName(importedSpec.getName(), importedSpec.getVersion(),
importedPolicyData.getApiUUID(), null, organization, false);

Check warning on line 7228 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7226-L7228

Added lines #L7226 - L7228 were not covered by tests

if (existingOperationPolicy != null) {
if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
if (log.isDebugEnabled()) {
log.debug("Matching API specific policy found for imported policy and MD5 hashes match.");

Check warning on line 7233 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7233

Added line #L7233 was not covered by tests
}
} else {
if (log.isDebugEnabled()) {
log.debug("Even though existing API specific policy name match with imported policy, "
+ "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId()

Check warning on line 7238 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7237-L7238

Added lines #L7237 - L7238 were not covered by tests
+ ".Therefore updating the existing policy");
}
updateOperationPolicy(existingOperationPolicy.getPolicyId(), importedPolicyData, organization);

Check warning on line 7241 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7241

Added line #L7241 was not covered by tests
}
policyId = existingOperationPolicy.getPolicyId();

Check warning on line 7243 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7243

Added line #L7243 was not covered by tests
} else {
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData,

Check warning on line 7245 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7245

Added line #L7245 was not covered by tests
organization);
if (log.isDebugEnabled()) {
log.debug(

Check warning on line 7248 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7248

Added line #L7248 was not covered by tests
"There aren't any existing policies for the imported policy. A new policy created with ID "
+ policyId);
}
}
}

return policyId;

Check warning on line 7255 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7255

Added line #L7255 was not covered by tests
}

@Override
public String addAPISpecificOperationPolicy(String apiUUID, OperationPolicyData operationPolicyData,
String tenantDomain)
Expand Down Expand Up @@ -7146,6 +7308,13 @@
.getAPISpecificOperationPolicyByPolicyID(policyId, apiUUID, organization, isWithPolicyDefinition);
}

public Map<String, String> getClonedAPISpecificOperationPolicyIdsList(String apiUUID)
throws APIManagementException {

return apiMgtDAO
.getClonedIdsMappedApiSpecificOperationPolicies(apiUUID);

Check warning on line 7315 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.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/APIProviderImpl.java#L7314-L7315

Added lines #L7314 - L7315 were not covered by tests
}

@Override
public OperationPolicyData getCommonOperationPolicyByPolicyId(String policyId, String organization,
boolean isWithPolicyDefinition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20828,6 +20828,50 @@
return policyData;
}

/**
* Get the list of API specific operation policy IDs from AM_API_OPERATION_POLICY table where cloned policy ID is
* non null. This method is intended to get the common operation policy IDs which have been attached to the
* given API.
*
* @param apiUUID UUID of the API
* @return operation policy
* @throws APIManagementException
*/
public Map<String, String> getClonedIdsMappedApiSpecificOperationPolicies(String apiUUID)
throws APIManagementException {

try (Connection connection = APIMgtDBUtil.getConnection()) {
return getClonedIdsMappedApiSpecificOperationPolicies(connection, apiUUID);
} catch (SQLException e) {
handleException("Failed to get the API specific operation policy IDs from API "

Check warning on line 20846 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#L20843-L20846

Added lines #L20843 - L20846 were not covered by tests
+ apiUUID, e);
}
return null;

Check warning on line 20849 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#L20849

Added line #L20849 was not covered by tests
}

private Map<String, String> getClonedIdsMappedApiSpecificOperationPolicies(Connection connection, String apiUUID)
throws SQLException, APIManagementException {

String dbQuery;
boolean isAPIRevision = checkAPIUUIDIsARevisionUUID(apiUUID) != null;
if (isAPIRevision) {
dbQuery = SQLConstants.OperationPolicyConstants.

Check warning on line 20858 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#L20858

Added line #L20858 was not covered by tests
GET_REVISION_SPECIFIC_OPERATION_POLICY_IDS_FROM_REVISION_UUID;
} else {
dbQuery = SQLConstants.OperationPolicyConstants.GET_API_SPECIFIC_OPERATION_POLICY_IDS_FROM_API_UUID;

Check warning on line 20861 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#L20861

Added line #L20861 was not covered by tests
}
Map<String, String> policyMap = new HashMap<>();
try (PreparedStatement statement = connection.prepareStatement(dbQuery)) {
statement.setString(1, apiUUID);
try (ResultSet rs = statement.executeQuery()) {

Check warning on line 20866 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#L20863-L20866

Added lines #L20863 - L20866 were not covered by tests
while (rs.next()) {
policyMap.put(rs.getString("POLICY_UUID"), rs.getString("CLONED_POLICY_UUID"));

Check warning on line 20868 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#L20868

Added line #L20868 was not covered by tests
}
}
}
return policyMap;

Check warning on line 20872 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#L20872

Added line #L20872 was not covered by tests
}

private List<OperationPolicyDefinition> getPolicyDefinitionForPolicyId(Connection connection, String policyId)
throws SQLException {

Expand Down Expand Up @@ -21197,7 +21241,13 @@
while (rs.next()) {
String policyName = rs.getString("POLICY_NAME");
String policyVersion = rs.getString("POLICY_VERSION");
policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion));
policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion, null));

Check warning on line 21244 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#L21244

Added line #L21244 was not covered by tests
/*since the only usage of this method is to load the common operation policies from the
specifications and we are keeping only the common policies without appending the string "common"
to the file name, it's not required to append the policyType string
(policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion,
ImportExportConstants.POLICY_TYPE_COMMON));)here as well.
*/
}
}
} catch (SQLException e) {
Expand Down
Loading
Loading