Skip to content

Commit 944698a

Browse files
committed
fixed issue arangodb#6 : added method to create edges outside graph context
1 parent 4a8f129 commit 944698a

File tree

6 files changed

+268
-6
lines changed

6 files changed

+268
-6
lines changed

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public class ArangoDriver extends BaseArangoDriver {
6464
private InternalEndpointDriver endpointDriver;
6565
private InternalReplicationDriver replicationDriver;
6666
private InternalGraphDriver graphDriver;
67+
private InternalEdgeDriver edgeDriver;
6768
private InternalTransactionDriver transactionDriver;
6869

6970
private String database;
@@ -114,6 +115,7 @@ private void createModuleDrivers(boolean createProxys) {
114115
this.endpointDriver = ImplFactory.createEndpointDriver(configure, this.httpManager);
115116
this.replicationDriver = ImplFactory.createReplicationDriver(configure, this.httpManager);
116117
this.graphDriver = ImplFactory.createGraphDriver(configure, cursorDriver, this.httpManager);
118+
this.edgeDriver = ImplFactory.createEdgeDriver(configure, cursorDriver, this.httpManager);
117119
this.jobsDriver = ImplFactory.createJobsDriver(configure, this.httpManager);
118120
this.transactionDriver = ImplFactory.createTransactionDriver(configure, this.httpManager);
119121
} else {
@@ -174,9 +176,13 @@ private void createModuleDrivers(boolean createProxys) {
174176
new Class<?>[] { InternalReplicationDriver.class },
175177
new InvocationHandlerImpl(this.replicationDriver));
176178
this.graphDriver = (InternalGraphDriver) Proxy.newProxyInstance(
177-
InternalGraphDriver.class.getClassLoader(),
178-
new Class<?>[] { InternalGraphDriver.class },
179-
new InvocationHandlerImpl(this.graphDriver));
179+
InternalGraphDriver.class.getClassLoader(),
180+
new Class<?>[] { InternalGraphDriver.class },
181+
new InvocationHandlerImpl(this.graphDriver));
182+
this.edgeDriver = (InternalEdgeDriver) Proxy.newProxyInstance(
183+
InternalEdgeDriver.class.getClassLoader(),
184+
new Class<?>[] { InternalEdgeDriver.class },
185+
new InvocationHandlerImpl(this.edgeDriver));
180186
}
181187
}
182188

