Search in sources :

Example 41 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class APIKeyValidationServiceTest method Init.

@Before
public void Init() throws Exception {
    System.setProperty(CARBON_HOME, "");
    privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
    apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    metricService = Mockito.mock(MetricService.class);
    org.wso2.carbon.metrics.manager.ServiceReferenceHolder serviceReferenceHolder1 = Mockito.mock(org.wso2.carbon.metrics.manager.ServiceReferenceHolder.class);
    Timer timer = Mockito.mock(Timer.class);
    Timer.Context timerContext = Mockito.mock(Timer.Context.class);
    MessageContext messageContext = Mockito.mock(MessageContext.class);
    OperationContext operationContext = Mockito.mock(OperationContext.class);
    MessageContext responseMessageContext = Mockito.mock(MessageContext.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(ApiMgtDAO.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(org.wso2.carbon.metrics.manager.ServiceReferenceHolder.class);
    PowerMockito.mockStatic(APIKeyMgtUtil.class);
    PowerMockito.mockStatic(MessageContext.class);
    PowerMockito.mockStatic(APIKeyMgtDataHolder.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    PowerMockito.when(privilegedCarbonContext.getUsername()).thenReturn(USER_NAME);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    PowerMockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
    PowerMockito.when(MessageContext.getCurrentMessageContext()).thenReturn(messageContext);
    PowerMockito.when(APIKeyMgtDataHolder.isJwtGenerationEnabled()).thenReturn(true);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_KEY_MANGER_VALIDATIONHANDLER_CLASS_NAME)).thenReturn(API_KEY_MANGER_VALIDATION_HANDLER_CLASS_NAME);
    Mockito.when(org.wso2.carbon.metrics.manager.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder1);
    Mockito.when(serviceReferenceHolder1.getMetricService()).thenReturn(metricService);
    Mockito.when(timer.start()).thenReturn(timerContext);
    Mockito.when(metricService.timer(Mockito.anyString(), Mockito.any(org.wso2.carbon.metrics.manager.Level.class))).thenReturn(timer);
    Mockito.when(messageContext.getOperationContext()).thenReturn(operationContext);
    Mockito.when(operationContext.getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE)).thenReturn(responseMessageContext);
    Map headers = new HashMap();
    headers.put("activityID", "1s2f2g4g5");
    Mockito.when(messageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(headers);
    String cacheKey = APIUtil.getAccessTokenCacheKey(ACCESS_TOKEN, API_CONTEXT, API_VERSION, "/*", "GET", REQUIRED_AUTHENTICATION_LEVEL);
    APIKeyValidationInfoDTO infoDTO = new APIKeyValidationInfoDTO();
    infoDTO.setApiPublisher(USER_NAME);
    infoDTO.setEndUserName(USER_NAME);
    PowerMockito.when(APIKeyMgtUtil.getFromKeyManagerCache(cacheKey)).thenReturn(infoDTO);
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) ServiceReferenceHolder(org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) HashMap(java.util.HashMap) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) MetricService(org.wso2.carbon.metrics.manager.MetricService) Timer(org.wso2.carbon.metrics.manager.Timer) MessageContext(org.apache.axis2.context.MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) APIKeyValidationInfoDTO(org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO) Before(org.junit.Before)

Example 42 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class OAuthTokenGeneratorTest method setup.

@Before
public void setup() throws ParseException, IOException, APIManagementException {
    PowerMockito.spy(TokenCache.class);
    tokenCache = TokenCache.getInstance();
    PowerMockito.when(TokenCache.getInstance()).thenReturn(tokenCache);
    PowerMockito.mockStatic(OAuthClient.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.isRedisEnabled()).thenReturn(false);
    latch = new CountDownLatch(1);
    // Initialize mock token response.
    mockTokenResponse = new TokenResponse();
    mockTokenResponse.setAccessToken("testAccessToken");
    mockTokenResponse.setTokenType("Bearer");
    PowerMockito.when(OAuthClient.generateToken(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.anyString(), Mockito.any(), Mockito.anyString())).thenReturn(mockTokenResponse);
    // Initialize properties of oAuthEndpoint object having common values.
    oAuthEndpoint = new OAuthEndpoint();
    oAuthEndpoint.setTokenApiUrl("testTokenURL");
    oAuthEndpoint.setClientId("testClientID");
    oAuthEndpoint.setClientSecret("decryptedClientSecret");
    JSONParser parser = new JSONParser();
    oAuthEndpoint.setCustomParameters((JSONObject) parser.parse("{}"));
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder) TokenResponse(org.wso2.carbon.apimgt.gateway.mediators.oauth.client.TokenResponse) OAuthEndpoint(org.wso2.carbon.apimgt.gateway.mediators.oauth.conf.OAuthEndpoint) JSONParser(org.json.simple.parser.JSONParser) CountDownLatch(java.util.concurrent.CountDownLatch) Before(org.junit.Before)

Example 43 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method init.

