Search in sources :

Example 1 with DispatcherHelper

use of org.apache.synapse.api.dispatch.DispatcherHelper in project carbon-apimgt by wso2.

the class APIKeyValidatorTestCase method testFindMatchingVerbWithValidResources.

@Test
public void testFindMatchingVerbWithValidResources() throws Exception {
    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("abc");
    Mockito.when(synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH)).thenReturn("");
    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);
    Resource resource = Mockito.mock(Resource.class);
    SynapseConfiguration synapseConfiguration = Mockito.mock(SynapseConfiguration.class);
    Mockito.when(synCtx.getConfiguration()).thenReturn(synapseConfiguration);
    API api2 = Mockito.mock(API.class);
    PowerMockito.whenNew(API.class).withArguments("abc", "/").thenReturn(api2);
    Mockito.when(synapseConfiguration.getAPI("abc")).thenReturn(api2);
    Resource resource1 = Mockito.mock(Resource.class);
    Mockito.when(resource1.getMethods()).thenReturn(new String[] { "GET" });
    Resource[] resourceArray = new Resource[1];
    resourceArray[0] = resource1;
    // Mockito.when(resourceArray[0]).thenReturn(resource1);
    Mockito.when(api2.getResources()).thenReturn(resourceArray);
    Mockito.when(synCtx.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
    DispatcherHelper helper = Mockito.mock(DispatcherHelper.class);
    Mockito.when(resource1.getDispatcherHelper()).thenReturn(helper);
    Mockito.when(helper.getString()).thenReturn("/test");
    VerbInfoDTO verbInfoDTO = getDefaultVerbInfoDTO();
    APIKeyValidator apiKeyValidator = createAPIKeyValidator(true, getDefaultURITemplates("/menu", "GET"), verbInfoDTO);
    try {
        // Test for ResourceNotFoundexception
        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);
        assertNotNull(apiKeyValidator.findMatchingVerb(synCtx));
    // todo    Mockito.when(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION_STRATEGY)).thenReturn("url");
    } catch (ResourceNotFoundException e) {
        assert true;
    } catch (APISecurityException e) {
        fail("APISecurityException is thrown " + e);
    }
    APIKeyValidator apiKeyValidator1 = createAPIKeyValidator(false, getDefaultURITemplates("/menu", "GET"), verbInfoDTO);
    API api = new API("abc", "/");
    Mockito.when(synCtx.getProperty(APIConstants.API_ELECTED_RESOURCE)).thenReturn("/menu");
    api.addResource(resource);
    Mockito.when(synapseConfiguration.getAPI("abc")).thenReturn((api));
    try {
        // Test for matching verb is found path
        List<VerbInfoDTO> verbInfoList = new ArrayList<>();
        verbInfoList.add(verbInfoDTO);
        assertEquals("", verbInfoList, apiKeyValidator1.findMatchingVerb(synCtx));
    } catch (ResourceNotFoundException e) {
        fail("ResourceNotFoundException exception is thrown " + e);
    } catch (APISecurityException e) {
        fail("APISecurityException is thrown " + e);
    }
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) DispatcherHelper(org.apache.synapse.api.dispatch.DispatcherHelper) Resource(org.apache.synapse.api.Resource) ArrayList(java.util.ArrayList) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) CacheProvider(org.wso2.carbon.apimgt.impl.caching.CacheProvider) VerbInfoDTO(org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Cache(javax.cache.Cache) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with DispatcherHelper

use of org.apache.synapse.api.dispatch.DispatcherHelper in project carbon-apimgt by wso2.

the class CORSRequestHandlerTestCase method testHandleRequest.

@Test
public void testHandleRequest() throws Exception {
    PowerMockito.mockStatic(Utils.class);
    SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
    MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
    org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
    Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
    Map<String, String> headers = new HashMap<>();
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(headers);
    Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
    Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/ishara");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin-AT-wso2.com--PizzaShackAPI");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0");
    SynapseConfiguration synapseConfiguration = Mockito.mock(SynapseConfiguration.class);
    API api = Mockito.mock(API.class);
    Mockito.when(api.getContext()).thenReturn("/ishara");
    Resource resource = Mockito.mock(Resource.class);
    Resource[] resources = new Resource[1];
    resources[0] = resource;
    Mockito.when(api.getResources()).thenReturn(resources);
    VersionStrategy versionStrategy = Mockito.mock(VersionStrategy.class);
    Mockito.when(versionStrategy.getVersionType()).thenReturn("url");
    Mockito.when(versionStrategy.getVersion()).thenReturn("1.0");
    Mockito.when(api.getVersionStrategy()).thenReturn(versionStrategy);
    Mockito.when(Utils.getSelectedAPI(messageContext)).thenReturn(api);
    Mockito.when(synapseConfiguration.getAPI("admin-AT-wso2.com--PizzaShackAPI")).thenReturn(api);
    Mockito.when(messageContext.getConfiguration()).thenReturn(synapseConfiguration);
    CORSRequestHandler corsRequestHandler = createCORSRequestHandler();
    corsRequestHandler.init(synapseEnvironment);
    // test ResourceNotFound path
    Assert.assertFalse(corsRequestHandler.handleRequest(messageContext));
    // test for resource that is found
    String[] methods = { "GET", "POST" };
    Mockito.when(resource.getMethods()).thenReturn(methods);
    DispatcherHelper dispatcherHelper = new URLMappingHelper("/ishara/1.0") {

        @Override
        public String getString() {
            return "/xx";
        }
    };
    Mockito.when(resource.getDispatcherHelper()).thenReturn(dispatcherHelper);
    Mockito.when(messageContext.getProperty("REST_SUB_REQUEST_PATH")).thenReturn("/ishara/1.0");
    TreeMap transportHeaders = new TreeMap();
    transportHeaders.put("Origin", "");
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
    Assert.assertTrue(corsRequestHandler.handleRequest(messageContext));
    DispatcherHelper dispatcherHelper1 = new URLMappingHelper("/ishara/1.0") {

        @Override
        public String getString() {
            return "/xx";
        }
    };
    Mockito.when(resource.getDispatcherHelper()).thenReturn(dispatcherHelper1);
    Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("OPTIONS");
    // test for OPTIONS request when OPTIONS is not supported by SupportedHTTPVerbs
    Assert.assertFalse(corsRequestHandler.handleRequest(messageContext));
    // test for OPTIONS request when OPTIONS is supported by SupportedHTTPVerbs
    String[] methodsWithOptions = { "GET", "POST", "OPTIONS" };
    Mockito.when(resource.getMethods()).thenReturn(methodsWithOptions);
    Assert.assertTrue(corsRequestHandler.handleRequest(messageContext));
}
Also used : HashMap(java.util.HashMap) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) DispatcherHelper(org.apache.synapse.api.dispatch.DispatcherHelper) URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) Resource(org.apache.synapse.api.Resource) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) TreeMap(java.util.TreeMap) VersionStrategy(org.apache.synapse.api.version.VersionStrategy) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with DispatcherHelper

