use of org.wso2.carbon.apimgt.keymgt.model.entity.Application 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();
}
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.
the class TestMappingUtilTestCase method testSubscriptionListToSubscriptionListDTOMapping.
@Test(description = "Subscription list to Subscription DTO list mapping")
void testSubscriptionListToSubscriptionListDTOMapping() {
Policy subscriptionPolicy1 = SampleTestObjectCreator.goldSubscriptionPolicy;
API api1 = SampleTestObjectCreator.createDefaultAPI().name("newName1").build();
Application application1 = SampleTestObjectCreator.createDefaultApplication();
application1.setName("newNameApp1");
String uuid1 = UUID.randomUUID().toString();
Subscription subscription1 = new Subscription(uuid1, application1, api1, subscriptionPolicy1);
subscription1.setStatus(APIMgtConstants.SubscriptionStatus.ACTIVE);
Policy subscriptionPolicy2 = SampleTestObjectCreator.silverSubscriptionPolicy;
API api2 = SampleTestObjectCreator.createDefaultAPI().name("newName2").build();
Application application2 = SampleTestObjectCreator.createDefaultApplication();
application1.setName("newNameApp2");
String uuid2 = UUID.randomUUID().toString();
Subscription subscription2 = new Subscription(uuid2, application2, api2, subscriptionPolicy2);
subscription2.setStatus(APIMgtConstants.SubscriptionStatus.ACTIVE);
List<Subscription> subscriptions = new ArrayList<>();
subscriptions.add(subscription1);
subscriptions.add(subscription2);
SubscriptionListDTO subscriptionListDTO = MappingUtil.fromSubscriptionListToDTO(subscriptions, 10, 0);
assertEquals((Integer) subscriptions.size(), subscriptionListDTO.getCount());
assertEquals(subscription1.getId(), subscriptionListDTO.getList().get(0).getSubscriptionId());
assertEquals(subscription1.getStatus().name(), subscriptionListDTO.getList().get(0).getSubscriptionStatus().name());
assertEquals(subscription1.getApplication().getId(), subscriptionListDTO.getList().get(0).getApplicationInfo().getApplicationId());
assertEquals(subscription1.getPolicy().getPolicyName(), subscriptionListDTO.getList().get(0).getPolicy());
assertEquals(subscription2.getId(), subscriptionListDTO.getList().get(1).getSubscriptionId());
assertEquals(subscription2.getStatus().name(), subscriptionListDTO.getList().get(1).getSubscriptionStatus().name());
assertEquals(subscription2.getApplication().getId(), subscriptionListDTO.getList().get(1).getApplicationInfo().getApplicationId());
assertEquals(subscription2.getPolicy().getPolicyName(), subscriptionListDTO.getList().get(1).getPolicy());
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.Application 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());
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.
the class EndpointsApiServiceImplTestCase method testEndpointsEndpointIdGetException.
@Test
public void testEndpointsEndpointIdGetException() throws Exception {
printTestMethodName();
EndpointsApiServiceImpl endpointsApiService = new EndpointsApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
Endpoint endpoint = SampleTestObjectCreator.createMockEndpoint();
String endpointId = endpoint.getId();
Mockito.doThrow(new APIManagementException("Error occurred", ExceptionCodes.APPLICATION_INACTIVE)).when(apiPublisher).getEndpoint(endpointId);
Response response = endpointsApiService.endpointsEndpointIdGet(endpointId, null, null, getRequest());
assertEquals(response.getStatus(), 400);
assertTrue(response.getEntity().toString().contains("Application is not active"));
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultApplication.
public static Application createDefaultApplication() {
// created by admin
Application application = new Application(TEST_APP_1, ADMIN);
application.setId(UUID.randomUUID().toString());
application.setDescription("This is a test application");
application.setStatus(APIMgtConstants.ApplicationStatus.APPLICATION_CREATED);
application.setPolicy(fiftyPerMinApplicationPolicy);
application.setCreatedTime(LocalDateTime.now());
application.setUpdatedUser(ADMIN);
application.setUpdatedTime(LocalDateTime.now());
return application;
}
Aggregations