use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.
the class InsertTask method insertBatch.
@SuppressWarnings("unchecked")
protected void insertBatch(List<Record> batch, boolean checkVertex) {
HugeClient client = this.context.client();
List<GraphElement> elements = new ArrayList<>(batch.size());
batch.forEach(r -> elements.add(r.element()));
if (this.type().isVertex()) {
client.graph().addVertices((List<Vertex>) (Object) elements);
} else {
client.graph().addEdges((List<Edge>) (Object) elements, checkVertex);
}
}
use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.
the class AuthManagerTest method testAuth.
@Test
public void testAuth() {
User user = new User();
user.name("bob");
user.password("123456");
user = auth().createUser(user);
Group group = new Group();
group.name("managers");
group = auth().createGroup(group);
Target gremlin = new Target();
gremlin.name("gremlin");
gremlin.graph("hugegraph");
gremlin.url("127.0.0.1:8080");
gremlin.resources(new HugeResource(HugeResourceType.GREMLIN));
gremlin = auth().createTarget(gremlin);
Target task = new Target();
task.name("task");
task.graph("hugegraph");
task.url("127.0.0.1:8080");
task.resources(new HugeResource(HugeResourceType.TASK));
task = auth().createTarget(task);
Belong belong = new Belong();
belong.user(user);
belong.group(group);
belong = auth().createBelong(belong);
Access access1 = new Access();
access1.group(group);
access1.target(gremlin);
access1.permission(HugePermission.EXECUTE);
access1 = auth().createAccess(access1);
Access access2 = new Access();
access2.group(group);
access2.target(task);
access2.permission(HugePermission.READ);
access2 = auth().createAccess(access2);
Project project1 = new Project("test");
project1 = auth().createProject(project1);
Assert.assertEquals("test", project1.name());
Project project2 = new Project("test2");
project2 = auth().createProject(project2);
Assert.assertEquals("test2", project2.name());
Project newProject1 = auth().getProject(project1);
Assert.assertEquals(newProject1.id(), project1.id());
Assert.assertTrue(CollectionUtils.isEmpty(newProject1.graphs()));
List<Project> projects = auth().listProjects();
Assert.assertNotNull(projects);
Assert.assertEquals(2, projects.size());
Set<String> graphs = ImmutableSet.of("graph1", "graph2");
newProject1 = auth().projectAddGraphs(project1, graphs);
Assert.assertNotNull(newProject1);
Assert.assertEquals(graphs, newProject1.graphs());
graphs = ImmutableSet.of("graph2");
newProject1 = auth().projectRemoveGraphs(project1, ImmutableSet.of("graph1"));
Assert.assertNotNull(newProject1);
Assert.assertEquals(graphs, newProject1.graphs());
Object project1Id = project1.id();
project1 = new Project(project1Id);
project1.description("test description");
newProject1 = auth().updateProject(project1);
Assert.assertEquals(newProject1.description(), project1.description());
auth().deleteProject(project2);
projects.remove(project2);
List<Project> newProjects = auth().listProjects();
Assert.assertEquals(newProjects, projects);
UserRole role = auth().getUserRole(user);
String r = "{\"roles\":{\"hugegraph\":" + "{\"READ\":[{\"type\":\"TASK\",\"label\":\"*\",\"properties\":null}]," + "\"EXECUTE\":[{\"type\":\"GREMLIN\",\"label\":\"*\",\"properties\":null}]}}}";
Assert.assertEquals(r, role.toString());
Login login = new Login();
login.name("bob");
login.password("123456");
LoginResult result = auth().login(login);
String token = result.token();
HugeClient client = baseClient();
client.setAuthContext("Bearer " + token);
TokenPayload payload = auth().verifyToken();
Assert.assertEquals("bob", payload.username());
Assert.assertEquals(user.id(), payload.userId());
auth().logout();
client.resetAuthContext();
}
use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.
the class GraphConnectionController method get.
@GetMapping("{id}")
public GraphConnection get(@PathVariable("id") int id) {
GraphConnection entity = this.connService.get(id);
if (entity == null) {
throw new ExternalException("graph-connection.not-exist.id", id);
}
if (!this.poolService.containsKey(id)) {
this.sslService.configSSL(this.config, entity);
HugeClient client = HugeClientUtil.tryConnect(entity);
this.poolService.put(entity, client);
}
return entity;
}
use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.
the class HugeClientTest method testContext.
@Test
public void testContext() {
HugeClient client = HugeClient.builder(BASE_URL, GRAPH).configUser(USERNAME, PASSWORD).build();
String token = "Bearer token";
client.setAuthContext(token);
Assert.assertEquals(token, client.getAuthContext());
client.resetAuthContext();
Assert.assertNull(client.getAuthContext());
}
use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.
the class GraphsApiTest method testCreateAndDropGraph.
@Test
public void testCreateAndDropGraph() {
int initialGraphNumber = graphsAPI.list().size();
// Create new graph dynamically
String config;
try {
config = FileUtils.readFileToString(new File(CONFIG2_PATH), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new ClientException("Failed to read config file: %s", CONFIG2_PATH);
}
Map<String, String> result = graphsAPI.create(GRAPH2, null, config);
Assert.assertEquals(2, result.size());
Assert.assertEquals(GRAPH2, result.get("name"));
Assert.assertEquals("rocksdb", result.get("backend"));
Assert.assertEquals(initialGraphNumber + 1, graphsAPI.list().size());
HugeClient client = new HugeClient(baseClient(), GRAPH2);
// Insert graph schema and data
initPropertyKey(client);
initVertexLabel(client);
initEdgeLabel(client);
List<Vertex> vertices = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
Vertex vertex = new Vertex("person").property("name", "person" + i).property("city", "Beijing").property("age", 19);
vertices.add(vertex);
}
vertices = client.graph().addVertices(vertices);
List<Edge> edges = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
Edge edge = new Edge("knows").source(vertices.get(i)).target(vertices.get((i + 1) % 100)).property("date", "2016-01-10");
edges.add(edge);
}
client.graph().addEdges(edges, false);
// Query vertices and edges count from new created graph
ResultSet resultSet = client.gremlin().gremlin("g.V().count()").execute();
Assert.assertEquals(100, resultSet.iterator().next().getInt());
resultSet = client.gremlin().gremlin("g.E().count()").execute();
Assert.assertEquals(100, resultSet.iterator().next().getInt());
// Clear graph schema and data from new created graph
graphsAPI.clear(GRAPH2, "I'm sure to delete all data");
resultSet = client.gremlin().gremlin("g.V().count()").execute();
Assert.assertEquals(0, resultSet.iterator().next().getInt());
resultSet = client.gremlin().gremlin("g.E().count()").execute();
Assert.assertEquals(0, resultSet.iterator().next().getInt());
Assert.assertTrue(client.schema().getPropertyKeys().isEmpty());
Assert.assertEquals(initialGraphNumber + 1, graphsAPI.list().size());
// Remove new created graph dynamically
graphsAPI.drop(GRAPH2, "I'm sure to drop the graph");
Assert.assertEquals(initialGraphNumber, graphsAPI.list().size());
}
Aggregations