use of org.wso2.carbon.registry.core.ResourceImpl 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.registry.core.ResourceImpl 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.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetWsdl.
@Test
public void testGetWsdl() throws APIManagementException, RegistryException, IOException {
Resource resourceMock = Mockito.mock(Resource.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
String wsdlName = identifier.getProviderName() + "--" + identifier.getApiName() + identifier.getVersion() + ".wsdl";
String wsdlResourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + wsdlName;
Resource resource = new ResourceImpl(wsdlResourcePath, new ResourceDO());
Mockito.when(registry.get(wsdlResourcePath)).thenThrow(RegistryException.class).thenReturn(resource);
Mockito.when(registry.resourceExists(wsdlResourcePath)).thenReturn(true);
try {
abstractAPIManager.getWSDL(identifier);
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while getting wsdl file from the registry"));
}
String wsdlContent = "sample wsdl";
resource.setContent(wsdlContent);
InputStream inputStream = new ArrayInputStream();
Mockito.when(resourceMock.getContentStream()).thenReturn(inputStream);
Assert.assertEquals(wsdlContent, IOUtils.toString(abstractAPIManager.getWSDL(identifier).getContent()));
PowerMockito.mockStatic(IOUtils.class);
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetAllWsdls.
@Test
public void testGetAllWsdls() throws RegistryException, APIManagementException {
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
Collection parentCollection = new CollectionImpl();
String wsdlResourcepath = APIConstants.API_WSDL_RESOURCE;
String resourcePath = wsdlResourcepath + "/wsdl1";
parentCollection.setChildren(new String[] { resourcePath });
Mockito.when(registry.get(wsdlResourcepath)).thenReturn(parentCollection);
Resource resource = new ResourceImpl();
Mockito.when(registry.get(resourcePath)).thenThrow(RegistryException.class).thenReturn(resource);
Mockito.when(registry.resourceExists(wsdlResourcepath)).thenReturn(true);
try {
abstractAPIManager.getAllWsdls();
Assert.fail("Exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get wsdl list"));
}
resource.setUUID(SAMPLE_RESOURCE_ID);
List<Wsdl> wsdls = abstractAPIManager.getAllWsdls();
Assert.assertNotNull(wsdls);
Assert.assertEquals(wsdls.size(), 1);
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetDocumentationContent.
@Test
public void testGetDocumentationContent() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
int tenantId = -1234;
UserRegistry registry = Mockito.mock(UserRegistry.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapperExtended(genericArtifactManager, registryService, registry, tenantManager);
Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(tenantId);
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
String docName = "doc1";
try {
abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get document content found for documentation:"));
}
Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
try {
abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("No document content found for documentation:"));
}
Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(false, true, true);
String docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.assertNull(docContent);
String docObject = "samlple doc content";
Resource resource = new ResourceImpl();
resource.setContent(docObject.getBytes(StandardCharsets.UTF_8));
Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.assertEquals(docContent, docObject);
abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.assertEquals(docContent, docObject);
Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(registry);
abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.assertEquals(docContent, docObject);
}
Aggregations