Search in sources :

Example 16 with APIProduct

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

the class APIProviderImplTest method createMockAPIProduct.

private APIProduct createMockAPIProduct(String provider) {
    APIProductIdentifier productIdentifier = new APIProductIdentifier(provider, APIConstants.API_PRODUCT, APIConstants.API_PRODUCT_VERSION);
    APIProduct apiProduct = new APIProduct(productIdentifier);
    apiProduct.setContext("/test");
    apiProduct.setState(APIConstants.CREATED);
    apiProduct.setType(APIConstants.API_PRODUCT);
    apiProduct.setOrganization(APIConstants.SUPER_TENANT_DOMAIN);
    return apiProduct;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct)

Example 17 with APIProduct

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

the class APIProviderImplTest method prepareForLCStateChangeOfAPIProduct.

private void prepareForLCStateChangeOfAPIProduct(APIProviderImplWrapper apiProvider, APIProduct apiProduct) throws Exception {
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PrivilegedCarbonContext prContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(prContext);
    PowerMockito.doNothing().when(prContext).setUsername(Mockito.anyString());
    PowerMockito.doNothing().when(prContext).setTenantDomain(Mockito.anyString(), Mockito.anyBoolean());
    Mockito.when(artifactManager.getGenericArtifact(any(String.class))).thenReturn(artifact);
    PowerMockito.when(RegistryPersistenceUtil.createAPIProductArtifactContent(artifact, apiProduct)).thenReturn(artifact);
    PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, artifact.getId())).thenReturn(artifactPath);
    Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
    Mockito.when(apiProvider.registry.resourceExists(artifactPath)).thenReturn(false);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) RealmService(org.wso2.carbon.user.core.service.RealmService) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager)

Example 18 with APIProduct

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

the class APIProviderImplTest method testUpdateAPIProductForStateChange.

