Search in sources :

Example 41 with APIRevision

use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.

the class ApiMgtDAO method getURITemplatesWithOperationPolicies.

/**
 * Get the set of URI templates that have Operation policies
 *
 * @param apiUUID Unique Identifier of API
 * @return URITemplate set
 * @throws APIManagementException
 */
public Set<URITemplate> getURITemplatesWithOperationPolicies(String apiUUID) throws APIManagementException {
    String query;
    APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(apiUUID);
    if (apiRevision == null) {
        query = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_OF_API_SQL;
    } else {
        query = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_FOR_API_REVISION_SQL;
    }
    Map<String, URITemplate> uriTemplates = new HashMap<>();
    Set<URITemplate> uriTemplateList = new HashSet<>();
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement prepStmt = connection.prepareStatement(query)) {
        if (apiRevision == null) {
            int apiId = getAPIID(apiUUID, connection);
            prepStmt.setInt(1, apiId);
        } else {
            int apiId = getAPIID(apiRevision.getApiUUID(), connection);
            prepStmt.setInt(1, apiId);
            prepStmt.setString(2, apiRevision.getRevisionUUID());
        }
        try (ResultSet rs = prepStmt.executeQuery()) {
            URITemplate uriTemplate;
            while (rs.next()) {
                String httpMethod = rs.getString("HTTP_METHOD");
                String urlPattern = rs.getString("URL_PATTERN");
                String urlTemplateKey = httpMethod + ":" + urlPattern;
                if (!uriTemplates.containsKey(urlTemplateKey)) {
                    uriTemplate = new URITemplate();
                } else {
                    uriTemplate = uriTemplates.get(urlTemplateKey);
                }
                OperationPolicy operationPolicy = populateOperationPolicyWithRS(rs);
                uriTemplate.addOperationPolicy(operationPolicy);
                uriTemplate.setHTTPVerb(httpMethod);
                uriTemplate.setUriTemplate(urlPattern);
                uriTemplate.setId(rs.getInt("URL_MAPPING_ID"));
                uriTemplates.put(urlTemplateKey, uriTemplate);
            }
        }
        uriTemplateList.addAll(uriTemplates.values());
    } catch (SQLException e) {
        handleException("Error while fetching URI templates with operation policies for " + apiUUID, e);
    }
    return uriTemplateList;
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) ResultSet(java.sql.ResultSet) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 42 with APIRevision

use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.

the class ApiMgtDAO method restoreAPIProductRevision.

/**
 * Restore API Product revision database records as the Current API Product of an API Product
 *
 * @param apiRevision content of the revision
 * @throws APIManagementException if an error occurs when restoring an API revision
 */
