Skip to content

Commit

Permalink
Added test cases for HTTP layer
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Jul 17, 2015
1 parent 4d55c73 commit 923183a
Show file tree
Hide file tree
Showing 10 changed files with 592 additions and 143 deletions.
17 changes: 15 additions & 2 deletions server/pom.xml
Expand Up @@ -12,7 +12,7 @@
~ */ -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -36,7 +36,7 @@
</osgi.import>
<osgi.export>com.orientechnologies.orient.server.*</osgi.export>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<argLine>-ea -Xmx2048m </argLine>
<argLine>-ea -Xmx2048m</argLine>
</properties>

<dependencies>
Expand Down Expand Up @@ -65,6 +65,19 @@
<version>1.9.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.4</version>
</dependency>

</dependencies>

<build>
Expand Down
@@ -1,150 +1,149 @@
/*
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
* *
* * For more information: http://www.orientechnologies.com
*
*/
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
* *
* * For more information: http://www.orientechnologies.com
*
*/

package com.orientechnologies.orient.server.network.protocol.http;

import com.orientechnologies.common.util.OPatternConst;
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.server.OServer;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommand;
import com.orientechnologies.orient.server.OServer;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommand;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OHttpNetworkCommandManager {

private static final String URL_PART_PATTERN = "([a-zA-Z0-9%:\\\\+]*)";


private final Map<String, OServerCommand> exactCommands = new ConcurrentHashMap<String, OServerCommand>();
private final Map<String, OServerCommand> wildcardCommands = new ConcurrentHashMap<String, OServerCommand>();
private final Map<String, OServerCommand> restCommands = new ConcurrentHashMap<String, OServerCommand>();
private final OHttpNetworkCommandManager parent;
private final OServer server;

public OHttpNetworkCommandManager(final OServer iServer, final OHttpNetworkCommandManager iParent) {
server = iServer;
parent = iParent;
}

public Object getCommand(final String iName) {
OServerCommand cmd = exactCommands.get(iName);

if (cmd == null) {
for (Entry<String, OServerCommand> entry : restCommands.entrySet()) {
if (matches(entry.getKey(), iName)) {
return entry.getValue();
}
}
}
if (cmd == null) {
// TRY WITH WILDCARD COMMANDS
// TODO: OPTIMIZE SEARCH!
String partLeft, partRight;
for (Entry<String, OServerCommand> entry : wildcardCommands.entrySet()) {
final int wildcardPos = entry.getKey().indexOf('*');
partLeft = entry.getKey().substring(0, wildcardPos);
partRight = entry.getKey().substring(wildcardPos + 1);

if (iName.startsWith(partLeft) && iName.endsWith(partRight)) {
cmd = entry.getValue();
break;
}
}
}

if (cmd == null && parent != null)
cmd = (OServerCommand) parent.getCommand(iName);

return cmd;
}

/**
* Register all the names for the same instance.
*
* @param iServerCommandInstance
*/
public void registerCommand(final OServerCommand iServerCommandInstance) {
for (String name : iServerCommandInstance.getNames())
if (OStringSerializerHelper.contains(name, '{')) {
restCommands.put(name, iServerCommandInstance);
} else if (OStringSerializerHelper.contains(name, '*'))
wildcardCommands.put(name, iServerCommandInstance);
else
exactCommands.put(name, iServerCommandInstance);
iServerCommandInstance.configure(server);
}

public Map<String, String> extractUrlTokens(String requestUrl) {
Map<String, String> result = new HashMap<String, String>();
String urlPattern = findUrlPattern(requestUrl);
if (urlPattern == null) {
return result;
}
String matcherUrl = OPatternConst.PATTERN_REST_URL.matcher(urlPattern).replaceAll(URL_PART_PATTERN);

matcherUrl = matcherUrl.substring(matcherUrl.indexOf('|') + 1);
requestUrl = requestUrl.substring(requestUrl.indexOf('|') + 1);

Pattern pattern = Pattern.compile(matcherUrl);
Matcher matcher = pattern.matcher(requestUrl);
if (matcher.find()) {
Matcher templateMatcher = OPatternConst.PATTERN_REST_URL.matcher(urlPattern);
int i = 1;
String key;
while (templateMatcher.find()) {
key = templateMatcher.group();
key = key.substring(1);
key = key.substring(0, key.length() - 1);
String value = matcher.group(i++);
result.put(key, value);
}
}
return result;
}

protected String findUrlPattern(String requestUrl) {
for (Entry<String, OServerCommand> entry : restCommands.entrySet()) {
if (matches(entry.getKey(), requestUrl)) {
return entry.getKey();
}
}
if (parent == null) {
return null;
} else {
return parent.findUrlPattern(requestUrl);
}
}

private boolean matches(String urlPattern, String requestUrl) {
String matcherUrl = OPatternConst.PATTERN_REST_URL.matcher(urlPattern).replaceAll(URL_PART_PATTERN);

if (!matcherUrl.substring(0, matcherUrl.indexOf('|') + 1).equals(requestUrl.substring(0, requestUrl.indexOf('|') + 1))) {
return false;
}
matcherUrl = matcherUrl.substring(matcherUrl.indexOf('|') + 1);
requestUrl = requestUrl.substring(requestUrl.indexOf('|') + 1);
return requestUrl.matches(matcherUrl);
}
}
private static final String URL_PART_PATTERN = "([a-zA-Z0-9%:\\\\+]*)";

