org.apache.synapse.rest.RESTConstants Java Examples

The following examples show how to use org.apache.synapse.rest.RESTConstants. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: APIAuthenticationHandlerTestCase.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testHandleRequest() {

    APIAuthenticationHandler apiAuthenticationHandler = createAPIAuthenticationHandler();
    apiAuthenticationHandler.init(synapseEnvironment);
    Assert.assertEquals(apiAuthenticationHandler.startMetricTimer(), null);

    Options options = Mockito.mock(Options.class);
    Mockito.when(options.getMessageId()).thenReturn("1");
    Mockito.when(axis2MsgCntxt.getOptions()).thenReturn(options);

    TreeMap transportHeaders = new TreeMap();
    transportHeaders.put(APIConstants.USER_AGENT, "");
    transportHeaders.put(APIMgtGatewayConstants.AUTHORIZATION, "gsu64r874tcin7ry8oe");
    messageContext.setProperty(RESTConstants.REST_FULL_REQUEST_PATH, "");
    messageContext.setProperty(APIMgtGatewayConstants.APPLICATION_NAME, "abc");
    messageContext.setProperty(APIMgtGatewayConstants.END_USER_NAME, "admin");
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
    Assert.assertTrue(apiAuthenticationHandler.handleRequest(messageContext));
}
 
Example #2
Source File: APIManagerExtensionHandler.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@MethodStats
public boolean mediate(MessageContext messageContext, String direction) {
    // In order to avoid a remote registry call occurring on each invocation, we
    // directly get the extension sequences from the local registry.
    Map localRegistry = messageContext.getConfiguration().getLocalRegistry();

    Object sequence = localRegistry.get(EXT_SEQUENCE_PREFIX + direction);
    if (sequence instanceof Mediator) {
        if (!((Mediator) sequence).mediate(messageContext)) {
            return false;
        }
    }

    String apiName = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API);
    sequence = localRegistry.get(apiName + "--" + direction);
    if (sequence instanceof Mediator) {
        return ((Mediator) sequence).mediate(messageContext);
    }
    return true;
}
 
Example #3
Source File: APIManagerCacheExtensionHandler.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public boolean mediate(MessageContext messageContext, String direction) {
    // In order to avoid a remote registry call occurring on each invocation, we
    // directly get the extension sequences from the local registry.
    Map localRegistry = messageContext.getConfiguration().getLocalRegistry();

    Object sequence = localRegistry.get(EXT_SEQUENCE_PREFIX + direction);
    if (sequence instanceof Mediator) {
        if (!((Mediator) sequence).mediate(messageContext)) {
            return false;
        }
    }

    String apiName = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API);
    sequence = localRegistry.get(apiName + "--" + direction);
    if (sequence instanceof Mediator) {
        return ((Mediator) sequence).mediate(messageContext);
    }
    return true;
}
 
Example #4
Source File: APIMgtThrottleUsageHandlerTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void mediateWhileEventReceiverIsSkipped() throws Exception {
    APIManagerAnalyticsConfiguration apiManagerAnalyticsConfiguration = Mockito.mock
            (APIManagerAnalyticsConfiguration.class);
    APIMgtUsageDataPublisher apiMgtUsageDataPublisher = Mockito.mock(APIMgtUsageDataPublisher.class);
    APIMgtThrottleUsageHandler apiMgtThrottleUsageHandler = new APIMgtThrottleUsageHandlerWrapper
            (apiMgtUsageDataPublisher, true, false, apiManagerAnalyticsConfiguration);
    MessageContext messageContext = Mockito.mock(MessageContext.class);
    AuthenticationContext authContext = new AuthenticationContext();
    authContext.setApiKey("ac-def");
    authContext.setUsername("admin");
    Mockito.when(messageContext.getProperty("__API_AUTH_CONTEXT")).thenReturn(authContext);
    Mockito.when(messageContext.getProperty(SYNAPSE_REST_API)).thenReturn("admin--api1");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0.0");
    Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/api1");
    Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/api1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_PUBLISHER)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(RESTConstants.REST_FULL_REQUEST_PATH)).thenReturn("/api1/aa?vc=1");
    Mockito.when(messageContext.getProperty(APIConstants.THROTTLE_OUT_REASON_KEY)).thenReturn("application level " +
            "throttled out");
    apiMgtThrottleUsageHandler.mediate(messageContext);
}
 
Example #5
Source File: MutualSSLAuthenticator.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * To get the API Identifier of the current API.
 *
 * @param messageContext Current message context
 * @return API Identifier of currently accessed API.
 */
private APIIdentifier getAPIIdentifier(MessageContext messageContext) {
    String apiWithversion = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API);
    String apiPublisher = (String) messageContext.getProperty(APIMgtGatewayConstants.API_PUBLISHER);
    String api = null;
    //if publisher is null,extract the publisher from the api_version
    if (apiPublisher == null && apiWithversion != null) {
        int ind = apiWithversion.indexOf("--");
        apiPublisher = apiWithversion.substring(0, ind);
    }

    if (apiWithversion != null) {
        int index = apiWithversion.indexOf("--");
        if (index != -1) {
            apiWithversion = apiWithversion.substring(index + 2);
        }
        String[] splitParts = apiWithversion.split(":");
        api = splitParts[0];
        apiWithversion = splitParts[1].substring(1);
    }
    return new APIIdentifier(apiPublisher, api, apiWithversion);
}
 
