use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetAllApiSpecificMediationPolicies.
@Test
public void testGetAllApiSpecificMediationPolicies() throws RegistryException, APIManagementException, IOException, XMLStreamException {
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
String parentCollectionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + APIConstants.API_RESOURCE_NAME;
parentCollectionPath = parentCollectionPath.substring(0, parentCollectionPath.lastIndexOf("/"));
Collection parentCollection = new CollectionImpl();
parentCollection.setChildren(new String[] { parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN, parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT, parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT });
Collection childCollection = new CollectionImpl();
childCollection.setChildren(new String[] { "mediation1" });
Mockito.when(registry.get(parentCollectionPath)).thenThrow(RegistryException.class).thenReturn(parentCollection);
Mockito.when(registry.get(parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN)).thenReturn(childCollection);
Resource resource = new ResourceImpl();
resource.setUUID(SAMPLE_RESOURCE_ID);
String mediationPolicyContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<sequence xmlns=\"http://ws.apache.org/ns/synapse\" name=\"default-endpoint\">\n</sequence>";
resource.setContent(mediationPolicyContent);
Mockito.when(registry.get("mediation1")).thenReturn(resource);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
try {
abstractAPIManager.getAllApiSpecificMediationPolicies(identifier);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error occurred while getting Api Specific mediation policies "));
}
Assert.assertEquals(abstractAPIManager.getAllApiSpecificMediationPolicies(identifier).size(), 1);
PowerMockito.mockStatic(IOUtils.class);
PowerMockito.mockStatic(AXIOMUtil.class);
PowerMockito.when(IOUtils.toString((InputStream) Mockito.any(), Mockito.anyString())).thenThrow(IOException.class).thenReturn(mediationPolicyContent);
PowerMockito.when(AXIOMUtil.stringToOM(Mockito.anyString())).thenThrow(XMLStreamException.class);
// covers exception which is only logged
abstractAPIManager.getAllApiSpecificMediationPolicies(identifier);
// covers exception which is only logged
abstractAPIManager.getAllApiSpecificMediationPolicies(identifier);
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetGlobalMediationPolicy.
@Test
public void testGetGlobalMediationPolicy() throws RegistryException, APIManagementException, XMLStreamException, IOException {
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
String resourceUUID = SAMPLE_RESOURCE_ID;
Collection parentCollection = new CollectionImpl();
String mediationResourcePath = APIConstants.API_CUSTOM_SEQUENCE_LOCATION;
String childCollectionPath = mediationResourcePath + "/testMediation";
parentCollection.setChildren(new String[] { childCollectionPath });
Mockito.when(registry.get(mediationResourcePath)).thenThrow(RegistryException.class).thenReturn(parentCollection);
Collection childCollection = new CollectionImpl();
String resourcePath = childCollectionPath + "/policy1";
childCollection.setChildren(new String[] { resourcePath });
Mockito.when(registry.get(childCollectionPath)).thenReturn(childCollection);
Resource resource = new ResourceImpl(resourcePath, new ResourceDO());
resource.setUUID(resourceUUID);
Mockito.when(registry.get(resourcePath)).thenReturn(resource);
try {
abstractAPIManager.getGlobalMediationPolicy(resourceUUID);
Assert.fail("Registry Exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while accessing registry objects"));
}
// test for registry exception
abstractAPIManager.getGlobalMediationPolicy(resourceUUID);
String mediationPolicyContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<sequence xmlns=\"http://ws.apache.org/ns/synapse\" name=\"default-endpoint\">\n</sequence>";
resource.setContent(mediationPolicyContent);
Mediation policy = abstractAPIManager.getGlobalMediationPolicy(resourceUUID);
Assert.assertNotNull(policy);
PowerMockito.mockStatic(IOUtils.class);
PowerMockito.mockStatic(AXIOMUtil.class);
PowerMockito.when(IOUtils.toString((InputStream) Mockito.any(), Mockito.anyString())).thenThrow(IOException.class).thenReturn(mediationPolicyContent);
PowerMockito.when(AXIOMUtil.stringToOM(Mockito.anyString())).thenThrow(XMLStreamException.class);
// cover the logged only exceptions
abstractAPIManager.getGlobalMediationPolicy(resourceUUID);
// cover the logged only exceptions
abstractAPIManager.getGlobalMediationPolicy(resourceUUID);
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetApiSpecificMediationPolicy.
@Test
public void testGetApiSpecificMediationPolicy() throws RegistryException, APIManagementException, IOException, XMLStreamException {
String parentCollectionPath = "config/mediation/";
parentCollectionPath = parentCollectionPath.substring(0, parentCollectionPath.lastIndexOf("/"));
Collection parentCollection = new CollectionImpl();
parentCollection.setChildren(new String[] { parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT, parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN });
Collection childCollection = new CollectionImpl();
childCollection.setChildren(new String[] { "mediation1" });
Mockito.when(registry.get(parentCollectionPath)).thenThrow(RegistryException.class).thenReturn(null, parentCollection);
Mockito.when(registry.get(parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN)).thenReturn(childCollection);
Resource resource = new ResourceImpl("api/mediation/policy1", new ResourceDO());
resource.setUUID(SAMPLE_RESOURCE_ID);
String mediationPolicyContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<sequence xmlns=\"http://ws.apache.org/ns/synapse\" name=\"default-endpoint\">\n</sequence>";
resource.setContent(mediationPolicyContent);
Mockito.when(registry.get("mediation1")).thenReturn(resource);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
Identifier identifier = Mockito.mock(Identifier.class);
try {
abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while obtaining registry objects"));
}
Assert.assertNull(abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID));
Assert.assertEquals(abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID).getName(), "default-endpoint");
PowerMockito.mockStatic(IOUtils.class);
PowerMockito.mockStatic(AXIOMUtil.class);
PowerMockito.when(IOUtils.toString((InputStream) Mockito.any(), Mockito.anyString())).thenThrow(IOException.class).thenReturn(mediationPolicyContent);
PowerMockito.when(AXIOMUtil.stringToOM(Mockito.anyString())).thenThrow(XMLStreamException.class);
try {
abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID);
Assert.fail("IO exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error occurred while converting content stream into string"));
}
try {
abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID);
Assert.fail("XMLStream exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error occurred while getting omElement out of mediation content"));
}
resource.setContent(null);
try {
abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error occurred while accessing content stream of mediation"));
}
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetAllGlobalMediationPolicies.
@Test
public void testGetAllGlobalMediationPolicies() throws RegistryException, APIManagementException, IOException, XMLStreamException {
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
Collection parentCollection = new CollectionImpl();
String mediationResourcePath = APIConstants.API_CUSTOM_SEQUENCE_LOCATION;
String childCollectionPath = mediationResourcePath + "/testMediation";
parentCollection.setChildren(new String[] { childCollectionPath });
Mockito.when(registry.get(mediationResourcePath)).thenReturn(parentCollection);
Collection childCollection = new CollectionImpl();
String resourcePath = childCollectionPath + "/policy1";
childCollection.setChildren(new String[] { resourcePath });
Mockito.when(registry.get(childCollectionPath)).thenReturn(childCollection);
Resource resource = new ResourceImpl();
resource.setUUID(SAMPLE_RESOURCE_ID);
Mockito.when(registry.get(resourcePath)).thenReturn(resource);
try {
abstractAPIManager.getAllGlobalMediationPolicies();
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get global mediation policies"));
}
String mediationPolicyContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<sequence xmlns=\"http://ws.apache.org/ns/synapse\" name=\"default-endpoint\">\n</sequence>";
resource.setContent(mediationPolicyContent);
List<Mediation> policies = abstractAPIManager.getAllGlobalMediationPolicies();
Assert.assertNotNull(policies);
Assert.assertEquals(policies.size(), 1);
PowerMockito.mockStatic(IOUtils.class);
PowerMockito.mockStatic(AXIOMUtil.class);
PowerMockito.when(IOUtils.toString((InputStream) Mockito.any(), Mockito.anyString())).thenThrow(IOException.class).thenReturn(mediationPolicyContent);
PowerMockito.when(AXIOMUtil.stringToOM(Mockito.anyString())).thenThrow(XMLStreamException.class);
// cover the logged only exceptions
abstractAPIManager.getAllGlobalMediationPolicies();
// cover the logged only exceptions
abstractAPIManager.getAllGlobalMediationPolicies();
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class UserAwareAPIProviderTest method testDeleteApiSpecificMediationPolicy.
/**
* This method checks the behaviour of Delete API specific mediation policy method for a non-existing mediation
* policy.
*
* @throws APIManagementException API Management Exception.
*/
@Test
public void testDeleteApiSpecificMediationPolicy() throws APIManagementException {
Identifier identifier = Mockito.mock(Identifier.class);
Assert.assertFalse(userAwareAPIProvider.deleteApiSpecificMediationPolicy(identifier, "test", "test"));
}
Aggregations