Search in sources :

Example 51 with APIIdentifier

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

the class ApiMgtDAO method addExternalAPIStoresDetails.

/**
 * Store external APIStore details to which APIs successfully published
 *
 * @param uuid       API uuid
 * @param apiStoreSet APIStores set
 * @return added/failed
 * @throws APIManagementException
 */
public boolean addExternalAPIStoresDetails(String uuid, Set<APIStore> apiStoreSet) throws APIManagementException {
    Connection conn = null;
    PreparedStatement ps = null;
    boolean state = false;
    try {
        conn = APIMgtDBUtil.getConnection();
        conn.setAutoCommit(false);
        // This query to add external APIStores to database table
        String sqlQuery = SQLConstants.ADD_EXTERNAL_API_STORE_SQL;
        // Get API Id
        int apiIdentifier;
        apiIdentifier = getAPIID(uuid, conn);
        if (apiIdentifier == -1) {
            String msg = "Could not load API record for API with uuid: " + uuid;
            log.error(msg);
        }
        ps = conn.prepareStatement(sqlQuery);
        for (Object storeObject : apiStoreSet) {
            APIStore store = (APIStore) storeObject;
            ps.setInt(1, apiIdentifier);
            ps.setString(2, store.getName());
            ps.setString(3, store.getDisplayName());
            ps.setString(4, store.getEndpoint());
            ps.setString(5, store.getType());
            ps.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
            ps.addBatch();
        }
        ps.executeBatch();
        conn.commit();
        state = true;
    } catch (SQLException e) {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                log.error("Failed to rollback storing external apistore details ", e1);
            }
        }
        log.error("Failed to store external apistore details", e);
        state = false;
    } catch (APIManagementException e) {
        log.error("Failed to store external apistore details", e);
        state = false;
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, null);
    }
    return state;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) JSONObject(org.json.simple.JSONObject) Timestamp(java.sql.Timestamp) APIStore(org.wso2.carbon.apimgt.api.model.APIStore)

Example 52 with APIIdentifier

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

the class ApiMgtDAO method removeSubscription.

/**
 * Removes the subscription entry from AM_SUBSCRIPTIONS for identifier.
 *
 * @param identifier    Identifier
 * @param applicationId ID of the application which has the subscription
 * @throws APIManagementException
 */
public void removeSubscription(Identifier identifier, int applicationId) throws APIManagementException {
    Connection conn = null;
    ResultSet resultSet = null;
    PreparedStatement ps = null;
    int id = -1;
    String uuid;
    try {
        conn = APIMgtDBUtil.getConnection();
        conn.setAutoCommit(false);
        String subscriptionUUIDQuery = SQLConstants.GET_SUBSCRIPTION_UUID_SQL;
        if (identifier.getId() > 0) {
            id = identifier.getId();
        } else if (identifier instanceof APIIdentifier) {
            String apiUuid;
            if (identifier.getUUID() != null) {
                apiUuid = identifier.getUUID();
            } else {
                apiUuid = getUUIDFromIdentifier((APIIdentifier) identifier);
            }
            id = getAPIID(apiUuid, conn);
        } else if (identifier instanceof APIProductIdentifier) {
            id = ((APIProductIdentifier) identifier).getProductId();
        }
        ps = conn.prepareStatement(subscriptionUUIDQuery);
        ps.setInt(1, id);
        ps.setInt(2, applicationId);
        resultSet = ps.executeQuery();
        if (resultSet.next()) {
            uuid = resultSet.getString("UUID");
            SubscribedAPI subscribedAPI = new SubscribedAPI(uuid);
            removeSubscription(subscribedAPI, conn);
        } else {
            throw new APIManagementException("UUID does not exist for the given apiId:" + id + " and " + "application id:" + applicationId);
        }
        conn.commit();
    } catch (SQLException e) {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                log.error("Failed to rollback the add subscription ", ex);
            }
        }
        handleException("Failed to add subscriber data ", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, resultSet);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PreparedStatement(java.sql.PreparedStatement) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 53 with APIIdentifier

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