Example #6
Source File: APIMgtFaultHandlerTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void mediate() throws Exception {
    APIMgtUsageDataPublisher apiMgtUsageDataPublisher = Mockito.mock(APIMgtUsageDataPublisher.class);
    APIMgtFaultHandler apiMgtFaultHandler = new APIMgtFaultHandlerWrapper(apiMgtUsageDataPublisher, true, false);
    MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
    Mockito.when(messageContext.getProperty("REST_FULL_REQUEST_PATH")).thenReturn("/abc/1.0.0/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.REQUEST_START_TIME)).thenReturn("12345678");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONSUMER_KEY)).thenReturn("abc-def-ghi");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONTEXT)).thenReturn("/abc/1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_VERSION)).thenReturn("1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API)).thenReturn("api1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.RESOURCE)).thenReturn("/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HTTP_METHOD)).thenReturn("GET");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.VERSION)).thenReturn("1.0.0");
    Mockito.when((messageContext.getProperty(SynapseConstants.ERROR_CODE))).thenReturn("404");
    Mockito.when(messageContext.getProperty(SynapseConstants.ERROR_MESSAGE)).thenReturn("not found");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.USER_ID)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HOST_NAME)).thenReturn("127.0.0.1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_PUBLISHER)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_NAME)).thenReturn("App1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_ID)).thenReturn("1");
    Mockito.when(messageContext.getProperty(SynapseConstants.TRANSPORT_IN_NAME)).thenReturn("https");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--api1-1.0.0");
    apiMgtFaultHandler.mediate(messageContext);
}
 
Example #7
Source File: APIMgtFaultHandlerTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void mediate1() throws Exception {
    APIMgtUsageDataPublisher apiMgtUsageDataPublisher = Mockito.mock(APIMgtUsageDataPublisher.class);
    APIMgtFaultHandler apiMgtFaultHandler = new APIMgtFaultHandlerWrapper(apiMgtUsageDataPublisher, true, false);
    MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
    Mockito.when(messageContext.getProperty("REST_FULL_REQUEST_PATH")).thenReturn("/abc/1.0.0/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.REQUEST_START_TIME)).thenReturn("12345678");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONSUMER_KEY)).thenReturn("abc-def-ghi");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONTEXT)).thenReturn("/abc/1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_VERSION)).thenReturn("1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API)).thenReturn("api1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.RESOURCE)).thenReturn("/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HTTP_METHOD)).thenReturn("GET");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.VERSION)).thenReturn("1.0.0");
    Mockito.when((messageContext.getProperty(SynapseConstants.ERROR_CODE))).thenReturn("404");
    Mockito.when(messageContext.getProperty(SynapseConstants.ERROR_MESSAGE)).thenReturn("not found");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.USER_ID)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HOST_NAME)).thenReturn("127.0.0.1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_NAME)).thenReturn("App1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_ID)).thenReturn("1");
    Mockito.when(messageContext.getProperty(SynapseConstants.TRANSPORT_IN_NAME)).thenReturn("https");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--api1-1.0.0");
    apiMgtFaultHandler.mediate(messageContext);
}
 
Example #8
Source File: DataProcessAndPublishingAgentTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void setDataReferenceWithHeaderConditionEnableWithNullHeaderMap() throws Exception {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnabled(true);
    throttleProperties.setEnableHeaderConditions(true);
    DataProcessAndPublishingAgent dataProcessAndPublishingAgent = new DataProcessAndPublishingAgentWrapper
            (throttleProperties);
    AuthenticationContext authenticationContext = new AuthenticationContext();
    MessageContext messageContext = 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);
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--PizzaShackAPI");
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn
            (null);
    VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
    verbInfoDTO.setContentAware(false);
    ArrayList<VerbInfoDTO> list = new ArrayList<VerbInfoDTO>();
    list.add(verbInfoDTO);
    Mockito.when(messageContext.getProperty(APIConstants.VERB_INFO_DTO)).thenReturn(list);
    dataProcessAndPublishingAgent.setDataReference(applicationLevelThrottleKey, applicationLevelTier,
            apiLevelThrottleKey, null, subscriptionLevelThrottleKey, subscriptionLevelTier,
            resourceLevelThrottleKey, resourceLevelTier, authorizedUser, apiContext, apiVersion, appTenant,
            apiTenant, appId, messageContext, authenticationContext);
    dataProcessAndPublishingAgent.run();
}
 
Example #9
Source File: SecurityHandlerAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Clear headers map preserving cors headers
 * @param headers msg ctx headers map
 * @return cors headers preserved header map
 */
public Map clearHeaders(Map headers) {

    Object allowOriginCorsHeader = headers.get(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_ORIGIN);
    Object allowMethodsCorsHeader = headers.get(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_METHODS);
    Object allowHeadersCorsHeader = headers.get(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_HEADERS);
    headers.clear();
    if (allowOriginCorsHeader != null) {
        headers.put(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_ORIGIN, allowOriginCorsHeader);
    }
    if (allowMethodsCorsHeader != null) {
        headers.put(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_METHODS, allowMethodsCorsHeader);
    }
    if (allowHeadersCorsHeader != null) {
        headers.put(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_HEADERS, allowHeadersCorsHeader);
    }
    return headers;
}
 