private final Map<String, OServerCommand> exactCommands = new ConcurrentHashMap<String, OServerCommand>();
private final Map<String, OServerCommand> wildcardCommands = new ConcurrentHashMap<String, OServerCommand>();
private final Map<String, OServerCommand> restCommands = new ConcurrentHashMap<String, OServerCommand>();
private final OHttpNetworkCommandManager parent;
private final OServer server;

public OHttpNetworkCommandManager(final OServer iServer, final OHttpNetworkCommandManager iParent) {
server = iServer;
parent = iParent;
}

public Object getCommand(final String iName) {
OServerCommand cmd = exactCommands.get(iName);

if (cmd == null) {
for (Entry<String, OServerCommand> entry : restCommands.entrySet()) {
if (matches(entry.getKey(), iName)) {
return entry.getValue();
}
}
}
if (cmd == null) {
// TRY WITH WILDCARD COMMANDS
// TODO: OPTIMIZE SEARCH!
String partLeft, partRight;
for (Entry<String, OServerCommand> entry : wildcardCommands.entrySet()) {
final int wildcardPos = entry.getKey().indexOf('*');
partLeft = entry.getKey().substring(0, wildcardPos);
partRight = entry.getKey().substring(wildcardPos + 1);

if (iName.startsWith(partLeft) && iName.endsWith(partRight)) {
cmd = entry.getValue();
break;
}
}
}

if (cmd == null && parent != null)
cmd = (OServerCommand) parent.getCommand(iName);

return cmd;
}

/**
* Register all the names for the same instance.
*
* @param iServerCommandInstance
*/
public void registerCommand(final OServerCommand iServerCommandInstance) {
for (String name : iServerCommandInstance.getNames())
if (OStringSerializerHelper.contains(name, '{')) {
restCommands.put(name, iServerCommandInstance);
} else if (OStringSerializerHelper.contains(name, '*'))
wildcardCommands.put(name, iServerCommandInstance);
else
exactCommands.put(name, iServerCommandInstance);
iServerCommandInstance.configure(server);
}

public Map<String, String> extractUrlTokens(String requestUrl) {
Map<String, String> result = new HashMap<String, String>();
String urlPattern = findUrlPattern(requestUrl);
if (urlPattern == null) {
return result;
}
String matcherUrl = OPatternConst.PATTERN_REST_URL.matcher(urlPattern).replaceAll(URL_PART_PATTERN);

matcherUrl = matcherUrl.substring(matcherUrl.indexOf('|') + 1);
requestUrl = requestUrl.substring(requestUrl.indexOf('|') + 1);

Pattern pattern = Pattern.compile(matcherUrl);
Matcher matcher = pattern.matcher(requestUrl);
if (matcher.find()) {
Matcher templateMatcher = OPatternConst.PATTERN_REST_URL.matcher(urlPattern);
int i = 1;
String key;
while (templateMatcher.find()) {
key = templateMatcher.group();
key = key.substring(1);
key = key.substring(0, key.length() - 1);
String value = matcher.group(i++);
result.put(key, value);
}
}
return result;
}

protected String findUrlPattern(String requestUrl) {
for (Entry<String, OServerCommand> entry : restCommands.entrySet()) {
if (matches(entry.getKey(), requestUrl)) {
return entry.getKey();
}
}
if (parent == null) {
return null;
} else {
return parent.findUrlPattern(requestUrl);
}
}

private boolean matches(String urlPattern, String requestUrl) {
String matcherUrl = OPatternConst.PATTERN_REST_URL.matcher(urlPattern).replaceAll(URL_PART_PATTERN);

if (!matcherUrl.substring(0, matcherUrl.indexOf('|') + 1).equals(requestUrl.substring(0, requestUrl.indexOf('|') + 1))) {
return false;
}
matcherUrl = matcherUrl.substring(matcherUrl.indexOf('|') + 1);
requestUrl = requestUrl.substring(requestUrl.indexOf('|') + 1);
return requestUrl.matches(matcherUrl);
}
}
Expand Up @@ -51,7 +51,7 @@ public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) thr

doc.save();

iResponse.send(OHttpUtils.STATUS_CREATED_CODE, OHttpUtils.STATUS_CREATED_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN,
iResponse.send(OHttpUtils.STATUS_CREATED_CODE, OHttpUtils.STATUS_CREATED_DESCRIPTION, OHttpUtils.CONTENT_JSON,
doc.toJSON(), OHttpUtils.HEADER_ETAG + doc.getVersion());

} finally {
Expand Down
@@ -0,0 +1,29 @@
package com.orientechnologies.orient.server.network.http;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
* Test HTTP "query" command.
*
* @author Luca Garulli (l.garulli--at-orientechnologies.com)
*/
@Test
public abstract class BaseHttpDatabaseTest extends BaseHttpTest {
@BeforeClass
public void createDatabase() throws Exception {
super.startServer();
Assert.assertEquals(post("database/" + getDatabaseName() + "/memory").setUserName("root").setUserPassword("root").getResponse()
.getStatusLine().getStatusCode(), 200);
}

@AfterClass
public void dropDatabase() throws Exception {
Assert.assertEquals(delete("database/" + getDatabaseName()).setUserName("root").setUserPassword("root").getResponse()
.getStatusLine().getStatusCode(), 204);
super.stopServer();
}

}

0 comments on commit 923183a

Please sign in to comment.