From b74d446755e3732432193f835c262e9157c07312 Mon Sep 17 00:00:00 2001 From: hiranyakavishani Date: Wed, 6 Dec 2023 10:18:33 +0530 Subject: [PATCH] Adding migration related fixes --- .../wso2/carbon/apimgt/api/APIManager.java | 15 ++ .../carbon/apimgt/api/model/APIProduct.java | 3 +- .../wso2/carbon/apimgt/impl/APIConstants.java | 2 +- .../carbon/apimgt/impl/APIConsumerImpl.java | 9 +- .../carbon/apimgt/impl/APIProviderImpl.java | 26 +- .../apimgt/impl/AbstractAPIManager.java | 14 +- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 222 ++++++++++++++++-- .../impl/dao/SubscriptionValidationDAO.java | 93 ++++++-- .../impl/dao/constants/SQLConstants.java | 27 ++- .../SubscriptionValidationSQLConstants.java | 28 ++- .../apimgt/impl/APIProviderImplTest.java | 2 +- .../apimgt/impl/dao/test/APIMgtDAOTest.java | 18 ++ .../common/mappings/PublisherCommonUtils.java | 79 ++++++- .../mappings/PublisherCommonUtilsTest.java | 200 ++++++++++++++++ .../api/store/v1/mappings/APIMappingUtil.java | 1 + 15 files changed, 658 insertions(+), 81 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIManager.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIManager.java index d15659ff54fc..94ed069bbc9c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIManager.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIManager.java @@ -120,6 +120,21 @@ public interface APIManager { */ boolean isContextExist(String context, String organization) throws APIManagementException; + + /** + * Checks whether the given API Product context is already registered in the system for API products + * + * @param context A String representing an API product context + * @param contextWithVersion A String representing an API context appended with the version + * @param organization Organization + * @return true if the context already exists and false otherwise + * @throws APIManagementException if failed to check the context availability + */ + boolean isContextExistForAPIProducts(String context, String contextWithVersion, String organization) + throws APIManagementException; + + /** + /** * Checks whether the given API name is already registered in the system * diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIProduct.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIProduct.java index b469919bbcb0..12a1de214316 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIProduct.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIProduct.java @@ -132,7 +132,7 @@ public class APIProduct implements Serializable { * Used to set the workflow status in lifecycle state change workflow */ private String workflowStatus = null; - private Boolean isDefaultVersion = null; + private Boolean isDefaultVersion = true; private boolean isPublishedDefaultVersion = false; public APIProduct(){} @@ -202,6 +202,7 @@ public void setDefaultVersion(Boolean isDefaultVersion) { public void setAsPublishedDefaultVersion(boolean value) { isPublishedDefaultVersion = value; } + public Boolean isDefaultVersion() { return isDefaultVersion; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index e4c5f05aa81e..d9b24a041843 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -1907,7 +1907,7 @@ public enum RegistryResourceTypesForUI { public static final String ENABLE_STORE = "enableStore"; //api-product related constants - public static final String API_PRODUCT_VERSION = "1.0.0"; + public static final String API_PRODUCT_VERSION_1_0_0 = "1.0.0"; public static final String API_IDENTIFIER_TYPE = "API"; public static final String API_PRODUCT_IDENTIFIER_TYPE = "API Product"; public static final String[] API_SUPPORTED_TYPE_LIST = {"HTTP", "WS", "SOAPTOREST", "GRAPHQL", "SOAP", "WEBSUB", diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java index e816b2f541db..5aa67000b451 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java @@ -3636,6 +3636,7 @@ public ApiTypeWrapper getAPIorAPIProductByUUID(String uuid, String organization) apiProduct.setID(new APIProductIdentifier(devPortalApi.getProviderName(), devPortalApi.getApiName(), devPortalApi.getVersion())); populateAPIProductInformation(uuid, organization, apiProduct); + populateDefaultVersion(apiProduct); populateAPIStatus(apiProduct); apiProduct = addTiersToAPI(apiProduct, organization); return new ApiTypeWrapper(apiProduct); @@ -3798,6 +3799,12 @@ public API getLightweightAPIByUUID(String uuid, String organization) throws APIM } } + @Override + public boolean isContextExistForAPIProducts(String context, String contextWithVersion, String organization) + throws APIManagementException { + return false; + } + /** * Used to retrieve API/API Products without performing the visibility permission checks * @param uuid @@ -3816,7 +3823,7 @@ private ApiTypeWrapper getAPIorAPIProductByUUIDWithoutPermissionCheck(String uui apiProduct.setID(new APIProductIdentifier(devPortalApi.getProviderName(), devPortalApi.getApiName(), devPortalApi.getVersion())); populateAPIProductInformation(uuid, organization, apiProduct); - + populateDefaultVersion(apiProduct); return new ApiTypeWrapper(apiProduct); } else { API api = APIMapper.INSTANCE.toApi(devPortalApi); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 5ba6e411210e..253782128667 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -754,13 +754,17 @@ private String getDefaultVersion(Identifier apiid) throws APIManagementException } - public String getPublishedDefaultVersion(Identifier apiid) throws APIManagementException { + public String getPublishedDefaultVersion(Identifier apiId) throws APIManagementException { String defaultVersion = null; try { - defaultVersion = apiMgtDAO.getPublishedDefaultVersion(apiid); + if (apiId instanceof APIIdentifier) { + defaultVersion = apiMgtDAO.getPublishedDefaultVersion((APIIdentifier) apiId); + } else if (apiId instanceof APIProductIdentifier) { + defaultVersion = apiMgtDAO.getPublishedDefaultVersion((APIProductIdentifier) apiId); + } } catch (APIManagementException e) { - handleException("Error while getting published default version :" + apiid.getName(), e); + handleException("Error while getting published default version :" + apiId.getName(), e); } return defaultVersion; } @@ -4305,6 +4309,7 @@ public Map> updateAPIProduct(APIProduct product) Map> apiToProductResourceMapping = new HashMap<>(); //validate resources and set api identifiers and resource ids to product List resources = product.getProductResources(); + String publishedDefaultVersion = getPublishedDefaultVersion(product.getId()); String prevDefaultVersion = getDefaultVersion(product.getId()); for (APIProductResource apiProductResource : resources) { API api; @@ -4395,8 +4400,6 @@ public Map> updateAPIProduct(APIProduct product) product.setDefaultVersion(true); } apiMgtDAO.updateAPIProduct(product, userNameWithoutChange); - - String publishedDefaultVersion = getPublishedDefaultVersion(product.getId()); if (publishedDefaultVersion != null && product.isPublishedDefaultVersion() && !product.getId().getVersion() .equals(publishedDefaultVersion)) { sendUpdateEventToPreviousDefaultVersion(product.getId().getProviderName(), product.getId().getName(), @@ -5070,13 +5073,6 @@ private void populateAPIStatus(API api) throws APIManagementException { } } - private void populateDefaultVersion(APIProduct apiProduct) throws APIManagementException { - ApiTypeWrapper apiTypeWrapper = new ApiTypeWrapper(apiProduct); - apiMgtDAO.setDefaultVersion(apiTypeWrapper); - apiProduct.setDefaultVersion(apiTypeWrapper.getDefaultVersion()); - apiProduct.setAsPublishedDefaultVersion(apiTypeWrapper.getPublishedDefaultVersion()); - } - private void populateAPIStatus(APIProduct apiProduct) throws APIManagementException { if (apiProduct.isRevision()) { @@ -5238,6 +5234,12 @@ public API getLightweightAPIByUUID(String uuid, String organization) throws APIM } } + @Override + public boolean isContextExistForAPIProducts(String context, String contextWithVersion, String organization) + throws APIManagementException { + return apiMgtDAO.isContextExistForAPIProducts(context, contextWithVersion, organization); + } + @Override public List getUsedProductResources(String uuid) throws APIManagementException { List usedProductResources = new ArrayList<>(); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AbstractAPIManager.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AbstractAPIManager.java index 21bc4877c055..dd1bdedf325a 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AbstractAPIManager.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AbstractAPIManager.java @@ -171,10 +171,10 @@ protected String getTenantDomain(Identifier identifier) { } protected void populateDefaultVersion(API api) throws APIManagementException { - ApiTypeWrapper apiTypeWrapper = new ApiTypeWrapper(api); - apiMgtDAO.setDefaultVersion(apiTypeWrapper); - api.setDefaultVersion(apiTypeWrapper.getDefaultVersion()); - api.setAsPublishedDefaultVersion(apiTypeWrapper.getPublishedDefaultVersion()); + apiMgtDAO.setDefaultVersion(api); + } + protected void populateDefaultVersion(APIProduct apiProduct) throws APIManagementException { + apiMgtDAO.setDefaultVersion(apiProduct); } private boolean isTenantDomainNotMatching(String tenantDomain) { @@ -506,6 +506,12 @@ public boolean isContextExist(String context, String organization) throws APIMan return apiMgtDAO.isContextExist(context, organization); } + public boolean isContextExistForAPIProducts(String context, String contextWithVersion, String organization) + throws APIManagementException { + + return apiMgtDAO.isContextExistForAPIProducts(context, contextWithVersion, organization); + } + protected String getTenantDomainFromUrl(String url) { return MultitenantUtils.getTenantDomainFromUrl(url); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index a0a22543bb86..b2a52a0cf226 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -4989,7 +4989,13 @@ public void updateDefaultAPIPublishedVersion(Identifier identifier) try (Connection conn = APIMgtDBUtil.getConnection()) { try { conn.setAutoCommit(false); - String defaultVersion = getDefaultVersion(conn, identifier); + String defaultVersion = null; + if (identifier instanceof APIIdentifier) { + defaultVersion = getDefaultVersion(conn, (APIIdentifier) identifier); + } else if (identifier instanceof APIProductIdentifier) { + defaultVersion = getDefaultVersion(conn, (APIProductIdentifier) identifier); + } + if (identifier.getVersion().equals(defaultVersion)) { setPublishedDefVersion(identifier, conn, identifier.getVersion()); } @@ -5589,14 +5595,14 @@ public int addAPI(API api, int tenantId, String organization) throws APIManageme public String getDefaultVersion(Identifier apiId) throws APIManagementException { try (Connection connection = APIMgtDBUtil.getConnection()) { - return getDefaultVersion(connection, apiId); + return getDefaultVersion(connection, (APIIdentifier) apiId); } catch (SQLException e) { handleException("Error while getting default version for " + apiId.getName(), e); } return null; } - private String getDefaultVersion(Connection connection, Identifier apiId) throws SQLException { + private String getDefaultVersion(Connection connection, APIIdentifier apiId) throws SQLException { String oldDefaultVersion = null; @@ -5613,6 +5619,39 @@ private String getDefaultVersion(Connection connection, Identifier apiId) throws return null; } + private String getDefaultVersion(Connection connection, APIProductIdentifier apiId) throws SQLException { + + String query = SQLConstants.GET_DEFAULT_VERSION_SQL; + try (PreparedStatement prepStmt = connection.prepareStatement(query)) { + prepStmt.setString(1, apiId.getName()); + prepStmt.setString(2, APIUtil.replaceEmailDomainBack(apiId.getProviderName())); + try (ResultSet rs = prepStmt.executeQuery()) { + if (rs.next()) { + return rs.getString("DEFAULT_API_VERSION"); + } else { + return getMigratedAPIProductDefaultVersion(connection, apiId); + } + } + } + } + + private String getMigratedAPIProductDefaultVersion(Connection connection, APIProductIdentifier apiId) + throws SQLException { + + String query = SQLConstants.GET_MIGRATED_API_PRODUCT_DEFAULT_VERSION_SQL; + try (PreparedStatement prepStmt = connection.prepareStatement(query)) { + prepStmt.setString(1, apiId.getName()); + prepStmt.setString(2, APIUtil.replaceEmailDomainBack(apiId.getProviderName())); + prepStmt.setString(3, APIConstants.API_PRODUCT_VERSION_1_0_0); + try (ResultSet rs = prepStmt.executeQuery()) { + if (rs.next()) { + return rs.getString("API_VERSION"); + } + } + } + return null; + } + /** * Persists WorkflowDTO to Database * @@ -5839,7 +5878,7 @@ private void removeAPIFromDefaultVersion(List apiIdList, Connection } } - public String getPublishedDefaultVersion(Identifier apiId) throws APIManagementException { + public String getPublishedDefaultVersion(APIIdentifier apiId) throws APIManagementException { Connection connection = null; PreparedStatement prepStmt = null; @@ -5866,10 +5905,62 @@ public String getPublishedDefaultVersion(Identifier apiId) throws APIManagementE return publishedDefaultVersion; } + + public String getPublishedDefaultVersion(APIProductIdentifier apiId) throws APIManagementException { + + Connection connection = null; + PreparedStatement prepStmt = null; + ResultSet rs = null; + String publishedDefaultVersion = null; + + String query = SQLConstants.GET_PUBLISHED_DEFAULT_VERSION_SQL; + try { + connection = APIMgtDBUtil.getConnection(); + prepStmt = connection.prepareStatement(query); + prepStmt.setString(1, apiId.getName()); + prepStmt.setString(2, APIUtil.replaceEmailDomainBack(apiId.getProviderName())); + rs = prepStmt.executeQuery(); + if (rs.next()) { + publishedDefaultVersion = rs.getString("PUBLISHED_DEFAULT_API_VERSION"); + } else { + publishedDefaultVersion = getMigratedAPIProductPublishedDefaultVersion(connection, apiId); + } + } catch (SQLException e) { + handleException("Error while getting default version for " + apiId.getName(), e); + } finally { + APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs); + } + return publishedDefaultVersion; + } + + private String getMigratedAPIProductPublishedDefaultVersion(Connection connection, APIProductIdentifier apiId) + throws SQLException { + + String query = SQLConstants.GET_MIGRATED_API_PRODUCT_PUBLISHED_DEFAULT_VERSION_SQL; + try (PreparedStatement prepStmt = connection.prepareStatement(query)) { + prepStmt.setString(1, apiId.getName()); + prepStmt.setString(2, APIUtil.replaceEmailDomainBack(apiId.getProviderName())); + prepStmt.setString(3, APIConstants.API_PRODUCT_VERSION_1_0_0); + try (ResultSet rs = prepStmt.executeQuery()) { + if (rs.next()) { + return rs.getString("API_VERSION"); + } + } + } + return null; + } + + public void addUpdateAPIAsDefaultVersion(ApiTypeWrapper apiTypeWrapper, Connection connection) throws APIManagementException { - String publishedDefaultVersion = getPublishedDefaultVersion(apiTypeWrapper.getId()); + String publishedDefaultVersion; + if (apiTypeWrapper.isAPIProduct()) { + publishedDefaultVersion = getPublishedDefaultVersion((APIProductIdentifier) apiTypeWrapper.getId()); + } else { + publishedDefaultVersion = getPublishedDefaultVersion((APIIdentifier) apiTypeWrapper.getId()); + } + boolean deploymentAvailable = isDeploymentAvailableByAPIUUID(connection, apiTypeWrapper.getUuid()); ArrayList apiIdList = new ArrayList() {{ add(apiTypeWrapper.getId()); @@ -8068,6 +8159,35 @@ public boolean isContextExist(String context, String organization) { return false; } + public boolean isContextExistForAPIProducts(String context, String contextWithVersion, String organization) { + + Connection connection = null; + ResultSet resultSet = null; + PreparedStatement prepStmt = null; + + String sql = SQLConstants.GET_API_CONTEXT_SQL_FOR_API_PRODUCTS; + try { + connection = APIMgtDBUtil.getConnection(); + prepStmt = connection.prepareStatement(sql); + prepStmt.setString(1, context); + prepStmt.setString(2, contextWithVersion); + prepStmt.setString(3, context); + prepStmt.setString(4, organization); + resultSet = prepStmt.executeQuery(); + + while (resultSet.next()) { + if (resultSet.getString(1) != null) { + return true; + } + } + } catch (SQLException e) { + log.error("Failed to retrieve the API Context ", e); + } finally { + APIMgtDBUtil.closeAllConnections(prepStmt, connection, resultSet); + } + return false; + } + /** * Get API Context using a new DB connection. * @@ -9687,15 +9807,24 @@ public APIInfo getAPIInfoByUUID(String apiId) throws APIManagementException { } try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { + String contextTemplate = resultSet.getString("CONTEXT_TEMPLATE"); + String context = resultSet.getString("CONTEXT"); + String apiType = resultSet.getString("API_TYPE"); + String version = resultSet.getString("API_VERSION"); + if (APIConstants.API_PRODUCT.equals(apiType) + && APIConstants.API_PRODUCT_VERSION_1_0_0.equals(version) + && StringUtils.isBlank(contextTemplate)) { + context = context + "/" + APIConstants.API_PRODUCT_VERSION_1_0_0; + } APIInfo.Builder apiInfoBuilder = new APIInfo.Builder(); apiInfoBuilder = apiInfoBuilder.id(resultSet.getString("API_UUID")) .name(resultSet.getString("API_NAME")) - .version(resultSet.getString("API_VERSION")) + .version(version) .provider(resultSet.getString("API_PROVIDER")) - .context(resultSet.getString("CONTEXT")) - .contextTemplate(resultSet.getString("CONTEXT_TEMPLATE")) + .context(context) + .contextTemplate(contextTemplate) .status(APIUtil.getApiStatus(resultSet.getString("STATUS"))) - .apiType(resultSet.getString("API_TYPE")) + .apiType(apiType) .createdBy(resultSet.getString("CREATED_BY")) .createdTime(resultSet.getString("CREATED_TIME")) .updatedBy(resultSet.getString("UPDATED_BY")) @@ -9760,21 +9889,19 @@ public String getAPIStatusFromAPIUUID(String uuid) throws APIManagementException return null; } - public void setDefaultVersion(ApiTypeWrapper apiTypeWrapper) throws APIManagementException { + public void setDefaultVersion(API api) throws APIManagementException { try (Connection connection = APIMgtDBUtil.getConnection()) { try (PreparedStatement preparedStatement = connection.prepareStatement( SQLConstants.RETRIEVE_DEFAULT_VERSION)) { - preparedStatement.setString(1, apiTypeWrapper.getId().getName()); - preparedStatement.setString(2, - APIUtil.replaceEmailDomainBack(apiTypeWrapper.getId().getProviderName())); + preparedStatement.setString(1, api.getId().getName()); + preparedStatement.setString(2, APIUtil.replaceEmailDomainBack(api.getId().getProviderName())); try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { - apiTypeWrapper.setAsDefaultVersion( - apiTypeWrapper.getId().getVersion() - .equals(resultSet.getString("DEFAULT_API_VERSION"))); - apiTypeWrapper.setAsPublishedDefaultVersion(apiTypeWrapper.getId().getVersion() + api.setDefaultVersion(api.getId().getVersion() + .equals(resultSet.getString("DEFAULT_API_VERSION"))); + api.setAsPublishedDefaultVersion(api.getId().getVersion() .equals(resultSet.getString("PUBLISHED_DEFAULT_API_VERSION"))); } } @@ -9785,6 +9912,38 @@ public void setDefaultVersion(ApiTypeWrapper apiTypeWrapper) throws APIManagemen } } + public void setDefaultVersion(APIProduct apiProduct) throws APIManagementException { + + try (Connection connection = APIMgtDBUtil.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement( + SQLConstants.RETRIEVE_DEFAULT_VERSION_WITH_API_INFO)) { + preparedStatement.setString(1, apiProduct.getId().getName()); + preparedStatement.setString(2, + APIUtil.replaceEmailDomainBack(apiProduct.getId().getProviderName())); + preparedStatement.setString(3, apiProduct.getId().getVersion()); + + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + String defaultAPIVersion = resultSet.getString("DEFAULT_API_VERSION"); + String publishedDefaultAPIVersion = resultSet.getString("PUBLISHED_DEFAULT_API_VERSION"); + String contextTemplate = resultSet.getString("CONTEXT_TEMPLATE"); + + if (StringUtils.isBlank(defaultAPIVersion) && StringUtils.isBlank(contextTemplate)) { + defaultAPIVersion = apiProduct.getId().getVersion(); + publishedDefaultAPIVersion = apiProduct.getId().getVersion(); + } + apiProduct.setDefaultVersion(apiProduct.getId().getVersion().equals(defaultAPIVersion)); + apiProduct.setAsPublishedDefaultVersion(apiProduct.getId().getVersion() + .equals(publishedDefaultAPIVersion)); + } + } + } + } catch (SQLException e) { + throw new APIManagementException("Error while retrieving apimgt connection", e, + ExceptionCodes.INTERNAL_ERROR); + } + } + public API getLightWeightAPIInfoByAPIIdentifier(APIIdentifier apiIdentifier, String organization) throws APIManagementException { @@ -9800,9 +9959,9 @@ public API getLightWeightAPIInfoByAPIIdentifier(APIIdentifier apiIdentifier, Str apiIdentifier.setId(resultSet.getInt("API_ID")); API api = new API(apiIdentifier); api.setUuid(resultSet.getString("API_UUID")); - api.setContext(resultSet.getString("CONTEXT")); api.setType(resultSet.getString("API_TYPE")); api.setStatus(resultSet.getString("STATUS")); + setContext(apiIdentifier, resultSet, api); return api; } } @@ -9815,6 +9974,19 @@ public API getLightWeightAPIInfoByAPIIdentifier(APIIdentifier apiIdentifier, Str return null; } + private static void setContext(APIIdentifier apiIdentifier, ResultSet resultSet, API api) throws SQLException { + String context = resultSet.getString("CONTEXT"); + String contextTemplate = resultSet.getString("CONTEXT_TEMPLATE"); + // If context template is null for migrated API Products, set the default version as the Context/1.0.0 + if (APIConstants.API_PRODUCT_VERSION_1_0_0.equals(apiIdentifier.getVersion()) + && StringUtils.isBlank(contextTemplate)) { + context = context + "/" + APIConstants.API_PRODUCT_VERSION_1_0_0; + + } + api.setContext(context); + api.setContextTemplate(contextTemplate); + } + /** * Identify whether the loggedin user used his ordinal username or email * @@ -14840,6 +15012,18 @@ public void deleteAPIProduct(APIProductIdentifier productIdentifier) throws APIM deleteAllAPISpecificOperationPoliciesByAPIUUID(connection, productIdentifier.getUUID(), null); + // delete the default version if the deleted product is a default version + String curDefaultVersion = getDefaultVersion(productIdentifier); + String pubDefaultVersion = getPublishedDefaultVersion(productIdentifier); + if (productIdentifier.getVersion().equals(curDefaultVersion)) { + ArrayList apiIdList = new ArrayList() {{ + add(productIdentifier); + }}; + removeAPIFromDefaultVersion(apiIdList, connection); + } else if (productIdentifier.getVersion().equals(pubDefaultVersion)) { + setPublishedDefVersion(productIdentifier, connection, null); + } + connection.commit(); } catch (SQLException e) { handleException("Error while deleting api product " + productIdentifier, e); @@ -14882,7 +15066,7 @@ public int getAPIProductId(APIProductIdentifier identifier) throws APIManagement preparedStatement = conn.prepareStatement(queryGetProductId); preparedStatement.setString(1, identifier.getName()); preparedStatement.setString(2, APIUtil.replaceEmailDomainBack(identifier.getProviderName())); - preparedStatement.setString(3, identifier.getVersion()); //versioning is not supported atm + preparedStatement.setString(3, identifier.getVersion()); rs = preparedStatement.executeQuery(); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java index e45bce8c65d9..91e962a9396c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java @@ -21,6 +21,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.jetbrains.annotations.Nullable; import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.dto.ConditionDTO; import org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO; @@ -417,9 +418,12 @@ public List getAllApis(String organization, boolean isExpand) { api.setStatus(resultSet.getString("STATUS")); api.setOrganization(resultSet.getString("ORGANIZATION")); String publishedDefaultApiVersion = resultSet.getString("PUBLISHED_DEFAULT_API_VERSION"); - if (StringUtils.isNotBlank(publishedDefaultApiVersion)) { - api.setIsDefaultVersion(true); - } + String context = resultSet.getString("CONTEXT"); + String contextTemplate = resultSet.getString("CONTEXT_TEMPLATE"); + + setDefaultVersionContext(apiType, api, version, publishedDefaultApiVersion, context, + contextTemplate); + if (isExpand) { String revision = resultSet.getString("REVISION_UUID"); api.setPolicy(getAPILevelTier(connection, apiUuid, revision)); @@ -444,6 +448,8 @@ public List getAllApis(String organization, boolean isExpand) { return apiList; } + + /* * This method can be used to retrieve all the APIs of a given tesanat in the database * @@ -1081,6 +1087,7 @@ public List getAllApis(String organization, String deployment, boolean isEx API api = new API(); String provider = resultSet.getString("API_PROVIDER"); String name = resultSet.getString("API_NAME"); + String context = resultSet.getString("CONTEXT"); String version = resultSet.getString("API_VERSION"); String apiUuid = resultSet.getString("API_UUID"); api.setApiUUID(apiUuid); @@ -1091,12 +1098,13 @@ public List getAllApis(String organization, String deployment, boolean isEx api.setApiType(apiType); api.setStatus(resultSet.getString("STATUS")); api.setPolicy(resultSet.getString("API_TIER")); - api.setContext(resultSet.getString("CONTEXT")); api.setOrganization(resultSet.getString("ORGANIZATION")); String publishedDefaultApiVersion = resultSet.getString("PUBLISHED_DEFAULT_API_VERSION"); - if (StringUtils.isNotBlank(publishedDefaultApiVersion)) { - api.setIsDefaultVersion(true); - } + String contextTemplate = resultSet.getString("CONTEXT_TEMPLATE"); + + setDefaultVersionContext(apiType, api, version, context, contextTemplate, + publishedDefaultApiVersion); + if (isExpand) { String revision = resultSet.getString("REVISION_UUID"); api.setPolicy(getAPILevelTier(connection, apiUuid, revision)); @@ -1118,6 +1126,25 @@ public List getAllApis(String organization, String deployment, boolean isEx return apiList; } + private static void setDefaultVersionContext(String apiType, API api, String version, + String publishedDefaultApiVersion, String context, String contextTemplate) { + + if (StringUtils.isNotBlank(publishedDefaultApiVersion) + && StringUtils.equals(version, publishedDefaultApiVersion)) { + api.setIsDefaultVersion(true); + } + + if (APIConstants.API_PRODUCT.equals(apiType) + && APIConstants.API_PRODUCT_VERSION_1_0_0.equals(version) + && StringUtils.isBlank(contextTemplate)) { + if (StringUtils.isBlank(publishedDefaultApiVersion)) { + api.setIsDefaultVersion(true); + } + String synapseContext = context + "/" + APIConstants.API_PRODUCT_VERSION_1_0_0; + api.setContext(synapseContext); + } + } + private void attachURlMappingDetailsOfApiProduct(Connection connection, API api) throws SQLException { String sql = SubscriptionValidationSQLConstants.GET_ALL_API_PRODUCT_URI_TEMPLATES_SQL; @@ -1150,10 +1177,20 @@ private void attachURlMappingDetailsOfApiProduct(Connection connection, API api) public API getAPIByContextAndVersion(String context, String version, String deployment, boolean isExpand) { String sql = SubscriptionValidationSQLConstants.GET_API_BY_CONTEXT_AND_VERSION_SQL; + String contextWhenContextTemplateIsNull = context; + + String versionInContext = "/" + version; + int lastIndex = context.lastIndexOf(versionInContext); + if (lastIndex >= 0) { + contextWhenContextTemplateIsNull = context.substring(0, lastIndex) + + context.substring(lastIndex + versionInContext.length()); + } + try (Connection connection = APIMgtDBUtil.getConnection()) { try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - preparedStatement.setString(1, context); - preparedStatement.setString(2, version); + preparedStatement.setString(1, contextWhenContextTemplateIsNull); + preparedStatement.setString(2, context); + preparedStatement.setString(3, version); try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { String deploymentName = resultSet.getString("DEPLOYMENT_NAME"); @@ -1187,7 +1224,6 @@ public API getAPIByContextAndVersion(String context, String version, String depl } else { api.setPolicy(null); } - return api; } } } @@ -1256,12 +1292,15 @@ public API getApiByUUID(String apiId, String deployment, String organization, bo try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { String deploymentName = resultSet.getString("DEPLOYMENT_NAME"); + String context = resultSet.getString("CONTEXT"); + if (!deployment.equals(deploymentName)) { continue; } String apiType = resultSet.getString("API_TYPE"); String version = resultSet.getString("API_VERSION"); String apiUuid = resultSet.getString("API_UUID"); + String contextTemplate = resultSet.getString("CONTEXT_TEMPLATE"); API api = new API(); String provider = resultSet.getString("API_PROVIDER"); String name = resultSet.getString("API_NAME"); @@ -1273,10 +1312,13 @@ public API getApiByUUID(String apiId, String deployment, String organization, bo api.setApiType(apiType); api.setOrganization(resultSet.getString("ORGANIZATION")); api.setPolicy(resultSet.getString("API_TIER")); - api.setContext(resultSet.getString("CONTEXT")); + api.setContext(context); api.setStatus(resultSet.getString("STATUS")); String revision = resultSet.getString("REVISION_UUID"); - api.setIsDefaultVersion(isAPIDefaultVersion(connection, provider, name, version)); + String publishedDefaultApiVersion = getAPIDefaultVersion(connection, provider, name); + + setDefaultVersionContext(apiType, api, version, publishedDefaultApiVersion, context, + contextTemplate); if (isExpand) { api.setPolicy(getAPILevelTier(connection, apiUuid, revision)); if (APIConstants.API_PRODUCT.equals(apiType)) { @@ -1297,6 +1339,24 @@ public API getApiByUUID(String apiId, String deployment, String organization, bo return null; } + private String getAPIDefaultVersion(Connection connection, String provider, String name) + throws SQLException { + + try (PreparedStatement preparedStatement = + connection.prepareStatement(SubscriptionValidationSQLConstants.GET_API_DEFAULT_VERSION_STRING_SQL)) { + preparedStatement.setString(1, name); + preparedStatement.setString(2, provider); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return resultSet.getString("PUBLISHED_DEFAULT_API_VERSION"); + } + } + } catch (SQLException e) { + log.error("Error while loading default version", e); + } + return null; + } + private String getAPILevelTier(Connection connection, String apiUUID, String revisionUUID) throws SQLException { try (PreparedStatement preparedStatement = @@ -1328,6 +1388,7 @@ public List getAllApisByLabel(String gatewayLabel, Boolean expand) { API api = new API(); String provider = resultSet.getString("API_PROVIDER"); String name = resultSet.getString("API_NAME"); + String context = resultSet.getString("CONTEXT"); String version = resultSet.getString("API_VERSION"); api.setApiUUID(apiUuid); api.setApiId(resultSet.getInt("API_ID")); @@ -1342,9 +1403,11 @@ public List getAllApisByLabel(String gatewayLabel, Boolean expand) { api.setRevision(revision); api.setEnvironment(deploymentName); String publishedDefaultApiVersion = resultSet.getString("PUBLISHED_DEFAULT_API_VERSION"); - if (StringUtils.isNotBlank(publishedDefaultApiVersion)) { - api.setIsDefaultVersion(true); - } + String contextTemplate = resultSet.getString("CONTEXT_TEMPLATE"); + + setDefaultVersionContext(apiType, api, version, publishedDefaultApiVersion, context, + contextTemplate); + if (expand) { api.setPolicy(getAPILevelTier(connection, apiUuid, revision)); if (APIConstants.API_PRODUCT.equals(apiType)) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java index 69f5e4070230..d3cf235e1b8e 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java @@ -1353,9 +1353,10 @@ public class SQLConstants { "SELECT API.API_ID FROM AM_API API WHERE API.API_PROVIDER = ? AND API.API_NAME = ? AND API.API_VERSION = ? "; public static final String GET_API_ID_SQL_BY_UUID = "SELECT API.API_ID FROM AM_API API WHERE API.API_UUID = ?"; + public static final String GET_LIGHT_WEIGHT_API_INFO_BY_API_IDENTIFIER = "SELECT API_ID,API_UUID,API_PROVIDER," + - "API_NAME,API_VERSION,CONTEXT,API_TYPE,STATUS FROM AM_API WHERE API_PROVIDER = ? AND API_NAME = ? AND " + - "API_VERSION = ? AND ORGANIZATION = ?"; + "API_NAME, API_VERSION, CONTEXT_TEMPLATE, CONTEXT, API_TYPE, STATUS FROM AM_API WHERE API_PROVIDER = ? " + + "AND API_NAME = ? AND API_VERSION = ? AND ORGANIZATION = ?"; public static final String GET_API_PRODUCT_ID_SQL = "SELECT API_ID FROM AM_API WHERE API_PROVIDER = ? AND API_NAME = ? " @@ -1459,6 +1460,10 @@ public class SQLConstants { public static final String GET_DEFAULT_VERSION_SQL = "SELECT DEFAULT_API_VERSION FROM AM_API_DEFAULT_VERSION WHERE API_NAME= ? AND API_PROVIDER= ? "; + public static final String GET_MIGRATED_API_PRODUCT_DEFAULT_VERSION_SQL = + "SELECT API_VERSION FROM AM_API WHERE API_NAME= ? AND API_PROVIDER= ? AND API_VERSION= ? " + + "AND CONTEXT_TEMPLATE IS NULL"; + public static final String ADD_WORKFLOW_ENTRY_SQL = " INSERT INTO AM_WORKFLOWS (WF_REFERENCE,WF_TYPE,WF_STATUS,WF_CREATED_TIME,WF_STATUS_DESC,TENANT_ID," + "TENANT_DOMAIN,WF_EXTERNAL_REFERENCE,WF_METADATA,WF_PROPERTIES)" + @@ -1519,6 +1524,10 @@ public class SQLConstants { public static final String GET_PUBLISHED_DEFAULT_VERSION_SQL = "SELECT PUBLISHED_DEFAULT_API_VERSION FROM AM_API_DEFAULT_VERSION WHERE API_NAME= ? AND API_PROVIDER= ? "; + public static final String GET_MIGRATED_API_PRODUCT_PUBLISHED_DEFAULT_VERSION_SQL = + "SELECT API_VERSION FROM AM_API WHERE API_NAME= ? AND API_PROVIDER= ? AND API_VERSION= ? AND " + + "CONTEXT_TEMPLATE IS NULL AND REVISIONS_CREATED != 0"; + public static final String ADD_API_DEFAULT_VERSION_SQL = " INSERT INTO " + " AM_API_DEFAULT_VERSION(API_NAME,API_PROVIDER,DEFAULT_API_VERSION,PUBLISHED_DEFAULT_API_VERSION," @@ -1935,6 +1944,9 @@ public class SQLConstants { public static final String GET_API_CONTEXT_SQL = "SELECT CONTEXT FROM AM_API WHERE CONTEXT= ? AND ORGANIZATION = ?"; + public static final String GET_API_CONTEXT_SQL_FOR_API_PRODUCTS = + "SELECT CONTEXT FROM AM_API WHERE (CONTEXT= ? OR CONTEXT= ? OR CONTEXT_TEMPLATE= ?) AND ORGANIZATION = ?"; + public static final String GET_API_IDENTIFIER_BY_UUID_SQL = "SELECT API_PROVIDER, API_NAME, API_VERSION FROM AM_API WHERE API_UUID = ?"; public static final String GET_API_OR_API_PRODUCT_IDENTIFIER_BY_UUID_SQL = @@ -3001,6 +3013,17 @@ public class SQLConstants { " ORGANIZATION, REVISIONS_CREATED, STATUS FROM AM_API WHERE API_UUID = ?"; public static final String RETRIEVE_DEFAULT_VERSION = "SELECT DEFAULT_API_VERSION,PUBLISHED_DEFAULT_API_VERSION " + "FROM AM_API_DEFAULT_VERSION WHERE API_NAME = ? AND API_PROVIDER =?"; + + public static final String RETRIEVE_DEFAULT_VERSION_WITH_API_INFO = "SELECT AM_API.API_PROVIDER, AM_API.API_NAME, " + + "AM_API.API_VERSION, AM_API.CONTEXT_TEMPLATE, AM_API_DEFAULT_VERSION.PUBLISHED_DEFAULT_API_VERSION AS " + + "PUBLISHED_DEFAULT_API_VERSION, AM_API_DEFAULT_VERSION.DEFAULT_API_VERSION AS DEFAULT_API_VERSION " + + "FROM AM_API " + + "LEFT JOIN AM_API_DEFAULT_VERSION " + + "ON AM_API_DEFAULT_VERSION.API_NAME = AM_API.API_NAME AND " + + "AM_API_DEFAULT_VERSION.API_PROVIDER = AM_API.API_PROVIDER AND " + + "AM_API_DEFAULT_VERSION.ORGANIZATION = AM_API.ORGANIZATION " + + "WHERE AM_API.API_NAME = ? AND AM_API.API_PROVIDER = ? AND AM_API.API_VERSION = ?"; + public static final String UPDATE_REVISION_CREATED_BY_API_SQL = "UPDATE AM_API SET REVISIONS_CREATED = ? WHERE " + "API_UUID = ?"; public static final String ADD_API_REVISION_METADATA = "INSERT INTO AM_API_REVISION_METADATA (API_UUID," + diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java index e740b69c76cd..de6206a46e82 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java @@ -553,10 +553,10 @@ public class SubscriptionValidationSQLConstants { public static final String GET_API_BY_UUID_SQL = "SELECT " + - "AM_API.API_PROVIDER,AM_API.API_NAME,AM_API.CONTEXT,AM_API.API_UUID,AM_API.API_ID,AM_API" + - ".API_TIER,AM_API.API_VERSION,AM_API.API_TYPE,AM_API.STATUS,AM_REVISION.REVISION_UUID AS " + - "REVISION_UUID,AM_DEPLOYMENT_REVISION_MAPPING.NAME AS DEPLOYMENT_NAME,AM_API.ORGANIZATION " + - "FROM " + + "AM_API.API_PROVIDER,AM_API.API_NAME,AM_API.CONTEXT, AM_API.CONTEXT_TEMPLATE, AM_API.API_UUID, " + + "AM_API.API_ID, AM_API.API_TIER, AM_API.API_VERSION, AM_API.API_TYPE, AM_API.STATUS, " + + "AM_REVISION.REVISION_UUID AS REVISION_UUID,AM_DEPLOYMENT_REVISION_MAPPING.NAME AS " + + "DEPLOYMENT_NAME,AM_API.ORGANIZATION FROM " + "AM_API LEFT JOIN AM_REVISION ON AM_API.API_UUID = AM_REVISION.API_UUID " + "LEFT JOIN AM_DEPLOYMENT_REVISION_MAPPING " + "ON AM_REVISION.REVISION_UUID=AM_DEPLOYMENT_REVISION_MAPPING.REVISION_UUID " + @@ -565,6 +565,9 @@ public class SubscriptionValidationSQLConstants { public static final String GET_DEFAULT_VERSION_API_SQL = "SELECT PUBLISHED_DEFAULT_API_VERSION FROM " + "AM_API_DEFAULT_VERSION WHERE API_NAME = ? AND API_PROVIDER = ? AND PUBLISHED_DEFAULT_API_VERSION = ?"; + public static final String GET_API_DEFAULT_VERSION_STRING_SQL = "SELECT PUBLISHED_DEFAULT_API_VERSION FROM " + + "AM_API_DEFAULT_VERSION WHERE API_NAME = ? AND API_PROVIDER = ?"; + public static final String GET_URI_TEMPLATES_BY_API_SQL = "SELECT AM_API_URL_MAPPING.HTTP_METHOD," + "AM_API_URL_MAPPING.AUTH_SCHEME,AM_API_URL_MAPPING.URL_PATTERN,AM_API_URL_MAPPING.THROTTLING_TIER," + "AM_API_RESOURCE_SCOPE_MAPPING.SCOPE_NAME FROM AM_API_URL_MAPPING LEFT JOIN AM_API_RESOURCE_SCOPE_MAPPING" + @@ -572,7 +575,7 @@ public class SubscriptionValidationSQLConstants { "AM_API_URL_MAPPING.API_ID = ? AND AM_API_URL_MAPPING.REVISION_UUID = ?"; public static final String GET_ALL_APIS_BY_ORGANIZATION_AND_DEPLOYMENT_SQL = "SELECT AM_API.API_PROVIDER,AM_API" + - ".API_NAME,AM_API.CONTEXT,AM_API.API_UUID,AM_API.API_ID,AM_API.API_TIER,AM_API.API_VERSION,AM_API" + + ".API_NAME,AM_API.CONTEXT, AM_API.CONTEXT_TEMPLATE, AM_API.API_UUID,AM_API.API_ID,AM_API.API_TIER,AM_API.API_VERSION,AM_API" + ".API_TYPE,AM_API.STATUS,AM_REVISION.REVISION_UUID AS REVISION_UUID,AM_DEPLOYMENT_REVISION_MAPPING.NAME " + "AS DEPLOYMENT_NAME, " + "AM_API_DEFAULT_VERSION.PUBLISHED_DEFAULT_API_VERSION AS PUBLISHED_DEFAULT_API_VERSION,AM_API.ORGANIZATION " + @@ -580,7 +583,6 @@ public class SubscriptionValidationSQLConstants { "AM_DEPLOYMENT_REVISION_MAPPING ON AM_REVISION.REVISION_UUID=AM_DEPLOYMENT_REVISION_MAPPING.REVISION_UUID" + " LEFT JOIN AM_API_DEFAULT_VERSION ON AM_API_DEFAULT_VERSION.API_NAME = AM_API.API_NAME AND " + "AM_API_DEFAULT_VERSION.API_PROVIDER=AM_API.API_PROVIDER AND " + - "AM_API_DEFAULT_VERSION.PUBLISHED_DEFAULT_API_VERSION = AM_API.API_VERSION AND " + "AM_API_DEFAULT_VERSION.ORGANIZATION = AM_API.ORGANIZATION "; public static final String GET_ALL_API_PRODUCT_URI_TEMPLATES_SQL = "SELECT AM_API_URL_MAPPING.URL_MAPPING_ID," + @@ -590,10 +592,12 @@ public class SubscriptionValidationSQLConstants { ".URL_MAPPING_ID WHERE AM_API_URL_MAPPING.URL_MAPPING_ID IN (SELECT URL_MAPPING_ID FROM " + "AM_API_PRODUCT_MAPPING WHERE API_ID = ? )"; public static final String GET_API_BY_CONTEXT_AND_VERSION_SQL = "SELECT AM_API.API_PROVIDER,AM_API.API_NAME," + - "AM_API.CONTEXT,AM_API.API_UUID,AM_API.API_ID,AM_API.API_TIER,AM_API.API_VERSION,AM_API.API_TYPE,AM_API" + - ".STATUS,AM_REVISION.REVISION_UUID AS REVISION_UUID,AM_DEPLOYMENT_REVISION_MAPPING.NAME AS " + - "DEPLOYMENT_NAME,AM_API.ORGANIZATION FROM AM_API LEFT JOIN AM_REVISION ON " + - "AM_API.API_UUID=AM_REVISION.API_UUID LEFT JOIN AM_DEPLOYMENT_REVISION_MAPPING ON " + - "AM_REVISION.REVISION_UUID=AM_DEPLOYMENT_REVISION_MAPPING.REVISION_UUID WHERE AM_API.CONTEXT = ? AND " + - "AM_API.API_VERSION = ?"; + "AM_API.CONTEXT, AM_API.CONTEXT_TEMPLATE, AM_API.API_UUID, AM_API.API_ID,AM_API.API_TIER, " + + "AM_API.API_VERSION,AM_API.API_TYPE, AM_API.STATUS, AM_REVISION.REVISION_UUID AS REVISION_UUID, " + + "AM_DEPLOYMENT_REVISION_MAPPING.NAME AS DEPLOYMENT_NAME,AM_API.ORGANIZATION FROM AM_API LEFT JOIN " + + "AM_REVISION ON AM_API.API_UUID=AM_REVISION.API_UUID LEFT JOIN AM_DEPLOYMENT_REVISION_MAPPING ON " + + "AM_REVISION.REVISION_UUID=AM_DEPLOYMENT_REVISION_MAPPING.REVISION_UUID WHERE " + + "((AM_API.CONTEXT_TEMPLATE IS NULL AND AM_API.CONTEXT = ?) " + + "OR (AM_API.CONTEXT_TEMPLATE IS NOT NULL AND AM_API.CONTEXT = ?)) AND AM_API.API_VERSION = ?"; + } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/APIProviderImplTest.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/APIProviderImplTest.java index 4df38a328b6a..4d370d3c8246 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/APIProviderImplTest.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/APIProviderImplTest.java @@ -734,7 +734,7 @@ public void testGetAPIUsageByAPIId() throws APIManagementException, RegistryExce private APIProduct createMockAPIProduct(String provider) { APIProductIdentifier productIdentifier = new APIProductIdentifier(provider, APIConstants.API_PRODUCT, - APIConstants.API_PRODUCT_VERSION); + APIConstants.API_PRODUCT_VERSION_1_0_0); APIProduct apiProduct = new APIProduct(productIdentifier); apiProduct.setContext("/test"); apiProduct.setState(APIConstants.CREATED); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/dao/test/APIMgtDAOTest.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/dao/test/APIMgtDAOTest.java index 2538fb27b7ab..b8cbc6192600 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/dao/test/APIMgtDAOTest.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/dao/test/APIMgtDAOTest.java @@ -379,6 +379,24 @@ public void testKeyForwardCompatibilityWhenNewAPIVersion() throws Exception { apiMgtDAO.makeKeysForwardCompatibleForNewAPIVersion(apiTypeWrapper, oldApiVersionList); } + @Test + public void testKeyForwardCompatibilityWhenNewAPIProductVersion() throws Exception { + List oldApiProductVersionList = new ArrayList<>(); + APIProduct apiProductOld = new APIProduct(new APIProductIdentifier("SUMEDHA", + "APIPRODUCT1", "V1.0.0")); + oldApiProductVersionList.add(apiProductOld); + + APIProduct apiProduct = new APIProduct(new APIProductIdentifier("SUMEDHA", + "APIPRODUCT1", "V2.0.0")); + apiProduct.setContext("/context1"); + apiProduct.setContextTemplate("/context1/{version}"); + apiProduct.setVersionTimestamp(String.valueOf(System.currentTimeMillis())); + apiProduct.setUuid(UUID.randomUUID().toString()); + apiMgtDAO.addAPIProduct(apiProduct, "testOrg"); + ApiTypeWrapper apiTypeWrapper = new ApiTypeWrapper(apiProduct); + apiMgtDAO.makeKeysForwardCompatibleForNewAPIProductVersion(apiTypeWrapper, oldApiProductVersionList); + } + @Test public void testForwardingBlockedAndProdOnlyBlockedSubscriptionsToNewAPIVersion() throws APIManagementException { List oldApiVersionList = new ArrayList<>(); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java index 6a881b795309..2250caf4a002 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java @@ -1843,19 +1843,7 @@ public static APIProduct addAPIProductWithGeneratedSwaggerDefinition(APIProductD //isDefaultVersion is true for a new API Product. apiProductDTO.setIsDefaultVersion(true); - - //Remove the /{version} from the context. - if (context.endsWith("/" + RestApiConstants.API_VERSION_PARAM)) { - context = context.replace("/" + RestApiConstants.API_VERSION_PARAM, ""); - } - //Make sure context starts with "/". ex: /pizzaProduct - context = context.startsWith("/") ? context : ("/" + context); - //Check whether the context already exists - if (apiProvider.isContextExist(context, organization)) { - throw new APIManagementException( - "Error occurred while adding API Product. API Product with the context " + context + " already " + - "exists.", ExceptionCodes.from(ExceptionCodes.API_PRODUCT_CONTEXT_ALREADY_EXISTS, context)); - } + checkDuplicateContext(apiProvider, apiProductDTO, username, organization); // Set default gatewayVendor if (apiProductDTO.getGatewayVendor() == null) { @@ -1879,6 +1867,71 @@ public static APIProduct addAPIProductWithGeneratedSwaggerDefinition(APIProductD return createdProduct; } + private static void checkDuplicateContext(APIProvider apiProvider, APIProductDTO apiProductDTO, String username, + String organization) + throws APIManagementException { + + String context = apiProductDTO.getContext(); + //Remove the /{version} from the context. + if (context.endsWith("/" + RestApiConstants.API_VERSION_PARAM)) { + context = context.replace("/" + RestApiConstants.API_VERSION_PARAM, ""); + } + + //Make sure context starts with "/". ex: /pizzaProduct + context = context.startsWith("/") ? context : ("/" + context); + + //Create tenant aware context for API + if (context.startsWith("/t/" + organization)) { + context = context.replace("/t/" + organization, ""); + } + if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(organization) && + !context.contains("/t/" + organization)) { + context = "/t/" + organization + context; + } + + // Check whether the context already exists for migrated API products which were created with + // version appended context + String contextWithVersion = context; + if (contextWithVersion.contains("/" + RestApiConstants.API_VERSION_PARAM)) { + contextWithVersion = contextWithVersion.replace(RestApiConstants.API_VERSION_PARAM, + apiProductDTO.getVersion()); + } else { + contextWithVersion = contextWithVersion + "/" + apiProductDTO.getVersion(); + } + + //Get all existing versions of api product been adding + List apiVersions = apiProvider.getApiVersionsMatchingApiNameAndOrganization(apiProductDTO.getName(), + username, organization); + if (!apiVersions.isEmpty()) { + //If any previous version exists + for (String version : apiVersions) { + if (version.equalsIgnoreCase(apiProductDTO.getVersion())) { + //If version already exists + if (apiProvider.isDuplicateContextTemplateMatchingOrganization(context, organization)) { + throw new APIManagementException( + "Error occurred while adding the API Product. A duplicate API context already exists " + + "for " + context + " in the organization : " + organization, + ExceptionCodes.API_ALREADY_EXISTS); + } else { + throw new APIManagementException( + "Error occurred while adding API Product. API Product with name " + + apiProductDTO.getName() + " already exists with different context " + context + + " in the organization" + " : " + organization, + ExceptionCodes.API_ALREADY_EXISTS); + } + } + } + } else { + //If no any previous version exists + if (apiProvider.isContextExistForAPIProducts(context, contextWithVersion, organization)) { + throw new APIManagementException( + "Error occurred while adding the API Product. A duplicate API context already exists for " + + context + " in the organization" + " : " + organization, ExceptionCodes + .from(ExceptionCodes.API_CONTEXT_ALREADY_EXISTS, context)); + } + } + } + public static boolean isStreamingAPI(APIDTO apidto) { return APIDTO.TypeEnum.WS.equals(apidto.getType()) || APIDTO.TypeEnum.SSE.equals(apidto.getType()) || diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java index 071df0cee43e..368edc25fef2 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java @@ -29,6 +29,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.APIProvider; +import org.wso2.carbon.apimgt.api.FaultGatewaysException; import org.wso2.carbon.apimgt.api.model.APIProduct; import org.wso2.carbon.apimgt.api.model.APIProductIdentifier; import org.wso2.carbon.apimgt.api.model.APIStatus; @@ -36,9 +37,11 @@ import org.wso2.carbon.apimgt.api.model.Tier; import org.wso2.carbon.apimgt.impl.APIConstants; import org.wso2.carbon.apimgt.impl.utils.APIUtil; +import org.wso2.carbon.apimgt.impl.utils.TierNameComparator; import org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory; import org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.AdvertiseInfoDTO; import java.util.ArrayList; @@ -49,6 +52,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeSet; import static org.mockito.Mockito.when; import static org.wso2.carbon.apimgt.impl.APIConstants.API_DATA_PRODUCTION_ENDPOINTS; @@ -63,8 +67,17 @@ public class PublisherCommonUtilsTest { private static final String API_PRODUCT_NAME = "test"; private static final String API_PRODUCT_VERSION = "1.0.0"; private static final String ORGANIZATION = "carbon.super"; + + private static final String TENANT_ORGANIZATION = "wso2.com"; private static final String UUID = "63e1e37e-a5b8-4be6-86a5-d6ae0749f131"; + private static final String API_PRODUCT_CONTEXT = "/test-context"; + private static final String API_PRODUCT_VERSION_APPENDED_CONTEXT = "/test-context/1.0.0"; + + private static final String API_PRODUCT_CONTEXT_FOR_TENANT = "/t/wso2.com/test-context"; + + private static final String API_PRODUCT_CONTEXT_TEMPLATE = "/test-context/{version}"; + @Test public void testGetInvalidTierNames() throws Exception { @@ -426,4 +439,191 @@ public void testValidateEndpointConfigs() { Assert.assertFalse(flag); } + + @Test + public void testCheckDuplicateContextForExistingVersions() throws APIManagementException { + + APIProductDTO apiProductDTO = getAPIProductDTOForDuplicateContextTest(); + APIProvider apiProvider = Mockito.mock(APIProvider.class); + + List apiVersions = new ArrayList<>(Arrays.asList("1.0.0", "2.0.0", "3.0.0")); + Set tiers = new TreeSet(new TierNameComparator()); + Mockito.when(apiProvider.getApiVersionsMatchingApiNameAndOrganization(API_PRODUCT_NAME, PROVIDER, ORGANIZATION)) + .thenReturn(apiVersions); + Mockito.when(apiProvider.isDuplicateContextTemplateMatchingOrganization(API_PRODUCT_CONTEXT, ORGANIZATION)) + .thenReturn(true); + Mockito.when(apiProvider.getTiers()).thenReturn(tiers); + + PowerMockito.mockStatic(APIUtil.class); + PowerMockito.mockStatic(RestApiCommonUtil.class); + PowerMockito.when(RestApiCommonUtil.getLoggedInUserProvider()).thenReturn(apiProvider); + + String expectedMessage = + "Error occurred while adding the API Product. A duplicate API context already exists for " + + API_PRODUCT_CONTEXT + " in the organization : " + ORGANIZATION; + + testDuplicateContextValidation(apiProductDTO, PROVIDER, ORGANIZATION, expectedMessage); + + // Test for context with "/{version}" + Mockito.when(apiProductDTO.getContext()).thenReturn(API_PRODUCT_CONTEXT_TEMPLATE); + + testDuplicateContextValidation(apiProductDTO, PROVIDER, ORGANIZATION, expectedMessage); + + // Test for context which has version already appended + Mockito.when(apiProductDTO.getContext()).thenReturn(API_PRODUCT_VERSION_APPENDED_CONTEXT); + Mockito.when(apiProvider.isDuplicateContextTemplateMatchingOrganization(API_PRODUCT_VERSION_APPENDED_CONTEXT, + ORGANIZATION)).thenReturn(true); + expectedMessage = "Error occurred while adding the API Product. A duplicate API context already exists for " + + API_PRODUCT_VERSION_APPENDED_CONTEXT + " in the organization : " + ORGANIZATION; + + testDuplicateContextValidation(apiProductDTO, PROVIDER, ORGANIZATION, expectedMessage); + + // Test for tenant context + Mockito.when(apiProductDTO.getContext()).thenReturn(API_PRODUCT_CONTEXT_FOR_TENANT); + Mockito.when(apiProvider.getApiVersionsMatchingApiNameAndOrganization(API_PRODUCT_NAME, PROVIDER, + TENANT_ORGANIZATION)).thenReturn(apiVersions); + Mockito.when(apiProvider.isDuplicateContextTemplateMatchingOrganization(API_PRODUCT_CONTEXT_FOR_TENANT, + TENANT_ORGANIZATION)).thenReturn(true); + expectedMessage = "Error occurred while adding the API Product. A duplicate API context already exists for " + + API_PRODUCT_CONTEXT_FOR_TENANT + " in the organization : " + TENANT_ORGANIZATION; + + testDuplicateContextValidation(apiProductDTO, PROVIDER, TENANT_ORGANIZATION, expectedMessage); + } + + @Test + public void testCheckDuplicateContextForSimilarAPINameWithDifferentContext() throws APIManagementException { + + APIProductDTO apiProductDTO = getAPIProductDTOForDuplicateContextTest(); + APIProvider apiProvider = Mockito.mock(APIProvider.class); + + List apiVersions = new ArrayList<>(Arrays.asList("1.0.0", "2.0.0", "3.0.0")); + Set tiers = new TreeSet(new TierNameComparator()); + Mockito.when(apiProvider.getApiVersionsMatchingApiNameAndOrganization(API_PRODUCT_NAME, PROVIDER, ORGANIZATION)) + .thenReturn(apiVersions); + Mockito.when(apiProvider.isDuplicateContextTemplateMatchingOrganization(API_PRODUCT_CONTEXT, ORGANIZATION)) + .thenReturn(false); + Mockito.when(apiProvider.getTiers()).thenReturn(tiers); + + PowerMockito.mockStatic(APIUtil.class); + PowerMockito.mockStatic(RestApiCommonUtil.class); + PowerMockito.when(RestApiCommonUtil.getLoggedInUserProvider()).thenReturn(apiProvider); + + String expectedMessage = "Error occurred while adding API Product. API Product with name " + API_PRODUCT_NAME + + " already exists with different context " + API_PRODUCT_CONTEXT + " in the organization : " + + ORGANIZATION; + + testDuplicateContextValidation(apiProductDTO, PROVIDER, ORGANIZATION, expectedMessage); + + // Test for context with "/{version}" + Mockito.when(apiProductDTO.getContext()).thenReturn(API_PRODUCT_CONTEXT_TEMPLATE); + + testDuplicateContextValidation(apiProductDTO, PROVIDER, ORGANIZATION, expectedMessage); + + // Test for context which has version already appended + Mockito.when(apiProductDTO.getContext()).thenReturn(API_PRODUCT_VERSION_APPENDED_CONTEXT); + Mockito.when(apiProvider.isDuplicateContextTemplateMatchingOrganization(API_PRODUCT_VERSION_APPENDED_CONTEXT, + ORGANIZATION)).thenReturn(false); + expectedMessage = "Error occurred while adding API Product. API Product with name " + API_PRODUCT_NAME + + " already exists with different context " + API_PRODUCT_VERSION_APPENDED_CONTEXT + + " in the organization : " + ORGANIZATION; + + testDuplicateContextValidation(apiProductDTO, PROVIDER, ORGANIZATION, expectedMessage); + + // Test for tenant context + Mockito.when(apiProductDTO.getContext()).thenReturn(API_PRODUCT_CONTEXT_FOR_TENANT); + Mockito.when(apiProvider.getApiVersionsMatchingApiNameAndOrganization(API_PRODUCT_NAME, PROVIDER, + TENANT_ORGANIZATION)).thenReturn(apiVersions); + Mockito.when(apiProvider.isDuplicateContextTemplateMatchingOrganization(API_PRODUCT_CONTEXT_FOR_TENANT, + TENANT_ORGANIZATION)).thenReturn(false); + expectedMessage = "Error occurred while adding API Product. API Product with name " + API_PRODUCT_NAME + + " already exists with different context " + API_PRODUCT_CONTEXT_FOR_TENANT + " in the organization : " + + TENANT_ORGANIZATION; + + testDuplicateContextValidation(apiProductDTO, PROVIDER, TENANT_ORGANIZATION, expectedMessage); + } + + @Test + public void testCheckDuplicateContextForNoPreviousVersions() throws APIManagementException { + + APIProductDTO apiProductDTO = getAPIProductDTOForDuplicateContextTest(); + APIProvider apiProvider = Mockito.mock(APIProvider.class); + + List apiVersions = new ArrayList<>(); + Set tiers = new TreeSet(new TierNameComparator()); + String contextWithVersion = API_PRODUCT_CONTEXT + "/" + API_PRODUCT_VERSION; + Mockito.when(apiProvider.getApiVersionsMatchingApiNameAndOrganization(API_PRODUCT_NAME, PROVIDER, ORGANIZATION)) + .thenReturn(apiVersions); + Mockito.when(apiProvider.isDuplicateContextTemplateMatchingOrganization(API_PRODUCT_CONTEXT, ORGANIZATION)) + .thenReturn(false); + Mockito.when(apiProvider.isContextExistForAPIProducts(API_PRODUCT_CONTEXT, contextWithVersion, ORGANIZATION)) + .thenReturn(true); + Mockito.when(apiProvider.getTiers()).thenReturn(tiers); + + PowerMockito.mockStatic(APIUtil.class); + PowerMockito.mockStatic(RestApiCommonUtil.class); + PowerMockito.when(RestApiCommonUtil.getLoggedInUserProvider()).thenReturn(apiProvider); + + String expectedMessage = + "Error occurred while adding the API Product. A duplicate API context already " + "exists for " + + API_PRODUCT_CONTEXT + " in the organization : " + ORGANIZATION; + + testDuplicateContextValidation(apiProductDTO, PROVIDER, ORGANIZATION, expectedMessage); + + // Test for context with "/{version}" + Mockito.when(apiProductDTO.getContext()).thenReturn(API_PRODUCT_CONTEXT_TEMPLATE); + + testDuplicateContextValidation(apiProductDTO, PROVIDER, ORGANIZATION, expectedMessage); + + // Test for context which has version already appended + contextWithVersion = API_PRODUCT_VERSION_APPENDED_CONTEXT + "/" + API_PRODUCT_VERSION; + Mockito.when(apiProductDTO.getContext()).thenReturn(API_PRODUCT_VERSION_APPENDED_CONTEXT); + Mockito.when(apiProvider.isDuplicateContextTemplateMatchingOrganization(API_PRODUCT_VERSION_APPENDED_CONTEXT, + ORGANIZATION)).thenReturn(false); + Mockito.when(apiProvider.isContextExistForAPIProducts(API_PRODUCT_VERSION_APPENDED_CONTEXT, contextWithVersion, + ORGANIZATION)).thenReturn(true); + expectedMessage = "Error occurred while adding the API Product. A duplicate API context already exists for " + + API_PRODUCT_VERSION_APPENDED_CONTEXT + " in the organization : " + ORGANIZATION; + + testDuplicateContextValidation(apiProductDTO, PROVIDER, ORGANIZATION, expectedMessage); + + // Test for tenant context + contextWithVersion = API_PRODUCT_CONTEXT_FOR_TENANT + "/" + API_PRODUCT_VERSION; + Mockito.when(apiProductDTO.getContext()).thenReturn(API_PRODUCT_CONTEXT_FOR_TENANT); + Mockito.when(apiProvider.isDuplicateContextTemplateMatchingOrganization(API_PRODUCT_CONTEXT_FOR_TENANT, + TENANT_ORGANIZATION)).thenReturn(false); + Mockito.when(apiProvider.isContextExistForAPIProducts(API_PRODUCT_CONTEXT_FOR_TENANT, contextWithVersion, + TENANT_ORGANIZATION)).thenReturn(true); + expectedMessage = "Error occurred while adding the API Product. A duplicate API context already exists for " + + API_PRODUCT_CONTEXT_FOR_TENANT + " in the organization : " + TENANT_ORGANIZATION; + + testDuplicateContextValidation(apiProductDTO, PROVIDER, TENANT_ORGANIZATION, expectedMessage); + } + + private APIProductDTO getAPIProductDTOForDuplicateContextTest() { + + APIProductDTO apiProductDTO = Mockito.mock(APIProductDTO.class); + Mockito.when(apiProductDTO.getContext()).thenReturn(API_PRODUCT_CONTEXT); + Mockito.when(apiProductDTO.getVersion()).thenReturn(API_PRODUCT_VERSION); + Mockito.when(apiProductDTO.getName()).thenReturn(API_PRODUCT_NAME); + Mockito.when(apiProductDTO.getProvider()).thenReturn(PROVIDER); + Mockito.when(apiProductDTO.getPolicies()).thenReturn(new ArrayList<>()); + Mockito.when(apiProductDTO.getAdditionalProperties()).thenReturn(null); + Mockito.when(apiProductDTO.getVisibility()).thenReturn(APIProductDTO.VisibilityEnum.PUBLIC); + Mockito.when(apiProductDTO.getAuthorizationHeader()).thenReturn(APIConstants.AUTHORIZATION_HEADER_DEFAULT); + return apiProductDTO; + } + + private void testDuplicateContextValidation(APIProductDTO apiProductDTO, String provider, String organization, + String expectedMessage) { + + try { + PublisherCommonUtils.addAPIProductWithGeneratedSwaggerDefinition(apiProductDTO, provider, organization); + Assert.fail("Duplicate context did not get identified"); + } catch (APIManagementException e) { + Assert.assertTrue("Received an incorrect error message", e.getMessage().contains(expectedMessage)); + } catch (FaultGatewaysException e) { + Assert.fail("Received an incorrect exception"); + } + } + } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/APIMappingUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/APIMappingUtil.java index fc8967c19c7b..4ae598be28f7 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/APIMappingUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/APIMappingUtil.java @@ -288,6 +288,7 @@ public static APIDTO fromAPItoDTO(APIProduct model, String organization) throws dto.setProvider(APIUtil.replaceEmailDomainBack(providerName)); dto.setId(model.getUuid()); dto.setContext(model.getContext()); + dto.setIsDefaultVersion(model.isPublishedDefaultVersion()); dto.setDescription(model.getDescription()); dto.setLifeCycleStatus(model.getState()); dto.setType(model.getType());