Example #10
Source File: TestUtils.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public static MessageContext getMessageContext(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);
    Map map = new TreeMap();
    map.put(X_FORWARDED_FOR, "127.0.0.1,1.10.0.4");
    ((Axis2MessageContext) synCtx).getAxis2MessageContext()
            .setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, map);
    return synCtx;
}
 
Example #11
Source File: DataProcessAndPublishingAgentTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void setDataReferenceWithHeaderConditionEnable() throws Exception {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnabled(true);
    throttleProperties.setEnableHeaderConditions(true);
    DataProcessAndPublishingAgent dataProcessAndPublishingAgent = new DataProcessAndPublishingAgentWrapper
            (throttleProperties);
    AuthenticationContext authenticationContext = new AuthenticationContext();
    MessageContext messageContext = 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);
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--PizzaShackAPI");
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS))
            .thenReturn(new TreeMap<>());
    VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
    verbInfoDTO.setContentAware(false);
    ArrayList<VerbInfoDTO> list = new ArrayList<VerbInfoDTO>();
    list.add(verbInfoDTO);
    Mockito.when(messageContext.getProperty(APIConstants.VERB_INFO_DTO)).thenReturn(list);
    dataProcessAndPublishingAgent.setDataReference(applicationLevelThrottleKey, applicationLevelTier,
            apiLevelThrottleKey, null, subscriptionLevelThrottleKey, subscriptionLevelTier,
            resourceLevelThrottleKey, resourceLevelTier, authorizedUser, apiContext, apiVersion, appTenant,
            apiTenant, appId, messageContext, authenticationContext);
    dataProcessAndPublishingAgent.run();
}
 
Example #12
Source File: MetricHandler.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Override
public boolean handleResponseOutFlow(MessageContext synCtx) {
    if (null != synCtx.getProperty(SynapseConstants.PROXY_SERVICE)) {
        stopTimers(synCtx.getProperty(MetricConstants.PROXY_LATENCY_TIMER), synCtx);
    } else {
        serviceInvokePort = getServiceInvokePort(synCtx);
        if (null != synCtx.getProperty(SynapseConstants.IS_INBOUND) &&
                (serviceInvokePort != internalHttpApiPort)) {
            stopTimers(synCtx.
                    getProperty(MetricConstants.INBOUND_ENDPOINT_LATENCY_TIMER), synCtx);
        }
        if (serviceInvokePort != internalHttpApiPort) {
            stopTimers(synCtx.getProperty(MetricConstants.API_LATENCY_TIMER), synCtx);
            synCtx.setProperty(RESTConstants.IS_PROMETHEUS_ENGAGED, true);
        }
    }
    return true;
}
 
Example #13
Source File: TestUtils.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public static MessageContext getMessageContextWithOutAuthContext(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, Path.SEPARATOR+context+Path.SEPARATOR+version
            + Path.SEPARATOR+"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(APIConstants.API_ELECTED_RESOURCE, "resource");
    Map map = new TreeMap();
    map.put("host","127.0.0.1");
    map.put("X-FORWARDED-FOR", "127.0.0.1");
    map.put("Authorization", "Bearer 123456789");
    synCtx.setProperty(RESTConstants.REST_API_CONTEXT, context);
    synCtx.setProperty(RESTConstants.SYNAPSE_REST_API_VERSION,version);
    ((Axis2MessageContext) synCtx).getAxis2MessageContext()
            .setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, map);
    return synCtx;
}
 
Example #14
Source File: DispatcherTestCase.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Test request dispatching of internal apis.
 */
@Test
public void testDispatching() throws Exception {
    System.setProperty(
            org.wso2.carbon.inbound.endpoint.internal.http.api.Constants.PREFIX_TO_ENABLE_INTERNAL_APIS
                    + "SampleAPI", "true");

    ConfigurationLoader.loadInternalApis("internal/http/api/internal-apis.xml");
    List<InternalAPI> apis = ConfigurationLoader.getHttpInternalApis();
    Assert.assertEquals("Expected API not loaded", 1, apis.size());

    InternalAPIDispatcher internalAPIDispatcher = new InternalAPIDispatcher(apis);
    MessageContext synCtx = createMessageContext();
    setRequestProperties(synCtx, "/foo/bar?q=abc", "GET");

    boolean dispatchingCompleted = internalAPIDispatcher.dispatch(synCtx);
    Assert.assertTrue("Dispatcher failed to find correct API or Resource", dispatchingCompleted);
    Assert.assertTrue("Correct resource not invoked", (Boolean) synCtx.getProperty("Success"));
    Assert.assertEquals("abc", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "q"));
}
 
