use of org.wso2.carbon.identity.core.bean.context.MessageContext in project carbon-apimgt by wso2.
the class GatewayUtils method getStatus.
public static String getStatus(org.apache.synapse.MessageContext messageContext) {
Object status = messageContext.getProperty(APIMgtGatewayConstants.API_STATUS);
if (status != null) {
return (String) status;
}
API api = getAPI(messageContext);
if (api != null) {
String apiStatus = api.getStatus();
messageContext.setProperty(APIMgtGatewayConstants.API_STATUS, apiStatus);
return apiStatus;
}
return null;
}
use of org.wso2.carbon.identity.core.bean.context.MessageContext in project carbon-apimgt by wso2.
the class APIKeyValidatorTestCase method testGetVerbInfoDTOFromAPIDataWithRequestPath.
@Test
public void testGetVerbInfoDTOFromAPIDataWithRequestPath() throws Exception {
String context = "/";
String apiVersion = "1.0";
String requestPath = "/menu";
String httpMethod = "GET";
VerbInfoDTO verbDTO = getDefaultVerbInfoDTO();
verbDTO.setRequestKey("//1.0/:https");
APIKeyValidator apiKeyValidator = createAPIKeyValidator(true, getDefaultURITemplates("/menu", "GET"), verbDTO);
// If isAPIResourceValidationEnabled==true
apiKeyValidator.setGatewayAPIResourceValidationEnabled(true);
PowerMockito.mockStatic(Cache.class);
Cache cache = Mockito.mock(Cache.class);
PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIManagerConfigurationService.class);
PowerMockito.mockStatic(CacheProvider.class);
org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
final APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
PowerMockito.when(cacheProvider.getDefaultCacheTimeout()).thenReturn((long) 900);
Mockito.when(CacheProvider.getResourceCache()).thenReturn(cache);
VerbInfoDTO verbInfoDTO1 = new VerbInfoDTO();
verbInfoDTO1.setHttpVerb("get");
Mockito.when(APIUtil.getAPIInfoDTOCacheKey("", "1.0")).thenReturn("abc");
Mockito.when((VerbInfoDTO) CacheProvider.getResourceCache().get("abc")).thenReturn(verbInfoDTO1);
MessageContext messageContext = Mockito.mock(MessageContext.class);
Assert.assertEquals("", verbDTO, apiKeyValidator.getVerbInfoDTOFromAPIData(messageContext, context, apiVersion, requestPath, httpMethod));
}
use of org.wso2.carbon.identity.core.bean.context.MessageContext in project carbon-apimgt by wso2.
the class APIKeyValidatorTestCase method testFindMatchingVerbFails.
@Test
public void testFindMatchingVerbFails() {
MessageContext synCtx = Mockito.mock(Axis2MessageContext.class);
Mockito.when(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION_STRATEGY)).thenReturn(null);
Mockito.when(synCtx.getProperty(APIConstants.API_RESOURCE_CACHE_KEY)).thenReturn("xyz");
Mockito.when(synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH)).thenReturn("abc");
Mockito.when(synCtx.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("");
Mockito.when(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0");
org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
Mockito.when(((Axis2MessageContext) synCtx).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
SynapseConfiguration synapseConfiguration = Mockito.mock(SynapseConfiguration.class);
Mockito.when(synapseConfiguration.getAPI("abc")).thenReturn(new API("abc", "/"));
Mockito.when(synCtx.getConfiguration()).thenReturn(synapseConfiguration);
Mockito.when(synCtx.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
Mockito.when(synCtx.getProperty(APIConstants.API_ELECTED_RESOURCE)).thenReturn("/menu");
VerbInfoDTO verbInfoDTO = getDefaultVerbInfoDTO();
APIKeyValidator apiKeyValidator = createAPIKeyValidator(true, getDefaultURITemplates("/wrong-resource", "GET"), verbInfoDTO);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.GATEWAY_RESOURCE_CACHE_ENABLED)).thenReturn("true");
try {
// Test for matching verb is Not found path
PowerMockito.mockStatic(Cache.class);
Cache cache = Mockito.mock(Cache.class);
PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIManagerConfigurationService.class);
PowerMockito.mockStatic(CacheProvider.class);
org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
final APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
PowerMockito.when(cacheProvider.getDefaultCacheTimeout()).thenReturn((long) 900);
Mockito.when(CacheProvider.getResourceCache()).thenReturn(cache);
assertNull(apiKeyValidator.findMatchingVerb(synCtx));
} catch (ResourceNotFoundException e) {
fail("ResourceNotFoundException exception is thrown " + e);
} catch (APISecurityException e) {
fail("APISecurityException is thrown " + e);
}
}
use of org.wso2.carbon.identity.core.bean.context.MessageContext in project carbon-apimgt by wso2.
the class APIKeyValidatorTestCase method testGetVerbInfoDTOFromAPIDataWithInvalidRequestPath.
@Test
public void testGetVerbInfoDTOFromAPIDataWithInvalidRequestPath() throws Exception {
String context = "/";
String apiVersion = "1.0";
String requestPath = "";
String httpMethod = "https";
APIKeyValidator apiKeyValidator = createAPIKeyValidator(true, getDefaultURITemplates("/menu", "GET"), getDefaultVerbInfoDTO());
// If isAPIResourceValidationEnabled==true
apiKeyValidator.setGatewayAPIResourceValidationEnabled(true);
PowerMockito.mockStatic(Cache.class);
Cache cache = Mockito.mock(Cache.class);
PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIManagerConfigurationService.class);
PowerMockito.mockStatic(CacheProvider.class);
org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
final APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
PowerMockito.when(cacheProvider.getDefaultCacheTimeout()).thenReturn((long) 900);
Mockito.when(CacheProvider.getResourceCache()).thenReturn(cache);
MessageContext messageContext = Mockito.mock(MessageContext.class);
Assert.assertEquals("", null, apiKeyValidator.getVerbInfoDTOFromAPIData(messageContext, context, apiVersion, requestPath, httpMethod));
}
use of org.wso2.carbon.identity.core.bean.context.MessageContext in project carbon-apimgt by wso2.
the class TestUtils method getMessageContextWithAuthContext.
public static MessageContext getMessageContextWithAuthContext(String context, String version) {
SynapseConfiguration synCfg = new SynapseConfiguration();
org.apache.axis2.context.MessageContext axisMsgCtx = new org.apache.axis2.context.MessageContext();
axisMsgCtx.setIncomingTransportName("http");
axisMsgCtx.setProperty(Constants.Configuration.TRANSPORT_IN_URL, "/test/1.0.0/search.atom");
AxisConfiguration axisConfig = new AxisConfiguration();
ConfigurationContext cfgCtx = new ConfigurationContext(axisConfig);
MessageContext synCtx = new Axis2MessageContext(axisMsgCtx, synCfg, new Axis2SynapseEnvironment(cfgCtx, synCfg));
synCtx.setProperty(RESTConstants.REST_API_CONTEXT, context);
synCtx.setProperty(RESTConstants.SYNAPSE_REST_API_VERSION, version);
synCtx.setProperty(APIMgtGatewayConstants.API_STATUS, APIConstants.PUBLISHED);
AuthenticationContext authenticationContext = new AuthenticationContext();
authenticationContext.setUsername("sanjeewa");
authenticationContext.setApiKey("123456789");
authenticationContext.setApplicationId("123");
authenticationContext.setApplicationName("test-app");
authenticationContext.setAuthenticated(true);
authenticationContext.setCallerToken("987654321");
authenticationContext.setTier("Silver");
authenticationContext.setSpikeArrestLimit(600);
authenticationContext.setSubscriber("testSubscriber");
Map map = new TreeMap();
map.put("host", "127.0.0.1");
map.put("X-FORWARDED-FOR", "127.0.0.1");
((Axis2MessageContext) synCtx).getAxis2MessageContext().setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, map);
synCtx.setProperty(API_AUTH_CONTEXT, authenticationContext);
return synCtx;
}
Aggregations