Search in sources :

Example 51 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class SelfSignupUtilTestCase method testGetSelfSignupConfigFromRegistry.

@Test
public void testGetSelfSignupConfigFromRegistry() throws Exception {
    System.setProperty(CARBON_HOME, "");
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn("foo.com");
    Mockito.when(privilegedCarbonContext.getRegistry(RegistryType.SYSTEM_GOVERNANCE)).thenReturn(registry);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    APIMConfigService apimConfigService = Mockito.mock(APIMConfigService.class);
    Mockito.when(serviceReferenceHolder.getApimConfigService()).thenReturn(apimConfigService);
    PowerMockito.mockStatic(APIUtil.class);
    Mockito.when(apimConfigService.getSelfSighupConfig("foo.com")).thenReturn("wsdl");
    OMElement omElement = Mockito.mock(OMElement.class);
    Mockito.when(omElement.getFirstChildWithName(Matchers.any(QName.class))).thenReturn(omElement);
    PowerMockito.mockStatic(AXIOMUtil.class);
    Mockito.when(omElement.getChildrenWithLocalName(APIConstants.SELF_SIGN_UP_REG_ROLE_ELEM)).thenReturn(Mockito.mock(Iterator.class));
    PowerMockito.when(AXIOMUtil.stringToOM("wsdl")).thenReturn(omElement);
    PowerMockito.mockStatic(PasswordResolverFactory.class);
    PasswordResolver passwordResolver = Mockito.mock(PasswordResolver.class);
    PowerMockito.when(PasswordResolverFactory.getInstance()).thenReturn(passwordResolver);
    UserRegistrationConfigDTO userRegistrationConfigDTO = SelfSignUpUtil.getSignupConfiguration("foo.com");
    Assert.assertNotNull(userRegistrationConfigDTO);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) PasswordResolver(org.wso2.carbon.apimgt.api.PasswordResolver) QName(javax.xml.namespace.QName) UserRegistrationConfigDTO(org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO) Iterator(java.util.Iterator) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) OMElement(org.apache.axiom.om.OMElement) APIMConfigService(org.wso2.carbon.apimgt.impl.config.APIMConfigService) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 52 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class SequenceUtilsTestCase method testRestToSoapConvertedSequence.

