use of org.wso2.carbon.apimgt.gateway.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.gateway.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.gateway.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]);
}
use of org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class AbstractKeyManagerTestCase method buildFromJSONTest.
@Test
public void buildFromJSONTest() throws APIManagementException {
AbstractKeyManager keyManager = new AMDefaultKeyManagerImpl();
KeyManagerConnectorConfiguration keyManagerConnectorConfiguration = Mockito.mock(DefaultKeyManagerConnectorConfiguration.class);
ServiceReferenceHolder serviceReferenceHolder = PowerMockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getKeyManagerConnectorConfiguration(APIConstants.KeyManager.DEFAULT_KEY_MANAGER_TYPE)).thenReturn(keyManagerConnectorConfiguration);
// test with empty json payload
assertNotNull(keyManager.buildFromJSON(new OAuthApplicationInfo(), "{}"));
// test with valid json
String jsonPayload2 = "{ \"callbackUrl\": \"www.google.lk\", \"client_id\": \"XBPcXSfGK47WiEX7enchoP2Dcvga\"," + "\"client_secret\": \"4UD8VX8NaQMtrHCwqzI1tHJLPoca\", \"owner\": \"admin\", \"grantType\": \"password" + " refresh_token\", " + "\"validityPeriod\": \"3600\" }";
OAuthApplicationInfo oAuthApplicationInfo1 = keyManager.buildFromJSON(new OAuthApplicationInfo(), jsonPayload2);
assertEquals("XBPcXSfGK47WiEX7enchoP2Dcvga", oAuthApplicationInfo1.getClientId());
// test with invalid json
try {
keyManager.buildFromJSON(new OAuthApplicationInfo(), "{invalid}");
assertTrue(false);
} catch (APIManagementException e) {
assertEquals("Error occurred while parsing JSON String", e.getMessage());
}
// test with invalid additionalProperties
OAuthApplicationInfo applicationInfo = new OAuthApplicationInfo();
applicationInfo.addParameter("additionalProperties", "{invalid}");
try {
keyManager.buildFromJSON(applicationInfo, "{}");
fail();
} catch (APIManagementException e) {
assertEquals("Error while parsing the addition properties of OAuth application", e.getMessage());
}
}
use of org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class TestUtils method mockAPIMConfiguration.
public static void mockAPIMConfiguration() throws RegistryException, UserStoreException, XMLStreamException {
ServiceReferenceHolder sh = mockRegistryAndUserRealm(-1234);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
Map<String, Environment> apiGatewayEnvironments = new HashMap<String, Environment>();
Environment env1 = new Environment();
env1.setApiGatewayEndpoint("https://abc.com, http://abc.com");
apiGatewayEnvironments.put("PROD", env1);
// Mocking some commonly used configs
PowerMockito.when(amConfig.getApiGatewayEnvironments()).thenReturn(apiGatewayEnvironments);
PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_GATEWAY_TYPE)).thenReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE);
PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_PUBLISHER_ENABLE_API_DOC_VISIBILITY_LEVELS)).thenReturn("true", "false");
ThrottleProperties throttleProperties = new ThrottleProperties();
PowerMockito.when(amConfig.getThrottleProperties()).thenReturn(throttleProperties);
}
Aggregations