use of org.wso2.charon.core.objects.User in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testObserverEventListener.
@Test(description = "Event Observers for event listening")
public void testObserverEventListener() throws APIManagementException {
EventLogger observer = Mockito.mock(EventLogger.class);
APIPublisherImpl apiPub = getApiPublisherImpl();
apiPub.registerObserver(observer);
Event event = Event.APP_CREATION;
String username = USER;
Map<String, String> metaData = new HashMap<>();
ZonedDateTime eventTime = ZonedDateTime.now(ZoneOffset.UTC);
apiPub.notifyObservers(event, username, eventTime, metaData);
Mockito.verify(observer, Mockito.times(1)).captureEvent(event, username, eventTime, metaData);
}
use of org.wso2.charon.core.objects.User 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();
}
}
use of org.wso2.charon.core.objects.User in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method getRequest.
// Sample request to be used by tests
private Request getRequest() throws Exception {
HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
Mockito.when(carbonMessage.getProperty("LOGGED_IN_USER")).thenReturn(USER);
Request request = new Request(carbonMessage);
return request;
}
use of org.wso2.charon.core.objects.User in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method testSubscriptionsUnblockSubscriptionPostException.
@Test
public void testSubscriptionsUnblockSubscriptionPostException() throws Exception {
printTestMethodName();
SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
List<Subscription> subscriptions = new ArrayList<>();
String sub1 = UUID.randomUUID().toString();
String sub2 = UUID.randomUUID().toString();
subscriptions.add(SampleTestObjectCreator.createSubscription(sub1));
subscriptions.add(SampleTestObjectCreator.createSubscription(sub2));
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String apiId = UUID.randomUUID().toString();
Mockito.doThrow(new APIManagementException("Error occurred", ExceptionCodes.SUBSCRIPTION_STATE_INVALID)).when(apiPublisher).getSubscriptionsByAPI(apiId);
Response response = subscriptionsApiService.subscriptionsGet(apiId, 10, 0, null, getRequest());
assertEquals(response.getStatus(), 400);
assertTrue(response.getEntity().toString().contains("Invalid state change for subscription"));
}
use of org.wso2.charon.core.objects.User in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method testSubscriptionsBlockSubscriptionPostNotExist.
@Test
public void testSubscriptionsBlockSubscriptionPostNotExist() throws Exception {
printTestMethodName();
SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String subscriptionId = UUID.randomUUID().toString();
Mockito.doReturn(null).doThrow(new IllegalArgumentException()).when(apiPublisher).getSubscriptionByUUID(subscriptionId);
Response response = subscriptionsApiService.subscriptionsBlockSubscriptionPost(subscriptionId, null, null, null, getRequest());
assertEquals(response.getStatus(), 404);
assertTrue(response.getEntity().toString().contains("Subscription not found"));
}
Aggregations