Example #15
Source File: APIManagerExtensionHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandleRequestWithGlobalInSeqWithoutFurtherMediationAndCustomInSeq() {
	MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
	SynapseConfiguration synapseConfig = Mockito.mock(SynapseConfiguration.class);
	org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito
			.mock(org.apache.axis2.context.MessageContext.class);

	Map localRegistry = Mockito.mock(Map.class);

	Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
	Mockito.when(((Axis2MessageContext) messageContext).getConfiguration()).thenReturn(synapseConfig);
	Mockito.when(synapseConfig.getLocalRegistry()).thenReturn(localRegistry);

	Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn(API_NAME);
	

	SequenceMediator inSeq = Mockito.mock(SequenceMediator.class);
	SequenceMediator globalInSeq = Mockito.mock(SequenceMediator.class);
	Mockito.when(localRegistry.get(API_NAME + "--" + DIRECTION_IN)).thenReturn(inSeq);
	Mockito.when(localRegistry.get(EXT_SEQUENCE_PREFIX + DIRECTION_IN)).thenReturn(globalInSeq);
	Mockito.when(((Mediator) inSeq).mediate(messageContext)).thenReturn(true);
	
	//Global mediation returns a false to prevent any further mediation
	Mockito.when(((Mediator) globalInSeq).mediate(messageContext)).thenReturn(false);

	APIManagerExtensionHandler handler = createAPIManagerExtensionHandler();
	// both methods are executed during a full request path
	handler.handleRequest(messageContext);
	handler.handleResponse(messageContext);

	// check whether custom in sequnce is not executed
	Mockito.verify(inSeq, Mockito.never()).mediate(messageContext);
	// check whether global in sequnce is executed once 
	Mockito.verify(globalInSeq, Mockito.times(1)).mediate(messageContext);

}
 
Example #16
Source File: APIMgtResponseHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void mediate() throws Exception {
    APIMgtUsageDataPublisher apiMgtUsageDataPublisher = Mockito.mock(APIMgtUsageDataPublisher.class);
    APIManagerAnalyticsConfiguration apiManagerAnalyticsConfiguration = Mockito.mock
            (APIManagerAnalyticsConfiguration.class);
    Mockito.when(apiManagerAnalyticsConfiguration.isBuildMsg()).thenReturn(false);
    APIMgtResponseHandler apiMgtResponseHandler = new APIMgtResponseHandlerWrapper(apiMgtUsageDataPublisher,
            true, false, apiManagerAnalyticsConfiguration);
    MessageContext messageContext = 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);
    Mockito.when(axis2MsgCntxt.getProperty(SynapseConstants.HTTP_SC)).thenReturn(200);
    Mockito.when(messageContext.getProperty("REST_FULL_REQUEST_PATH")).thenReturn("/abc/1.0.0/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.REQUEST_START_TIME)).thenReturn("12345678");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONSUMER_KEY)).thenReturn("abc-def-ghi");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONTEXT)).thenReturn("/abc/1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_VERSION)).thenReturn("1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API)).thenReturn("api1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.RESOURCE)).thenReturn("/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HTTP_METHOD)).thenReturn("GET");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.VERSION)).thenReturn("1.0.0");
    Mockito.when((messageContext.getProperty(SynapseConstants.ERROR_CODE))).thenReturn("404");
    Mockito.when(messageContext.getProperty(SynapseConstants.ERROR_MESSAGE)).thenReturn("not found");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.USER_ID)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HOST_NAME)).thenReturn("127.0.0.1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_PUBLISHER)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_NAME)).thenReturn("App1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_ID)).thenReturn("1");
    Mockito.when(messageContext.getProperty(SynapseConstants.TRANSPORT_IN_NAME)).thenReturn("https");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--api1-1.0.0");
    Mockito.when(messageContext.getProperty(RESTConstants.REST_URL_PREFIX)).thenReturn("https://localhost");
    apiMgtResponseHandler.mediate(messageContext);
}
 
Example #17
Source File: APIMgtResponseHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void mediateWithStartTimeNotSet() throws Exception {
    APIMgtUsageDataPublisher apiMgtUsageDataPublisher = Mockito.mock(APIMgtUsageDataPublisher.class);
    APIManagerAnalyticsConfiguration apiManagerAnalyticsConfiguration = Mockito.mock
            (APIManagerAnalyticsConfiguration.class);
    Mockito.when(apiManagerAnalyticsConfiguration.isBuildMsg()).thenReturn(false);
    APIMgtResponseHandler apiMgtResponseHandler = new APIMgtResponseHandlerWrapper(apiMgtUsageDataPublisher,
            true, false, apiManagerAnalyticsConfiguration);
    MessageContext messageContext = 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);
    Mockito.when(axis2MsgCntxt.getProperty(SynapseConstants.HTTP_SC)).thenReturn(200);
    Mockito.when(messageContext.getProperty("REST_FULL_REQUEST_PATH")).thenReturn("/abc/1.0.0/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONSUMER_KEY)).thenReturn("abc-def-ghi");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONTEXT)).thenReturn("/abc/1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_VERSION)).thenReturn("1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API)).thenReturn("api1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.RESOURCE)).thenReturn("/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HTTP_METHOD)).thenReturn("GET");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.VERSION)).thenReturn("1.0.0");
    Mockito.when((messageContext.getProperty(SynapseConstants.ERROR_CODE))).thenReturn("404");
    Mockito.when(messageContext.getProperty(SynapseConstants.ERROR_MESSAGE)).thenReturn("not found");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.USER_ID)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HOST_NAME)).thenReturn("127.0.0.1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_PUBLISHER)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_NAME)).thenReturn("App1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_ID)).thenReturn("1");
    Mockito.when(messageContext.getProperty(SynapseConstants.TRANSPORT_IN_NAME)).thenReturn("https");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--api1-1.0.0");
    Mockito.when(messageContext.getProperty(RESTConstants.REST_URL_PREFIX)).thenReturn("https://localhost");
    apiMgtResponseHandler.mediate(messageContext);
}
 
