use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddScopeToApi.
@Test(description = "Add Scope to API")
public void testAddScopeToApi() 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 deleteScope = new Scope("api_delete", "apim:api_delete");
Mockito.when(keyManager.registerScope(deleteScope)).thenReturn(true);
Mockito.when(keyManager.retrieveScope(deleteScope.getName())).thenReturn(deleteScope);
Mockito.when(keyManager.retrieveScope("apim:api_create")).thenReturn(new Scope("apim:api_create", "Create " + "API"));
Mockito.when(apiDAO.getApiSwaggerDefinition(uuid)).thenReturn(oldSwagger);
apiPublisher.addScopeToTheApi(api.getId(), deleteScope);
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testDeleteScopeOfNonExistingScope.
@Test(description = "delete existing Scope to API")
public void testDeleteScopeOfNonExistingScope() 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("apim:api_delete", "apim:api_delete");
Mockito.when(apiDAO.getApiSwaggerDefinition(uuid)).thenReturn(oldSwagger);
try {
apiPublisher.deleteScopeFromApi(api.getId(), scope.getName());
Assert.fail();
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Scope couldn't found by name: "));
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testSaveSwagger20DefinitionException.
@Test(description = "Exception when saving swagger definition for API", expectedExceptions = APIManagementException.class)
public void testSaveSwagger20DefinitionException() 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);
Mockito.doThrow(new APIMgtDAOException("Couldn't update the Swagger Definition")).when(apiDAO).updateApiDefinition(uuid, SampleTestObjectCreator.apiDefinition, USER);
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(keyManager.retrieveScope("apim:api_create")).thenReturn(new Scope("apim:api_create", "Create " + "API"));
String oldSwagger = IOUtils.toString(new FileInputStream("src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "swaggerWithAuthorization" + ".yaml"));
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.ADMIN_ROLE_ID)).thenReturn(ADMIN_ROLE);
Mockito.when(apiDAO.getApiSwaggerDefinition(uuid)).thenReturn(oldSwagger);
apiPublisher.saveSwagger20Definition(uuid, SampleTestObjectCreator.apiDefinition);
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdScopesPost.
@Override
public Response apisApiIdScopesPost(String apiId, ScopeDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
try {
String username = RestApiUtil.getLoggedInUsername(request);
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
KeyMgtConfigurations keyManagerConfiguration = APIMConfigurationService.getInstance().getApimConfigurations().getKeyManagerConfigs();
if (body.getBindings() != null && StringUtils.isNotEmpty(body.getBindings().getType())) {
if (!keyManagerConfiguration.getScopeBindingType().equalsIgnoreCase(body.getBindings().getType())) {
String message = "binding type not valid";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(message, 900313L, message);
return Response.status(Response.Status.PRECONDITION_FAILED).entity(errorDTO).build();
}
}
Scope scope = MappingUtil.toScope(body);
apiPublisher.addScopeToTheApi(apiId, scope);
URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + "/scopes/" + scope.getName());
return Response.created(location).entity(body).build();
} catch (APIManagementException e) {
String errorMessage = "Error while creating scope" + body.getName();
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
paramList.put(APIMgtConstants.ExceptionsConstants.SCOPE_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 retrieving source URI location of " + body.getName();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdScopesNamePut.
@Override
public Response apisApiIdScopesNamePut(String apiId, String name, ScopeDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
try {
String username = RestApiUtil.getLoggedInUsername(request);
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
Scope scope = MappingUtil.toScope(body);
apiPublisher.updateScopeOfTheApi(apiId, scope);
return Response.ok().entity(body).build();
} catch (APIManagementException e) {
String errorMessage = "Error while updating the scope of API : " + apiId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
paramList.put(APIMgtConstants.ExceptionsConstants.SCOPE_NAME, body.getName());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
Aggregations