use of org.wso2.carbon.apimgt.gateway.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.gateway.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.gateway.internal.ServiceReferenceHolder 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.apimgt.gateway.internal.ServiceReferenceHolder 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.apimgt.gateway.internal.ServiceReferenceHolder 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));
}
Aggregations