@Test
public void testUpdateAPIProductForStateChange() throws Exception {
    String provider = "admin";
    PowerMockito.mockStatic(MultitenantUtils.class);
    Mockito.when(MultitenantUtils.getTenantDomain(provider)).thenReturn(APIConstants.SUPER_TENANT_DOMAIN);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
    APIProduct apiProduct = createMockAPIProduct(provider);
    prepareForLCStateChangeOfAPIProduct(apiProvider, apiProduct);
    PublisherAPIProduct publisherAPIProduct = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
    Organization organization = new Organization(APIConstants.SUPER_TENANT_DOMAIN);
    Mockito.when(apiPersistenceInstance.updateAPIProduct(organization, publisherAPIProduct)).thenReturn(publisherAPIProduct);
    apiProvider.updateAPIProductForStateChange(apiProduct, APIConstants.CREATED, APIConstants.PUBLISHED);
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 19 with APIProduct

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

the class ApiMgtDAO method updateAPIProduct.

public void updateAPIProduct(APIProduct product, String username) throws APIManagementException {
    Connection conn = null;
    PreparedStatement ps = null;
    if (log.isDebugEnabled()) {
        log.debug("updateAPIProduct() : product- " + product.toString());
    }
    try {
        conn = APIMgtDBUtil.getConnection();
        conn.setAutoCommit(false);
        String query = SQLConstants.UPDATE_PRODUCT_SQL;
        ps = conn.prepareStatement(query);
        ps.setString(1, product.getProductLevelPolicy());
        ps.setString(2, username);
        ps.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
        ps.setString(4, product.getGatewayVendor());
        APIProductIdentifier identifier = product.getId();
        ps.setString(5, identifier.getName());
        ps.setString(6, APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
        ps.setString(7, identifier.getVersion());
        ps.executeUpdate();
        int productId = getAPIID(product.getUuid(), conn);
        updateAPIProductResourceMappings(product, productId, conn);
        conn.commit();
    } catch (SQLException e) {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                log.error("Error while rolling back the failed operation", e1);
            }
        }
        handleException("Error in updating API Product: " + e.getMessage(), e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, null);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 20 with APIProduct

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

the class APIProviderImpl method addAPIProductWithoutPublishingToGateway.

@Override
public Map<API, List<APIProductResource>> addAPIProductWithoutPublishingToGateway(APIProduct product) throws APIManagementException {
    Map<API, List<APIProductResource>> apiToProductResourceMapping = new HashMap<>();
    validateApiProductInfo(product);
    String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(product.getId().getProviderName()));
    if (log.isDebugEnabled()) {
        log.debug("API Product details successfully added to the registry. API Product Name: " + product.getId().getName() + ", API Product Version : " + product.getId().getVersion() + ", API Product context : " + // todo: log context
        "change");
    }
    List<APIProductResource> resources = product.getProductResources();
    // list to hold resources which are actually in an existing api. If user has created an API product with invalid
    // API or invalid resource of a valid API, that content will be removed .validResources array will have only
    // legitimate apis
    List<APIProductResource> validResources = new ArrayList<APIProductResource>();
    for (APIProductResource apiProductResource : resources) {
        API api;
        String apiUUID;
        if (apiProductResource.getProductIdentifier() != null) {
            APIIdentifier productAPIIdentifier = apiProductResource.getApiIdentifier();
            String emailReplacedAPIProviderName = APIUtil.replaceEmailDomain(productAPIIdentifier.getProviderName());
            APIIdentifier emailReplacedAPIIdentifier = new APIIdentifier(emailReplacedAPIProviderName, productAPIIdentifier.getApiName(), productAPIIdentifier.getVersion());
            apiUUID = apiMgtDAO.getUUIDFromIdentifier(emailReplacedAPIIdentifier, product.getOrganization());
            api = getAPIbyUUID(apiUUID, product.getOrganization());
        } else {
            apiUUID = apiProductResource.getApiId();
            api = getAPIbyUUID(apiUUID, product.getOrganization());
        // if API does not exist, getLightweightAPIByUUID() method throws exception.
        }
        if (api != null) {
            validateApiLifeCycleForApiProducts(api);
            if (api.getSwaggerDefinition() != null) {
                api.setSwaggerDefinition(getOpenAPIDefinition(apiUUID, product.getOrganization()));
            }
            if (!apiToProductResourceMapping.containsKey(api)) {
                apiToProductResourceMapping.put(api, new ArrayList<>());
            }
            List<APIProductResource> apiProductResources = apiToProductResourceMapping.get(api);
            apiProductResources.add(apiProductResource);
            apiProductResource.setApiIdentifier(api.getId());
            apiProductResource.setProductIdentifier(product.getId());
            if (api.isAdvertiseOnly()) {
                apiProductResource.setEndpointConfig(APIUtil.generateEndpointConfigForAdvertiseOnlyApi(api));
            } else {
                apiProductResource.setEndpointConfig(api.getEndpointConfig());
            }
            apiProductResource.setEndpointSecurityMap(APIUtil.setEndpointSecurityForAPIProduct(api));
            URITemplate uriTemplate = apiProductResource.getUriTemplate();
            Map<String, URITemplate> templateMap = apiMgtDAO.getURITemplatesForAPI(api);
            if (uriTemplate == null) {
            // if no resources are define for the API, we ingore that api for the product
            } else {
                String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getResourceURI();
                if (templateMap.containsKey(key)) {
                    // Since the template ID is not set from the request, we manually set it.
                    uriTemplate.setId(templateMap.get(key).getId());
                    // request has a valid API id and a valid resource. we add it to valid resource map
                    validResources.add(apiProductResource);
                } else {
                    // ignore
                    log.warn("API with id " + apiProductResource.getApiId() + " does not have a resource " + uriTemplate.getResourceURI() + " with http method " + uriTemplate.getHTTPVerb());
                }
            }
        }
    }
    // set the valid resources only
    product.setProductResources(validResources);
    // now we have validated APIs and it's resources inside the API product. Add it to database
    String provider = APIUtil.replaceEmailDomain(product.getId().getProviderName());
    // Set version timestamp
    product.setVersionTimestamp(String.valueOf(System.currentTimeMillis()));
    // Create registry artifact
    String apiProductUUID = createAPIProduct(product);
    product.setUuid(apiProductUUID);
    // Add to database
    apiMgtDAO.addAPIProduct(product, product.getOrganization());
    return apiToProductResourceMapping;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) ArrayList(java.util.ArrayList) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) ArrayList(java.util.ArrayList) List(java.util.List) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Aggregations

APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)71 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)52 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)51 API (org.wso2.carbon.apimgt.api.model.API)37 ArrayList (java.util.ArrayList)31 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)22 PublisherAPIProduct (org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)22 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)21 Tier (org.wso2.carbon.apimgt.api.model.Tier)21 HashMap (java.util.HashMap)19 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)19 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)18 JSONObject (org.json.simple.JSONObject)17 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)17 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)16 HashSet (java.util.HashSet)15 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)15 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)14 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)14 ParseException (org.json.simple.parser.ParseException)12