@Before
public void init() {
    timer = Mockito.mock(Timer.class);
    timer = Mockito.mock(Timer.class);
    context = Mockito.mock(Timer.Context.class);
    throttleEvaluator = Mockito.mock(ThrottleConditionEvaluator.class);
    accessInformation = Mockito.mock(AccessInformation.class);
    Mockito.when(timer.start()).thenReturn(context);
    verbInfoDTO = new ArrayList<>();
    verbInfo = new VerbInfoDTO();
    verbInfo.setHttpVerb(httpVerb);
    verbInfo.setRequestKey(apiContext + "/" + apiVersion + resourceUri + ":" + httpVerb);
    verbInfo.setThrottling(throttlingTier);
    verbInfoDTO.add(verbInfo);
    conditionGroupDTO = new ConditionGroupDTO();
    conditionGroupDTO.setConditionGroupId("_default");
    conditionGroupDTOs = new ConditionGroupDTO[1];
    conditionGroupDTOs[0] = conditionGroupDTO;
    apiLevelThrottleKey = apiContext + ":" + apiVersion;
    resourceLevelThrottleKey = apiContext + "/" + apiVersion + resourceUri + ":" + httpVerb;
    org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
    PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
    Mockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getExtensionListenerMap()).thenReturn(extensionListenerMap);
}
Also used : MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) AccessInformation(org.apache.synapse.commons.throttle.core.AccessInformation) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) Timer(org.wso2.carbon.metrics.manager.Timer) VerbInfoDTO(org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO) Before(org.junit.Before)

Example 44 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class APISecurityUtilsTestCase method testSetAuthenticationContext.

public void testSetAuthenticationContext() {
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    APIManagerConfiguration apiMgtConfig = Mockito.mock(APIManagerConfiguration.class);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfiguration()).thenReturn(apiMgtConfig);
    MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
    AuthenticationContext authenticationContext = Mockito.mock(AuthenticationContext.class);
    Mockito.when(authenticationContext.getKeyType()).thenReturn("keyType");
    APISecurityUtils.setAuthenticationContext(messageContext, authenticationContext, "abc");
    // test when caller token is not null
    Mockito.when(authenticationContext.getCallerToken()).thenReturn("callertoken");
    Mockito.when(messageContext.getProperty(APIConstants.API_KEY_TYPE)).thenReturn("keyType");
    // Axis2MessageContext axis2MessageContext = Mockito.mock(Axis2MessageContext.class);
    org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
    Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
    Map transportHeaders = new HashMap();
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
    APISecurityUtils.setAuthenticationContext(messageContext, authenticationContext, "abc");
    Assert.assertEquals(APISecurityUtils.getAuthenticationContext(messageContext).getCallerToken(), "callertoken");
    Assert.assertEquals("keyType", messageContext.getProperty(APIConstants.API_KEY_TYPE));
    // test for IllegalStateException
    String API_AUTH_CONTEXT = "__API_AUTH_CONTEXT";
    Mockito.when(authenticationContext.getCallerToken()).thenReturn("newCallerToken");
    Mockito.when(messageContext.getProperty(API_AUTH_CONTEXT)).thenReturn("abc");
    APISecurityUtils.setAuthenticationContext(messageContext, authenticationContext, "abc");
    Assert.assertEquals(APISecurityUtils.getAuthenticationContext(messageContext).getCallerToken(), "newCallerToken");
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) HashMap(java.util.HashMap) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 45 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class APIUtilTest method testGetMediationSequenceUuidCustomSequence.

@Test
public void testGetMediationSequenceUuidCustomSequence() throws Exception {
    APIIdentifier apiIdentifier = Mockito.mock(APIIdentifier.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
    Collection collection = Mockito.mock(Collection.class);
    String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
    String path = artifactPath + RegistryConstants.PATH_SEPARATOR + "custom" + RegistryConstants.PATH_SEPARATOR;
    Mockito.when(registry.get(eq(path))).thenReturn(collection);
    String[] childPaths = { "test" };
    Mockito.when(collection.getChildren()).thenReturn(childPaths);
    String expectedUUID = UUID.randomUUID().toString();
    InputStream sampleSequence = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("sampleSequence.xml").getFile());
    Resource resource = Mockito.mock(Resource.class);
    Mockito.when(registry.get(eq("test"))).thenReturn(resource);
    Mockito.when(resource.getContentStream()).thenReturn(sampleSequence);
    Mockito.when(resource.getUUID()).thenReturn(expectedUUID);
    String actualUUID = APIUtil.getMediationSequenceUuid("sample", 1, "custom", apiIdentifier);
    Assert.assertEquals(expectedUUID, actualUUID);
    sampleSequence.close();
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Collection(org.wso2.carbon.registry.core.Collection) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)109 Test (org.junit.Test)105 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)102 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)64 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)63 RealmService (org.wso2.carbon.user.core.service.RealmService)53 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)49 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)47 Resource (org.wso2.carbon.registry.core.Resource)40 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)38 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)30 InputStream (java.io.InputStream)29 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)28 Before (org.junit.Before)27 API (org.wso2.carbon.apimgt.api.model.API)24 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)23 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)22 HashMap (java.util.HashMap)21 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)21 QName (javax.xml.namespace.QName)19