@Test
public void testRestToSoapConvertedSequence() throws Exception {
    Resource resource = Mockito.mock(Resource.class);
    Mockito.when(userRegistry.resourceExists(RESOURCE_PATH)).thenReturn(false);
    Mockito.when(userRegistry.newResource()).thenReturn(resource);
    try {
        SequenceUtils.saveRestToSoapConvertedSequence(userRegistry, SEQUENCE, HTTP_METHOD, RESOURCE_PATH, RESOURCE_NAME);
        Mockito.when(userRegistry.resourceExists(RESOURCE_PATH)).thenReturn(true);
        Mockito.when(userRegistry.get(RESOURCE_PATH)).thenReturn(resource);
        SequenceUtils.saveRestToSoapConvertedSequence(userRegistry, SEQUENCE, HTTP_METHOD, RESOURCE_PATH, RESOURCE_NAME);
    } catch (APIManagementException e) {
        Assert.fail("Failed to save the sequence in the registry");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 53 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class SequenceUtilsTestCase method testUpdateRestToSoapConvertedSequences.

@Test
public void testUpdateRestToSoapConvertedSequences() throws Exception {
    String provider = "admin";
    String apiName = "test-api";
    String version = "1.0.0";
    String seqType = "in";
    String sequence = "{\"test\":{\"method\":\"post\",\"content\":\"<header></header>\"}}";
    Resource resource = Mockito.mock(Resource.class);
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("carbon.super");
    PowerMockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(userRegistry.resourceExists(Mockito.anyString())).thenReturn(true);
    Mockito.when(userRegistry.get(Mockito.anyString())).thenReturn(resource);
    Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenReturn(-1);
    try {
        SequenceUtils.updateRestToSoapConvertedSequences(apiName, version, provider, seqType, sequence);
    } catch (APIManagementException e) {
        Assert.fail("Failed to update the sequence in the registry");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 54 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class ExportUtils method exportApiProduct.

/**
 * Exports an API Product from API Manager for a given API Product. MMeta information, API Product icon,
 * documentation, client certificates and dependent APIs are exported.
 *
 * @param apiProvider           API Provider
 * @param apiProductIdentifier  API Product Identifier
 * @param apiProductDtoToReturn API Product DTO
 * @param userName              Username
 * @param exportFormat          Format of output documents. Can be YAML or JSON
 * @param preserveStatus        Preserve API Product status on export
 * @param organization          Organization Identifier
 * @return
 * @throws APIManagementException If an error occurs while getting governance registry
 */
public static File exportApiProduct(APIProvider apiProvider, APIProductIdentifier apiProductIdentifier, APIProductDTO apiProductDtoToReturn, String userName, ExportFormat exportFormat, Boolean preserveStatus, boolean preserveDocs, boolean preserveCredentials, String organization) throws APIManagementException, APIImportExportException {
    int tenantId = 0;
    // Create temp location for storing API Product data
    File exportFolder = CommonUtil.createTempDirectory(apiProductIdentifier);
    String exportAPIBasePath = exportFolder.toString();
    String archivePath = exportAPIBasePath.concat(File.separator + apiProductIdentifier.getName() + "-" + apiProductIdentifier.getVersion());
    tenantId = APIUtil.getTenantId(userName);
    CommonUtil.createDirectory(archivePath);
    if (preserveDocs) {
        addThumbnailToArchive(archivePath, apiProductIdentifier, apiProvider);
        addDocumentationToArchive(archivePath, apiProductIdentifier, exportFormat, apiProvider, APIConstants.API_PRODUCT_IDENTIFIER_TYPE);
    }
    // Set API Product status to created if the status is not preserved
    if (!preserveStatus) {
        apiProductDtoToReturn.setState(APIProductDTO.StateEnum.CREATED);
    }
    addGatewayEnvironmentsToArchive(archivePath, apiProductDtoToReturn.getId(), exportFormat, apiProvider);
    addAPIProductMetaInformationToArchive(archivePath, apiProductDtoToReturn, exportFormat, apiProvider);
    addDependentAPIsToArchive(archivePath, apiProductDtoToReturn, exportFormat, apiProvider, userName, Boolean.TRUE, preserveDocs, preserveCredentials, organization);
    // Export mTLS authentication related certificates
    if (log.isDebugEnabled()) {
        log.debug("Mutual SSL enabled. Exporting client certificates.");
    }
    addClientCertificatesToArchive(archivePath, apiProductIdentifier, tenantId, apiProvider, exportFormat, organization);
    CommonUtil.archiveDirectory(exportAPIBasePath);
    FileUtils.deleteQuietly(new File(exportAPIBasePath));
    return new File(exportAPIBasePath + APIConstants.ZIP_FILE_EXTENSION);
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) File(java.io.File)

Example 55 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class ExportUtils method addWSDLtoArchive.

/**
 * Retrieve WSDL for the exporting API and store it in the archive directory.
 *
 * @param archivePath   File path to export the WSDL
 * @param apiIdentifier ID of the requesting API
 * @throws APIImportExportException If an error occurs while retrieving WSDL from the registry or
 *                                  storing in the archive directory
 */
public static void addWSDLtoArchive(String archivePath, APIIdentifier apiIdentifier, APIProvider apiProvider) throws APIImportExportException {
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    String wsdlPath = APIConstants.API_WSDL_RESOURCE_LOCATION + apiIdentifier.getProviderName() + "--" + apiIdentifier.getApiName() + apiIdentifier.getVersion() + APIConstants.WSDL_FILE_EXTENSION;
    try {
        ResourceFile wsdlResource = apiProvider.getWSDL(apiIdentifier.getUUID(), tenantDomain);
        if (wsdlResource != null) {
            CommonUtil.createDirectory(archivePath + File.separator + "WSDL");
            try (InputStream wsdlStream = wsdlResource.getContent();
                OutputStream outputStream = new FileOutputStream(archivePath + File.separator + "WSDL" + File.separator + apiIdentifier.getApiName() + "-" + apiIdentifier.getVersion() + APIConstants.WSDL_FILE_EXTENSION)) {
                IOUtils.copy(wsdlStream, outputStream);
                if (log.isDebugEnabled()) {
                    log.debug("WSDL file: " + wsdlPath + " retrieved successfully");
                }
            }
        } else if (log.isDebugEnabled()) {
            log.debug("WSDL resource does not exists in path: " + wsdlPath + ". Skipping WSDL export.");
        }
    } catch (IOException | APIManagementException e) {
        throw new APIImportExportException("I/O error while writing WSDL: " + wsdlPath + " to file", e);
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException)

Aggregations

RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)230 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)225 Resource (org.wso2.carbon.registry.core.Resource)210 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)197 Registry (org.wso2.carbon.registry.core.Registry)125 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)114 Test (org.junit.Test)111 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)107 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)91 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)89 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)80 IOException (java.io.IOException)75 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)74 UserStoreException (org.wso2.carbon.user.api.UserStoreException)71 API (org.wso2.carbon.apimgt.api.model.API)70 ArrayList (java.util.ArrayList)67 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)58 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)51 HashMap (java.util.HashMap)48 OMElement (org.apache.axiom.om.OMElement)48