Example #18
Source File: APIMgtResponseHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void mediateWithContentLength() throws Exception {
    APIMgtUsageDataPublisher apiMgtUsageDataPublisher = Mockito.mock(APIMgtUsageDataPublisher.class);
    APIManagerAnalyticsConfiguration apiManagerAnalyticsConfiguration = Mockito.mock
            (APIManagerAnalyticsConfiguration.class);
    Mockito.when(apiManagerAnalyticsConfiguration.isBuildMsg()).thenReturn(true);
    APIMgtResponseHandler apiMgtResponseHandler = new APIMgtResponseHandlerWrapper(apiMgtUsageDataPublisher,
            true, false, apiManagerAnalyticsConfiguration);
    MessageContext messageContext = 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);
    Mockito.when(axis2MsgCntxt.getProperty(SynapseConstants.HTTP_SC)).thenReturn(200);
    Map headers = new HashMap();
    headers.put(HttpHeaders.CONTENT_LENGTH, "103");
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn
            (headers);
    Mockito.when(messageContext.getProperty("REST_FULL_REQUEST_PATH")).thenReturn("/abc/1.0.0/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.REQUEST_START_TIME)).thenReturn("12345678");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONSUMER_KEY)).thenReturn("abc-def-ghi");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONTEXT)).thenReturn("/abc/1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_VERSION)).thenReturn("1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API)).thenReturn("api1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.RESOURCE)).thenReturn("/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HTTP_METHOD)).thenReturn("GET");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.VERSION)).thenReturn("1.0.0");
    Mockito.when((messageContext.getProperty(SynapseConstants.ERROR_CODE))).thenReturn("404");
    Mockito.when(messageContext.getProperty(SynapseConstants.ERROR_MESSAGE)).thenReturn("not found");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.USER_ID)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HOST_NAME)).thenReturn("127.0.0.1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_PUBLISHER)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_NAME)).thenReturn("App1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_ID)).thenReturn("1");
    Mockito.when(messageContext.getProperty(SynapseConstants.TRANSPORT_IN_NAME)).thenReturn("https");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--api1-1.0.0");
    Mockito.when(messageContext.getProperty(RESTConstants.REST_URL_PREFIX)).thenReturn("https://localhost");
    apiMgtResponseHandler.mediate(messageContext);
}
 
Example #19
Source File: APIManagerExtensionHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandleRequestWithGlobalInOutSeq() {
	// Test for both in and out sequences
	MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
	SynapseConfiguration synapseConfig = Mockito.mock(SynapseConfiguration.class);
	org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito
			.mock(org.apache.axis2.context.MessageContext.class);
	Options options = Mockito.mock(Options.class);
	EndpointReference endPoint = Mockito.mock(EndpointReference.class);

	Map localRegistry = Mockito.mock(Map.class);

	Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
	Mockito.when(((Axis2MessageContext) messageContext).getConfiguration()).thenReturn(synapseConfig);
	Mockito.when(synapseConfig.getLocalRegistry()).thenReturn(localRegistry);
	Mockito.when(axis2MsgCntxt.getOptions()).thenReturn(options);
	Mockito.when(options.getTo()).thenReturn(endPoint);
	Mockito.when(endPoint.getAddress()).thenReturn("http://localhost:9443/test");

	Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn(API_NAME);

	SequenceMediator globalOutSeq = Mockito.mock(SequenceMediator.class);
	SequenceMediator globalInSeq = Mockito.mock(SequenceMediator.class);
	Mockito.when(localRegistry.get(EXT_SEQUENCE_PREFIX + DIRECTION_IN)).thenReturn(globalInSeq);
	Mockito.when(localRegistry.get(EXT_SEQUENCE_PREFIX + DIRECTION_OUT)).thenReturn(globalOutSeq);
	Mockito.when(((Mediator) globalOutSeq).mediate(messageContext)).thenReturn(true);
	Mockito.when(((Mediator) globalInSeq).mediate(messageContext)).thenReturn(true);

	APIManagerExtensionHandler handler = createAPIManagerExtensionHandler();
	// both methods are executed during a full request path
	handler.handleRequest(messageContext);
	handler.handleResponse(messageContext);

	// check whether custom out sequnce is only executed once
	Mockito.verify(globalOutSeq, Mockito.times(1)).mediate(messageContext);
	// check whether custom in sequnce is only executed once
	Mockito.verify(globalInSeq, Mockito.times(1)).mediate(messageContext);
}
 
Example #20
Source File: APIManagerExtensionHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandleRequestWithCustomOutSeq() {
	MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
	SynapseConfiguration synapseConfig = Mockito.mock(SynapseConfiguration.class);
	org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito
			.mock(org.apache.axis2.context.MessageContext.class);
	Options options = Mockito.mock(Options.class);
	EndpointReference endPoint = Mockito.mock(EndpointReference.class);

	Map localRegistry = Mockito.mock(Map.class);

	Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
	Mockito.when(((Axis2MessageContext) messageContext).getConfiguration()).thenReturn(synapseConfig);
	Mockito.when(synapseConfig.getLocalRegistry()).thenReturn(localRegistry);
	Mockito.when(axis2MsgCntxt.getOptions()).thenReturn(options);
	Mockito.when(options.getTo()).thenReturn(endPoint);
	Mockito.when(endPoint.getAddress()).thenReturn("http://localhost:9443/test");

	Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn(API_NAME);

	SequenceMediator outSeq = Mockito.mock(SequenceMediator.class);
	Mockito.when(localRegistry.get(API_NAME + "--" + DIRECTION_OUT)).thenReturn(outSeq);
	Mockito.when(((Mediator) outSeq).mediate(messageContext)).thenReturn(true);

	APIManagerExtensionHandler handler = createAPIManagerExtensionHandler();
	// both methods are executed during a full request path
	handler.handleRequest(messageContext);
	handler.handleResponse(messageContext);

	// check whether custom out sequnce is only executed once
	Mockito.verify(outSeq, Mockito.times(1)).mediate(messageContext);
}
 
