Search in sources :

Example 11 with ApiMgtDAO

use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetApplicationPolicyByUUID.

@Test
public void testGetApplicationPolicyByUUID() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    ApplicationPolicy applicationPolicy = Mockito.mock(ApplicationPolicy.class);
    Mockito.when(apimgtDAO.getApplicationPolicyByUUID("1111")).thenReturn(applicationPolicy, null);
    apiProvider.getApplicationPolicyByUUID("1111");
    try {
        assertNotNull(apiProvider.getApplicationPolicyByUUID("1111"));
    } catch (APIManagementException e) {
        assertEquals("Application Policy: 1111 was not found.", e.getMessage());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 12 with ApiMgtDAO

use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testUpdateAPIforStateChange_ToPublished.

@Test
public void testUpdateAPIforStateChange_ToPublished() throws RegistryException, UserStoreException, APIManagementException, FaultGatewaysException, APIPersistenceException, XMLStreamException {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
    Set<String> environments = new HashSet<String>();
    environments.add("Production");
    environments.add("Sandbox");
    API api = new API(apiId);
    api.setContext("/test");
    api.setStatus(APIConstants.CREATED);
    api.setAsDefaultVersion(true);
    api.setEnvironments(environments);
    api.setOrganization("carbon.super");
    Mockito.when(apimgtDAO.getPublishedDefaultVersion(apiId)).thenReturn("1.0.0");
    Map<String, String> failedGWEnv = new HashMap<String, String>();
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
    Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
    Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
    PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
    apiProvider.addAPI(api);
    // Mock Updating API
    Resource apiSourceArtifact = Mockito.mock(Resource.class);
    Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
    String apiSourcePath = "path";
    PowerMockito.when(APIUtil.getAPIPath(api.getId())).thenReturn(apiSourcePath);
    PowerMockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("CREATED");
    Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    Mockito.when(artifact.getId()).thenReturn("12640983654");
    PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, "12640983654")).thenReturn(apiSourcePath);
    Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
    apiProvider.updateAPIforStateChange(api, APIConstants.CREATED, APIConstants.PUBLISHED);
    // From the 2 environments, only 1 was successful
    Assert.assertEquals(2, api.getEnvironments().size());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RealmService(org.wso2.carbon.user.core.service.RealmService) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with ApiMgtDAO

use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.

the class APIProviderImplTest method init.

