Search in sources :

Example 86 with APIProduct

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

the class RegistryPersistenceImplTestCase method testUpdateAPIProduct.

@Test
public void testUpdateAPIProduct() throws APIPersistenceException, RegistryException, APIManagementException {
    PublisherAPIProduct publisherAPI = new PublisherAPIProduct();
    publisherAPI.setDescription("Modified description");
    APIProduct api = APIProductMapper.INSTANCE.toApiProduct(publisherAPI);
    Registry registry = Mockito.mock(UserRegistry.class);
    Resource resource = new ResourceImpl();
    Mockito.when(registry.get(anyString())).thenReturn(resource);
    Tag[] tags = new Tag[0];
    Mockito.when(registry.getTags(anyString())).thenReturn(tags);
    GenericArtifact existArtifact = PersistenceHelper.getSampleAPIProductArtifact();
    String apiUUID = existArtifact.getId();
    PowerMockito.mockStatic(RegistryPersistenceUtil.class);
    GenericArtifactManager manager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY)).thenReturn(manager);
    Mockito.when(manager.getGenericArtifact(apiUUID)).thenReturn(existArtifact);
    Mockito.doNothing().when(manager).updateGenericArtifact(existArtifact);
    PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY)).thenReturn(manager);
    GenericArtifact updatedArtifact = PersistenceHelper.getSampleAPIProductArtifact();
    updatedArtifact.setAttribute(APIConstants.API_OVERVIEW_DESCRIPTION, api.getDescription());
    PowerMockito.when(RegistryPersistenceUtil.createAPIProductArtifactContent(any(GenericArtifact.class), any(APIProduct.class))).thenReturn(updatedArtifact);
    Organization org = new Organization(SUPER_TENANT_DOMAIN);
    APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, existArtifact);
    PublisherAPIProduct updatedAPI = apiPersistenceInstance.updateAPIProduct(org, publisherAPI);
    Assert.assertEquals("Updated API description does not match", "Modified description", updatedAPI.getDescription());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Matchers.anyString(org.mockito.Matchers.anyString) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) Tag(org.wso2.carbon.registry.core.Tag) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 87 with APIProduct

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

the class RegistryPersistenceUtilTestCase method testAPIProductGet.

@Test
public void testAPIProductGet() throws GovernanceException, APIManagementException {
    GenericArtifact artifact = PersistenceHelper.getSampleAPIProductArtifact();
    APIProduct apiProduct = RegistryPersistenceUtil.getAPIProduct(artifact, registry);
    Assert.assertEquals("Attibute overview_type does not match", artifact.getAttribute("overview_type"), apiProduct.getType());
    Assert.assertEquals("API product id does not match", artifact.getId(), apiProduct.getUuid());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 88 with APIProduct

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

the class APIUtils method extractEndpointURLs.

/**
 * Extracts the API environment details with access url for each endpoint
 *
 * @param apiProduct   API object
 * @param tenantDomain Tenant domain of the API
 * @return the API environment details
 * @throws APIManagementException error while extracting the information
 */
public static List<APIEndpointURLsDTO> extractEndpointURLs(APIProduct apiProduct, String tenantDomain) throws APIManagementException {
    List<APIEndpointURLsDTO> apiEndpointsList = new ArrayList<>();
    String organization = apiProduct.getOrganization();
    Map<String, Environment> environments = APIUtil.getEnvironments(organization);
    Set<String> environmentsPublishedByAPI = new HashSet<>(apiProduct.getEnvironments());
    environmentsPublishedByAPI.remove("none");
    Set<String> apiTransports = new HashSet<>(Arrays.asList(apiProduct.getTransports().split(",")));
    APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
    for (String environmentName : environmentsPublishedByAPI) {
        Environment environment = environments.get(environmentName);
        if (environment != null) {
            APIURLsDTO apiURLsDTO = new APIURLsDTO();
            String[] gwEndpoints = null;
            gwEndpoints = environment.getApiGatewayEndpoint().split(",");
            Map<String, String> domains = new HashMap<>();
            if (tenantDomain != null) {
                domains = apiConsumer.getTenantDomainMappings(tenantDomain, APIConstants.API_DOMAIN_MAPPINGS_GATEWAY);
            }
            String customGatewayUrl = null;
            if (domains != null) {
                customGatewayUrl = domains.get(APIConstants.CUSTOM_URL);
            }
            for (String gwEndpoint : gwEndpoints) {
                StringBuilder endpointBuilder = new StringBuilder(gwEndpoint);
                if (customGatewayUrl != null) {
                    int index = endpointBuilder.indexOf("//");
                    endpointBuilder.replace(index + 2, endpointBuilder.length(), customGatewayUrl);
                    endpointBuilder.append(apiProduct.getContext().replace("/t/" + tenantDomain, ""));
                } else {
                    endpointBuilder.append(apiProduct.getContext());
                }
                if (gwEndpoint.contains("http:") && apiTransports.contains("http")) {
                    apiURLsDTO.setHttp(endpointBuilder.toString());
                } else if (gwEndpoint.contains("https:") && apiTransports.contains("https")) {
                    apiURLsDTO.setHttps(endpointBuilder.toString());
                }
            }
            APIEndpointURLsDTO apiEndpointURLsDTO = new APIEndpointURLsDTO();
            apiEndpointURLsDTO.setUrLs(apiURLsDTO);
            apiEndpointURLsDTO.setEnvironmentName(environment.getName());
            apiEndpointURLsDTO.setEnvironmentType(environment.getType());
            apiEndpointsList.add(apiEndpointURLsDTO);
        }
    }
    return apiEndpointsList;
}
Also used : APIURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIURLsDTO) HashMap(java.util.HashMap) APIEndpointURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO) ArrayList(java.util.ArrayList) Environment(org.wso2.carbon.apimgt.api.model.Environment) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) HashSet(java.util.HashSet)