Example #21
Source File: APIManagerExtensionHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandleRequestWithCustomInOutSeq() {
	// Test for both in and out sequences
	MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
	SynapseConfiguration synapseConfig = Mockito.mock(SynapseConfiguration.class);
	org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito
			.mock(org.apache.axis2.context.MessageContext.class);
	Options options = Mockito.mock(Options.class);
	EndpointReference endPoint = Mockito.mock(EndpointReference.class);

	Map localRegistry = Mockito.mock(Map.class);

	Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
	Mockito.when(((Axis2MessageContext) messageContext).getConfiguration()).thenReturn(synapseConfig);
	Mockito.when(axis2MsgCntxt.getOptions()).thenReturn(options);
	Mockito.when(options.getTo()).thenReturn(endPoint);
	Mockito.when(endPoint.getAddress()).thenReturn("http://localhost:9443/test");
	Mockito.when(synapseConfig.getLocalRegistry()).thenReturn(localRegistry);

	Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn(API_NAME);

	SequenceMediator outSeq = Mockito.mock(SequenceMediator.class);
	Mockito.when(localRegistry.get(API_NAME + "--" + DIRECTION_OUT)).thenReturn(outSeq);
	SequenceMediator inSeq = Mockito.mock(SequenceMediator.class);
	Mockito.when(localRegistry.get(API_NAME + "--" + DIRECTION_IN)).thenReturn(inSeq);
	Mockito.when(((Mediator) outSeq).mediate(messageContext)).thenReturn(true);
	Mockito.when(((Mediator) inSeq).mediate(messageContext)).thenReturn(true);

	APIManagerExtensionHandler handler = createAPIManagerExtensionHandler();
	// both methods are executed during a full request path
	handler.handleRequest(messageContext);
	handler.handleResponse(messageContext);

	// check whether custom out sequnce is only executed once
	Mockito.verify(outSeq, Mockito.times(1)).mediate(messageContext);
	// check whether custom in sequnce is only executed once
	Mockito.verify(inSeq, Mockito.times(1)).mediate(messageContext);
}
 
Example #22
Source File: APIManagerExtensionHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandleRequestWithoutCustomSeq() {

	MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
	SynapseConfiguration synapseConfig = Mockito.mock(SynapseConfiguration.class);
	org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito
			.mock(org.apache.axis2.context.MessageContext.class);
	Options options = Mockito.mock(Options.class);
	EndpointReference endPoint = Mockito.mock(EndpointReference.class);

	Map localRegistry = Mockito.mock(Map.class);

	Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
	Mockito.when(((Axis2MessageContext) messageContext).getConfiguration()).thenReturn(synapseConfig);
	Mockito.when(synapseConfig.getLocalRegistry()).thenReturn(localRegistry);
	Mockito.when(axis2MsgCntxt.getOptions()).thenReturn(options);
	Mockito.when(options.getTo()).thenReturn(endPoint);
	Mockito.when(endPoint.getAddress()).thenReturn("http://localhost:9443/test");

	Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn(API_NAME);
	Mockito.when(localRegistry.get(API_NAME + "--" + DIRECTION_OUT)).thenReturn(null);


	APIManagerExtensionHandler handler = createAPIManagerExtensionHandler();

	Assert.assertTrue("Invalid request handling from extension handler ", handler.handleRequest(messageContext));
	Assert.assertTrue("Invalid response handling from extension handler ", handler.handleResponse(messageContext));

}
 
Example #23
Source File: APIManagerExtensionHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandleRequestWithGlobalInSeq() {

	MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
	SynapseConfiguration synapseConfig = Mockito.mock(SynapseConfiguration.class);
	org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito
			.mock(org.apache.axis2.context.MessageContext.class);
	Options options = Mockito.mock(Options.class);
	EndpointReference endPoint = Mockito.mock(EndpointReference.class);

	Map localRegistry = Mockito.mock(Map.class);

	Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
	Mockito.when(((Axis2MessageContext) messageContext).getConfiguration()).thenReturn(synapseConfig);
	Mockito.when(synapseConfig.getLocalRegistry()).thenReturn(localRegistry);
	Mockito.when(axis2MsgCntxt.getOptions()).thenReturn(options);
	Mockito.when(options.getTo()).thenReturn(endPoint);
	Mockito.when(endPoint.getAddress()).thenReturn("http://localhost:9443/test");

	Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn(API_NAME);

	SequenceMediator globalInSeq = Mockito.mock(SequenceMediator.class);
	Mockito.when(localRegistry.get(EXT_SEQUENCE_PREFIX + DIRECTION_IN)).thenReturn(globalInSeq);
	Mockito.when(((Mediator) globalInSeq).mediate(messageContext)).thenReturn(true);

	APIManagerExtensionHandler handler = createAPIManagerExtensionHandler();
	// both methods are executed during a full request path
	handler.handleRequest(messageContext);
	handler.handleResponse(messageContext);

	// check whether custom in sequnce is only executed once
	Mockito.verify(globalInSeq, Mockito.times(1)).mediate(messageContext);
}
 
