Search in sources :

Example 1 with Scope

use of org.wso2.carbon.apimgt.core.models.Scope in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method populateScopeMap.

/**
 * Populate Scope Object map from OAuth2SecurityDefinitions.
 *
 * @param oAuth2SecurityDefinitionMap oAuth2SecurityDefinition Map<String, String> to be converted to Scope objects
 * @return Scope object map
 */
private Map<String, Scope> populateScopeMap(Map<String, String> oAuth2SecurityDefinitionMap) {
    Map<String, Scope> scopeMap = new HashMap<>();
    for (Map.Entry<String, String> scopeEntry : oAuth2SecurityDefinitionMap.entrySet()) {
        Scope scope = new Scope();
        scope.setName(scopeEntry.getKey());
        scope.setDescription(scopeEntry.getValue());
        scopeMap.put(scopeEntry.getKey(), scope);
    }
    return scopeMap;
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 2 with Scope

use of org.wso2.carbon.apimgt.core.models.Scope in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddExistingScopeToApi2.

@Test(description = "Add existing Scope to API")
public void testAddExistingScopeToApi2() throws APIManagementException, IOException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    String uuid = api.getId();
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    KeyManager keyManager = Mockito.mock(KeyManager.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, gatewaySourceGenerator, gateway, keyManager);
    String oldSwagger = IOUtils.toString(new FileInputStream("src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "swaggerWithAuthorization" + ".yaml"));
    Scope scope = new Scope("api_delete", "api_delete");
    Mockito.when(apiDAO.getApiSwaggerDefinition(uuid)).thenReturn(oldSwagger);
    Mockito.when(keyManager.registerScope(scope)).thenReturn(false);
    try {
        apiPublisher.addScopeToTheApi(api.getId(), scope);
        Assert.fail();
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Scope already registered");
    }
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) FileInputStream(java.io.FileInputStream) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Test(org.testng.annotations.Test)

Example 3 with Scope

use of org.wso2.carbon.apimgt.core.models.Scope in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUpdateSwagger20DefinitionWithDifferentSwagger2.

@Test(description = "Save swagger definition for API")
public void testUpdateSwagger20DefinitionWithDifferentSwagger2() throws APIManagementException, IOException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    String uuid = api.getId();
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    KeyManager keyManager = Mockito.mock(KeyManager.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, gatewaySourceGenerator, gateway, keyManager);
    Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.DEVELOPER_ROLE_ID)).thenReturn(DEVELOPER_ROLE);
    Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.ADMIN_ROLE_ID)).thenReturn(ADMIN_ROLE);
    String newSwagger = IOUtils.toString(new FileInputStream("src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "swaggerWithAuthorization" + ".yaml"));
    Mockito.when(apiDAO.getApiSwaggerDefinition(uuid)).thenReturn(SampleTestObjectCreator.apiDefinition);
    Mockito.when(keyManager.retrieveScope("apim:api_create")).thenReturn(new Scope("apim:api_create", "Create " + "API"));
    apiPublisher.saveSwagger20Definition(uuid, newSwagger);
    Mockito.verify(apiDAO, Mockito.times(1)).updateApiDefinition(uuid, newSwagger, USER);
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) API(org.wso2.carbon.apimgt.core.models.API) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) FileInputStream(java.io.FileInputStream) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Test(org.testng.annotations.Test)

Example 4 with Scope

use of org.wso2.carbon.apimgt.core.models.Scope in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testGetScopeInformationForNonExistingScope.

@Test
public void testGetScopeInformationForNonExistingScope() throws APIManagementException, IOException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    KeyManager keyManager = Mockito.mock(KeyManager.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, keyManager);
    String newSwagger = IOUtils.toString(new FileInputStream("src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "swaggerWithAuthorization" + ".yaml"));
    Mockito.when(apiDAO.getApiSwaggerDefinition("abcd")).thenReturn(newSwagger);
    Scope scope = new Scope("apim:api_create", "apim:api_delete");
    Mockito.when(keyManager.retrieveScope("apim:api_create")).thenReturn(scope);
    try {
        apiPublisher.getScopeInformationOfApi("abcd", "apim:api_delete");
        Assert.fail();
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getErrorHandler().getErrorCode(), 900981);
    }
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 5 with Scope

use of org.wso2.carbon.apimgt.core.models.Scope in project carbon-apimgt by wso2.

the class MappingUtil method scopeDto.

/**
 * used to convert {@link Scope} to {@link ScopeDTO}
 * @param scope scope Object
 * @param scopeBindingType type of bindings
 * @return ScopeDTO object
 */
public static ScopeDTO scopeDto(Scope scope, String scopeBindingType) {
    ScopeDTO scopeDTO = new ScopeDTO();
    scopeDTO.setName(scope.getName());
    scopeDTO.setDescription(scope.getDescription());
    Scope_bindingsDTO scopeBindingsDTO = new Scope_bindingsDTO();
    scopeBindingsDTO.setType(scopeBindingType);
    if (scope.getBindings() != null) {
        scopeBindingsDTO.setValues(scope.getBindings());
    } else {
        scopeBindingsDTO.setValues(Collections.emptyList());
    }
    scopeDTO.setBindings(scopeBindingsDTO);
    return scopeDTO;
}
Also used : Scope_bindingsDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.Scope_bindingsDTO) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.ScopeDTO)

Aggregations

Scope (org.wso2.carbon.apimgt.core.models.Scope)41 Test (org.testng.annotations.Test)23 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)19 HashMap (java.util.HashMap)17 ScopeInfo (org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo)15 FileInputStream (java.io.FileInputStream)14 API (org.wso2.carbon.apimgt.core.models.API)14 KeyManager (org.wso2.carbon.apimgt.core.api.KeyManager)13 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)13 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)12 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)12 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)12 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)11 Response (javax.ws.rs.core.Response)8 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)8 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)8 Gson (com.google.gson.Gson)7 KeyManagementException (org.wso2.carbon.apimgt.core.exception.KeyManagementException)7