use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetIcon.
@Test
public void testGetIcon() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
APIIdentifier identifier = new APIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
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);
try {
abstractAPIManager.getIcon(identifier);
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while loading API icon of API"));
}
Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
try {
abstractAPIManager.getIcon(identifier);
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while loading API icon of API"));
}
Assert.assertNull(abstractAPIManager.getIcon(identifier));
AbstractAPIManager abstractAPIManager1 = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(registry);
Assert.assertNull(abstractAPIManager1.getIcon(identifier));
abstractAPIManager1.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true);
Resource resource = new ResourceImpl();
resource.setContent("sample conetent");
resource.setMediaType("api");
Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
Assert.assertTrue(abstractAPIManager1.getIcon(identifier).getContentType().equals("api"));
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetTagsWithAttributes.
@Test
public void testGetTagsWithAttributes() throws Exception {
Registry userRegistry = Mockito.mock(Registry.class);
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO, apiPersistenceInstance);
System.setProperty(CARBON_HOME, "");
PowerMockito.mockStatic(GovernanceUtils.class);
UserRegistry userRegistry1 = Mockito.mock(UserRegistry.class);
Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(userRegistry1);
Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry1);
List<TermData> list = new ArrayList<TermData>();
TermData termData = new TermData("testTerm", 10);
list.add(termData);
Mockito.when(GovernanceUtils.getTermDataList(Mockito.anyMap(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean())).thenReturn(list);
ResourceDO resourceDO = Mockito.mock(ResourceDO.class);
Resource resource = new ResourceImpl("dw", resourceDO);
resource.setContent("testContent");
Mockito.when(userRegistry1.resourceExists(Mockito.anyString())).thenReturn(true);
Mockito.when(userRegistry1.get(Mockito.anyString())).thenReturn(resource);
assertNotNull(apiConsumer.getTagsWithAttributes("testDomain"));
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class DocumentIndexer method fetchDocumentContent.
/**
* Write document content to document artifact as its raw content
*
* @param registry
* @param documentResource
* @return
* @throws RegistryException
* @throws IOException
* @throws APIManagementException
*/
private String fetchDocumentContent(Registry registry, Resource documentResource) throws RegistryException, IOException, APIManagementException {
GenericArtifactManager docArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
GenericArtifact documentArtifact = docArtifactManager.getGenericArtifact(documentResource.getUUID());
String sourceType = documentArtifact.getAttribute(APIConstants.DOC_SOURCE_TYPE);
String contentString = null;
if (Documentation.DocumentSourceType.FILE.name().equals(sourceType)) {
String path = documentArtifact.getAttribute(APIConstants.DOC_FILE_PATH);
int indexOfApimgt = path.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION);
String filepath = path.substring(indexOfApimgt);
Resource contentResource = registry.get(filepath);
int indexOfFiles = filepath.indexOf(APIConstants.DOCUMENT_FILE_DIR) + APIConstants.DOCUMENT_FILE_DIR.length() + 1;
String fileName = filepath.substring(indexOfFiles);
String extension = FilenameUtils.getExtension(fileName);
InputStream inputStream = null;
try {
inputStream = contentResource.getContentStream();
switch(extension) {
case APIConstants.PDF_EXTENSION:
PDFParser pdfParser = new PDFParser(new RandomAccessBufferedFileInputStream(inputStream));
pdfParser.parse();
COSDocument cosDocument = pdfParser.getDocument();
PDFTextStripper stripper = new PDFTextStripper();
contentString = stripper.getText(new PDDocument(cosDocument));
break;
case APIConstants.DOC_EXTENSION:
{
POIFSFileSystem pfs = new POIFSFileSystem(inputStream);
WordExtractor msWord2003Extractor = new WordExtractor(pfs);
contentString = msWord2003Extractor.getText();
break;
}
case APIConstants.DOCX_EXTENSION:
XWPFDocument doc = new XWPFDocument(inputStream);
XWPFWordExtractor msWord2007Extractor = new XWPFWordExtractor(doc);
contentString = msWord2007Extractor.getText();
break;
case APIConstants.XLS_EXTENSION:
{
POIFSFileSystem pfs = new POIFSFileSystem(inputStream);
ExcelExtractor extractor = new ExcelExtractor(pfs);
contentString = extractor.getText();
break;
}
case APIConstants.XLSX_EXTENSION:
XSSFWorkbook xssfSheets = new XSSFWorkbook(inputStream);
XSSFExcelExtractor xssfExcelExtractor = new XSSFExcelExtractor(xssfSheets);
contentString = xssfExcelExtractor.getText();
break;
case APIConstants.PPT_EXTENSION:
{
POIFSFileSystem fs = new POIFSFileSystem(inputStream);
PowerPointExtractor extractor = new PowerPointExtractor(fs);
contentString = extractor.getText();
break;
}
case APIConstants.PPTX_EXTENSION:
XMLSlideShow xmlSlideShow = new XMLSlideShow(inputStream);
XSLFPowerPointExtractor xslfPowerPointExtractor = new XSLFPowerPointExtractor(xmlSlideShow);
contentString = xslfPowerPointExtractor.getText();
break;
case APIConstants.TXT_EXTENSION:
case APIConstants.WSDL_EXTENSION:
case APIConstants.XML_DOC_EXTENSION:
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder contentBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
contentBuilder.append(line);
}
contentString = contentBuilder.toString();
break;
}
} finally {
IOUtils.closeQuietly(inputStream);
}
} else if (Documentation.DocumentSourceType.INLINE.name().equals(sourceType)) {
String fileName = ((ResourceImpl) documentResource).getName();
String pathToDocFile = documentResource.getPath();
String pathToContent = pathToDocFile.substring(0, pathToDocFile.lastIndexOf(fileName)) + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + fileName;
if (registry.resourceExists(pathToContent)) {
Resource contentResource = registry.get(pathToContent);
InputStream instream = null;
BufferedReader reader = null;
String line;
try {
instream = contentResource.getContentStream();
reader = new BufferedReader(new InputStreamReader(instream));
StringBuilder contentBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
contentBuilder.append(line);
}
contentString = contentBuilder.toString();
} finally {
if (reader != null) {
IOUtils.closeQuietly(reader);
}
}
}
}
return contentString;
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class GAConfigMediaTypeHandler method put.
public void put(RequestContext requestContext) throws RegistryException {
ResourceImpl resource = (ResourceImpl) requestContext.getResource();
if (!resource.isContentModified()) {
return;
}
// Local entry is updated only if the content of ga-config is updated
Object content = resource.getContent();
if (!(content instanceof String)) {
if (!(content instanceof byte[])) {
log.warn("The resource content is not of expected type");
return;
}
}
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
GoogleAnalyticsConfigEvent googleAnalyticsConfigEvent = new GoogleAnalyticsConfigEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.GA_CONFIG_UPDATE.toString(), tenantId, tenantDomain);
APIUtil.sendNotification(googleAnalyticsConfigEvent, APIConstants.NotifierType.GA_CONFIG.name());
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class CustomAPIIndexerTest method testIndexDocumentForNewAPI.
/**
* This method checks the indexer's behaviour for new APIs which does not have the relevant properties.
*
* @throws RegistryException Registry Exception.
* @throws APIManagementException API Management Exception.
*/
@Test
public void testIndexDocumentForNewAPI() throws APIManagementException, RegistryException {
Resource resource = new ResourceImpl();
PowerMockito.mockStatic(APIUtil.class);
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
GenericArtifact genericArtifact = Mockito.mock(GenericArtifact.class);
Mockito.when(artifactManager.getGenericArtifact(Mockito.anyString())).thenReturn(genericArtifact);
Mockito.when(genericArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY)).thenReturn("public");
PowerMockito.when(APIUtil.getAPI(genericArtifact, userRegistry)).thenReturn(Mockito.mock(API.class));
resource.setProperty(APIConstants.ACCESS_CONTROL, APIConstants.NO_ACCESS_CONTROL);
resource.setProperty(APIConstants.PUBLISHER_ROLES, APIConstants.NULL_USER_ROLE_LIST);
resource.setProperty(APIConstants.STORE_VIEW_ROLES, APIConstants.NULL_USER_ROLE_LIST);
Mockito.doReturn(resource).when(userRegistry).get(Mockito.anyString());
indexer.getIndexedDocument(file2Index);
Assert.assertNull(APIConstants.CUSTOM_API_INDEXER_PROPERTY + " property was set for the API which does not " + "require migration", resource.getProperty(APIConstants.CUSTOM_API_INDEXER_PROPERTY));
}
Aggregations