public void restoreAPIProductRevision(APIRevision apiRevision) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            // Retrieve API ID
            APIProductIdentifier apiProductIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiRevision.getApiUUID());
            int apiId = getAPIID(apiRevision.getApiUUID(), connection);
            int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()));
            String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
            // Remove Current API Product entries from AM_API_URL_MAPPING table
            PreparedStatement removeURLMappingsFromCurrentAPIProduct = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_CURRENT_API_PRODUCT_ENTRIES_IN_AM_API_URL_MAPPING);
            removeURLMappingsFromCurrentAPIProduct.setString(1, Integer.toString(apiId));
            removeURLMappingsFromCurrentAPIProduct.executeUpdate();
            // Copy Revision resources
            PreparedStatement getURLMappingsFromRevisionedAPIProduct = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_API_PRODUCT_REVISION_URL_MAPPINGS_BY_REVISION_UUID);
            getURLMappingsFromRevisionedAPIProduct.setString(1, apiRevision.getRevisionUUID());
            Map<String, URITemplate> urlMappingList = new HashMap<>();
            try (ResultSet rs = getURLMappingsFromRevisionedAPIProduct.executeQuery()) {
                String key, httpMethod, urlPattern;
                while (rs.next()) {
                    String script = null;
                    URITemplate uriTemplate = new URITemplate();
                    httpMethod = rs.getString("HTTP_METHOD");
                    urlPattern = rs.getString("URL_PATTERN");
                    uriTemplate.setHTTPVerb(httpMethod);
                    uriTemplate.setAuthType(rs.getString("AUTH_SCHEME"));
                    uriTemplate.setUriTemplate(rs.getString("URL_PATTERN"));
                    uriTemplate.setThrottlingTier(rs.getString("THROTTLING_TIER"));
                    InputStream mediationScriptBlob = rs.getBinaryStream("MEDIATION_SCRIPT");
                    if (mediationScriptBlob != null) {
                        script = APIMgtDBUtil.getStringFromInputStream(mediationScriptBlob);
                    }
                    uriTemplate.setMediationScript(script);
                    if (rs.getInt("API_ID") != 0) {
                        // Adding product id to uri template id just to store value
                        uriTemplate.setId(rs.getInt("API_ID"));
                    }
                    key = urlPattern + httpMethod;
                    urlMappingList.put(key, uriTemplate);
                }
            }
            // Populate Scope Mappings
            PreparedStatement getScopeMappingsFromRevisionedAPIProduct = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_API_PRODUCT_REVISION_SCOPE_MAPPINGS_BY_REVISION_UUID);
            getScopeMappingsFromRevisionedAPIProduct.setString(1, apiRevision.getRevisionUUID());
            try (ResultSet rs = getScopeMappingsFromRevisionedAPIProduct.executeQuery()) {
                while (rs.next()) {
                    String key = rs.getString("URL_PATTERN") + rs.getString("HTTP_METHOD");
                    if (urlMappingList.containsKey(key)) {
                        URITemplate uriTemplate = urlMappingList.get(key);
                        Scope scope = new Scope();
                        scope.setKey(rs.getString("SCOPE_NAME"));
                        uriTemplate.setScope(scope);
                        uriTemplate.setScopes(scope);
                    }
                }
            }
            setAPIProductOperationPoliciesToURITemplatesMap(apiRevision.getRevisionUUID(), urlMappingList);
            PreparedStatement insertURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_URL_MAPPINGS);
            for (URITemplate urlMapping : urlMappingList.values()) {
                insertURLMappingsStatement.setInt(1, urlMapping.getId());
                insertURLMappingsStatement.setString(2, urlMapping.getHTTPVerb());
                insertURLMappingsStatement.setString(3, urlMapping.getAuthType());
                insertURLMappingsStatement.setString(4, urlMapping.getUriTemplate());
                insertURLMappingsStatement.setString(5, urlMapping.getThrottlingTier());
                insertURLMappingsStatement.setString(6, Integer.toString(apiId));
                insertURLMappingsStatement.addBatch();
            }
            insertURLMappingsStatement.executeBatch();
            // Insert Scope Mappings and operation policy mappings
            PreparedStatement getRevisionedURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_REVISIONED_URL_MAPPINGS_ID);
            PreparedStatement addResourceScopeMapping = connection.prepareStatement(SQLConstants.ADD_API_RESOURCE_SCOPE_MAPPING);
            PreparedStatement addOperationPolicyStatement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.ADD_API_OPERATION_POLICY_MAPPING);
            Map<String, String> clonedPoliciesMap = new HashMap<>();
            Set<String> usedClonedPolicies = new HashSet<String>();
            for (URITemplate urlMapping : urlMappingList.values()) {
                getRevisionedURLMappingsStatement.setInt(1, urlMapping.getId());
                getRevisionedURLMappingsStatement.setString(2, Integer.toString(apiId));
                getRevisionedURLMappingsStatement.setString(3, urlMapping.getHTTPVerb());
                getRevisionedURLMappingsStatement.setString(4, urlMapping.getAuthType());
                getRevisionedURLMappingsStatement.setString(5, urlMapping.getUriTemplate());
                getRevisionedURLMappingsStatement.setString(6, urlMapping.getThrottlingTier());
                try (ResultSet rs = getRevisionedURLMappingsStatement.executeQuery()) {
                    if (rs.next()) {
                        int newURLMappingId = rs.getInt("URL_MAPPING_ID");
                        if (urlMapping.getScopes() != null && urlMapping.getScopes().size() > 0) {
                            for (Scope scope : urlMapping.getScopes()) {
                                addResourceScopeMapping.setString(1, scope.getKey());
                                addResourceScopeMapping.setInt(2, newURLMappingId);
                                addResourceScopeMapping.setInt(3, tenantId);
                                addResourceScopeMapping.addBatch();
                            }
                        }
                        if (urlMapping.getOperationPolicies().size() > 0) {
                            for (OperationPolicy policy : urlMapping.getOperationPolicies()) {
                                if (!clonedPoliciesMap.keySet().contains(policy.getPolicyName())) {
                                    String policyId = restoreOperationPolicyRevision(connection, apiRevision.getApiUUID(), policy.getPolicyId(), apiRevision.getId(), tenantDomain);
                                    clonedPoliciesMap.put(policy.getPolicyName(), policyId);
                                    usedClonedPolicies.add(policyId);
                                }
                                Gson gson = new Gson();
                                String paramJSON = gson.toJson(policy.getParameters());
                                addOperationPolicyStatement.setInt(1, rs.getInt(1));
                                addOperationPolicyStatement.setString(2, clonedPoliciesMap.get(policy.getPolicyName()));
                                addOperationPolicyStatement.setString(3, policy.getDirection());
                                addOperationPolicyStatement.setString(4, paramJSON);
                                addOperationPolicyStatement.setInt(5, policy.getOrder());
                                addOperationPolicyStatement.executeUpdate();
                            }
                        }
                    }
                }
            }
            addResourceScopeMapping.executeBatch();
            cleanUnusedClonedOperationPolicies(connection, usedClonedPolicies, apiRevision.getApiUUID());
            // Get URL_MAPPING_IDs from table and add records to product mapping table
            PreparedStatement getURLMappingOfAPIProduct = connection.prepareStatement(SQLConstants.GET_URL_MAPPING_IDS_OF_API_PRODUCT_SQL);
            PreparedStatement insertProductResourceMappingStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_PRODUCT_REVISION_RESOURCE_MAPPING);
            getURLMappingOfAPIProduct.setString(1, Integer.toString(apiId));
            try (ResultSet rs = getURLMappingOfAPIProduct.executeQuery()) {
                while (rs.next()) {
                    insertProductResourceMappingStatement.setInt(1, apiId);
                    insertProductResourceMappingStatement.setInt(2, rs.getInt("URL_MAPPING_ID"));
                    insertProductResourceMappingStatement.setString(3, "Current API");
                    insertProductResourceMappingStatement.addBatch();
                }
                insertProductResourceMappingStatement.executeBatch();
            }
            // Restoring AM_API_CLIENT_CERTIFICATE table entries
            PreparedStatement removeClientCertificatesStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_CURRENT_API_ENTRIES_IN_AM_API_CLIENT_CERTIFICATE_BY_API_ID);
            removeClientCertificatesStatement.setInt(1, apiId);
            removeClientCertificatesStatement.executeUpdate();
            PreparedStatement getClientCertificatesStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_CLIENT_CERTIFICATES_BY_REVISION_UUID);
            getClientCertificatesStatement.setInt(1, apiId);
            getClientCertificatesStatement.setString(2, apiRevision.getRevisionUUID());
            List<ClientCertificateDTO> clientCertificateDTOS = new ArrayList<>();
            try (ResultSet rs = getClientCertificatesStatement.executeQuery()) {
                while (rs.next()) {
                    ClientCertificateDTO clientCertificateDTO = new ClientCertificateDTO();
                    clientCertificateDTO.setAlias(rs.getString(1));
                    clientCertificateDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(rs.getBinaryStream(2)));
                    clientCertificateDTO.setTierName(rs.getString(3));
                    clientCertificateDTOS.add(clientCertificateDTO);
                }
            }
            PreparedStatement insertClientCertificateStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_CLIENT_CERTIFICATES_AS_CURRENT_API);
            for (ClientCertificateDTO clientCertificateDTO : clientCertificateDTOS) {
                insertClientCertificateStatement.setInt(1, tenantId);
                insertClientCertificateStatement.setString(2, clientCertificateDTO.getAlias());
                insertClientCertificateStatement.setInt(3, apiId);
                insertClientCertificateStatement.setBinaryStream(4, getInputStream(clientCertificateDTO.getCertificate()));
                insertClientCertificateStatement.setBoolean(5, false);
                insertClientCertificateStatement.setString(6, clientCertificateDTO.getTierName());
                insertClientCertificateStatement.setString(7, "Current API");
                insertClientCertificateStatement.addBatch();
            }
            insertClientCertificateStatement.executeBatch();
            // Restoring AM_GRAPHQL_COMPLEXITY table
            PreparedStatement removeGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_CURRENT_API_ENTRIES_IN_AM_GRAPHQL_COMPLEXITY_BY_API_ID);
            removeGraphQLComplexityStatement.setInt(1, apiId);
            removeGraphQLComplexityStatement.executeUpdate();
            PreparedStatement getGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_GRAPHQL_COMPLEXITY_BY_REVISION_UUID);
            List<CustomComplexityDetails> customComplexityDetailsList = new ArrayList<>();
            getGraphQLComplexityStatement.setInt(1, apiId);
            getGraphQLComplexityStatement.setString(2, apiRevision.getRevisionUUID());
            try (ResultSet rs1 = getGraphQLComplexityStatement.executeQuery()) {
                while (rs1.next()) {
                    CustomComplexityDetails customComplexityDetails = new CustomComplexityDetails();
                    customComplexityDetails.setType(rs1.getString("TYPE"));
                    customComplexityDetails.setField(rs1.getString("FIELD"));
                    customComplexityDetails.setComplexityValue(rs1.getInt("COMPLEXITY_VALUE"));
                    customComplexityDetailsList.add(customComplexityDetails);
                }
            }
            PreparedStatement insertGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_GRAPHQL_COMPLEXITY_AS_CURRENT_API);
            for (CustomComplexityDetails customComplexityDetails : customComplexityDetailsList) {
                insertGraphQLComplexityStatement.setString(1, UUID.randomUUID().toString());
                insertGraphQLComplexityStatement.setInt(2, apiId);
                insertGraphQLComplexityStatement.setString(3, customComplexityDetails.getType());
                insertGraphQLComplexityStatement.setString(4, customComplexityDetails.getField());
                insertGraphQLComplexityStatement.setInt(5, customComplexityDetails.getComplexityValue());
                insertGraphQLComplexityStatement.addBatch();
            }
            insertGraphQLComplexityStatement.executeBatch();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            handleException("Failed to restore API Revision entry of API UUID " + apiRevision.getApiUUID(), e);
        }
    } catch (SQLException e) {
        handleException("Failed to restore API Revision entry of API UUID " + apiRevision.getApiUUID(), e);
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) CustomComplexityDetails(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.CustomComplexityDetails) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Scope(org.wso2.carbon.apimgt.api.model.Scope) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) ResultSet(java.sql.ResultSet) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 43 with APIRevision