@@ -4277,4 +4283,38 @@ public TransactionEntity createTransaction(String action) {
42774283
public TransactionResultEntity executeTransaction(TransactionEntity transactionEntity) throws ArangoException {
42784284
return this.transactionDriver.executeTransaction(getDefaultDatabase(), transactionEntity);
42794285
}
4286+
4287+
/**
4288+
* Create an edge in an edge collection.
4289+
*
4290+
* @param databaseName the database name
4291+
* @param collectionName name of the edge collection
4292+
* @param object the edge object
4293+
* @param from id of document 'from'
4294+
* @param to id of document 'to'
4295+
* @param createCollection if true, the collection will be created if it does not exists
4296+
* @param waitForSync wait for sync
4297+
* @return the new created EdgeEntity object
4298+
* @throws ArangoException
4299+
*/
4300+
public <T> EdgeEntity<T> createEdge(
4301+
String databaseName,
4302+
String collectionName,
4303+
Object object,
4304+
String from,
4305+
String to,
4306+
Boolean createCollection,
4307+
Boolean waitForSync
4308+
) throws ArangoException {
4309+
4310+
return this.edgeDriver.createEdge(
4311+
databaseName,
4312+
collectionName,
4313+
object,
4314+
from,
4315+
to,
4316+
createCollection,
4317+
waitForSync);
4318+
}
4319+
42804320
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
* Copyright holder is triAGENS GmbH, Cologne, Germany
16+
*
17+
* @author fbartels
18+
* @author gschwab
19+
* @author Copyright 2014, triAGENS GmbH, Cologne, Germany
20+
*/
21+
22+
package com.arangodb;
23+
24+
import java.util.Collection;
25+
import java.util.List;
26+
27+
import com.arangodb.entity.CursorEntity;
28+
import com.arangodb.entity.DeletedEntity;
29+
import com.arangodb.entity.Direction;
30+
import com.arangodb.entity.DocumentEntity;
31+
import com.arangodb.entity.EdgeDefinitionEntity;
32+
import com.arangodb.entity.EdgeEntity;
33+
import com.arangodb.entity.FilterCondition;
34+
import com.arangodb.entity.GraphEntity;
35+
import com.arangodb.entity.GraphsEntity;
36+
import com.arangodb.impl.BaseDriverInterface;
37+
38+
public interface InternalEdgeDriver extends BaseDriverInterface {
39+
40+
<T> EdgeEntity<T> createEdge(
41+
String databaseName,
42+
String collectionName,
43+
Object object,
44+
String from,
45+
String to,
46+
Boolean createCollection,
47+
Boolean waitForSync
48+
) throws ArangoException;
49+
50+
}

src/main/java/com/arangodb/impl/ImplFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,7 @@ public static InternalReplicationDriverImpl createReplicationDriver(ArangoConfig
7878
public static InternalGraphDriverImpl createGraphDriver(ArangoConfigure configure, InternalCursorDriver cursorDriver, HttpManager httpManager) {
7979
return new InternalGraphDriverImpl(configure, cursorDriver, httpManager);
8080
}
81+
public static InternalEdgeDriverImpl createEdgeDriver(ArangoConfigure configure, InternalCursorDriver cursorDriver, HttpManager httpManager) {
82+
return new InternalEdgeDriverImpl(configure, cursorDriver, httpManager);
83+
}
8184
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2012,2013 tamtam180
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb.impl;
18+
19+
import java.util.Map;
20+
21+
import com.arangodb.ArangoConfigure;
22+
import com.arangodb.ArangoException;
23+
import com.arangodb.InternalCursorDriver;
24+
import com.arangodb.entity.EdgeEntity;
25+
import com.arangodb.entity.EntityFactory;
26+
import com.arangodb.http.HttpManager;
27+
import com.arangodb.http.HttpResponseEntity;
28+
import com.arangodb.util.MapBuilder;
29+
30+
/**
31+
* @author gschwab
32+
*/
33+
public class InternalEdgeDriverImpl extends BaseArangoDriverWithCursorImpl implements com.arangodb.InternalEdgeDriver {
34+
35+
InternalEdgeDriverImpl(ArangoConfigure configure, InternalCursorDriver cursorDriver, HttpManager httpManager) {
36+
super(configure, cursorDriver, httpManager);
37+
}
38+
39+
@Override
40+
public <T> EdgeEntity<T> createEdge(
41+
String databaseName,
42+
String collectionName,
43+
Object object,
44+
String from,
45+
String to,
46+
Boolean createCollection,
47+
Boolean waitForSync) throws ArangoException {
48+
49+
Map<String, Object> params =
50+
new MapBuilder()
51+
.put("collection", collectionName)
52+
.put("from", from)
53+
.put("to", to)
54+
.put("createCollection", createCollection)
55+
.put("waitForSync", waitForSync)
56+
.get();
57+
58+
String body = EntityFactory.toJsonString(object);
59+
60+
HttpResponseEntity response = httpManager.doPost(
61+
createEndpointUrl(baseUrl, databaseName, "/_api/edge"),
62+
params,
63+
body);
64+
65+
EdgeEntity<T> edgeEntity = createEntity(response, EdgeEntity.class);
66+
edgeEntity.setEntity((T) object);
67+
return edgeEntity;
68+
}
69+
70+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (C) 2012 tamtam180
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb;
18+
19+
import static org.hamcrest.CoreMatchers.instanceOf;
20+
import static org.hamcrest.CoreMatchers.is;
21+
import static org.hamcrest.CoreMatchers.not;
22+
import static org.hamcrest.CoreMatchers.notNullValue;
23+
import static org.hamcrest.CoreMatchers.nullValue;
24+
import static org.junit.Assert.assertThat;
25+
import static org.junit.Assert.fail;
26+
27+
import java.util.Arrays;
28+
import java.util.Set;
29+
import java.util.TreeSet;
30+
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
36+
37+
import com.arangodb.entity.CollectionEntity;
38+
import com.arangodb.entity.DocumentEntity;
39+
import com.arangodb.entity.EdgeEntity;
40+
41+
/**
42+
* @author tamtam180 - kirscheless at gmail.com
43+
*/
44+
public class ArangoDriverEdgeTest extends BaseTest {
45+
46+
public ArangoDriverEdgeTest(ArangoConfigure configure, ArangoDriver driver) {
47+
super(configure, driver);
48+
}
49+
50+
final String collectionName = "unit_test_edge_collection_EdgeTest";
51+
final String collectionName2 = "unit_test_normal_collection_EdgeTest";
52+
53+
@Before
54+
public void before() throws ArangoException {
55+
try {
56+
driver.deleteCollection(collectionName);
57+
} catch (ArangoException e) {
58+
}
59+
try {
60+
driver.deleteCollection(collectionName2);
61+
} catch (ArangoException e) {
62+
}
63+
}
64+
65+
@After
66+
public void after() throws ArangoException {
67+
try {
68+
driver.deleteCollection(collectionName);
69+
} catch (ArangoException e) {
70+
}
71+
try {
72+
driver.deleteCollection(collectionName2);
73+
} catch (ArangoException e) {
74+
}
75+
}
76+
77+
@Test
78+
public void test_create_normal() throws ArangoException {
79+
80+
TestComplexEntity01 value = new TestComplexEntity01("user", "desc", 42);
81+
DocumentEntity<TestComplexEntity01> fromDoc = driver.createDocument(collectionName2, value, true, true);
82+
DocumentEntity<TestComplexEntity01> toDoc = driver.createDocument(collectionName2, value, true, true);
83+
84+
EdgeEntity<TestComplexEntity01> doc = driver.createEdge(
85+
databaseName,
86+
collectionName,
87+
value,
88+
fromDoc.getDocumentHandle(),
89+
toDoc.getDocumentHandle(),
90+
true,
91+
true);
92+
93+
assertThat(doc.getDocumentKey(), is(notNullValue()));
94+
assertThat(doc.getDocumentHandle(), is(collectionName + "/" + doc.getDocumentKey()));
95+
assertThat(doc.getDocumentRevision(), is(not(0L)));
96+
97+
}
98+
99+
}

src/test/java/com/arangodb/BaseTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
public class BaseTest {
3939

4040
protected static ArangoConfigure configure;
41+
protected static final String databaseName = "unitTestDatabase";
4142

4243
// Suite.classを使った場合、Parametersがテストクラスの数だけ最初に一気に連続で呼ばれる。
4344
// そのため、単純にクラス変数にconfigureを保持すると、AfterClassの時に別のテストケースのものを終了してしまう。
@@ -58,15 +59,14 @@ public class BaseTest {
5859
@Parameters()
5960
public static Collection<Object[]> getParameterizedDrivers() {
6061

61-
String database = "unitTestDatabase";
6262
ArangoConfigure configure = new ArangoConfigure();
6363
configure.init();
6464
ArangoDriver driver = new ArangoDriver(configure);
65-
ArangoDriver driverMDB = new ArangoDriver(configure, database);
65+
ArangoDriver driverMDB = new ArangoDriver(configure, databaseName);
6666

6767
// create mydb
6868
try {
69-
driver.createDatabase(database);
69+
driver.createDatabase(databaseName);
7070
} catch (ArangoException e) {
7171
}
7272

0 commit comments

Comments
 (0)