@Before
public void init() throws Exception {
    System.setProperty("carbon.home", APIProviderImplTest.class.getResource("/").getFile());
    PowerMockito.mockStatic(ApiMgtDAO.class);
    PowerMockito.mockStatic(GatewayArtifactsMgtDAO.class);
    PowerMockito.mockStatic(ScopesDAO.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(RegistryUtils.class);
    PowerMockito.mockStatic(GovernanceUtils.class);
    PowerMockito.mockStatic(WorkflowExecutorFactory.class);
    PowerMockito.mockStatic(LifecycleBeanPopulator.class);
    PowerMockito.mockStatic(KeyManagerHolder.class);
    PowerMockito.mockStatic(Caching.class);
    PowerMockito.mockStatic(PaginationContext.class);
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.mockStatic(APIGatewayManager.class);
    PowerMockito.mockStatic(CertificateManagerImpl.class);
    PowerMockito.mockStatic(RegistryPersistenceUtil.class);
    apimgtDAO = Mockito.mock(ApiMgtDAO.class);
    gatewayArtifactsMgtDAO = Mockito.mock(GatewayArtifactsMgtDAO.class);
    scopesDAO = Mockito.mock(ScopesDAO.class);
    keyManager = Mockito.mock(KeyManager.class);
    apiPersistenceInstance = Mockito.mock(APIPersistence.class);
    certificateManager = Mockito.mock(CertificateManagerImpl.class);
    Mockito.when(keyManager.getResourceByApiId(Mockito.anyString())).thenReturn(null);
    Mockito.when(keyManager.registerNewResource(Mockito.any(API.class), Mockito.any(Map.class))).thenReturn(true);
    KeyManagerDto keyManagerDto = new KeyManagerDto();
    keyManagerDto.setName("default");
    keyManagerDto.setKeyManager(keyManager);
    keyManagerDto.setIssuer("https://localhost");
    Map<String, KeyManagerDto> tenantKeyManagerDtoMap = new HashMap<>();
    tenantKeyManagerDtoMap.put("default", keyManagerDto);
    PowerMockito.when(KeyManagerHolder.getTenantKeyManagers("carbon.super")).thenReturn(tenantKeyManagerDtoMap);
    PowerMockito.when(CertificateManagerImpl.getInstance()).thenReturn(certificateManager);
    PowerMockito.when(APIUtil.isAPIManagementEnabled()).thenReturn(false);
    PowerMockito.when(APIUtil.replaceEmailDomainBack(Mockito.anyString())).thenReturn("admin");
    Mockito.when(APIUtil.replaceEmailDomain(Mockito.anyString())).thenReturn("admin");
    PrivilegedCarbonContext prcontext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(prcontext);
    PowerMockito.doNothing().when(prcontext).setUsername(Mockito.anyString());
    PowerMockito.doNothing().when(prcontext).setTenantDomain(Mockito.anyString(), Mockito.anyBoolean());
    artifactManager = Mockito.mock(GenericArtifactManager.class);
    registry = Mockito.mock(Registry.class);
    PowerMockito.when(APIUtil.getArtifactManager(any(Registry.class), Mockito.anyString())).thenReturn(artifactManager);
    artifact = Mockito.mock(GenericArtifact.class);
    gatewayManager = Mockito.mock(APIGatewayManager.class);
    Mockito.when(APIGatewayManager.getInstance()).thenReturn(gatewayManager);
    TestUtils.mockRegistryAndUserRealm(-1234);
    TestUtils.mockAPICacheClearence();
    TestUtils.mockAPIMConfiguration();
    mockDocumentationCreation();
    config = Mockito.mock(APIManagerConfiguration.class);
    APIManagerConfigurationService apiManagerConfigurationService = new APIManagerConfigurationServiceImpl(config);
    ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(apiManagerConfigurationService);
    APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
    GatewayArtifactSynchronizerProperties synchronizerProperties = new GatewayArtifactSynchronizerProperties();
    Mockito.when(config.getGatewayArtifactSynchronizerProperties()).thenReturn(synchronizerProperties);
    Mockito.when(config.getApiRecommendationEnvironment()).thenReturn(null);
    PowerMockito.when(APIUtil.replaceSystemProperty(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> {
        Object[] args = invocation.getArguments();
        return (String) args[0];
    });
    TestUtils.initConfigurationContextService(true);
    superTenantDomain = "carbon.super";
}
Also used : RegistryAuthorizationManager(org.wso2.carbon.registry.core.jdbc.realm.RegistryAuthorizationManager) StringUtils(org.apache.commons.lang.StringUtils) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PaginationContext(org.wso2.carbon.registry.core.pagination.PaginationContext) LifecycleBean(org.wso2.carbon.governance.custom.lifecycles.checklist.beans.LifecycleBean) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) RealmService(org.wso2.carbon.user.core.service.RealmService) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) AuthorizationManager(org.wso2.carbon.user.api.AuthorizationManager) DocumentationType(org.wso2.carbon.apimgt.api.model.DocumentationType) GatewayArtifactSynchronizerProperties(org.wso2.carbon.apimgt.impl.dto.GatewayArtifactSynchronizerProperties) ContentType(org.apache.http.entity.ContentType) Set(java.util.Set) WorkflowConstants(org.wso2.carbon.apimgt.impl.workflow.WorkflowConstants) HTTPVerbCondition(org.wso2.carbon.apimgt.api.model.policy.HTTPVerbCondition) RegistryPersistenceUtil(org.wso2.carbon.apimgt.persistence.utils.RegistryPersistenceUtil) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) Matchers.any(org.mockito.Matchers.any) IOUtils(org.apache.commons.io.IOUtils) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) MultitenantUtils(org.wso2.carbon.utils.multitenancy.MultitenantUtils) Assert.assertFalse(org.junit.Assert.assertFalse) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) QName(javax.xml.namespace.QName) CarbonConstants(org.wso2.carbon.CarbonConstants) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) APIStore(org.wso2.carbon.apimgt.api.model.APIStore) BlockConditionNotFoundException(org.wso2.carbon.apimgt.api.BlockConditionNotFoundException) APIStateChangeSimpleWorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.APIStateChangeSimpleWorkflowExecutor) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) UserRealm(org.wso2.carbon.user.api.UserRealm) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) RunWith(org.junit.runner.RunWith) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) Association(org.wso2.carbon.registry.core.Association) SuppressStaticInitializationFor(org.powermock.core.classloader.annotations.SuppressStaticInitializationFor) ArrayList(java.util.ArrayList) DocumentationContent(org.wso2.carbon.apimgt.api.model.DocumentationContent) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RegistryConstants(org.wso2.carbon.registry.core.RegistryConstants) WorkflowProperties(org.wso2.carbon.apimgt.impl.dto.WorkflowProperties) Resource(org.wso2.carbon.registry.core.Resource) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) Collection(org.wso2.carbon.registry.core.Collection) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) DuplicateAPIException(org.wso2.carbon.apimgt.api.model.DuplicateAPIException) Condition(org.wso2.carbon.apimgt.api.model.policy.Condition) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) ScopesDAO(org.wso2.carbon.apimgt.impl.dao.ScopesDAO) Field(java.lang.reflect.Field) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) File(java.io.File) Assert.assertNull(org.junit.Assert.assertNull) TreeMap(java.util.TreeMap) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyManagerHolder(org.wso2.carbon.apimgt.impl.factory.KeyManagerHolder) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) RegistryContext(org.wso2.carbon.registry.core.config.RegistryContext) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) APIProductMapper(org.wso2.carbon.apimgt.persistence.mapper.APIProductMapper) RegistryUtils(org.wso2.carbon.registry.core.utils.RegistryUtils) XMLInputFactory(javax.xml.stream.XMLInputFactory) PolicyConstants(org.wso2.carbon.apimgt.api.model.policy.PolicyConstants) APIPersistence(org.wso2.carbon.apimgt.persistence.APIPersistence) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline) BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) XMLStreamReader(javax.xml.stream.XMLStreamReader) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) GovernanceUtils(org.wso2.carbon.governance.api.util.GovernanceUtils) NotifierConstants(org.wso2.carbon.apimgt.impl.notification.NotifierConstants) NotificationExecutor(org.wso2.carbon.apimgt.impl.notification.NotificationExecutor) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) UUID(java.util.UUID) DocumentSourceType(org.wso2.carbon.apimgt.api.model.Documentation.DocumentSourceType) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) List(java.util.List) JSONObject(org.json.simple.JSONObject) DocumentVisibility(org.wso2.carbon.apimgt.api.model.Documentation.DocumentVisibility) Modifier(java.lang.reflect.Modifier) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) SortedMap(java.util.SortedMap) AssertNotNull(org.apache.synapse.unittest.testcase.data.classes.AssertNotNull) Provider(org.wso2.carbon.apimgt.api.model.Provider) Matchers(org.mockito.Matchers) UserStoreException(org.wso2.carbon.user.api.UserStoreException) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) HashMap(java.util.HashMap) HashSet(java.util.HashSet) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Property(org.wso2.carbon.governance.custom.lifecycles.checklist.util.Property) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) API(org.wso2.carbon.apimgt.api.model.API) ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) JavaUtils(org.apache.axis2.util.JavaUtils) NotificationDTO(org.wso2.carbon.apimgt.impl.notification.NotificationDTO) DEFAULT_DIALECT_URI(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever.DEFAULT_DIALECT_URI) OMElement(org.apache.axiom.om.OMElement) Tier(org.wso2.carbon.apimgt.api.model.Tier) UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) APIPublisher(org.wso2.carbon.apimgt.api.model.APIPublisher) WorkflowExecutorFactory(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) Assert.assertNotNull(org.junit.Assert.assertNotNull) CertificateManagerImpl(org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManagerImpl) Caching(javax.cache.Caching) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) GatewayArtifactsMgtDAO(org.wso2.carbon.apimgt.impl.dao.GatewayArtifactsMgtDAO) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ArtifactSaver(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.ArtifactSaver) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) Mockito(org.mockito.Mockito) OMException(org.apache.axiom.om.OMException) Ignore(org.junit.Ignore) Registry(org.wso2.carbon.registry.core.Registry) CheckListItem(org.wso2.carbon.governance.custom.lifecycles.checklist.util.CheckListItem) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) WorkflowStatus(org.wso2.carbon.apimgt.impl.workflow.WorkflowStatus) OASParserUtil(org.wso2.carbon.apimgt.impl.definitions.OASParserUtil) CORSConfiguration(org.wso2.carbon.apimgt.api.model.CORSConfiguration) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) LifecycleBeanPopulator(org.wso2.carbon.governance.custom.lifecycles.checklist.util.LifecycleBeanPopulator) InputStream(java.io.InputStream) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) HashMap(java.util.HashMap) GatewayArtifactsMgtDAO(org.wso2.carbon.apimgt.impl.dao.GatewayArtifactsMgtDAO) GatewayArtifactSynchronizerProperties(org.wso2.carbon.apimgt.impl.dto.GatewayArtifactSynchronizerProperties) ScopesDAO(org.wso2.carbon.apimgt.impl.dao.ScopesDAO) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) CertificateManagerImpl(org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManagerImpl) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) APIPersistence(org.wso2.carbon.apimgt.persistence.APIPersistence) Before(org.junit.Before)