Example 89 with APIProduct

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

the class ImportUtils method isTierAvailable.

/**
 * Check whether a target Tier is available to subscribe
 *
 * @param targetTierName Target Tier Name
 * @param apiTypeWrapper - {@link ApiTypeWrapper}
 * @return true, if the target tier is available
 */
private static boolean isTierAvailable(String targetTierName, ApiTypeWrapper apiTypeWrapper) {
    Set<Tier> availableTiers;
    API api = null;
    APIProduct apiProduct = null;
    if (!apiTypeWrapper.isAPIProduct()) {
        api = apiTypeWrapper.getApi();
        availableTiers = api.getAvailableTiers();
    } else {
        apiProduct = apiTypeWrapper.getApiProduct();
        availableTiers = apiProduct.getAvailableTiers();
    }
    for (Tier tier : availableTiers) {
        if (StringUtils.equals(tier.getName(), targetTierName)) {
            return true;
        }
    }
    if (!apiTypeWrapper.isAPIProduct()) {
        log.error("Tier:" + targetTierName + " is not available for API " + api.getId().getApiName() + "-" + api.getId().getVersion());
    } else {
        log.error("Tier:" + targetTierName + " is not available for API Product " + apiProduct.getId().getName() + "-" + apiProduct.getId().getVersion());
    }
    return false;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) Tier(org.wso2.carbon.apimgt.api.model.Tier) API(org.wso2.carbon.apimgt.api.model.API) ExportedSubscribedAPI(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedSubscribedAPI)

Example 90 with APIProduct

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

the class OAS2Parser method getOASDefinitionForStore.

/**
 * Update OAS definition for store
 *
 * @param product        APIProduct
 * @param oasDefinition  OAS definition
 * @param hostsWithSchemes host addresses with protocol mapping
 * @return OAS definition
 * @throws APIManagementException throws if an error occurred
 */
@Override
public String getOASDefinitionForStore(APIProduct product, String oasDefinition, Map<String, String> hostsWithSchemes) throws APIManagementException {
    Swagger swagger = getSwagger(oasDefinition);
    updateOperations(swagger);
    updateEndpoints(product, hostsWithSchemes, swagger);
    return updateSwaggerSecurityDefinitionForStore(swagger, new SwaggerData(product), hostsWithSchemes);
}
Also used : SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) Swagger(io.swagger.models.Swagger)

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