use of org.wso2.carbon.user.core.common.User in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testDeleteApiWithZeroSubscriptionsAndPendingStateChange.
@Test(description = "Delete API with zero Subscriptions and pending wf state change")
public void testDeleteApiWithZeroSubscriptionsAndPendingStateChange() throws APIManagementException, LifecycleException, SQLException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
builder.workflowStatus(APILCWorkflowStatus.PENDING.toString());
API api = builder.build();
String uuid = api.getId();
String lifecycleId = api.getLifecycleInstanceId();
Mockito.when(apiSubscriptionDAO.getSubscriptionCountByAPI(uuid)).thenReturn(0L);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
KeyManager keyManager = Mockito.mock(KeyManager.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(USER, identityProvider, keyManager, apiDAO, null, apiSubscriptionDAO, null, apiLifecycleManager, null, workflowDAO, null, null, null, gateway);
String externalRef = UUID.randomUUID().toString();
Mockito.when(workflowDAO.getExternalWorkflowReferenceForPendingTask(uuid, WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(Optional.ofNullable(externalRef));
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.DEVELOPER_ROLE_ID)).thenReturn(DEVELOPER_ROLE);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.ADMIN_ROLE_ID)).thenReturn(ADMIN_ROLE);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
apiPublisher.deleteAPI(uuid);
Mockito.verify(apiDAO, Mockito.times(1)).getAPI(uuid);
Mockito.verify(apiLifecycleManager, Mockito.times(1)).removeLifecycle(lifecycleId);
Mockito.verify(apiDAO, Mockito.times(1)).deleteAPI(uuid);
Mockito.verify(workflowDAO, Mockito.times(1)).getExternalWorkflowReferenceForPendingTask(uuid, WorkflowConstants.WF_TYPE_AM_API_STATE);
}
use of org.wso2.carbon.user.core.common.User in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddUpdateAPIFromWSDLFile.
@Test(description = "Test adding an API from WSDL file")
public void testAddUpdateAPIFromWSDLFile() throws APIManagementException, LifecycleException, IOException {
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(SampleTestObjectCreator.getMockEndpointMap());
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
APILifecycleManager apiLifecycleManager = getDefaultMockedAPILifecycleManager();
PolicyDAO policyDAO = getDefaultMockedPolicyDAO();
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO, labelDao);
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.addAPIFromWSDLFile(apiBuilder, SampleTestObjectCreator.createDefaultWSDL11ContentInputStream(), true);
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiDAO, Mockito.times(1)).addOrUpdateWSDL(apiBuilder.getId(), IOUtils.toByteArray(SampleTestObjectCreator.createDefaultWSDL11ContentInputStream()), USER);
Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
apiPublisher.updateAPIWSDL(apiBuilder.getId(), SampleTestObjectCreator.createAlternativeWSDL11ContentInputStream());
Mockito.verify(apiDAO, Mockito.times(1)).addOrUpdateWSDL(apiBuilder.getId(), IOUtils.toByteArray(SampleTestObjectCreator.createAlternativeWSDL11ContentInputStream()), USER);
}
use of org.wso2.carbon.user.core.common.User in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApi.
@Test(description = "Test add api with production endpoint")
public void testAddApi() throws APIManagementException, LifecycleException {
/**
* this test method verify the API Add with correct API object get invoked correctly
*/
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id("").endpoint(SampleTestObjectCreator.getMockEndpointMap());
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);
// Error path
// When an APIMgtDAOException is being thrown when the API is created
Mockito.doThrow(APIMgtDAOException.class).when(apiDAO).addAPI(apiBuilder.build());
try {
apiPublisher.addAPI(apiBuilder);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while creating the API - " + apiBuilder.getName());
}
// Error path
// When an GatewayException is being thrown when an error occurred while adding API to the Gateway
Mockito.doThrow(GatewayException.class).when(gateway).addAPI(apiBuilder.build());
try {
apiPublisher.addAPI(apiBuilder);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while adding API - " + apiBuilder.getName() + " to gateway");
}
// Error path
// When an APITemplateException is being thrown when generating API configuration for API
Mockito.when(gatewaySourceGenerator.getConfigStringFromTemplate(Mockito.any())).thenThrow(APITemplateException.class);
try {
apiPublisher.addAPI(apiBuilder);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error generating API configuration for API " + apiBuilder.getName());
}
}
use of org.wso2.carbon.user.core.common.User in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiDefinitionErrorIOException.
@Test(description = "Test IO exception when adding api definition from swagger resource", expectedExceptions = APIManagementException.class)
public void testAddApiDefinitionErrorIOException() 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.doThrow(new IOException()).when(httpURLConnection).getResponseCode();
apiPublisher.addApiFromDefinition(httpURLConnection);
Mockito.verify(apiLifecycleManager, Mockito.times(0)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
use of org.wso2.carbon.user.core.common.User in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiSandboxEndpoint.
@Test(description = "Test add api with sandbox endpoint")
public void testAddApiSandboxEndpoint() throws APIManagementException, LifecycleException {
Map<String, Endpoint> endpointMap = new HashMap<>();
Map<String, Endpoint> resourceEndpointMap = new HashMap<>();
Map<String, UriTemplate> uriTemplateMap = new HashMap();
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
uriTemplateBuilder.endpoint(resourceEndpointMap);
uriTemplateBuilder.templateId("getApisApiIdGet");
uriTemplateBuilder.uriTemplate("/apis/");
uriTemplateBuilder.authType(APIMgtConstants.AUTH_APPLICATION_LEVEL_TOKEN);
uriTemplateBuilder.policy(APIUtils.getDefaultAPIPolicy());
uriTemplateBuilder.httpVerb("GET");
uriTemplateMap.put("getApisApiIdGet", uriTemplateBuilder.build());
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id("").endpoint(endpointMap).uriTemplates(uriTemplateMap);
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
String endpointId = String.valueOf(apiBuilder.getEndpoint().get("sandbox"));
Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.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);
apiPublisher.addAPI(apiBuilder);
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
Aggregations