use of org.wso2.carbon.apimgt.throttle.policy.deployer.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class ServiceReferenceHolderTestCase method testGetRestAPIConfigurationMap.
@Test
public void testGetRestAPIConfigurationMap() throws ConfigurationException {
// // Happy Path
ServiceReferenceHolder instance = ServiceReferenceHolder.getInstance();
ConfigProvider configProvider = Mockito.mock(ConfigProvider.class);
instance.setConfigProvider(configProvider);
Map<String, String> expectedConfigMap = new HashMap<>();
expectedConfigMap.put("apimBaseUrl", "https://localhost:9443/");
Mockito.when(configProvider.getConfigurationObject("xxx-namespace-xxx")).thenReturn(expectedConfigMap);
Map<String, String> actualConfigMap = instance.getRestAPIConfigurationMap("xxx-namespace-xxx");
Assert.assertEquals(expectedConfigMap, actualConfigMap);
// // Error path
// // ConfigurationException
Mockito.when(configProvider.getConfigurationObject("xxx-namespace-xxx")).thenThrow(ConfigurationException.class);
actualConfigMap = instance.getRestAPIConfigurationMap("xxx-namespace-xxx");
Assert.assertNull(actualConfigMap);
// // config provider is null
instance.setConfigProvider(null);
actualConfigMap = instance.getRestAPIConfigurationMap("xxx-namespace-xxx");
Assert.assertNull(actualConfigMap);
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class ServiceReferenceHolderTestCase method testGetEnvironmentConfigurations.
@Test
public void testGetEnvironmentConfigurations() {
ServiceReferenceHolder instance = ServiceReferenceHolder.getInstance();
// //Happy Path
ConfigProvider configProvider = Mockito.mock(ConfigProvider.class);
instance.setConfigProvider(configProvider);
APIMUIConfigurations apimUIConfigurations = instance.getApimUIConfigurations();
Assert.assertNotNull(apimUIConfigurations);
// //ConfigProvider is null
instance.setConfigProvider(null);
apimUIConfigurations = instance.getApimUIConfigurations();
Assert.assertNotNull(apimUIConfigurations);
// //CarbonConfigurationException when reading configs
configProvider = new ConfigProvider() {
@Override
public <T> T getConfigurationObject(Class<T> configClass) throws ConfigurationException {
throw new ConfigurationException("Error while creating configuration instance");
}
@Override
public Object getConfigurationObject(String namespace) throws ConfigurationException {
throw new ConfigurationException("Error while creating configuration instance");
}
public <T> T getConfigurationObject(String s, Class<T> aClass) throws ConfigurationException {
return null;
}
};
instance.setConfigProvider(configProvider);
apimUIConfigurations = instance.getApimUIConfigurations();
Assert.assertNotNull(apimUIConfigurations);
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class CEPPolicyManagementServiceClientTest method testShouldInitializeServiceClient.
@Test
public void testShouldInitializeServiceClient() throws AxisFault {
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ConfigurationContextFactory.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
APIManagerConfiguration config = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService configService = Mockito.mock(APIManagerConfigurationService.class);
ConfigurationContextFactory configFactory = Mockito.mock(ConfigurationContextFactory.class);
ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(configService);
Mockito.when(configService.getAPIManagerConfiguration()).thenReturn(config);
Mockito.when(config.getFirstProperty(Mockito.anyString())).thenReturn("").thenReturn(USER_NAME).thenReturn("").thenReturn(USER_NAME).thenReturn(null).thenReturn(USER_NAME);
Mockito.when(configFactory.createConfigurationContextFromFileSystem(null, null)).thenReturn(configurationContext).thenThrow(AxisFault.class).thenReturn(configurationContext);
try {
new CEPPolicyManagementServiceClient();
} catch (APIManagementException e) {
Assert.fail("Should not throw an exception");
}
try {
// should throw exception this time
new CEPPolicyManagementServiceClient();
Assert.fail("Should throw an exception");
} catch (APIManagementException e) {
}
try {
// should throw exception this time
new CEPPolicyManagementServiceClient();
Assert.fail("Should throw an exception");
} catch (APIManagementException e) {
}
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class DefaultGroupIDExtractorImplTest method getGroupingIdentifierListTestCase.
@Test
public void getGroupingIdentifierListTestCase() throws UserStoreException {
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
RealmService realmService = Mockito.mock(RealmService.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(tenantManager.getTenantId("carbon.super")).thenReturn(-1234);
Mockito.when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
Mockito.when(userStoreManager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername("user"), "http://wso2.org/claims/organization", null)).thenReturn("org1,org2,org3");
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_STORE_GROUP_EXTRACTOR_CLAIM_URI)).thenReturn("http://wso2.org/claims/organization");
DefaultGroupIDExtractorImpl defaultGroupIDExtractor = new DefaultGroupIDExtractorImpl();
String[] expectedArr = new String[] { "org1", "org2", "org3" };
Assert.assertEquals(expectedArr[0], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":true}")[0]);
Assert.assertEquals(expectedArr[1], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":true}")[1]);
Assert.assertEquals(expectedArr[2], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":true}")[2]);
Assert.assertEquals(expectedArr[0], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":false}")[0]);
Mockito.when(userStoreManager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername("user"), "http://wso2.org/claims/organization", null)).thenReturn("org1|org2|org3");
Assert.assertEquals("org1|org2|org3", defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":false}")[0]);
Mockito.when(userStoreManager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername("user"), "http://wso2.org/claims/organization", null)).thenReturn(null);
Assert.assertEquals(0, defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", " + "\"isSuperTenant\":false}").length);
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class SAMLGroupIDExtractorImplTest method getGroupingIdentifierListTestCase.
@Test
public void getGroupingIdentifierListTestCase() throws ParserConfigurationException, IOException, SAXException, UnmarshallingException, UserStoreException {
String claim = "http://wso2.org/claims/organization";
String organizationValue = "organization";
SAMLGroupIDExtractorImpl samlGroupIDExtractor = new SAMLGroupIDExtractorImplWrapper();
Mockito.when(DocumentBuilderFactory.newInstance()).thenReturn(documentBuilderFactory);
Mockito.when(documentBuilderFactory.newDocumentBuilder()).thenReturn(documentBuilder);
Mockito.when(documentBuilder.parse(samlGroupIDExtractor.getByteArrayInputStream("test"))).thenReturn(document);
Mockito.when(document.getDocumentElement()).thenReturn(element);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.mockStatic(XMLObjectProviderRegistrySupport.class);
Response response = Mockito.mock(Response.class);
List<Assertion> assertion = new ArrayList();
Subject subject = Mockito.mock(Subject.class);
NameID nameID = Mockito.mock(NameID.class);
Assertion assertion1 = Mockito.mock(Assertion.class);
assertion.add(assertion1);
Mockito.when(XMLObjectProviderRegistrySupport.getUnmarshallerFactory()).thenReturn(unmarshallerFactory);
Mockito.when(unmarshallerFactory.getUnmarshaller(element)).thenReturn(unmarshaller);
Mockito.when(unmarshaller.unmarshall(element)).thenReturn(response);
Mockito.when(response.getAssertions()).thenReturn(assertion);
Mockito.when(assertion.get(0).getSubject()).thenReturn(subject);
Mockito.when(subject.getNameID()).thenReturn(nameID);
Mockito.when(nameID.getValue()).thenReturn("user");
System.setProperty(APIConstants.READ_ORGANIZATION_FROM_SAML_ASSERTION, "true");
APIManagerConfigurationService apiManagerConfigService = Mockito.mock(APIManagerConfigurationService.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigService);
APIManagerConfiguration apiManagerConfig = Mockito.mock(APIManagerConfiguration.class);
Mockito.when(apiManagerConfigService.getAPIManagerConfiguration()).thenReturn(apiManagerConfig);
Mockito.when(apiManagerConfig.getFirstProperty(APIConstants.API_STORE_GROUP_EXTRACTOR_CLAIM_URI)).thenReturn("http://wso2.org/claims/organization");
System.setProperty("carbon.home", "");
PrivilegedCarbonContext carbonContext;
carbonContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()).thenReturn(-1234);
PowerMockito.doNothing().when(carbonContext).setTenantDomain("carbon.super", true);
AttributeStatement mockAttributeStatement = PowerMockito.mock(AttributeStatement.class);
List<AttributeStatement> attributeStatementList = Collections.singletonList(mockAttributeStatement);
PowerMockito.when(assertion1.getAttributeStatements()).thenReturn(attributeStatementList);
Attribute mockAttribute = PowerMockito.mock(Attribute.class);
List<Attribute> attributesList = Collections.singletonList(mockAttribute);
PowerMockito.when(mockAttributeStatement.getAttributes()).thenReturn(attributesList);
XMLObject rawAttribute = PowerMockito.mock(XMLObject.class);
PowerMockito.when(rawAttribute.toString()).thenReturn(organizationValue);
List<XMLObject> mockedAttributeValues = Collections.singletonList(rawAttribute);
AttributedStringImpl mockedAttributedStringImpl = new AttributedStringImpl("nameSpaceURI", "elementLocalName", "namespacePrefix");
String sampleAttrValue = "MockedAuthParamSampleAttribute";
mockedAttributedStringImpl.setValue(sampleAttrValue);
List<XMLObject> mockedXSSAttributeValues = Collections.singletonList((XMLObject) mockedAttributedStringImpl);
XSAnyImpl mockedXSAnyImpl = Mockito.mock(XSAnyImpl.class);
PowerMockito.when(mockedXSAnyImpl.getTextContent()).thenReturn(sampleAttrValue);
List<XMLObject> mockedXSAnyImplAttributeValues = Collections.singletonList((XMLObject) mockedXSAnyImpl);
List<XMLObject> multiMockedAttributeValues = Arrays.asList(rawAttribute, PowerMockito.mock(XMLObject.class));
AuthenticatorsConfiguration.AuthenticatorConfig mockedAuthenticatorConfig = Mockito.mock(AuthenticatorsConfiguration.AuthenticatorConfig.class);
PowerMockito.when(mockAttribute.getAttributeValues()).thenReturn(mockedAttributeValues, multiMockedAttributeValues, mockedXSSAttributeValues, mockedXSAnyImplAttributeValues);
PowerMockito.mockStatic(AuthenticatorsConfiguration.class);
AuthenticatorsConfiguration mockedAuthenticatorsConfiguration = PowerMockito.mock(AuthenticatorsConfiguration.class);
PowerMockito.when(AuthenticatorsConfiguration.getInstance()).thenReturn(mockedAuthenticatorsConfiguration);
Map<String, String> mockedConfigParameters = new HashMap<String, String>();
mockedConfigParameters.put(APIConstants.ORGANIZATION_CLAIM_ATTRIBUTE, claim);
PowerMockito.when(mockedAuthenticatorConfig.getParameters()).thenReturn(mockedConfigParameters);
PowerMockito.when(mockedAuthenticatorsConfiguration.getAuthenticatorConfig(APIConstants.SAML2_SSO_AUTHENTICATOR_NAME)).thenReturn(mockedAuthenticatorConfig);
PowerMockito.when(mockAttribute.getName()).thenReturn(claim);
String[] organizations = samlGroupIDExtractor.getGroupingIdentifierList("test");
Assert.assertEquals(organizationValue, organizations[0]);
}
Aggregations