use of org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiSpecificEndpoint.
@Test(description = "Test add api with Api Specific Endpoint", expectedExceptions = { APIManagementException.class })
public void testAddApiSpecificEndpoint() throws APIManagementException, LifecycleException {
/**
* this test method verify the API Add with correct API object get invoked correctly
*/
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Endpoint globalEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("testEndpoint").applicableLevel(APIMgtConstants.GLOBAL_ENDPOINT).build();
Endpoint apiEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("apiEndpoint").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
Map<String, Endpoint> endpointMap = new HashMap();
endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, globalEndpoint);
endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, apiEndpoint);
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(endpointMap);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
APIGateway gateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
Policy apiPolicy = new APIPolicy(UUID.randomUUID().toString(), APIMgtConstants.DEFAULT_API_POLICY);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(apiPolicy);
APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
Mockito.when(apiDAO.getEndpoint(globalEndpoint.getId())).thenReturn(globalEndpoint);
Mockito.when(apiDAO.getEndpointByName(apiEndpoint.getName())).thenReturn(null);
Mockito.when(apiDAO.isAPINameExists(apiBuilder.getName(), USER)).thenReturn(false);
Mockito.when(apiDAO.isEndpointAssociated(globalEndpoint.getId())).thenReturn(true);
apiPublisher.addAPI(apiBuilder);
apiPublisher.deleteEndpoint(globalEndpoint.getId());
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(apiEndpoint.getName());
Mockito.verify(apiDAO, Mockito.times(1)).isAPINameExists(apiBuilder.getName(), USER);
}
use of org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator 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.GatewaySourceGenerator in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateApiGatewayConfigException.
@Test(description = "Exception updating Gateway Config for API", expectedExceptions = APIManagementException.class)
public void testUpdateApiGatewayConfigException() throws APIManagementException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
API api = SampleTestObjectCreator.createDefaultAPI().build();
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
String uuid = api.getId();
String configString = SampleTestObjectCreator.createSampleGatewayConfig();
APIGateway gateway = Mockito.mock(APIGateway.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, gatewaySourceGenerator, gateway);
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.doThrow(new APIMgtDAOException("Couldn't update configuration for apiId " + uuid)).when(apiDAO).updateGatewayConfig(uuid, configString, USER);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
apiPublisher.updateApiGatewayConfig(uuid, configString);
}
use of org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateApiForLabelsWhenAPIHasOwnGateway.
@Test(description = "Test label update when api has own gateway", expectedExceptions = APIManagementException.class)
public void testUpdateApiForLabelsWhenAPIHasOwnGateway() throws APIManagementException {
List<String> labels = new ArrayList<>();
labels.add("default");
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id(UUID.randomUUID().toString()).endpoint(Collections.emptyMap()).hasOwnGateway(true).labels(labels);
apiBuilder.apiPermission("");
apiBuilder.permissionMap(null);
apiBuilder.policies(Collections.emptySet());
apiBuilder.apiPolicy(null);
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
Mockito.when(apiDAO.getAPI(apiBuilder.getId())).thenReturn(apiBuilder.build());
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
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));
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO, labelDao);
Mockito.when(apiDAO.getApiSwaggerDefinition(apiBuilder.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
apiPublisher.updateAPI(apiBuilder);
}
use of org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateDedicatedGatewayWhenAPIIsInPublishedState.
@Test(description = "Update dedicated gateway when API is in Published state", expectedExceptions = APIManagementException.class)
public void testUpdateDedicatedGatewayWhenAPIIsInPublishedState() throws APIManagementException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
LabelDAO labelDAO = Mockito.mock(LabelDAO.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
API api = SampleTestObjectCreator.createDefaultAPI().lifeCycleStatus(APIStatus.PUBLISHED.getStatus()).build();
String uuid = api.getId();
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);
APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, labelDAO, gatewaySourceGenerator);
DedicatedGateway dedicatedGateway = SampleTestObjectCreator.createDedicatedGateway(uuid, true, api.getCreatedBy());
apiPublisher.updateDedicatedGateway(dedicatedGateway);
}
Aggregations