use of org.wso2.carbon.apimgt.core.api.KeyManager in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateScopeOfNonExistingScope.
@Test(description = "update existing Scope to API")
public void testUpdateScopeOfNonExistingScope() 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.updateScopeOfTheApi(api.getId(), scope);
Assert.fail();
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Scope couldn't found by name: "));
}
}
use of org.wso2.carbon.apimgt.core.api.KeyManager 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.carbon.apimgt.core.api.KeyManager 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.carbon.apimgt.core.api.KeyManager 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.carbon.apimgt.core.api.KeyManager in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testDeleteApiWithZeroSubscriptionsException.
@Test(description = "Error occurred while deleting API with zero subscriptions")
public void testDeleteApiWithZeroSubscriptionsException() throws APIManagementException, LifecycleException, SQLException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
API api = SampleTestObjectCreator.createDefaultAPI().build();
String uuid = api.getId();
Mockito.when(apiSubscriptionDAO.getSubscriptionCountByAPI(uuid)).thenReturn(0L);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
KeyManager keyManager = Mockito.mock(KeyManager.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(USER, identityProvider, keyManager, apiDAO, null, apiSubscriptionDAO, null, apiLifecycleManager, labelDao, workflowDAO, null, null, null, gateway);
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
// LifeCycleException
Mockito.doThrow(LifecycleException.class).when(apiLifecycleManager).removeLifecycle(api.getLifecycleInstanceId());
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
try {
apiPublisher.deleteAPI(uuid);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while Disassociating the API with Lifecycle id " + uuid);
}
// ApiDAOException
Mockito.doThrow(APIMgtDAOException.class).when(apiDAO).deleteAPI(uuid);
try {
apiPublisher.deleteAPI(uuid);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while deleting the API with id " + uuid);
}
// GatewayException
Mockito.doThrow(GatewayException.class).when(gateway).deleteAPI(api);
try {
apiPublisher.deleteAPI(uuid);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while deleting API with id - " + uuid + " from gateway");
}
}
Aggregations