Skip to content

Commit

Permalink
chore: code style
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxxoo committed Jun 10, 2021
1 parent 4227ce6 commit a06ce80
Show file tree
Hide file tree
Showing 31 changed files with 523 additions and 475 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public final class ApiVersion {
*/

// The second parameter of Version.of() is for IDE running without JAR
public static final Version VERSION = Version.of(ApiVersion.class, "0.58");
public static final Version VERSION = Version.of(ApiVersion.class, "0.61");

public static final void check() {
// Check version of hugegraph-core. Firstly do check from version 0.3
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,7 @@
TaskApiTest.class,
GremlinApiTest.class,
MetricsApiTest.class,
UserApiTest.class,
AllShortestPathsAPITest.class,
CountAPITest.class,
CrosspointsAPITest.class,
CustomizedCrosspointsAPITest.class,
EdgesAPITest.class,
FusiformSimilarityAPITest.class,
JaccardSimilarityAPITest.class,
KneighborAPITest.class,
KoutAPITest.class,
MultiNodeShortestPathAPITest.class,
NeighborRankAPITest.class,
PathsAPITest.class,
PersonalRankAPITest.class,
RaysAPITest.class
UserApiTest.class
})
public class ApiTestSuite {

Expand Down
215 changes: 103 additions & 112 deletions hugegraph-test/src/main/java/com/baidu/hugegraph/api/BaseApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,21 @@

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.glassfish.grizzly.utils.Pair;
import org.apache.http.util.TextUtils;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.client.filter.EncodingFilter;
import org.glassfish.jersey.message.GZipEncoder;
Expand All @@ -53,6 +50,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;

public class BaseApiTest {

Expand Down Expand Up @@ -98,7 +96,7 @@ public static RestClient newClient() {
return new RestClient(BASE_URL);
}

static class RestClient {
public static class RestClient {

private Client client;
private WebTarget target;
Expand Down Expand Up @@ -132,42 +130,20 @@ public Response get(String path, String id) {
return this.target.path(path).path(id).request().get();
}

public Response get(String path, Map<String, Object> params) {
return this.get(path,
params.entrySet().stream()
.map(entry -> new Pair<String, Object>(
entry.getKey(), entry.getValue()))
.iterator());
public Response get(String path, Multimap<String, Object> params) {
WebTarget target = this.target.path(path);
for (Map.Entry<String, Object> entries : params.entries()) {
target = target.queryParam(entries.getKey(), entries.getValue());
}
return target.request().get();
}

public Response get(String path,
Iterator<Pair<String, Object>> params) {
class WrapWebTarget {
WebTarget target;

WrapWebTarget(WebTarget target) {
this.target = target;
}

public void accept(Pair<String, Object> pair) {
this.target = this.target.queryParam(pair.getFirst(),
pair.getSecond());
}
public Response get(String path, Map<String, Object> params) {
WebTarget target = this.target.path(path);
for (Map.Entry<String, Object> i : params.entrySet()) {
target = target.queryParam(i.getKey(), i.getValue());
}

return StreamSupport
.stream(Spliterators.spliteratorUnknownSize(params,
Spliterator.ORDERED),
false)
.collect(() -> new WrapWebTarget(this.target.path(path)),
WrapWebTarget::accept,
(t1, t2) -> {
throw new IllegalStateException(
"unreach code");
})
.target
.request()
.get();
return target.request().get();
}

public Response post(String path, String content) {
Expand Down Expand Up @@ -322,67 +298,66 @@ protected static void initIndexLabel() {

protected static void initEdge() {
String path = URL_PREFIX + GRAPH_EDGE + BATCH;
Map<String, String> ret = getAllName2VertexIds();
System.out.println(ret);
Map<String, String> ret = listAllVertexName2Ids();
String markoId = ret.get("marko");
String peterId = ret.get("peter");
String joshId = ret.get("josh");
String vadasId = ret.get("vadas");
String rippleId = ret.get("ripple");

createAndAssert(path, "[" +
"{" +
"\"label\": \"knows\"," +
"\"outV\": \"" + markoId + "\"," +
"\"inV\": \"" + peterId + "\"," +
"\"outVLabel\": \"person\"," +
"\"inVLabel\": \"person\"," +
"\"properties\": {" +
"\"date\": \"2021-01-01\"," +
"\"weight\":0.5}" +
"}," +
"{" +
"\"label\": \"knows\"," +
"\"outV\": \"" + peterId + "\"," +
"\"inV\": \"" + joshId + "\"," +
"\"outVLabel\": \"person\"," +
"\"inVLabel\": \"person\"," +
"\"properties\": {" +
"\"date\": \"2021-01-01\"," +
"\"weight\":0.5}" +
"}," +
"{" +
"\"label\": \"knows\"," +
"\"outV\": \"" + joshId + "\"," +
"\"inV\": \"" + vadasId + "\"," +
"\"outVLabel\": \"person\"," +
"\"inVLabel\": \"person\"," +
"\"properties\": {" +
"\"date\": \"2021-01-01\"," +
"\"weight\":0.5}" +
"}," +
"{" +
"\"label\": \"created\"," +
"\"outV\": \"" + markoId + "\"," +
"\"inV\": \"" + rippleId + "\"," +
"\"outVLabel\": \"person\"," +
"\"inVLabel\": \"software\"," +
"\"properties\": {" +
"\"date\": \"2021-01-01\"," +
"\"weight\":0.5}" +
"}," +
"{" +
"\"label\": \"created\"," +
"\"outV\": \"" + peterId + "\"," +
"\"inV\": \"" + rippleId + "\"," +
"\"outVLabel\": \"person\"," +
"\"inVLabel\": \"software\"," +
"\"properties\": {" +
"\"date\": \"2021-01-01\"," +
"\"weight\":0.5}" +
"}" +
"]");

String body = String.format("[{"
+ "\"label\": \"knows\","
+ "\"outV\": \"%s\","
+ "\"inV\": \"%s\","
+ "\"outVLabel\": \"person\","
+ "\"inVLabel\": \"person\","
+ "\"properties\": {"
+ " \"date\": \"2021-01-01\","
+ " \"weight\":0.5}"
+ "},"
+ "{"
+ "\"label\": \"knows\","
+ "\"outV\": \"%s\","
+ "\"inV\": \"%s\","
+ "\"outVLabel\": \"person\","
+ "\"inVLabel\": \"person\","
+ "\"properties\": {"
+ " \"date\": \"2021-01-01\","
+ " \"weight\":0.5}"
+ "},"
+ "{"
+ "\"label\": \"knows\","
+ "\"outV\": \"%s\","
+ "\"inV\": \"%s\","
+ "\"outVLabel\": \"person\","
+ "\"inVLabel\": \"person\","
+ "\"properties\": {"
+ " \"date\": \"2021-01-01\","
+ " \"weight\":0.5}"
+ "},"
+ "{"
+ "\"label\": \"created\","
+ "\"outV\": \"%s\","
+ "\"inV\": \"%s\","
+ "\"outVLabel\": \"person\","
+ "\"inVLabel\": \"software\","
+ "\"properties\": {"
+ " \"date\": \"2021-01-01\","
+ " \"weight\":0.5}"
+ "},"
+ "{"
+ "\"label\": \"created\","
+ "\"outV\": \"%s\","
+ "\"inV\": \"%s\","
+ "\"outVLabel\": \"person\","
+ "\"inVLabel\": \"software\","
+ "\"properties\": {"
+ " \"date\": \"2021-01-01\","
+ " \"weight\":0.5}"
+ "}]", markoId, peterId, peterId, joshId,
joshId, vadasId, markoId, rippleId,
peterId, rippleId);
createAndAssert(path, body);
}

protected static void initVertex() {
Expand Down Expand Up @@ -450,24 +425,39 @@ protected static Response createAndAssert(String path, String body) {
return r;
}

protected static Map<String, String> getAllName2VertexIds() {
protected static Map<String, String> listAllVertexName2Ids() {
Response r = client.get(URL_PREFIX + GRAPH_VERTEX);
String content = r.readEntity(String.class);
if (r.getStatus() != 200) {
throw new HugeException("Failed to get vertex id: %s", content);
}
String content = assertResponseStatus(200, r);

@SuppressWarnings("rawtypes")
List<Map> list = readList(content, "vertices", Map.class);
List<Map> vertices = readList(content, "vertices", Map.class);

Map<String, String> vertextName2Ids = new HashMap<>();
for (Map vertice : vertices) {
Map properties = (Map) vertice.get("properties");
if (properties == null ||
!properties.containsKey("name") ||
!vertice.containsKey("id")) {
continue;
}
String name = (String) properties.get("name");
if (TextUtils.isEmpty(name)) {
continue;
}

String id = (String) vertice.get("id");
if (TextUtils.isEmpty(id)) {
continue;
}

vertextName2Ids.put(name, id);
}

return vertextName2Ids;
}

return list.stream().filter(map -> map.get("properties") != null &&
((Map) map.get("properties"))
.get("name") != null)
.collect(
Collectors.toMap(map -> ((Map) map.get("properties"))
.get(
"name").toString(),
map -> map.get("id").toString()));
protected static String id2Json(String params) {
return String.format("\"%s\"", params);
}

protected static String getVertexId(String label, String key, String value)
Expand All @@ -478,10 +468,7 @@ protected static String getVertexId(String label, String key, String value)
"properties", URLEncoder.encode(props, "UTF-8")
);
Response r = client.get(URL_PREFIX + GRAPH_VERTEX, params);
String content = r.readEntity(String.class);
if (r.getStatus() != 200) {
throw new HugeException("Failed to get vertex id: %s", content);
}
String content = assertResponseStatus(200, r);

@SuppressWarnings("rawtypes")
List<Map> list = readList(content, "vertices", Map.class);
Expand Down Expand Up @@ -603,6 +590,10 @@ protected static String assertResponseStatus(int status,
return content;
}

protected static Map<String, Object> parseMap(String json) {
return JsonUtil.fromJson(json, Map.class);
}

public static <T> T assertJsonContains(String response, String key) {
Map<?, ?> json = JsonUtil.fromJson(response, Map.class);
return assertMapContains(json, key);
Expand Down
Loading

0 comments on commit a06ce80

Please sign in to comment.