Search in sources :

Example 66 with Add

use of org.wso2.siddhi.query.api.expression.math.Add in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddApiSandboxEndpoint.

@Test(description = "Test add api with sandbox endpoint")
public void testAddApiSandboxEndpoint() throws APIManagementException, LifecycleException {
    Map<String, Endpoint> endpointMap = new HashMap<>();
    Map<String, Endpoint> resourceEndpointMap = new HashMap<>();
    Map<String, UriTemplate> uriTemplateMap = new HashMap();
    UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
    uriTemplateBuilder.endpoint(resourceEndpointMap);
    uriTemplateBuilder.templateId("getApisApiIdGet");
    uriTemplateBuilder.uriTemplate("/apis/");
    uriTemplateBuilder.authType(APIMgtConstants.AUTH_APPLICATION_LEVEL_TOKEN);
    uriTemplateBuilder.policy(APIUtils.getDefaultAPIPolicy());
    uriTemplateBuilder.httpVerb("GET");
    uriTemplateMap.put("getApisApiIdGet", uriTemplateBuilder.build());
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id("").endpoint(endpointMap).uriTemplates(uriTemplateMap);
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    String endpointId = String.valueOf(apiBuilder.getEndpoint().get("sandbox"));
    Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
    Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)).thenReturn(new SubscriptionPolicy(GOLD_TIER));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER));
    APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
    apiPublisher.addAPI(apiBuilder);
    Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
    Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
Also used : HashMap(java.util.HashMap) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 67 with Add

use of org.wso2.siddhi.query.api.expression.math.Add in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingCustomPost.

/**
 * Add a Custom Policy.
 *
 * @param body        DTO of new policy to be created
 * @param request     msf4j request object
 * @return Created policy along with the location of it with Location header
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingCustomPost(CustomRuleDTO body, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Custom Policy POST request " + body);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        CustomPolicy customPolicy = CustomPolicyMappingUtil.fromCustomPolicyDTOToModel(body);
        String uuid = apiMgtAdminService.addCustomRule(customPolicy);
        return Response.status(Response.Status.CREATED).entity(CustomPolicyMappingUtil.fromCustomPolicyToDTO(apiMgtAdminService.getCustomRuleByUUID(uuid))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding custom policy, policy name: " + body.getPolicyName();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 68 with Add

use of org.wso2.siddhi.query.api.expression.math.Add in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingSubscriptionPost.

/**
 * Add subscription level policy
 *
 * @param body              DTO object including the Policy meta information
 * @param request           msf4j request object
 * @return Response object
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingSubscriptionPost(SubscriptionThrottlePolicyDTO body, Request request) throws NotFoundException {
    APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.subscription;
    if (log.isDebugEnabled()) {
        log.info("Received Subscription Policy POST request " + body + " with tierLevel = " + tierLevel);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
        String policyId = apiMgtAdminService.addSubscriptionPolicy(subscriptionPolicy);
        return Response.status(Response.Status.CREATED).entity(SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(apiMgtAdminService.getSubscriptionPolicyByUuid(policyId))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding Subscription Policy. policy name: " + body.getPolicyName();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 69 with Add

use of org.wso2.siddhi.query.api.expression.math.Add in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdUserRatingPut.

/**
 * Add or update raating to an API
 *
 * @param apiId   APIID
 * @param body    RatingDTO object
 * @param request msf4j request
 * @return 201 response if successful
 * @throws NotFoundException if failed to find method implementation
 */
@Override
public Response apisApiIdUserRatingPut(String apiId, RatingDTO body, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    String ratingId;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Rating ratingFromPayload = RatingMappingUtil.fromDTOToRating(username, apiId, body);
        Rating existingRating = apiStore.getRatingForApiFromUser(apiId, username);
        if (existingRating != null) {
            String existingRatingUUID = existingRating.getUuid();
            apiStore.updateRating(apiId, existingRatingUUID, ratingFromPayload);
            Rating updatedRating = apiStore.getRatingByUUID(apiId, existingRatingUUID);
            RatingDTO updatedRatingDTO = RatingMappingUtil.fromRatingToDTO(updatedRating);
            return Response.ok().entity(updatedRatingDTO).build();
        } else {
            ratingId = apiStore.addRating(apiId, ratingFromPayload);
            Rating createdRating = apiStore.getRatingByUUID(apiId, ratingId);
            RatingDTO createdRatingDTO = RatingMappingUtil.fromRatingToDTO(createdRating);
            URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.SUBRESOURCE_PATH_RATINGS + "/" + ratingId);
            return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(createdRatingDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding/updating rating for user " + username + " for given API " + apiId;
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (URISyntaxException e) {
        String errorMessage = "Error while adding location header in response for comment";
        ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Rating(org.wso2.carbon.apimgt.core.models.Rating) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) RatingDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 70 with Add

use of org.wso2.siddhi.query.api.expression.math.Add in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method getDocumentInfoFromExtractedArchive.

/**
 * Retrieves {@link DocumentInfo} instance from the directory containing docs
 *
 * @param documentImportLocation path to the directory containing docs
 * @param apiName                API name
 * @param version                API version
 * @return Set of {@link DocumentInfo} insjtaces
 */
private Set<DocumentInfo> getDocumentInfoFromExtractedArchive(String documentImportLocation, String apiName, String version) {
    Set<DocumentInfo> documents = new HashSet<>();
    File rootDocumentationDirectoryForAPI = new File(documentImportLocation);
    if (!rootDocumentationDirectoryForAPI.isDirectory()) {
        // no Docs!
        log.debug("No documentation found for API name: " + apiName + ", version: " + version);
        return documents;
    }
    File[] documentationDirectories = rootDocumentationDirectoryForAPI.listFiles(File::isDirectory);
    if (documentationDirectories == null) {
        // do docs!
        log.debug("No documents found at " + documentImportLocation);
        return documents;
    }
    for (File docDir : documentationDirectories) {
        // read the 'doc.json'
        String content;
        try {
            content = APIFileUtils.readFileContentAsText(docDir.getPath() + File.separator + DOCUMENTATION_DEFINITION_FILE);
            Gson gson = new GsonBuilder().create();
            documents.add(gson.fromJson(content, DocumentInfo.class));
        // add the doc
        } catch (APIManagementException e) {
            // no need to throw, log and continue
            log.error("Error in importing documentation from file: " + docDir.getPath() + " for API: " + apiName + ", version: " + version);
        }
    }
    return documents;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) File(java.io.File) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) HashSet(java.util.HashSet)

Aggregations

Test (org.testng.annotations.Test)134 API (org.wso2.carbon.apimgt.core.models.API)63 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)60 ArrayList (java.util.ArrayList)57 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)55 HashMap (java.util.HashMap)51 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)44 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)43 Application (org.wso2.carbon.apimgt.core.models.Application)37 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)35 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)33 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)30 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)29 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)26 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)26 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)25 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)23 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)22 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)22 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)22