use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.

the class CertificateMgtDAO method getClientCertificates.

/**
 * Method to retrieve certificate metadata from db for specific tenant which matches alias or api identifier.
 * Both alias and api identifier are optional
 *
 * @param tenantId      : The id of the tenant which the certificate belongs to.
 * @param alias         : Alias for the certificate. (Optional)
 * @param apiIdentifier : The API which the certificate is mapped to. (Optional)
 * @param organization  : Organization
 * @return : A CertificateMetadataDTO object if the certificate is retrieved successfully, null otherwise.
 */
public List<ClientCertificateDTO> getClientCertificates(int tenantId, String alias, APIIdentifier apiIdentifier, String organization) throws CertificateManagementException {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    List<ClientCertificateDTO> clientCertificateDTOS = new ArrayList<>();
    int apiId = 0;
    int index = 1;
    String selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT;
    if (StringUtils.isNotEmpty(alias) && apiIdentifier != null) {
        selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT_ALIAS_APIID;
    } else if (StringUtils.isNotEmpty(alias)) {
        selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT_ALIAS;
    } else if (apiIdentifier != null) {
        selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT_APIID;
    }
    try {
        connection = APIMgtDBUtil.getConnection();
        if (apiIdentifier != null) {
            String apiUuid;
            if (apiIdentifier.getUUID() != null) {
                apiUuid = apiIdentifier.getUUID();
                APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiUuid);
                if (apiRevision != null && apiRevision.getApiUUID() != null) {
                    apiUuid = apiRevision.getApiUUID();
                }
            } else {
                apiUuid = ApiMgtDAO.getInstance().getUUIDFromIdentifier(apiIdentifier, organization);
            }
            apiId = ApiMgtDAO.getInstance().getAPIID(apiUuid, connection);
        }
        preparedStatement = connection.prepareStatement(selectQuery);
        preparedStatement.setBoolean(index, false);
        index++;
        preparedStatement.setInt(index, tenantId);
        index++;
        if (alias != null) {
            preparedStatement.setString(index, alias);
            index++;
        }
        if (apiIdentifier != null) {
            preparedStatement.setInt(index, apiId);
        }
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            alias = resultSet.getString("ALIAS");
            ClientCertificateDTO clientCertificateDTO = new ClientCertificateDTO();
            clientCertificateDTO.setTierName(resultSet.getString("TIER_NAME"));
            clientCertificateDTO.setAlias(alias);
            clientCertificateDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(resultSet.getBinaryStream("CERTIFICATE")));
            if (apiIdentifier == null) {
                apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(resultSet.getString("API_PROVIDER")), resultSet.getString("API_NAME"), resultSet.getString("API_VERSION"));
            }
            clientCertificateDTO.setApiIdentifier(apiIdentifier);
            clientCertificateDTOS.add(clientCertificateDTO);
        }
    } catch (SQLException e) {
        handleException("Error while searching client certificate details for the tenant " + tenantId, e);
    } catch (APIManagementException e) {
        handleException("API Management Exception while searching client certificate details for the tenant " + tenantId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(preparedStatement, connection, resultSet);
    }
    return clientCertificateDTOS;
}
Also used : APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 44 with APIRevision