use of org.apache.synapse.api.dispatch.DispatcherHelper in project wso2-synapse by wso2.

the class API method addResource.

public void addResource(Resource resource) {
    DispatcherHelper dispatcherHelper = resource.getDispatcherHelper();
    if (dispatcherHelper != null) {
        String mapping = dispatcherHelper.getString();
        for (Resource r : resources.values()) {
            DispatcherHelper helper = r.getDispatcherHelper();
            if (helper != null && helper.getString().equals(mapping) && resourceMatches(resource, r)) {
                handleException("Two resources cannot have the same path mapping and methods");
            }
        }
    } else {
        for (Resource r : resources.values()) {
            DispatcherHelper helper = r.getDispatcherHelper();
            if (helper == null) {
                handleException("Only one resource can be designated as default");
            }
        }
    }
    resources.put(resource.getName(), resource);
}
Also used : DispatcherHelper(org.apache.synapse.api.dispatch.DispatcherHelper)

Example 4 with DispatcherHelper

use of org.apache.synapse.api.dispatch.DispatcherHelper in project wso2-synapse by wso2.

the class ResourceSerializer method serializeResource.

public static OMElement serializeResource(Resource resource) {
    OMElement resourceElt = fac.createOMElement("resource", SynapseConstants.SYNAPSE_OMNAMESPACE);
    String[] methods = resource.getMethods();
    if (methods.length > 0) {
        String value = "";
        for (String method : methods) {
            value += method + " ";
        }
        resourceElt.addAttribute("methods", value.trim(), null);
    }
    if (resource.getBindsTo() != null) {
        resourceElt.addAttribute(ApiConstants.BINDS_TO, String.join(",", resource.getBindsTo()), null);
    }
    if (resource.getContentType() != null) {
        resourceElt.addAttribute("content-type", resource.getContentType(), null);
    }
    if (resource.getUserAgent() != null) {
        resourceElt.addAttribute("user-agent", resource.getUserAgent(), null);
    }
    if (resource.getProtocol() == RESTConstants.PROTOCOL_HTTP_ONLY) {
        resourceElt.addAttribute("protocol", "http", null);
    } else if (resource.getProtocol() == RESTConstants.PROTOCOL_HTTPS_ONLY) {
        resourceElt.addAttribute("protocol", "https", null);
    }
    DispatcherHelper helper = resource.getDispatcherHelper();
    if (helper != null) {
        if (helper instanceof URLMappingHelper) {
            resourceElt.addAttribute("url-mapping", helper.getString(), null);
        } else if (helper instanceof URITemplateHelper) {
            resourceElt.addAttribute("uri-template", helper.getString(), null);
        }
    }
    SequenceMediatorSerializer seqSerializer = new SequenceMediatorSerializer();
    if (resource.getInSequenceKey() != null) {
        resourceElt.addAttribute("inSequence", resource.getInSequenceKey(), null);
    } else if (resource.getInSequence() != null) {
        OMElement inSeqElement = seqSerializer.serializeAnonymousSequence(null, resource.getInSequence());
        inSeqElement.setLocalName("inSequence");
        resourceElt.addChild(inSeqElement);
    }
    if (resource.getOutSequenceKey() != null) {
        resourceElt.addAttribute("outSequence", resource.getOutSequenceKey(), null);
    } else if (resource.getOutSequence() != null) {
        OMElement outSeqElement = seqSerializer.serializeAnonymousSequence(null, resource.getOutSequence());
        outSeqElement.setLocalName("outSequence");
        resourceElt.addChild(outSeqElement);
    }
    if (resource.getFaultSequenceKey() != null) {
        resourceElt.addAttribute("faultSequence", resource.getFaultSequenceKey(), null);
    } else if (resource.getFaultSequence() != null) {
        OMElement faultSeqElement = seqSerializer.serializeAnonymousSequence(null, resource.getFaultSequence());
        faultSeqElement.setLocalName("faultSequence");
        resourceElt.addChild(faultSeqElement);
    }
    return resourceElt;
}
Also used : URITemplateHelper(org.apache.synapse.api.dispatch.URITemplateHelper) DispatcherHelper(org.apache.synapse.api.dispatch.DispatcherHelper) URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) OMElement(org.apache.axiom.om.OMElement) SequenceMediatorSerializer(org.apache.synapse.config.xml.SequenceMediatorSerializer)

Aggregations

DispatcherHelper (org.apache.synapse.api.dispatch.DispatcherHelper)4 MessageContext (org.apache.synapse.MessageContext)2 API (org.apache.synapse.api.API)2 Resource (org.apache.synapse.api.Resource)2 URLMappingHelper (org.apache.synapse.api.dispatch.URLMappingHelper)2 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 Cache (javax.cache.Cache)1 OMElement (org.apache.axiom.om.OMElement)1 URITemplateHelper (org.apache.synapse.api.dispatch.URITemplateHelper)1 VersionStrategy (org.apache.synapse.api.version.VersionStrategy)1 SequenceMediatorSerializer (org.apache.synapse.config.xml.SequenceMediatorSerializer)1 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)1 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)1 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)1