Search in sources :

Example 31 with ApiMgtDAO

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

the class APIProviderImplTest method testUpdateBlockConditionByUUID.

@Test
public void testUpdateBlockConditionByUUID() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    Mockito.when(apimgtDAO.updateBlockConditionStateByUUID("testID", "testState")).thenReturn(false, true);
    BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO();
    blockConditionsDTO.setConditionType("testType");
    blockConditionsDTO.setConditionValue("USER");
    Mockito.when(apimgtDAO.getBlockConditionByUUID("testState")).thenReturn(blockConditionsDTO);
    // updateState false
    assertFalse(apiProvider.updateBlockConditionByUUID("testID", "testState"));
    // updateState true
    assertTrue(apiProvider.updateBlockConditionByUUID("testID", "testState"));
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 32 with ApiMgtDAO

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

the class APIProviderImplTest method testGetCustomInSequences1.

@Test
public void testGetCustomInSequences1() throws Exception {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    mockSequences(APIConstants.API_CUSTOM_INSEQUENCE_LOCATION, APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN, apiId);
    List<String> sequenceList = apiProvider.getCustomInSequences();
    Assert.assertNotNull(sequenceList);
    Assert.assertEquals(1, sequenceList.size());
    // OMException when building OMElement
    PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
    apiProvider.getCustomOutSequences(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(APIConstants.API_CUSTOM_INSEQUENCE_LOCATION)).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
    String msg = "Error while processing the in in the registry";
    try {
        apiProvider.getCustomInSequences();
    } 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.getCustomInSequences();
    } 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 33 with ApiMgtDAO

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

the class APIProviderImplTest method testIsSynapseGateway.

@Test
public void testIsSynapseGateway() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
    Mockito.when(sh.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getFirstProperty(Mockito.anyString())).thenReturn("synapse");
    assertTrue(apiProvider.isSynapseGateway());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 34 with ApiMgtDAO

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

the class APIProviderImplTest method testAddFileToDocumentation.

/**
 * This method tests adding file to documentation method.
 * @throws Exception
 *
 * @throws GovernanceException    Governance Exception.
 */
@Test
public void testAddFileToDocumentation() throws Exception {
    String apiUUID = "xxxxxxxx";
    String docUUID = "yyyyyyyy";
    APIIdentifier identifier = new APIIdentifier("admin-AT-carbon.super", "API1", "1.0.0");
    Set<String> environments = new HashSet<String>();
    Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
    Tier tier = new Tier("Gold");
    Map<String, Tier> tiers = new TreeMap<>();
    tiers.put("Gold", tier);
    URITemplate uriTemplate1 = new URITemplate();
    uriTemplate1.setHTTPVerb("POST");
    uriTemplate1.setAuthType("Application");
    uriTemplate1.setUriTemplate("/add");
    uriTemplate1.setThrottlingTier("Gold");
    uriTemplates.add(uriTemplate1);
    final API api = new API(identifier);
    api.setStatus(APIConstants.CREATED);
    api.setVisibility("public");
    api.setAccessControl("all");
    api.setTransports("http,https");
    api.setContext("/test");
    api.setEnvironments(environments);
    api.setUriTemplates(uriTemplates);
    api.setOrganization("carbon.super");
    List<Documentation> documentationList = getDocumentationList();
    final APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, documentationList, null);
    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);
    Mockito.when(APIUtil.getTiers(APIConstants.TIER_RESOURCE_TYPE, "carbon.super")).thenReturn(tiers);
    Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
    Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    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);
    String fileName = "test.txt";
    String contentType = "application/force-download";
    Documentation doc = new Documentation(DocumentationType.HOWTO, fileName);
    doc.setSourceType(DocumentSourceType.FILE);
    PowerMockito.when(APIUtil.getDocumentationFilePath(api.getId(), fileName)).thenReturn("filePath");
    InputStream inputStream = Mockito.mock(InputStream.class);
    // apiProvider.addFileToDocumentation(api.getId(), doc, fileName, inputStream, contentType);
    DocumentationContent content = new DocumentationContent();
    ResourceFile resourceFile = new ResourceFile(inputStream, contentType);
    content.setResourceFile(resourceFile);
    apiProvider.addDocumentationContent(apiUUID, docUUID, "carbon.super", content);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) Tier(org.wso2.carbon.apimgt.api.model.Tier) QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) TreeMap(java.util.TreeMap) DocumentationContent(org.wso2.carbon.apimgt.api.model.DocumentationContent) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) 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 35 with ApiMgtDAO

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

the class APIProviderImplTest method testGetBlockConditionByUUID.

@Test
public void testGetBlockConditionByUUID() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO();
    Mockito.when(apimgtDAO.getBlockConditionByUUID("testUUID")).thenReturn(blockConditionsDTO);
    // Normal Path
    assertNotNull(apiProvider.getBlockConditionByUUID("testUUID"));
    Mockito.when(apimgtDAO.getBlockConditionByUUID("testUUID")).thenThrow(BlockConditionNotFoundException.class);
    // BlockConditionNotFound exception
    try {
        assertNull(apiProvider.getBlockConditionByUUID("testUUID"));
    } catch (APIManagementException e) {
        Assert.assertEquals(null, e.getMessage());
    }
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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