use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.

the class APIProviderImplTest method testDeleteAPIRevision.

/**
 * This method tests deleting an API Revision
 *
 * @throws APIManagementException
 */
@Test
public void testDeleteAPIRevision() throws APIManagementException, APIPersistenceException {
    ImportExportAPI importExportAPI = Mockito.mock(ImportExportAPI.class);
    ArtifactSaver artifactSaver = Mockito.mock(ArtifactSaver.class);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, importExportAPI, gatewayArtifactsMgtDAO, artifactSaver);
    APIIdentifier apiId = new APIIdentifier("admin", "PizzaShackAPI", "1.0.0", "63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
    API api = new API(apiId);
    api.setContext("/test");
    api.setStatus(APIConstants.CREATED);
    String apiPath = "/apimgt/applicationdata/provider/admin/PizzaShackAPI/1.0.0/api";
    APIRevision apiRevision = new APIRevision();
    apiRevision.setApiUUID("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
    apiRevision.setDescription("test description revision 1");
    Mockito.when(apimgtDAO.getRevisionCountByAPI(Mockito.anyString())).thenReturn(0);
    Mockito.when(apimgtDAO.getMostRecentRevisionId(Mockito.anyString())).thenReturn(0);
    Mockito.when(APIUtil.getAPIIdentifierFromUUID(Mockito.anyString())).thenReturn(apiId);
    Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiPath);
    Mockito.when(APIUtil.getTenantConfig(Mockito.anyString())).thenReturn(new JSONObject());
    PowerMockito.when(apiPersistenceInstance.addAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyInt())).thenReturn("b55e0fc3-9829-4432-b99e-02056dc91838");
    try {
        apiProvider.addAPIRevision(apiRevision, superTenantDomain);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
    Mockito.when(apimgtDAO.getRevisionByRevisionUUID(Mockito.anyString())).thenReturn(apiRevision);
    PowerMockito.doNothing().when(apiPersistenceInstance).deleteAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyString(), Mockito.anyInt());
    try {
        apiProvider.deleteAPIRevision("63e1e37e-a5b8-4be6-86a5-d6ae0749f131", "b55e0fc3-9829-4432-b99e-02056dc91838", superTenantDomain);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ArtifactSaver(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.ArtifactSaver) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) JSONObject(org.json.simple.JSONObject) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) BlockConditionNotFoundException(org.wso2.carbon.apimgt.api.BlockConditionNotFoundException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) DuplicateAPIException(org.wso2.carbon.apimgt.api.model.DuplicateAPIException) IOException(java.io.IOException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) OMException(org.apache.axiom.om.OMException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 45 with APIRevision

use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.

the class APIProviderImplTest method testAddAPIRevision.

/**
 * This method tests adding a new API Revision
 *
 * @throws APIManagementException
 */
@Test
public void testAddAPIRevision() throws APIManagementException, APIPersistenceException, APIImportExportException, ArtifactSynchronizerException {
    ImportExportAPI importExportAPI = Mockito.mock(ImportExportAPI.class);
    ArtifactSaver artifactSaver = Mockito.mock(ArtifactSaver.class);
    Mockito.doNothing().when(artifactSaver).saveArtifact(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(File.class));
    Mockito.when(GatewayArtifactsMgtDAO.getInstance()).thenReturn(gatewayArtifactsMgtDAO);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, importExportAPI, gatewayArtifactsMgtDAO, artifactSaver);
    APIIdentifier apiId = new APIIdentifier("admin", "PizzaShackAPI", "1.0.0", "63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
    API api = new API(apiId);
    api.setContext("/test");
    api.setStatus(APIConstants.CREATED);
    String apiPath = "/apimgt/applicationdata/provider/admin/PizzaShackAPI/1.0.0/api";
    APIRevision apiRevision = new APIRevision();
    apiRevision.setApiUUID("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
    apiRevision.setDescription("test description revision 1");
    Mockito.when(apimgtDAO.getRevisionCountByAPI(Mockito.anyString())).thenReturn(0);
    Mockito.when(apimgtDAO.getMostRecentRevisionId(Mockito.anyString())).thenReturn(0);
    Mockito.when(APIUtil.getAPIIdentifierFromUUID(Mockito.anyString())).thenReturn(apiId);
    Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiPath);
    PowerMockito.when(apiPersistenceInstance.addAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyInt())).thenReturn("b55e0fc3-9829-4432-b99e-02056dc91838");
    Mockito.when(APIUtil.getTenantConfig(Mockito.anyString())).thenReturn(new JSONObject());
    try {
        apiProvider.addAPIRevision(apiRevision, superTenantDomain);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ArtifactSaver(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.ArtifactSaver) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) JSONObject(org.json.simple.JSONObject) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) File(java.io.File) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) BlockConditionNotFoundException(org.wso2.carbon.apimgt.api.BlockConditionNotFoundException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) DuplicateAPIException(org.wso2.carbon.apimgt.api.model.DuplicateAPIException) IOException(java.io.IOException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) OMException(org.apache.axiom.om.OMException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

APIRevision (org.wso2.carbon.apimgt.api.model.APIRevision)48 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)28 DeployedAPIRevision (org.wso2.carbon.apimgt.api.model.DeployedAPIRevision)23 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)18 ArrayList (java.util.ArrayList)16 Connection (java.sql.Connection)15 PreparedStatement (java.sql.PreparedStatement)15 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)13 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)13 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)13 API (org.wso2.carbon.apimgt.api.model.API)12 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)12 HashSet (java.util.HashSet)11 LinkedHashSet (java.util.LinkedHashSet)11 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)11 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)11 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)11 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)10