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");
}
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"));
}
}
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)");
}
}
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");
}
}
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();
}
}
Aggregations