Search in sources :

Example 91 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class APIMgtGoogleAnalyticsUtilsTestCase method testPublishGATrackingData.

@Test
public void testPublishGATrackingData() {
    APIMgtGoogleAnalyticsUtils apiMgtGoogleAnalyticsUtils = new APIMgtGoogleAnalyticsUtils();
    GoogleAnalyticsData.DataBuilder dataBuilder = Mockito.mock(GoogleAnalyticsData.DataBuilder.class);
    // test when gaConfig == null
    apiMgtGoogleAnalyticsUtils.publishGATrackingData(dataBuilder, "abc", "567r637r6");
    // test when gaConfig !=  null
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    Resource resource = Mockito.mock(Resource.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    try {
        Mockito.when(resource.getContentStream()).thenReturn(new ByteArrayInputStream(Charset.forName("UTF-16").encode("<GoogleAnalyticsTracking>\n" + "\t<!--Enable/Disable Google Analytics Tracking -->\n" + "\t<Enabled>false</Enabled>\n" + "\n" + "\t<!-- Google Analytics Tracking ID -->\n" + "\t<TrackingID>UA-XXXXXXXX-X</TrackingID>\n" + "\n" + "</GoogleAnalyticsTracking>").array()));
        Mockito.when(userRegistry.get("/apimgt/statistics/ga-config.xml")).thenReturn(resource);
    } catch (RegistryException e) {
        fail("RegistryException is thrown when tesing .");
        e.printStackTrace();
    }
    try {
        Mockito.when(registryService.getGovernanceSystemRegistry()).thenReturn(userRegistry);
    } catch (RegistryException e) {
        fail("RegistryException is thrown.");
    }
    apiMgtGoogleAnalyticsUtils.init("abc.com");
    // test when gaconfig.enabled=false
    apiMgtGoogleAnalyticsUtils.publishGATrackingData(dataBuilder, "ishara", "jhgy");
    // test when gaconfig.enabled=true annonymous
    try {
        Mockito.when(resource.getContentStream()).thenReturn(new ByteArrayInputStream(Charset.forName("UTF-16").encode("<GoogleAnalyticsTracking>\n" + "\t<!--Enable/Disable Google Analytics Tracking -->\n" + "\t<Enabled>true</Enabled>\n" + "\n" + "\t<!-- Google Analytics Tracking ID -->\n" + "\t<TrackingID>UA-XXXXXXXX-X</TrackingID>\n" + "\n" + "</GoogleAnalyticsTracking>").array()));
        apiMgtGoogleAnalyticsUtils.init("abc.com");
        GoogleAnalyticsData.DataBuilder dataBuilder1 = Mockito.mock(GoogleAnalyticsData.DataBuilder.class);
        Mockito.when(dataBuilder.setProtocolVersion("1")).thenReturn(dataBuilder);
        Mockito.when(dataBuilder.setTrackingId("UA-XXXXXXXX-X")).thenReturn(dataBuilder);
        Mockito.when(dataBuilder.setClientId("0x05a823c101178dd5")).thenReturn(dataBuilder);
        Mockito.when(dataBuilder.setHitType("pageview")).thenReturn(dataBuilder);
        GoogleAnalyticsData data = Mockito.mock(GoogleAnalyticsData.class);
        Mockito.when(dataBuilder.build()).thenReturn(data);
        PowerMockito.mockStatic(GoogleAnalyticsDataPublisher.class);
        PowerMockito.when(GoogleAnalyticsDataPublisher.buildPayloadString(data)).thenReturn("payload");
    } catch (RegistryException e) {
        fail(e.getMessage());
    }
    apiMgtGoogleAnalyticsUtils.publishGATrackingData(dataBuilder, "ishara", "Autorization ishara");
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) GoogleAnalyticsData(org.wso2.carbon.ganalytics.publisher.GoogleAnalyticsData) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 92 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class GatewayUtilsTestCase method testSetRegistryProperty.

@Test
public void testSetRegistryProperty() throws RegistryException {
    try {
        GatewayUtils.setRegistryProperty(propertyName, propertyValue, path, tenantDomain);
    } catch (APIManagementException e) {
        fail("APIManagementException occurred. " + e.getStackTrace());
    }
    try {
        Mockito.when(userRegistry.get(path)).thenThrow(new RegistryException(""));
        GatewayUtils.setRegistryProperty(propertyName, propertyValue, path, tenantDomain);
        fail("Expected Registry Exception is not thrown");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().startsWith("Error while reading registry resource"));
    }
    try {
        Mockito.when(resource.getProperty(propertyName)).thenReturn("oldValue");
        GatewayUtils.setRegistryProperty(propertyName, propertyValue, path, "");
        fail("expected APIManagementException is not thrown when accessing registry");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().startsWith("Error while reading registry resource"));
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 93 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class GatewayUtilsTestCase method setup.

@Before
public void setup() {
    System.setProperty("carbon.home", "jhkjn");
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(RegistryServiceHolder.class);
    PowerMockito.mockStatic(SubscriptionDataHolder.class);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    RegistryServiceHolder registryServiceHolder = Mockito.mock(RegistryServiceHolder.class);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    userRegistry = Mockito.mock(UserRegistry.class);
    resource = Mockito.mock(Resource.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    PowerMockito.when(RegistryServiceHolder.getInstance()).thenReturn(registryServiceHolder);
    Mockito.when(registryServiceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(privilegedCarbonContext.getTenantId()).thenReturn(tenantID);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn("carbon.super");
    try {
        Mockito.when(registryService.getConfigSystemRegistry(tenantID)).thenReturn(userRegistry);
    } catch (RegistryException e) {
        fail("Error while mocking registryService.getConfigSystemRegistry");
    }
    try {
        Mockito.when(userRegistry.get(path)).thenReturn(resource);
    } catch (RegistryException e) {
        fail("Error while mocking userRegistry.get(path)");
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder) RegistryServiceHolder(org.wso2.carbon.mediation.registry.RegistryServiceHolder) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Before(org.junit.Before)

Example 94 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class NewAPIVersionEmailNotifierTest method testShouldLoadMessageTemplateWhenDtoValid.

@Test
public void testShouldLoadMessageTemplateWhenDtoValid() throws RegistryException {
    Resource resource = Mockito.mock(Resource.class);
    NewAPIVersionEmailNotifier emailNotifier = new NewAPIVersionEmailNotifier() {

        @Override
        protected Registry getConfigSystemRegistry(int tenantId) throws RegistryException {
            return registry;
        }
    };
    Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true).thenReturn(false);
    Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
    Mockito.when(resource.getContent()).thenReturn("<html>$1-$2-$3</html>".getBytes());
    try {
        NotificationDTO notification = emailNotifier.loadMessageTemplate(notificationDTO);
        Assert.assertFalse(!notification.getMessage().contains(API_NAME));
        // should use dto template if resource not found in registry
        notification = emailNotifier.loadMessageTemplate(notificationDTO);
        Assert.assertFalse(!notification.getMessage().contains(API_NAME));
    } catch (NotificationException e) {
        Assert.fail("Should not throw any exceptions");
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) Test(org.junit.Test)

Example 95 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class XMLIndexerTest method testShouldReturnIndexedDocumentWhenParameterCorrect.

@Test
public void testShouldReturnIndexedDocumentWhenParameterCorrect() throws RegistryException {
    String mediaType = "text/xml";
    final String MEDIA_TYPE = "mediaType";
    AsyncIndexer.File2Index file2Index = new AsyncIndexer.File2Index("".getBytes(), null, "", -1234, "");
    XMLIndexer indexer = new XMLIndexer();
    // should return the the default media type when media type is not defined in file2Index
    IndexDocument xml = indexer.getIndexedDocument(file2Index);
    if (xml.getFields().get(MEDIA_TYPE) != null) {
        Assert.fail();
    }
    // should return the media type we have set in the file2Index
    file2Index.mediaType = mediaType;
    xml = indexer.getIndexedDocument(file2Index);
    if (!mediaType.equals(xml.getFields().get(MEDIA_TYPE).get(0))) {
        Assert.fail();
    }
}
Also used : IndexDocument(org.wso2.carbon.registry.indexing.solr.IndexDocument) AsyncIndexer(org.wso2.carbon.registry.indexing.AsyncIndexer) Test(org.junit.Test)

Aggregations

RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)235 Resource (org.wso2.carbon.registry.core.Resource)196 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)167 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)145 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)104 Registry (org.wso2.carbon.registry.core.Registry)95 Test (org.junit.Test)81 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)81 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)75 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)67 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)61 UserStoreException (org.wso2.carbon.user.api.UserStoreException)60 API (org.wso2.carbon.apimgt.api.model.API)58 IOException (java.io.IOException)57 ArrayList (java.util.ArrayList)55 QName (javax.xml.namespace.QName)42 APIResource (org.wso2.carbon.apimgt.api.doc.model.APIResource)40 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)40 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)40