Example #24
Source File: APIManagerExtensionHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandleRequestWithGlobalOutSeq() {
	MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
	SynapseConfiguration synapseConfig = Mockito.mock(SynapseConfiguration.class);
	org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito
			.mock(org.apache.axis2.context.MessageContext.class);
	Options options = Mockito.mock(Options.class);
	EndpointReference endPoint = Mockito.mock(EndpointReference.class);

	Map localRegistry = Mockito.mock(Map.class);

	Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
	Mockito.when(((Axis2MessageContext) messageContext).getConfiguration()).thenReturn(synapseConfig);
	Mockito.when(synapseConfig.getLocalRegistry()).thenReturn(localRegistry);
	Mockito.when(axis2MsgCntxt.getOptions()).thenReturn(options);
	Mockito.when(options.getTo()).thenReturn(endPoint);
	Mockito.when(endPoint.getAddress()).thenReturn("http://localhost:9443/test");

	Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn(API_NAME);


	SequenceMediator globalOutSeq = Mockito.mock(SequenceMediator.class);
	Mockito.when(localRegistry.get(EXT_SEQUENCE_PREFIX + DIRECTION_OUT)).thenReturn(globalOutSeq);
	Mockito.when(((Mediator) globalOutSeq).mediate(messageContext)).thenReturn(true);

	APIManagerExtensionHandler handler = createAPIManagerExtensionHandler();
	// both methods are executed during a full request path
	handler.handleRequest(messageContext);
	handler.handleResponse(messageContext);

	// check whether custom out sequnce is only executed once
	Mockito.verify(globalOutSeq, Mockito.times(1)).mediate(messageContext);
}
 
Example #25
Source File: APIManagerExtensionHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandleRequestWithCustomInSeq() {

	MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
	SynapseConfiguration synapseConfig = Mockito.mock(SynapseConfiguration.class);
	org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito
			.mock(org.apache.axis2.context.MessageContext.class);
	Options options = Mockito.mock(Options.class);
	EndpointReference endPoint = Mockito.mock(EndpointReference.class);

	Map localRegistry = Mockito.mock(Map.class);

	Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
	Mockito.when(((Axis2MessageContext) messageContext).getConfiguration()).thenReturn(synapseConfig);
	Mockito.when(synapseConfig.getLocalRegistry()).thenReturn(localRegistry);
	Mockito.when(axis2MsgCntxt.getOptions()).thenReturn(options);
	Mockito.when(options.getTo()).thenReturn(endPoint);
	Mockito.when(endPoint.getAddress()).thenReturn("http://localhost:9443/test");

	Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn(API_NAME);

	SequenceMediator inSeq = Mockito.mock(SequenceMediator.class);
	Mockito.when(localRegistry.get(API_NAME + "--" + DIRECTION_IN)).thenReturn(inSeq);
	Mockito.when(((Mediator) inSeq).mediate(messageContext)).thenReturn(true);

	APIManagerExtensionHandler handler = createAPIManagerExtensionHandler();
	// both methods are executed during a full request path
	handler.handleRequest(messageContext);
	handler.handleResponse(messageContext);

	// check whether custom in sequnce is only executed once
	Mockito.verify(inSeq, Mockito.times(1)).mediate(messageContext);
}
 
Example #26
Source File: DataProcessAndPublishingAgentTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void setDataReference() throws Exception {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    DataPublisher dataPublisher = Mockito.mock(DataPublisher.class);
    Mockito.when(dataPublisher.tryPublish(Mockito.any(Event.class))).thenReturn(true);
    throttleProperties.setEnabled(true);
    DataProcessAndPublishingAgent dataProcessAndPublishingAgent = new DataProcessAndPublishingAgentWrapper
            (throttleProperties);
    AuthenticationContext authenticationContext = new AuthenticationContext();
    MessageContext messageContext = 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);
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--PizzaShackAPI");
    VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
    verbInfoDTO.setContentAware(false);
    ArrayList<VerbInfoDTO> list = new ArrayList<VerbInfoDTO>();
    list.add(verbInfoDTO);
    Mockito.when(messageContext.getProperty(APIConstants.VERB_INFO_DTO)).thenReturn(list);
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS))
            .thenReturn(new TreeMap<>());
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--PizzaShackAPI");
    dataProcessAndPublishingAgent.setDataReference(applicationLevelThrottleKey, applicationLevelTier,
            apiLevelThrottleKey, apiLevelTier, subscriptionLevelThrottleKey, subscriptionLevelTier,
            resourceLevelThrottleKey, resourceLevelTier, authorizedUser, apiContext, apiVersion, appTenant,
            apiTenant, appId, messageContext, authenticationContext);
    dataProcessAndPublishingAgent.run();
}
 
