Search in sources :

Example 56 with User

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);
}
Also used : HashMap(java.util.HashMap) ZonedDateTime(java.time.ZonedDateTime) Event(org.wso2.carbon.apimgt.core.models.Event) Test(org.testng.annotations.Test)

Example 57 with User

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();
    }
}
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 58 with User

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;
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Request(org.wso2.msf4j.Request)

Example 59 with User

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"));
}
Also used : Response(javax.ws.rs.core.Response) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ArrayList(java.util.ArrayList) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 60 with User

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"));
}
Also used : Response(javax.ws.rs.core.Response) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.testng.annotations.Test)420 ArrayList (java.util.ArrayList)322 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)322 HashMap (java.util.HashMap)309 UserStoreException (org.wso2.carbon.user.api.UserStoreException)285 Test (org.junit.Test)272 Response (javax.ws.rs.core.Response)233 SQLException (java.sql.SQLException)166 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)153 PreparedStatement (java.sql.PreparedStatement)151 Connection (java.sql.Connection)148 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)134 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)130 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)114 User (org.wso2.charon3.core.objects.User)114 Map (java.util.Map)112 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)112 ISIntegrationTest (org.wso2.identity.integration.common.utils.ISIntegrationTest)105 Request (org.wso2.msf4j.Request)105 RealmService (org.wso2.carbon.user.core.service.RealmService)99