Example 14 with ApiMgtDAO

use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetCustomApiFaultSequences.

@Test
public void testGetCustomApiFaultSequences() throws Exception {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    mockSequences(APIConstants.API_CUSTOM_FAULTSEQUENCE_LOCATION, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, apiId);
    List<String> sequenceList = apiProvider.getCustomApiFaultSequences(apiId);
    Assert.assertNotNull(sequenceList);
    Assert.assertEquals(1, sequenceList.size());
    // OMException when building OMElement
    PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
    apiProvider.getCustomApiFaultSequences(apiId);
    // org.wso2.carbon.registry.api.RegistryException
    ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenReturn(registry);
    Mockito.when(registry.resourceExists(APIUtil.getSequencePath(apiId, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT))).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
    String msg = "Error while processing the " + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT + " sequences of " + apiId + " in the registry";
    try {
        apiProvider.getCustomApiFaultSequences(apiId);
    } catch (APIManagementException e) {
        Assert.assertEquals(msg, e.getMessage());
    }
    // Registry Exception
    PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenThrow(RegistryException.class);
    String msg1 = "Error while retrieving registry for tenant -1";
    try {
        apiProvider.getCustomApiFaultSequences(apiId);
    } catch (APIManagementException e) {
        Assert.assertEquals(msg1, e.getMessage());
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) InputStream(java.io.InputStream) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) OMException(org.apache.axiom.om.OMException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with ApiMgtDAO

use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.

the class WorkflowUtils method sendNotificationAfterWFComplete.

public static void sendNotificationAfterWFComplete(WorkflowDTO workflowDTO, String wfType) throws APIManagementException {
    if (WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION.equalsIgnoreCase(wfType)) {
        String applicationId = workflowDTO.getWorkflowReference();
        int appId = Integer.parseInt(applicationId);
        ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
        Application application = apiMgtDAO.getApplicationById(appId);
        ApplicationWorkflowDTO appWFDto = (ApplicationWorkflowDTO) workflowDTO;
        appWFDto.setApplication(application);
        ApplicationEvent applicationEvent = new ApplicationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_CREATE.name(), appWFDto.getTenantId(), appWFDto.getTenantDomain(), appWFDto.getApplication().getId(), appWFDto.getApplication().getUUID(), appWFDto.getApplication().getName(), appWFDto.getApplication().getTokenType(), appWFDto.getApplication().getTier(), appWFDto.getApplication().getGroupId(), appWFDto.getApplication().getApplicationAttributes(), application.getSubscriber().getName());
        APIUtil.sendNotification(applicationEvent, APIConstants.NotifierType.APPLICATION.name());
    } else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION.equalsIgnoreCase(wfType)) {
        ApplicationWorkflowDTO appWFDto = (ApplicationWorkflowDTO) workflowDTO;
        ApplicationEvent applicationEvent = new ApplicationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_DELETE.name(), appWFDto.getTenantId(), appWFDto.getTenantDomain(), appWFDto.getApplication().getId(), appWFDto.getApplication().getUUID(), appWFDto.getApplication().getName(), appWFDto.getApplication().getTokenType(), appWFDto.getApplication().getTier(), appWFDto.getApplication().getGroupId(), appWFDto.getApplication().getApplicationAttributes(), "");
        APIUtil.sendNotification(applicationEvent, APIConstants.NotifierType.APPLICATION.name());
    } else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION.equalsIgnoreCase(wfType)) {
        SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
        SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
        SubscriptionEvent subscriptionEvent;
        if (sub.getApiId() != null) {
            subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_CREATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
        } else {
            subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_CREATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
        }
        APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
    } else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE.equalsIgnoreCase(wfType)) {
        SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
        SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
        SubscriptionEvent subscriptionEvent;
        if (sub.getApiId() != null) {
            subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
        } else {
            subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
        }
        APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
    } else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION.equalsIgnoreCase(wfType)) {
        SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
        SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
        SubscriptionEvent subscriptionEvent;
        if (sub.getApiId() != null) {
            subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
        } else {
            subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
        }
        APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
    } else if (WorkflowConstants.WF_TYPE_AM_API_STATE.equalsIgnoreCase(wfType) || WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE.equalsIgnoreCase(wfType)) {
        APIStateWorkflowDTO apiStateWFDto = (APIStateWorkflowDTO) workflowDTO;
        APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_LIFECYCLE_CHANGE.name(), apiStateWFDto.getTenantId(), apiStateWFDto.getTenantDomain(), apiStateWFDto.getApiName(), Integer.parseInt(apiStateWFDto.getWorkflowReference()), apiStateWFDto.getApiUUID(), apiStateWFDto.getApiVersion(), apiStateWFDto.getApiType(), apiStateWFDto.getApiContext(), apiStateWFDto.getApiProvider(), apiStateWFDto.getApiLCAction());
        APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
    } else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_PRODUCTION.equalsIgnoreCase(wfType)) {
        ApplicationRegistrationWorkflowDTO appRegWFDto = (ApplicationRegistrationWorkflowDTO) workflowDTO;
        ApplicationRegistrationEvent applicationRegistrationEvent = new ApplicationRegistrationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_REGISTRATION_CREATE.name(), appRegWFDto.getTenantId(), appRegWFDto.getTenantDomain(), appRegWFDto.getApplication().getId(), appRegWFDto.getApplication().getUUID(), appRegWFDto.getApplicationInfo().getClientId(), appRegWFDto.getApplication().getTokenType(), appRegWFDto.getKeyManager());
        APIUtil.sendNotification(applicationRegistrationEvent, APIConstants.NotifierType.APPLICATION_REGISTRATION.name());
    } else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_SANDBOX.equalsIgnoreCase(wfType)) {
        ApplicationRegistrationWorkflowDTO appRegWFDto = (ApplicationRegistrationWorkflowDTO) workflowDTO;
        ApplicationRegistrationEvent applicationRegistrationEvent = new ApplicationRegistrationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_REGISTRATION_CREATE.name(), appRegWFDto.getTenantId(), appRegWFDto.getTenantDomain(), appRegWFDto.getApplication().getId(), appRegWFDto.getApplication().getUUID(), appRegWFDto.getApplicationInfo().getClientId(), appRegWFDto.getApplication().getTokenType(), appRegWFDto.getKeyManager());
        APIUtil.sendNotification(applicationRegistrationEvent, APIConstants.NotifierType.APPLICATION_REGISTRATION.name());
    }
}
Also used : SubscriptionEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent) ApplicationRegistrationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO) ApplicationEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationEvent) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) ApplicationRegistrationEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationRegistrationEvent) APIEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIEvent) ApplicationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Application(org.wso2.carbon.apimgt.api.model.Application)

Aggregations

Test (org.junit.Test)197 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)197 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)106 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)82 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)80 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)53 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)52 API (org.wso2.carbon.apimgt.api.model.API)50 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)50 Application (org.wso2.carbon.apimgt.api.model.Application)39 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)37 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)36 Resource (org.wso2.carbon.registry.core.Resource)36 Matchers.anyString (org.mockito.Matchers.anyString)35 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)33 RealmService (org.wso2.carbon.user.core.service.RealmService)32 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)32 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)31 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)29 HashMap (java.util.HashMap)27