Skip to content

Commit cb6e5b2

Browse files
authored
Added users options to DBCreateOptions (#351)
* added users options to DBCreateOptions * remove invalid parameters from DatabaseUsersOptions * createDatabaseWithUsers test: added assertions on extra field
1 parent 4c192bd commit cb6e5b2

File tree

3 files changed

+244
-93
lines changed

3 files changed

+244
-93
lines changed

src/main/java/com/arangodb/model/DBCreateOptions.java

+20
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,38 @@
2020

2121
package com.arangodb.model;
2222

23+
import java.util.Collection;
24+
2325
/**
2426
* @author Mark Vollmary
2527
*/
2628
public class DBCreateOptions {
2729

30+
private Collection<DatabaseUsersOptions> users;
2831
private String name;
2932
private DatabaseOptions options;
3033

3134
public DBCreateOptions() {
3235
super();
3336
}
3437

38+
public Collection<DatabaseUsersOptions> getUsers() {
39+
return users;
40+
}
41+
42+
/**
43+
* @param users array of user objects to initially create for the new database.
44+
* User information will not be changed for users that already exist.
45+
* If users is not specified or does not contain any users, a default user
46+
* root will be created with an empty string password. This ensures that the
47+
* new database will be accessible after it is created.
48+
* @return options
49+
*/
50+
public DBCreateOptions users(final Collection<DatabaseUsersOptions> users) {
51+
this.users = users;
52+
return this;
53+
}
54+
3555
public String getName() {
3656
return name;
3757
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.model;
22+
23+
import java.util.Map;
24+
25+
/**
26+
* @author Michele Rastelli
27+
*/
28+
public class DatabaseUsersOptions {
29+
30+
private String username;
31+
private Map<String, Object> extra;
32+
private String passwd;
33+
private Boolean active;
34+
35+
public String getUsername() {
36+
return username;
37+
}
38+
39+
/**
40+
* @param username Login name of the user to be created
41+
* @return options
42+
*/
43+
public DatabaseUsersOptions username(final String username) {
44+
this.username = username;
45+
return this;
46+
}
47+
48+
public Map<String, Object> getExtra() {
49+
return extra;
50+
}
51+
52+
/**
53+
* @param extra extra user information. The data contained in extra
54+
* will be stored for the user but not be interpreted further by ArangoDB.
55+
* @return options
56+
*/
57+
public DatabaseUsersOptions extra(final Map<String, Object> extra) {
58+
this.extra = extra;
59+
return this;
60+
}
61+
62+
public String getPasswd() {
63+
return passwd;
64+
}
65+
66+
/**
67+
* @param passwd The user password as a string. If not specified, it will default to an empty string.
68+
* @return options
69+
*/
70+
public DatabaseUsersOptions passwd(final String passwd) {
71+
this.passwd = passwd;
72+
return this;
73+
}
74+
75+
public Boolean getActive() {
76+
return active;
77+
}
78+
79+
/**
80+
* @param active A flag indicating whether the user account should be activated or not.
81+
* The default value is true. If set to false, the user won't be able to
82+
* log into the database.
83+
* @return options
84+
*/
85+
public DatabaseUsersOptions active(final Boolean active) {
86+
this.active = active;
87+
return this;
88+
}
89+
90+
}

src/test/java/com/arangodb/ArangoDBTest.java

+134-93
Original file line numberDiff line numberDiff line change
@@ -83,105 +83,146 @@ public static void shutdown() {
8383

8484
@Parameters
8585
public static List<ArangoDB> builders() {
86-
return BaseTest.arangos;
87-
}
86+
return BaseTest.arangos;
87+
}
8888

89-
public ArangoDBTest(final ArangoDB arangoDB) {
90-
super();
91-
this.arangoDB = arangoDB;
92-
db1 = arangoDB.db(DB1);
93-
db2 = arangoDB.db(DB2);
94-
}
89+
public ArangoDBTest(final ArangoDB arangoDB) {
90+
super();
91+
this.arangoDB = arangoDB;
92+
db1 = arangoDB.db(DB1);
93+
db2 = arangoDB.db(DB2);
94+
}
9595

96-
private boolean isEnterprise() {
97-
return arangoDB.getVersion().getLicense() == License.ENTERPRISE;
98-
}
96+
private boolean isEnterprise() {
97+
return arangoDB.getVersion().getLicense() == License.ENTERPRISE;
98+
}
9999

100-
private boolean isCluster() {
101-
return arangoDB.getRole() == ServerRole.COORDINATOR;
102-
}
100+
private boolean isCluster() {
101+
return arangoDB.getRole() == ServerRole.COORDINATOR;
102+
}
103103

104-
private boolean isAtLeastVersion(final int major, final int minor) {
104+
private boolean isAtLeastVersion(final int major, final int minor) {
105105
return TestUtils.isAtLeastVersion(arangoDB.getVersion().getVersion(), major, minor, 0);
106-
}
107-
108-
@Test
109-
public void getVersion() {
110-
final ArangoDBVersion version = arangoDB.getVersion();
111-
assertThat(version, is(notNullValue()));
112-
assertThat(version.getServer(), is(notNullValue()));
113-
assertThat(version.getVersion(), is(notNullValue()));
114-
}
115-
116-
@Test
117-
public void createAndDeleteDatabase() {
118-
final String dbName = "testDB-" + UUID.randomUUID().toString();
119-
final Boolean resultCreate = arangoDB.createDatabase(dbName);
120-
assertThat(resultCreate, is(true));
121-
final Boolean resultDelete = arangoDB.db(dbName).drop();
122-
assertThat(resultDelete, is(true));
123-
}
124-
125-
@Test
126-
public void createDatabaseWithOptions() {
127-
assumeTrue(isCluster());
128-
assumeTrue(isAtLeastVersion(3, 6));
129-
final String dbName = "testDB-" + UUID.randomUUID().toString();
130-
final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions()
131-
.name(dbName)
132-
.options(new DatabaseOptions()
133-
.writeConcern(2)
134-
.replicationFactor(2)
135-
.sharding("")
136-
)
137-
);
138-
assertThat(resultCreate, is(true));
139-
140-
DatabaseEntity info = arangoDB.db(dbName).getInfo();
141-
assertThat(info.getReplicationFactor(), is(2));
142-
assertThat(info.getWriteConcern(), is(2));
143-
assertThat(info.getSharding(), is(""));
144-
assertThat(info.getSatellite(), nullValue());
145-
146-
final Boolean resultDelete = arangoDB.db(dbName).drop();
147-
assertThat(resultDelete, is(true));
148-
}
149-
150-
@Test
106+
}
107+
108+
@Test
109+
public void getVersion() {
110+
final ArangoDBVersion version = arangoDB.getVersion();
111+
assertThat(version, is(notNullValue()));
112+
assertThat(version.getServer(), is(notNullValue()));
113+
assertThat(version.getVersion(), is(notNullValue()));
114+
}
115+
116+
@Test
117+
public void createAndDeleteDatabase() {
118+
final String dbName = "testDB-" + UUID.randomUUID().toString();
119+
final Boolean resultCreate = arangoDB.createDatabase(dbName);
120+
assertThat(resultCreate, is(true));
121+
final Boolean resultDelete = arangoDB.db(dbName).drop();
122+
assertThat(resultDelete, is(true));
123+
}
124+
125+
@Test
126+
public void createDatabaseWithOptions() {
127+
assumeTrue(isCluster());
128+
assumeTrue(isAtLeastVersion(3, 6));
129+
final String dbName = "testDB-" + UUID.randomUUID().toString();
130+
final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions()
131+
.name(dbName)
132+
.options(new DatabaseOptions()
133+
.writeConcern(2)
134+
.replicationFactor(2)
135+
.sharding("")
136+
)
137+
);
138+
assertThat(resultCreate, is(true));
139+
140+
DatabaseEntity info = arangoDB.db(dbName).getInfo();
141+
assertThat(info.getReplicationFactor(), is(2));
142+
assertThat(info.getWriteConcern(), is(2));
143+
assertThat(info.getSharding(), is(""));
144+
assertThat(info.getSatellite(), nullValue());
145+
146+
final Boolean resultDelete = arangoDB.db(dbName).drop();
147+
assertThat(resultDelete, is(true));
148+
}
149+
150+
@Test
151151
public void createDatabaseWithOptionsSatellite() {
152-
assumeTrue(isCluster());
153-
assumeTrue(isEnterprise());
154-
assumeTrue(isAtLeastVersion(3, 6));
155-
156-
final String dbName = "testDB-" + UUID.randomUUID().toString();
157-
final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions()
158-
.name(dbName)
159-
.options(new DatabaseOptions()
160-
.writeConcern(2)
161-
.satellite(true)
162-
.sharding("")
163-
)
164-
);
165-
assertThat(resultCreate, is(true));
166-
167-
DatabaseEntity info = arangoDB.db(dbName).getInfo();
168-
assertThat(info.getReplicationFactor(), nullValue());
169-
assertThat(info.getSatellite(), is(true));
170-
assertThat(info.getWriteConcern(), is(2));
171-
assertThat(info.getSharding(), is(""));
172-
173-
final Boolean resultDelete = arangoDB.db(dbName).drop();
174-
assertThat(resultDelete, is(true));
175-
}
176-
177-
@Test
178-
public void getDatabases() {
179-
Collection<String> dbs = arangoDB.getDatabases();
180-
assertThat(dbs, is(notNullValue()));
181-
assertThat(dbs.size(), is(greaterThan(0)));
182-
assertThat(dbs.contains("_system"), is(true));
183-
assertThat(dbs, hasItem(DB1));
184-
}
152+
assumeTrue(isCluster());
153+
assumeTrue(isEnterprise());
154+
assumeTrue(isAtLeastVersion(3, 6));
155+
156+
final String dbName = "testDB-" + UUID.randomUUID().toString();
157+
final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions()
158+
.name(dbName)
159+
.options(new DatabaseOptions()
160+
.writeConcern(2)
161+
.satellite(true)
162+
.sharding("")
163+
)
164+
);
165+
assertThat(resultCreate, is(true));
166+
167+
DatabaseEntity info = arangoDB.db(dbName).getInfo();
168+
assertThat(info.getReplicationFactor(), nullValue());
169+
assertThat(info.getSatellite(), is(true));
170+
assertThat(info.getWriteConcern(), is(2));
171+
assertThat(info.getSharding(), is(""));
172+
173+
final Boolean resultDelete = arangoDB.db(dbName).drop();
174+
assertThat(resultDelete, is(true));
175+
}
176+
177+
@Test
178+
public void createDatabaseWithUsers() {
179+
final String dbName = "testDB-" + UUID.randomUUID().toString();
180+
final Map<String, Object> extra = Collections.singletonMap("key", "value");
181+
final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions()
182+
.name(dbName)
183+
.users(Collections.singletonList(new DatabaseUsersOptions()
184+
.active(true)
185+
.username("testUser")
186+
.passwd("testPasswd")
187+
.extra(extra)
188+
))
189+
);
190+
assertThat(resultCreate, is(true));
191+
192+
DatabaseEntity info = arangoDB.db(dbName).getInfo();
193+
assertThat(info.getName(), is(dbName));
194+
195+
Optional<UserEntity> retrievedUserOptional = arangoDB.getUsers().stream()
196+
.filter(it -> it.getUser().equals("testUser"))
197+
.findFirst();
198+
assertThat(retrievedUserOptional.isPresent(), is(true));
199+
200+
UserEntity retrievedUser = retrievedUserOptional.get();
201+
assertThat(retrievedUser.getActive(), is(true));
202+
assertThat(retrievedUser.getExtra(), is(extra));
203+
204+
ArangoDB arangoDBTestUser = new ArangoDB.Builder()
205+
.user("testUser")
206+
.password("testPasswd")
207+
.build();
208+
209+
// check if testUser has been created and can access the created db
210+
ArangoCollection collection = arangoDBTestUser.db(dbName).collection("col-" + UUID.randomUUID().toString());
211+
collection.create();
212+
arangoDBTestUser.shutdown();
213+
214+
final Boolean resultDelete = arangoDB.db(dbName).drop();
215+
assertThat(resultDelete, is(true));
216+
}
217+
218+
@Test
219+
public void getDatabases() {
220+
Collection<String> dbs = arangoDB.getDatabases();
221+
assertThat(dbs, is(notNullValue()));
222+
assertThat(dbs.size(), is(greaterThan(0)));
223+
assertThat(dbs.contains("_system"), is(true));
224+
assertThat(dbs, hasItem(DB1));
225+
}
185226

186227
@Test
187228
public void getAccessibleDatabases() {

0 commit comments

Comments
 (0)