use of org.apache.synapse.api.version.VersionStrategy 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));
}
use of org.apache.synapse.api.version.VersionStrategy in project wso2-synapse by wso2.
the class APIFactory method createAPI.
public static API createAPI(OMElement apiElt, Properties properties) {
OMAttribute nameAtt = apiElt.getAttribute(new QName("name"));
if (nameAtt == null || "".equals(nameAtt.getAttributeValue())) {
handleException("Attribute 'name' is required for an API definition");
}
OMAttribute contextAtt = apiElt.getAttribute(new QName("context"));
if (contextAtt == null || "".equals(contextAtt.getAttributeValue())) {
handleException("Attribute 'context' is required for an API definition");
}
API api = new API(nameAtt.getAttributeValue(), contextAtt.getAttributeValue());
OMAttribute hostAtt = apiElt.getAttribute(new QName("hostname"));
if (hostAtt != null && !"".equals(hostAtt.getAttributeValue())) {
api.setHost(hostAtt.getAttributeValue());
}
VersionStrategy vStrategy = VersionStrategyFactory.createVersioningStrategy(api, apiElt);
api.setVersionStrategy(vStrategy);
OMAttribute portAtt = apiElt.getAttribute(new QName("port"));
if (portAtt != null && !"".equals(portAtt.getAttributeValue())) {
api.setPort(Integer.parseInt(portAtt.getAttributeValue()));
}
OMAttribute publishSwagger = apiElt.getAttribute(new QName("publishSwagger"));
if (publishSwagger != null) {
api.setSwaggerResourcePath(publishSwagger.getAttributeValue());
}
InboundApiUtils.addBindsTo(api, apiElt);
Iterator resources = apiElt.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "resource"));
boolean noResources = true;
while (resources.hasNext()) {
OMElement resourceElt = (OMElement) resources.next();
api.addResource(ResourceFactory.createResource(resourceElt, properties));
noResources = false;
}
if (noResources) {
handleException("An API must contain at least one resource definition");
}
OMElement handlersElt = apiElt.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "handlers"));
if (handlersElt != null) {
Iterator handlers = handlersElt.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "handler"));
while (handlers.hasNext()) {
OMElement handlerElt = (OMElement) handlers.next();
defineHandler(api, handlerElt);
}
}
OMAttribute trans = apiElt.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "transports"));
if (trans != null) {
String transports = trans.getAttributeValue();
if (!"".equals(transports)) {
if (Constants.TRANSPORT_HTTP.equals(transports)) {
api.setProtocol(RESTConstants.PROTOCOL_HTTP_ONLY);
} else if (Constants.TRANSPORT_HTTPS.equals(transports)) {
api.setProtocol(RESTConstants.PROTOCOL_HTTPS_ONLY);
} else {
handleException("Invalid protocol name: " + transports);
}
}
}
String nameString = api.getName();
if (nameString == null || "".equals(nameString)) {
nameString = SynapseConstants.ANONYMOUS_API;
}
AspectConfiguration aspectConfiguration = new AspectConfiguration(nameString);
api.configure(aspectConfiguration);
OMAttribute statistics = apiElt.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.STATISTICS_ATTRIB_NAME));
if (statistics != null) {
String statisticsValue = statistics.getAttributeValue();
if (statisticsValue != null) {
if (XMLConfigConstants.STATISTICS_ENABLE.equals(statisticsValue)) {
aspectConfiguration.enableStatistics();
}
}
}
OMAttribute tracing = apiElt.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.TRACE_ATTRIB_NAME));
if (tracing != null) {
String tracingValue = tracing.getAttributeValue();
if (tracingValue != null) {
if (XMLConfigConstants.TRACE_ENABLE.equals(tracingValue)) {
aspectConfiguration.enableTracing();
}
}
}
CommentListUtil.populateComments(apiElt, api.getCommentsList());
return api;
}
Aggregations