Search in sources :

Example 1 with ApplicationDTO

use of org.wso2.micro.gateway.tests.common.model.ApplicationDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsPost.

/**
 * Adds a new application
 *
 * @param body        Application details to be added
 * @param request     msf4j request object
 * @return 201 Response if successful
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsPost(ApplicationDTO body, Request request) throws NotFoundException {
    URI location = null;
    ApplicationDTO createdApplicationDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiConsumer = RestApiUtil.getConsumer(username);
        Application application = ApplicationMappingUtil.fromDTOtoApplication(body, username);
        ApplicationCreationResponse applicationResponse = apiConsumer.addApplication(application);
        String applicationUUID = applicationResponse.getApplicationUUID();
        Application createdApplication = apiConsumer.getApplication(applicationUUID, username);
        createdApplicationDTO = ApplicationMappingUtil.fromApplicationToDTO(createdApplication);
        location = new URI(RestApiConstants.RESOURCE_PATH_APPLICATIONS + "/" + createdApplicationDTO.getApplicationId());
        // be in either pending or approved state) send back the workflow response
        if (ApplicationStatus.APPLICATION_ONHOLD.equals(createdApplication.getStatus())) {
            WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(applicationResponse.getWorkflowResponse());
            return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding new application : " + body.getName();
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, body.getName());
        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 application : " + body.getName();
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, body.getName());
        ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
    // .build()
    return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(createdApplicationDTO).build();
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 2 with ApplicationDTO

use of org.wso2.micro.gateway.tests.common.model.ApplicationDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdPut.

/**
 * Updates an existing application
 *
 * @param applicationId     application Id
 * @param body              Application details to be updated
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return Updated application details as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsApplicationIdPut(String applicationId, ApplicationDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    ApplicationDTO updatedApplicationDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiConsumer = RestApiUtil.getConsumer(username);
        String existingFingerprint = applicationsApplicationIdGetFingerprint(applicationId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        Application application = ApplicationMappingUtil.fromDTOtoApplication(body, username);
        if (!ApplicationStatus.APPLICATION_APPROVED.equals(application.getStatus())) {
            String errorMessage = "Application " + applicationId + " is not active";
            ExceptionCodes exceptionCode = ExceptionCodes.APPLICATION_INACTIVE;
            APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
            Map<String, String> paramList = new HashMap<>();
            paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        WorkflowResponse updateResponse = apiConsumer.updateApplication(applicationId, application);
        if (WorkflowStatus.REJECTED == updateResponse.getWorkflowStatus()) {
            String errorMessage = "Update request for application " + applicationId + " is rejected";
            ExceptionCodes exceptionCode = ExceptionCodes.WORKFLOW_REJCECTED;
            APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
            Map<String, String> paramList = new HashMap<>();
            paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        // retrieves the updated application and send as the response
        Application updatedApplication = apiConsumer.getApplication(applicationId, username);
        updatedApplicationDTO = ApplicationMappingUtil.fromApplicationToDTO(updatedApplication);
        String newFingerprint = applicationsApplicationIdGetFingerprint(applicationId, null, null, request);
        // be in either pending or approved state) send back the workflow response
        if (ApplicationStatus.APPLICATION_ONHOLD.equals(updatedApplication.getStatus())) {
            WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(updateResponse);
            URI location = new URI(RestApiConstants.RESOURCE_PATH_APPLICATIONS + "/" + applicationId);
            return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
        }
        return Response.ok().entity(updatedApplicationDTO).header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating application: " + body.getName();
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, body.getName());
        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 application : " + body.getName();
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, body.getName());
        ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) ExceptionCodes(org.wso2.carbon.apimgt.core.exception.ExceptionCodes) Application(org.wso2.carbon.apimgt.core.models.Application) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 3 with ApplicationDTO

use of org.wso2.micro.gateway.tests.common.model.ApplicationDTO in project carbon-apimgt by wso2.

the class TestMappingUtilTestCase method testApplicationToApplicationDTOMapping.

@Test(description = "Application to Application DTO mapping")
void testApplicationToApplicationDTOMapping() {
    Application application = SampleTestObjectCreator.createDefaultApplication();
    ApplicationDTO applicationDTO = MappingUtil.toApplicationDto(application);
    assertEquals(application.getId(), applicationDTO.getApplicationId());
    assertEquals(application.getDescription(), applicationDTO.getDescription());
    assertEquals(application.getName(), applicationDTO.getName());
    assertEquals(application.getCreatedUser(), applicationDTO.getSubscriber());
    assertEquals(application.getPolicy().getPolicyName(), applicationDTO.getThrottlingTier());
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.ApplicationDTO) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Example 4 with ApplicationDTO

use of org.wso2.micro.gateway.tests.common.model.ApplicationDTO in project carbon-apimgt by wso2.

the class MappingUtilTestCase method convertToApplicationDtoListTest.

@Test
public void convertToApplicationDtoListTest() {
    List<Application> applicationList = new ArrayList<>();
    applicationList.add(SampleTestObjectCreator.createRandomApplication());
    applicationList.add(SampleTestObjectCreator.createRandomApplication());
    applicationList.add(SampleTestObjectCreator.createRandomApplication());
    List<ApplicationDTO> applicationDTOList = MappingUtil.convertToApplicationDtoList(applicationList);
    Assert.assertEquals(applicationList.size(), applicationDTOList.size());
    for (int i = 0; i < applicationList.size(); i++) {
        Assert.assertEquals(applicationList.get(i).getName(), applicationDTOList.get(i).getName());
        Assert.assertEquals(applicationList.get(i).getId(), applicationDTOList.get(i).getApplicationId());
        Assert.assertEquals(applicationList.get(i).getPolicy().getUuid(), applicationDTOList.get(i).getThrottlingTier());
        Assert.assertEquals(applicationList.get(i).getCreatedUser(), applicationDTOList.get(i).getSubscriber());
    }
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ApplicationDTO) ArrayList(java.util.ArrayList) Application(org.wso2.carbon.apimgt.core.models.Application) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Test(org.testng.annotations.Test)

Example 5 with ApplicationDTO

use of org.wso2.micro.gateway.tests.common.model.ApplicationDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImplTestCase method testApplicationsPost.

@Test
public void testApplicationsPost() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    String applicationId = UUID.randomUUID().toString();
    String accessToken = UUID.randomUUID().toString();
    String clientID = UUID.randomUUID().toString();
    String clientSecret = UUID.randomUUID().toString();
    ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    Application application = getSampleApplication(applicationId);
    ApplicationTokenDTO applicationTokenDTO = new ApplicationTokenDTO();
    applicationTokenDTO.setAccessToken(accessToken);
    applicationTokenDTO.setTokenScopes("SCOPE1");
    applicationTokenDTO.setValidityTime((long) 100000);
    List<String> grantTypes = new ArrayList<>();
    grantTypes.add("password");
    grantTypes.add("jwt");
    ApplicationKeysDTO applicationKeysDTO = new ApplicationKeysDTO();
    applicationKeysDTO.setConsumerKey(clientID);
    applicationKeysDTO.setConsumerSecret(clientSecret);
    applicationKeysDTO.setKeyType(ApplicationKeysDTO.KeyTypeEnum.PRODUCTION);
    applicationKeysDTO.setCallbackUrl(null);
    applicationKeysDTO.setSupportedGrantTypes(grantTypes);
    List<ApplicationKeysDTO> applicationKeysDTOList = new ArrayList<>();
    applicationKeysDTOList.add(applicationKeysDTO);
    ApplicationDTO applicationDTO = new ApplicationDTO();
    applicationDTO.setApplicationId(applicationId);
    applicationDTO.setDescription("sample application");
    applicationDTO.setName("app1");
    applicationDTO.setSubscriber("subscriber");
    applicationDTO.setPermission("permission");
    applicationDTO.setLifeCycleStatus("APPROVED");
    applicationDTO.setThrottlingTier("UNLIMITED");
    applicationDTO.setToken(applicationTokenDTO);
    applicationDTO.setKeys(applicationKeysDTOList);
    Mockito.doThrow(new APIManagementException("Error Occurred", ExceptionCodes.INTERNAL_ERROR)).when(apiStore).addApplication(application);
    Response response = applicationsApiService.applicationsPost(applicationDTO, request);
    Assert.assertEquals(500, response.getStatus());
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) ApplicationTokenDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) Response(javax.ws.rs.core.Response) ApplicationKeysDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

BeforeClass (org.testng.annotations.BeforeClass)17 ApplicationDTO (org.wso2.micro.gateway.tests.common.model.ApplicationDTO)17 Application (org.wso2.carbon.apimgt.core.models.Application)11 ApplicationDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO)11 ArrayList (java.util.ArrayList)10 JSONObject (org.json.JSONObject)10 HashMap (java.util.HashMap)9 Application (org.wso2.carbon.apimgt.api.model.Application)8 API (org.wso2.micro.gateway.tests.common.model.API)8 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)7 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)7 KeyValidationInfo (org.wso2.micro.gateway.tests.common.KeyValidationInfo)7 MockAPIPublisher (org.wso2.micro.gateway.tests.common.MockAPIPublisher)7 Test (org.junit.Test)6 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)6 ApplicationKeysDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO)6 ApplicationDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)5 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)5