use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.
the class DocumentIndexer method fetchRequiredDetailsFromAssociatedAPI.
/**
* Fetch api status and access control details to document artifacts
*
* @param registry
* @param documentResource
* @param fields
* @throws RegistryException
* @throws APIManagementException
*/
private void fetchRequiredDetailsFromAssociatedAPI(Registry registry, Resource documentResource, Map<String, List<String>> fields) throws RegistryException, APIManagementException {
String pathToDocFile = documentResource.getPath();
String apiPath = pathToDocFile.substring(0, pathToDocFile.indexOf(APIConstants.DOC_DIR)) + APIConstants.API_KEY;
if (registry.resourceExists(apiPath)) {
Resource apiResource = registry.get(apiPath);
GenericArtifactManager apiArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiResource.getUUID());
String apiStatus = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_STATUS).toLowerCase();
String publisherRoles = apiResource.getProperty(APIConstants.PUBLISHER_ROLES);
fields.put(APIConstants.API_OVERVIEW_STATUS, Arrays.asList(apiStatus));
fields.put(APIConstants.PUBLISHER_ROLES, Arrays.asList(publisherRoles));
} else {
log.warn("API does not exist at " + apiPath);
}
}
use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.
the class RuntimeArtifactGeneratorUtil method generateMetadataArtifact.
public static RuntimeArtifactDto generateMetadataArtifact(String tenantDomain, String apiId, String gatewayLabel) throws APIManagementException {
List<APIRuntimeArtifactDto> gatewayArtifacts = getRuntimeArtifacts(apiId, gatewayLabel, tenantDomain);
if (gatewayArtifacts != null) {
try {
MetadataDescriptorDto metadataDescriptorDto = new MetadataDescriptorDto();
Map<String, ApiMetadataProjectDto> deploymentsMap = new HashMap<>();
// "tempDirectory" is the root artifact directory
File tempDirectory = CommonUtil.createTempDirectory(null);
for (APIRuntimeArtifactDto apiRuntimeArtifactDto : gatewayArtifacts) {
if (apiRuntimeArtifactDto.isFile()) {
String fileName = apiRuntimeArtifactDto.getApiId().concat("-").concat(apiRuntimeArtifactDto.getRevision());
ApiMetadataProjectDto apiProjectDto = deploymentsMap.get(fileName);
if (apiProjectDto == null) {
apiProjectDto = new ApiMetadataProjectDto();
deploymentsMap.put(fileName, apiProjectDto);
apiProjectDto.setApiFile(fileName);
apiProjectDto.setEnvironments(new HashSet<>());
apiProjectDto.setOrganizationId(apiRuntimeArtifactDto.getOrganization());
apiProjectDto.setVersion(apiRuntimeArtifactDto.getVersion());
apiProjectDto.setApiContext(apiRuntimeArtifactDto.getContext());
}
EnvironmentDto environment = new EnvironmentDto();
environment.setName(apiRuntimeArtifactDto.getLabel());
environment.setVhost(apiRuntimeArtifactDto.getVhost());
apiProjectDto.getEnvironments().add(environment);
}
}
metadataDescriptorDto.setMetadataDescriptor(new HashSet<>(deploymentsMap.values()));
String descriptorFile = Paths.get(tempDirectory.getAbsolutePath(), APIConstants.GatewayArtifactConstants.DEPLOYMENT_DESCRIPTOR_FILE).toString();
CommonUtil.writeDtoToFile(descriptorFile, ExportFormat.JSON, APIConstants.GatewayArtifactConstants.DEPLOYMENT_DESCRIPTOR_FILE_TYPE, metadataDescriptorDto);
RuntimeArtifactDto runtimeArtifactDto = new RuntimeArtifactDto();
runtimeArtifactDto.setArtifact(new File(descriptorFile.concat(APIConstants.JSON_FILE_EXTENSION)));
runtimeArtifactDto.setFile(true);
return runtimeArtifactDto;
} catch (APIImportExportException | IOException e) {
throw new APIManagementException("Error while Generating API artifact", e);
}
} else {
throw new APIManagementException("No API Artifacts", ExceptionCodes.NO_API_ARTIFACT_FOUND);
}
}
use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testRegistrySelectionForTenantUser.
@Test
public void testRegistrySelectionForTenantUser() throws Exception {
RegistryService registryService = Mockito.mock(RegistryService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
Mockito.when(tenantManager.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID);
PowerMockito.mockStatic(CarbonContext.class);
CarbonContext context = Mockito.mock(CarbonContext.class);
PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(context);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PrivilegedCarbonContext privilegedContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedContext);
PowerMockito.mockStatic(GovernanceUtils.class);
GovernanceArtifact artifact = Mockito.mock(GovernanceArtifact.class);
List<GovernanceArtifact> artifacts = new ArrayList<GovernanceArtifact>();
artifacts.add(artifact);
PowerMockito.when(GovernanceUtils.findGovernanceArtifacts(Mockito.any(String.class), Mockito.any(Registry.class), Mockito.any(String.class), Mockito.any(Boolean.class))).thenReturn(artifacts);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
ServiceReferenceHolder serviceRefHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceRefHolder);
RealmService realmService = Mockito.mock(RealmService.class);
PowerMockito.when(serviceRefHolder.getRealmService()).thenReturn(realmService);
UserRealm realm = Mockito.mock(UserRealm.class);
PowerMockito.when(realmService.getTenantUserRealm(TENANT_ID)).thenReturn(realm);
PowerMockito.doNothing().when(RegistryPersistenceUtil.class, "loadloadTenantAPIRXT", Mockito.any(String.class), Mockito.any(Integer.class));
Mockito.when(context.getTenantDomain()).thenReturn(TENANT_DOMAIN);
Mockito.when(context.getTenantId()).thenReturn(TENANT_ID);
// return null artifact because we are not testing artifact related params. this is only to get the registry obj
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(Mockito.any(Registry.class), Mockito.any(String.class))).thenReturn(artifactManager);
Mockito.when(artifactManager.getGenericArtifact(Mockito.any(String.class))).thenReturn(null);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(tenantManager, registryService);
// trigger registry object creation
UserContext ctx = new UserContext("user", new Organization(TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(TENANT_DOMAIN), "", 0, 10, ctx);
Mockito.verify(registryService, times(1)).getGovernanceUserRegistry("user", TENANT_ID);
ctx = new UserContext("wso2.anonymous.user", new Organization(TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(TENANT_DOMAIN), "", 0, 10, ctx);
Mockito.verify(registryService, times(1)).getGovernanceUserRegistry("wso2.anonymous.user", TENANT_ID);
}
use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testRegistrySelectionForSuperTenantUser.
@Test
public void testRegistrySelectionForSuperTenantUser() throws Exception {
RegistryService registryService = Mockito.mock(RegistryService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
Mockito.when(tenantManager.getTenantId(SUPER_TENANT_DOMAIN)).thenReturn(SUPER_TENANT_ID);
PowerMockito.mockStatic(CarbonContext.class);
CarbonContext context = Mockito.mock(CarbonContext.class);
PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(context);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PrivilegedCarbonContext privilegedContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedContext);
PowerMockito.mockStatic(GovernanceUtils.class);
GovernanceArtifact artifact = Mockito.mock(GovernanceArtifact.class);
List<GovernanceArtifact> artifacts = new ArrayList<GovernanceArtifact>();
artifacts.add(artifact);
PowerMockito.when(GovernanceUtils.findGovernanceArtifacts(Mockito.any(String.class), Mockito.any(Registry.class), Mockito.any(String.class), Mockito.any(Boolean.class))).thenReturn(artifacts);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
ServiceReferenceHolder serviceRefHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceRefHolder);
RealmService realmService = Mockito.mock(RealmService.class);
PowerMockito.when(serviceRefHolder.getRealmService()).thenReturn(realmService);
UserRealm realm = Mockito.mock(UserRealm.class);
PowerMockito.when(realmService.getTenantUserRealm(SUPER_TENANT_ID)).thenReturn(realm);
PowerMockito.doNothing().when(RegistryPersistenceUtil.class, "loadloadTenantAPIRXT", Mockito.any(String.class), Mockito.any(Integer.class));
Mockito.when(context.getTenantDomain()).thenReturn(SUPER_TENANT_DOMAIN);
Mockito.when(context.getTenantId()).thenReturn(SUPER_TENANT_ID);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(tenantManager, registryService);
// return null artifact because we are not testing artifact related params. this is only to get the registry obj
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(Mockito.any(Registry.class), Mockito.any(String.class))).thenReturn(artifactManager);
Mockito.when(artifactManager.getGenericArtifact(Mockito.any(String.class))).thenReturn(null);
// trigger registry object creation
UserContext ctx = new UserContext("user", new Organization(SUPER_TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(SUPER_TENANT_DOMAIN), "", 0, 10, ctx);
Mockito.verify(registryService, times(1)).getGovernanceUserRegistry("user", SUPER_TENANT_ID);
apiPersistenceInstance = new RegistryPersistenceImplWrapper(tenantManager, registryService);
// trigger registry object creation
ctx = new UserContext("wso2.anonymous.user", new Organization(SUPER_TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(SUPER_TENANT_DOMAIN), "", 0, 10, ctx);
Mockito.verify(registryService, times(1)).getGovernanceUserRegistry("wso2.anonymous.user", SUPER_TENANT_ID);
}
use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.
the class ExportUtils method exportApplication.
/**
* Export a given Application to a file system as zip archive.
* The export root location is given by {@link @path}/exported-application.
*
* @param exportApplication Application{@link Application} to be exported
* @param apiConsumer API Consumer
* @param exportFormat Format to export
* @param withKeys Export the Application with keys or not
* @return Path to the exported directory with exported artifacts
* @throws APIManagementException If an error occurs while exporting an application to a file system
*/
public static File exportApplication(Application exportApplication, APIConsumer apiConsumer, ExportFormat exportFormat, Boolean withKeys) throws APIManagementException {
String archivePath = null;
String exportApplicationBasePath;
String appName = exportApplication.getName();
String appOwner = exportApplication.getOwner();
try {
// Creates a temporary directory to store the exported application artifact
File exportFolder = createTempApplicationDirectory(appName, appOwner);
exportApplicationBasePath = exportFolder.toString();
archivePath = exportApplicationBasePath.concat(File.separator + appOwner.replace(File.separator, "#") + "-" + appName);
} catch (APIImportExportException e) {
throw new APIManagementException("Unable to create the temporary directory to export the Application", e);
}
ExportedApplication applicationDtoToExport = createApplicationDTOToExport(exportApplication, apiConsumer, withKeys);
try {
createDirectory(archivePath);
// Export application details
CommonUtil.writeDtoToFile(archivePath + File.separator + ImportExportConstants.TYPE_APPLICATION, exportFormat, ImportExportConstants.TYPE_APPLICATION, applicationDtoToExport);
CommonUtil.archiveDirectory(exportApplicationBasePath);
FileUtils.deleteQuietly(new File(exportApplicationBasePath));
return new File(exportApplicationBasePath + APIConstants.ZIP_FILE_EXTENSION);
} catch (IOException | APIImportExportException e) {
throw new APIManagementException("Error while exporting Application: " + exportApplication.getName(), e);
}
}
Aggregations