use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class TestUtils method initConfigurationContextService.
/**
* To initialize the (@link {@link ConfigurationContextService}} for testing.
*
* @return initialized configuration context service.
*/
public static ConfigurationContextService initConfigurationContextService(boolean initAPIMConfigurationService) throws XMLStreamException {
ConfigurationContextService configurationContextService = Mockito.mock(ConfigurationContextService.class);
ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
AxisConfiguration axisConfiguration = Mockito.mock(AxisConfiguration.class);
Mockito.doReturn(axisConfiguration).when(configurationContext).getAxisConfiguration();
TransportInDescription transportInDescription = Mockito.mock(TransportInDescription.class);
Mockito.doReturn(transportInDescription).when(axisConfiguration).getTransportIn(Mockito.anyString());
Parameter dynamicSSLProfilesConfigParameter = Mockito.mock(Parameter.class);
Mockito.when(dynamicSSLProfilesConfigParameter.getParameterElement()).thenReturn(getDynamicSSLElement());
Mockito.when(transportInDescription.getParameter("dynamicSSLProfilesConfig")).thenReturn(dynamicSSLProfilesConfigParameter);
TransportOutDescription transportOutDescription = Mockito.mock(TransportOutDescription.class);
Mockito.doReturn(transportOutDescription).when(axisConfiguration).getTransportOut(Mockito.anyString());
Mockito.when(transportOutDescription.getParameter("dynamicSSLProfilesConfig")).thenReturn(dynamicSSLProfilesConfigParameter);
Mockito.doReturn(configurationContext).when(configurationContextService).getServerConfigContext();
if (initAPIMConfigurationService) {
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
Mockito.doReturn("true").when(apiManagerConfiguration).getFirstProperty(APIConstants.ENABLE_MTLS_FOR_APIS);
Mockito.doReturn(apiManagerConfiguration).when(apiManagerConfigurationService).getAPIManagerConfiguration();
ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(apiManagerConfigurationService);
}
ServiceReferenceHolder.setContextService(configurationContextService);
System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
System.setProperty("javax.net.ssl.trustStore", ".");
return configurationContextService;
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService 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.impl.APIManagerConfigurationService 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.impl.APIManagerConfigurationService 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]);
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetDeniedTiers.
@Test
public void testGetDeniedTiers() throws APIManagementException, org.wso2.carbon.user.core.UserStoreException {
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getFirstProperty(Mockito.anyString())).thenReturn("true", "false");
APIConsumerImpl apiConsumer = new UserAwareAPIConsumerWrapper(userRegistry, apiMgtDAO);
Mockito.when(userRegistry.getUserRealm()).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
String[] currentUserRoles = { "role1", "role2" };
PowerMockito.when(APIUtil.getListOfRoles(Mockito.anyString())).thenReturn(currentUserRoles);
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
TierPermissionDTO tierPermissionDTO = new TierPermissionDTO();
TierPermissionDTO tierPermissionDTO1 = new TierPermissionDTO();
tierPermissionDTO.setRoles(new String[] { "role1" });
Set<TierPermissionDTO> tierPermissionDTOs = new HashSet<TierPermissionDTO>();
tierPermissionDTOs.add(tierPermissionDTO);
Mockito.when(apiMgtDAO.getThrottleTierPermissions(Mockito.anyInt())).thenReturn(tierPermissionDTOs);
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 1);
tierPermissionDTO.setRoles(new String[] { "role3" });
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
Mockito.when(apiMgtDAO.getTierPermissions(Mockito.anyInt())).thenReturn(tierPermissionDTOs);
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
tierPermissionDTO.setPermissionType(APIConstants.TIER_PERMISSION_ALLOW);
Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[0]);
tierPermissionDTOs.add(tierPermissionDTO1);
tierPermissionDTO1.setRoles(new String[] { "role4" });
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 1);
Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[] { "role1", "role2" });
tierPermissionDTO1.setRoles(new String[] { "role2" });
tierPermissionDTO1.setTierName("Silver");
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 2);
}
Aggregations