Example #27
Source File: DataProcessAndPublishingAgentTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void setDataReferenceWithoutApiLevelTier() throws Exception {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnabled(true);
    DataProcessAndPublishingAgent dataProcessAndPublishingAgent = new DataProcessAndPublishingAgentWrapper
            (throttleProperties);
    AuthenticationContext authenticationContext = new AuthenticationContext();
    MessageContext messageContext = Mockito.mock(MessageContext.class);
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--PizzaShackAPI");
    dataProcessAndPublishingAgent.setDataReference(applicationLevelThrottleKey, applicationLevelTier,
            apiLevelThrottleKey, null, subscriptionLevelThrottleKey, subscriptionLevelTier,
            resourceLevelThrottleKey, resourceLevelTier, authorizedUser, apiContext, apiVersion, appTenant,
            apiTenant, appId, messageContext, authenticationContext);
}
 
Example #28
Source File: APIMgtResponseHandlerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void mediateWithStartTimeAndEndTime() throws Exception {
    APIMgtUsageDataPublisher apiMgtUsageDataPublisher = Mockito.mock(APIMgtUsageDataPublisher.class);
    APIManagerAnalyticsConfiguration apiManagerAnalyticsConfiguration = Mockito.mock
            (APIManagerAnalyticsConfiguration.class);
    Mockito.when(apiManagerAnalyticsConfiguration.isBuildMsg()).thenReturn(false);
    APIMgtResponseHandler apiMgtResponseHandler = new APIMgtResponseHandlerWrapper(apiMgtUsageDataPublisher,
            true, false, apiManagerAnalyticsConfiguration);
    MessageContext messageContext = 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);
    Mockito.when(axis2MsgCntxt.getProperty(SynapseConstants.HTTP_SC)).thenReturn(200);
    Mockito.when(messageContext.getProperty("REST_FULL_REQUEST_PATH")).thenReturn("/abc/1.0.0/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONSUMER_KEY)).thenReturn("abc-def-ghi");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.CONTEXT)).thenReturn("/abc/1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_VERSION)).thenReturn("1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API)).thenReturn("api1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.RESOURCE)).thenReturn("/a");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HTTP_METHOD)).thenReturn("GET");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.VERSION)).thenReturn("1.0.0");
    Mockito.when((messageContext.getProperty(SynapseConstants.ERROR_CODE))).thenReturn("404");
    Mockito.when(messageContext.getProperty(SynapseConstants.ERROR_MESSAGE)).thenReturn("not found");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.USER_ID)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.HOST_NAME)).thenReturn("127.0.0.1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_PUBLISHER)).thenReturn("admin");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_NAME)).thenReturn("App1");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_ID)).thenReturn("1");
    Mockito.when(messageContext.getProperty(SynapseConstants.TRANSPORT_IN_NAME)).thenReturn("https");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--api1-1.0.0");
    Mockito.when(messageContext.getProperty(RESTConstants.REST_URL_PREFIX)).thenReturn("https://localhost");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.REQUEST_START_TIME)).thenReturn("10");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.BACKEND_REQUEST_START_TIME)).thenReturn("11");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.BACKEND_REQUEST_END_TIME)).thenReturn(13);
    apiMgtResponseHandler.mediate(messageContext);
}
 
Example #29
Source File: DataProcessAndPublishingAgentTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void setIPCondition() throws Exception {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnabled(true);
    DataProcessAndPublishingAgent dataProcessAndPublishingAgent = new DataProcessAndPublishingAgentWrapper
            (throttleProperties);
    AuthenticationContext authenticationContext = new AuthenticationContext();
    MessageContext messageContext = 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);
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--PizzaShackAPI");
    TreeMap headers = new TreeMap();
    headers.put(APIMgtGatewayConstants.X_FORWARDED_FOR, "192.168.1.1");
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS))
            .thenReturn(headers);
    VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
    verbInfoDTO.setContentAware(false);
    ArrayList<VerbInfoDTO> list = new ArrayList<VerbInfoDTO>();
    list.add(verbInfoDTO);
    Mockito.when(messageContext.getProperty(APIConstants.VERB_INFO_DTO)).thenReturn(list);
    dataProcessAndPublishingAgent.setDataReference(applicationLevelThrottleKey, applicationLevelTier,
            apiLevelThrottleKey, null, subscriptionLevelThrottleKey, subscriptionLevelTier,
            resourceLevelThrottleKey, resourceLevelTier, authorizedUser, apiContext, apiVersion, appTenant,
            apiTenant, appId, messageContext, authenticationContext);
    dataProcessAndPublishingAgent.run();
}
 
Example #30
Source File: InternalAPIDispatcher.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private APIResource findResource(MessageContext synCtx, InternalAPI internalApi) {

        org.apache.axis2.context.MessageContext axis2Ctx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
        String method = (String) axis2Ctx.getProperty(Constants.Configuration.HTTP_METHOD);

        String path = (String) synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH);
        String subPath = path.substring(internalApi.getContext().length());
        if ("".equals(subPath)) {
            subPath = "/";
        }

        for (APIResource resource : internalApi.getResources()) {
            if (!resource.getMethods().contains(method)) {
                continue;
            }
            DispatcherHelper helper = resource.getDispatcherHelper();
            URITemplateHelper templateHelper = (URITemplateHelper) helper;
            Map<String, String> variables = new HashMap<>();
            if (templateHelper.getUriTemplate().matches(subPath, variables)) {
                for (Map.Entry<String, String> entry : variables.entrySet()) {
                    synCtx.setProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + entry.getKey(), entry.getValue());
                }
                RESTUtils.populateQueryParamsToMessageContext(synCtx);
                return resource;
            }
        }
        return null;
    }