Search in sources :

Example 16 with User

use of org.wso2.broker.core.security.authentication.user.User in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAPIsByStatusStore.

@Test
public void testGetAPIsByStatusStore() throws Exception {
    // Add few APIs with different attributes.
    List<String> apiIDList = createAPIsAndGetIDsOfAddedAPIs();
    Set<String> userRoles = new HashSet<>();
    List<String> statuses = new ArrayList<>();
    statuses.add(APIStatus.PUBLISHED.getStatus());
    statuses.add(APIStatus.PROTOTYPED.getStatus());
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    String[] expectedAPINames;
    List<API> apiResults;
    // Asserting results for different search queries
    // Role based API retrieval for a user with "admin" role
    userRoles.add(ADMIN);
    apiResults = apiDAO.getAPIsByStatus(userRoles, statuses, new ArrayList<>());
    List<String> resultAPINameList = new ArrayList<>();
    for (API api : apiResults) {
        resultAPINameList.add(api.getName());
    }
    expectedAPINames = new String[] { "PublicAPI", "AdminManagerAPI" };
    Assert.assertTrue(resultAPINameList.containsAll(Arrays.asList(expectedAPINames)) && Arrays.asList(expectedAPINames).containsAll(resultAPINameList));
    userRoles.clear();
    apiResults.clear();
    resultAPINameList.clear();
    // Role based API retrieval for a user with "manager" role
    userRoles.add(MANAGER_ROLE);
    apiResults = apiDAO.getAPIsByStatus(userRoles, statuses, new ArrayList<>());
    for (API api : apiResults) {
        resultAPINameList.add(api.getName());
    }
    expectedAPINames = new String[] { "PublicAPI", "ManagerOnlyAPI", "AdminManagerAPI", "NonAdminAPI" };
    Assert.assertTrue(resultAPINameList.containsAll(Arrays.asList(expectedAPINames)) && Arrays.asList(expectedAPINames).containsAll(resultAPINameList));
    userRoles.clear();
    apiResults.clear();
    resultAPINameList.clear();
    // Role based API retrieval for a user with "manager", "employee" and "customer" roles
    userRoles.add(MANAGER_ROLE);
    userRoles.add(EMPLOYEE_ROLE);
    userRoles.add(CUSTOMER_ROLE);
    apiResults = apiDAO.getAPIsByStatus(userRoles, statuses, new ArrayList<>());
    for (API api : apiResults) {
        resultAPINameList.add(api.getName());
    }
    expectedAPINames = new String[] { "PublicAPI", "ManagerOnlyAPI", "AdminManagerAPI", "EmployeeAPI", "NonAdminAPI" };
    Assert.assertTrue(resultAPINameList.containsAll(Arrays.asList(expectedAPINames)) && Arrays.asList(expectedAPINames).containsAll(resultAPINameList));
    userRoles.clear();
    apiResults.clear();
    resultAPINameList.clear();
}
Also used : ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 17 with User

use of org.wso2.broker.core.security.authentication.user.User in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAPIsWhenUserIsProvider.

@Test(description = "Tests getting the APIs when the user is the provider of the API")
public void testGetAPIsWhenUserIsProvider() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Set<String> rolesOfUser = new HashSet<>();
    // The ID here is the group ID of the provider of the API. This ID is not assigned permissions for the API
    rolesOfUser.add(ALTERNATIVE_USER_ROLE_ID);
    // But this user is the provider of the API
    List<API> apiList = apiDAO.getAPIs(rolesOfUser, ADMIN);
    Assert.assertTrue(apiList.isEmpty());
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
    API api1 = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api1);
    apiList = apiDAO.getAPIs(rolesOfUser, ADMIN);
    List<API> expectedAPIs = new ArrayList<>();
    expectedAPIs.add(SampleTestObjectCreator.copyAPISummary(api1));
    // The provider will have all permissions for the API by default
    Assert.assertTrue(apiList.size() == 1);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, expectedAPIs, new APIComparator()), TestUtil.printDiff(apiList, expectedAPIs));
}
Also used : ArrayList(java.util.ArrayList) APIComparator(org.wso2.carbon.apimgt.core.util.APIComparator) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 18 with User

use of org.wso2.broker.core.security.authentication.user.User in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddApiDefinitionErrorGettingSwaggerResource.

@Test(description = "Response not 200 when getting swagger resource from url when adding api from swagger resource", expectedExceptions = APIManagementException.class)
public void testAddApiDefinitionErrorGettingSwaggerResource() throws APIManagementException, LifecycleException, IOException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    HttpURLConnection httpURLConnection = Mockito.mock(HttpURLConnection.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    APIGateway gateway = Mockito.mock(APIGateway.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
    Mockito.when(httpURLConnection.getResponseCode()).thenReturn(400);
    apiPublisher.addApiFromDefinition(httpURLConnection);
    Mockito.verify(apiLifecycleManager, Mockito.times(0)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
Also used : APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) HttpURLConnection(java.net.HttpURLConnection) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 19 with User

use of org.wso2.broker.core.security.authentication.user.User in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testSearchAPIsException.

@Test(description = "Exception when searching APIs")
public void testSearchAPIsException() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(ALTERNATIVE_USER, identityProvider, apiDAO);
    // Error path
    // APIMgtDAOException
    Mockito.when(apiDAO.searchAPIs(new HashSet<>(), ALTERNATIVE_USER, QUERY_STRING, 1, 2)).thenThrow(APIMgtDAOException.class);
    try {
        apiPublisher.searchAPIs(2, 1, QUERY_STRING);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while Searching the API with query " + QUERY_STRING);
    }
    // Error path
    // IdentityProviderException
    Mockito.when(identityProvider.getIdOfUser(ALTERNATIVE_USER)).thenThrow(IdentityProviderException.class);
    try {
        apiPublisher.searchAPIs(2, 1, QUERY_STRING);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while calling SCIM endpoint to retrieve user " + ALTERNATIVE_USER + "'s information");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 20 with User

use of org.wso2.broker.core.security.authentication.user.User in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddApiWithRestrictedVisibility.

@Test(description = "Test add api with restricted visibility")
public void testAddApiWithRestrictedVisibility() throws APIManagementException, LifecycleException {
    Set<String> visibleRoles = new HashSet<>();
    visibleRoles.add(ADMIN_ROLE);
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(SampleTestObjectCreator.getMockEndpointMap()).visibility(API.Visibility.RESTRICTED).visibleRoles(visibleRoles);
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    APIGateway gateway = Mockito.mock(APIGateway.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.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);
    String endpointId = apiBuilder.getEndpoint().get("production").getId();
    Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
    Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
    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 : APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) 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) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

Test (org.junit.Test)225 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)223 Response (javax.ws.rs.core.Response)215 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)130 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)114 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)112 Request (org.wso2.msf4j.Request)105 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)98 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)88 Test (org.testng.annotations.Test)74 API (org.wso2.carbon.apimgt.core.models.API)73 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)65 ArrayList (java.util.ArrayList)55 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)44 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)41 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)37 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)35 HashMap (java.util.HashMap)34 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)28 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)26