use of org.wso2.carbon.registry.core.Collection 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.registry.core.Collection 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.registry.core.Collection in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetAllDocumentationWithLoggedUser.
@Test
public void testGetAllDocumentationWithLoggedUser() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
int tenantId = -1234;
UserRegistry registry = Mockito.mock(UserRegistry.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
Mockito.when(tenantManager.getTenantId(SAMPLE_TENANT_DOMAIN)).thenThrow(UserStoreException.class).thenReturn(tenantId);
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
String loggedInUser = "admin";
abstractAPIManager.registry = registry;
GenericArtifact genericArtifact = getGenericArtifact(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION, "sample");
genericArtifact.setAttribute(APIConstants.DOC_TYPE, "Other");
genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "URL");
String apiDocPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR;
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.when(APIUtil.getAPIOrAPIProductDocPath(identifier)).thenReturn(apiDocPath);
Resource resource1 = new ResourceImpl();
resource1.setUUID(SAMPLE_RESOURCE_ID);
Mockito.when(genericArtifact.getPath()).thenReturn("test");
String docName = "sample";
Documentation documentation = new Documentation(DocumentationType.HOWTO, docName);
PowerMockito.when(APIUtil.getDocumentation(genericArtifact)).thenReturn(documentation);
Mockito.when(registry.resourceExists(apiDocPath)).thenReturn(true);
try {
abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get documentations for api"));
}
Resource resource2 = new ResourceImpl();
resource2.setUUID(SAMPLE_RESOURCE_ID);
Mockito.when(genericArtifactManager.getGenericArtifact(SAMPLE_RESOURCE_ID)).thenReturn(genericArtifact);
String documentationName = "doc1";
Collection documentCollection = new CollectionImpl();
documentCollection.setChildren(new String[] { apiDocPath + documentationName, apiDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR });
Mockito.when(registry.get(apiDocPath)).thenReturn(documentCollection);
Mockito.when(registry.get(apiDocPath + documentationName)).thenReturn(resource2);
PowerMockito.when(APIUtil.getDocumentation(genericArtifact, loggedInUser)).thenReturn(documentation);
List<Documentation> documentationList = abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
Assert.assertNotNull(documentationList);
Assert.assertEquals(documentationList.size(), 1);
String contentPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR + RegistryConstants.PATH_SEPARATOR + documentationName;
genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "In line");
genericArtifact.setAttribute(APIConstants.DOC_NAME, documentationName);
ResourceDO resourceDO = new ResourceDO();
resourceDO.setLastUpdatedOn(12344567);
Resource resource3 = new ResourceImpl(contentPath, resourceDO);
Mockito.when(registry.get(contentPath)).thenReturn(resource3);
documentationList = abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
Assert.assertNotNull(documentationList);
Assert.assertEquals(documentationList.size(), 1);
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class ApplicationThrottleController method lookup.
private static OMNode lookup(String key, int tenantId) {
try {
Resource resource = getResource(key, tenantId);
if (resource instanceof Collection || resource == null) {
return null;
}
ByteArrayInputStream inputStream = null;
Object content = resource.getContent();
if (content instanceof String) {
inputStream = new ByteArrayInputStream(content.toString().getBytes(Charset.defaultCharset()));
} else if (content instanceof byte[]) {
inputStream = new ByteArrayInputStream((byte[]) content);
}
OMNode result = null;
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
XMLStreamReader parser = factory.createXMLStreamReader(inputStream);
StAXOMBuilder builder = new StAXOMBuilder(parser);
result = builder.getDocumentElement();
} catch (OMException ignored) {
result = readNonXML(resource);
} catch (XMLStreamException ignored) {
result = readNonXML(resource);
} catch (Exception e) {
// a more general exception(e.g. a Runtime exception if the XML doc has an
// external DTD deceleration and if not connected to internet) which in case
// just log for debugging
log.error("Error while reading the resource '" + key + '\'', e);
} finally {
try {
resource.discard();
if (result != null && result.getParent() != null) {
result.detach();
OMDocumentImpl parent = new OMDocumentImpl(OMAbstractFactory.getOMFactory());
parent.addChild(result);
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
log.error("Error while closing the input stream", e);
}
}
return result;
} catch (RegistryException e) {
handleException("Error while fetching the resource " + key, e);
}
return null;
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class APIUtilTest method testGetMediationSequenceUuidCustomSequence.
@Test
public void testGetMediationSequenceUuidCustomSequence() throws Exception {
APIIdentifier apiIdentifier = Mockito.mock(APIIdentifier.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry registry = Mockito.mock(UserRegistry.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
Collection collection = Mockito.mock(Collection.class);
String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
String path = artifactPath + RegistryConstants.PATH_SEPARATOR + "custom" + RegistryConstants.PATH_SEPARATOR;
Mockito.when(registry.get(eq(path))).thenReturn(collection);
String[] childPaths = { "test" };
Mockito.when(collection.getChildren()).thenReturn(childPaths);
String expectedUUID = UUID.randomUUID().toString();
InputStream sampleSequence = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("sampleSequence.xml").getFile());
Resource resource = Mockito.mock(Resource.class);
Mockito.when(registry.get(eq("test"))).thenReturn(resource);
Mockito.when(resource.getContentStream()).thenReturn(sampleSequence);
Mockito.when(resource.getUUID()).thenReturn(expectedUUID);
String actualUUID = APIUtil.getMediationSequenceUuid("sample", 1, "custom", apiIdentifier);
Assert.assertEquals(expectedUUID, actualUUID);
sampleSequence.close();
}
Aggregations