the class APIProviderImplTest method mockSequencesMultiple.

private void mockSequencesMultiple(String seqLoc, String apiSeqLoc, APIIdentifier apiId) throws Exception {
    ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenReturn(registry);
    Mockito.when(registry.resourceExists(seqLoc)).thenReturn(true);
    Collection seqCollection = Mockito.mock(Collection.class);
    Mockito.when(registry.get(seqLoc)).thenReturn(seqCollection);
    String[] seqChildPaths = { "path1", "path2" };
    Mockito.when(seqCollection.getChildren()).thenReturn(seqChildPaths);
    Resource sequence = Mockito.mock(Resource.class);
    Mockito.when(registry.get(seqChildPaths[0])).thenReturn(sequence);
    InputStream responseStream = IOUtils.toInputStream("<sequence name=\"pqr\"></sequence>", "UTF-8");
    Resource sequence2 = Mockito.mock(Resource.class);
    Mockito.when(registry.get(seqChildPaths[1])).thenReturn(sequence2);
    InputStream responseStream2 = IOUtils.toInputStream("<sequence name=\"abc\"></sequence>", "UTF-8");
    OMElement seqElment = buildOMElement(responseStream);
    OMElement seqElment2 = buildOMElement(responseStream2);
    PowerMockito.when(APIUtil.buildOMElement(responseStream)).thenReturn(seqElment);
    PowerMockito.when(APIUtil.buildOMElement(responseStream2)).thenReturn(seqElment2);
    Mockito.when(sequence.getContentStream()).thenReturn(responseStream);
    Mockito.when(sequence2.getContentStream()).thenReturn(responseStream2);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Collection(org.wso2.carbon.registry.core.Collection) OMElement(org.apache.axiom.om.OMElement) RegistryService(org.wso2.carbon.registry.core.service.RegistryService)

Example 54 with APIIdentifier

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

the class APIProviderImplTest method testDeleteWorkflowTask.

@Test
public void testDeleteWorkflowTask() throws APIManagementException, WorkflowException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    Mockito.when(apimgtDAO.getAPIID(apiUUID)).thenReturn(1111);
    WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
    Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
    WorkflowExecutor apiStateChangeWFExecutor = Mockito.mock(WorkflowExecutor.class);
    Mockito.when(wfe.getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(apiStateChangeWFExecutor);
    WorkflowDTO workflowDTO = Mockito.mock(WorkflowDTO.class);
    Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(1111), WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(workflowDTO);
    APIIdentifier identifier = new APIIdentifier("admin", "API1", "1.0.0", apiUUID);
    apiProvider.deleteWorkflowTask(identifier);
    Mockito.verify(apimgtDAO, Mockito.times(1)).getAPIID(apiUUID);
}
Also used : WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) WorkflowExecutorFactory(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory) APIStateChangeSimpleWorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.APIStateChangeSimpleWorkflowExecutor) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 55 with APIIdentifier

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

the class APIProviderImplTest method testGetAPIRevisions.

/**
 * This method tests adding a new API Revision and then retrieving API Revisions by API UUID
 *
 * @throws APIManagementException
 */
@Test
public void testGetAPIRevisions() throws APIManagementException, APIPersistenceException, ArtifactSynchronizerException {
    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);
        apiProvider.getAPIRevisions("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
    } 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)

Aggregations

APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)305 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)171 API (org.wso2.carbon.apimgt.api.model.API)160 Test (org.junit.Test)155 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)119 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)105 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)85 Resource (org.wso2.carbon.registry.core.Resource)79 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)69 ArrayList (java.util.ArrayList)68 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)56 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)48 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)46 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)46 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)44 HashSet (java.util.HashSet)41 HashMap (java.util.HashMap)40 IOException (java.io.IOException)37 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)37 JSONObject (org.json.simple.JSONObject)36