From 72e5dfc5356f8083ef83ea81aab630bcfbbbf1ef Mon Sep 17 00:00:00 2001 From: Michele Rastelli Date: Mon, 13 Jul 2020 18:43:45 +0200 Subject: [PATCH 1/3] added users options to DBCreateOptions --- .../com/arangodb/model/DBCreateOptions.java | 20 ++ .../arangodb/model/DatabaseUsersOptions.java | 153 ++++++++++++ src/test/java/com/arangodb/ArangoDBTest.java | 219 ++++++++++-------- 3 files changed, 299 insertions(+), 93 deletions(-) create mode 100644 src/main/java/com/arangodb/model/DatabaseUsersOptions.java diff --git a/src/main/java/com/arangodb/model/DBCreateOptions.java b/src/main/java/com/arangodb/model/DBCreateOptions.java index 3de656623..abd574a95 100644 --- a/src/main/java/com/arangodb/model/DBCreateOptions.java +++ b/src/main/java/com/arangodb/model/DBCreateOptions.java @@ -20,11 +20,14 @@ package com.arangodb.model; +import java.util.Collection; + /** * @author Mark Vollmary */ public class DBCreateOptions { + private Collection users; private String name; private DatabaseOptions options; @@ -32,6 +35,23 @@ public DBCreateOptions() { super(); } + public Collection getUsers() { + return users; + } + + /** + * @param users array of user objects to initially create for the new database. + * User information will not be changed for users that already exist. + * If users is not specified or does not contain any users, a default user + * root will be created with an empty string password. This ensures that the + * new database will be accessible after it is created. + * @return options + */ + public DBCreateOptions users(final Collection users) { + this.users = users; + return this; + } + public String getName() { return name; } diff --git a/src/main/java/com/arangodb/model/DatabaseUsersOptions.java b/src/main/java/com/arangodb/model/DatabaseUsersOptions.java new file mode 100644 index 000000000..0f4bfd97d --- /dev/null +++ b/src/main/java/com/arangodb/model/DatabaseUsersOptions.java @@ -0,0 +1,153 @@ +/* + * DISCLAIMER + * + * Copyright 2016 ArangoDB GmbH, Cologne, Germany + * + * 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. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.model; + +import com.arangodb.entity.ReplicationFactor; + +import java.util.Map; + +/** + * @author Michele Rastelli + */ +public class DatabaseUsersOptions { + + private String username; + private ReplicationFactor replicationFactor; + private Map extra; + private String passwd; + private Integer writeConcern; + private Boolean active; + private String sharding; + + public DatabaseUsersOptions() { + replicationFactor = new ReplicationFactor(); + } + + public String getUsername() { + return username; + } + + /** + * @param username Login name of the user to be created + * @return options + */ + public DatabaseUsersOptions username(final String username) { + this.username = username; + return this; + } + + public Integer getReplicationFactor() { + return replicationFactor.getReplicationFactor(); + } + + /** + * @param replicationFactor Default replication factor for new collections created in this database. + * @return options + */ + public DatabaseUsersOptions replicationFactor(final Integer replicationFactor) { + this.replicationFactor.setReplicationFactor(replicationFactor); + return this; + } + + public Boolean getSatellite() { + return this.replicationFactor.getSatellite(); + } + + public DatabaseUsersOptions satellite(final Boolean satellite) { + this.replicationFactor.setSatellite(satellite); + return this; + } + + public Map getExtra() { + return extra; + } + + /** + * @param extra extra user information. The data contained in extra + * will be stored for the user but not be interpreted further by ArangoDB. + * @return options + */ + public DatabaseUsersOptions extra(final Map extra) { + this.extra = extra; + return this; + } + + public String getPasswd() { + return passwd; + } + + /** + * @param passwd The user password as a string. If not specified, it will default to an empty string. + * @return options + */ + public DatabaseUsersOptions passwd(final String passwd) { + this.passwd = passwd; + return this; + } + + public Integer getWriteConcern() { + return writeConcern; + } + + /** + * @param writeConcern Default write concern for new collections created in this database. + * It determines how many copies of each shard are required to be + * in sync on the different DBServers. If there are less then these many copies + * in the cluster a shard will refuse to write. Writes to shards with enough + * up-to-date copies will succeed at the same time however. The value of + * writeConcern can not be larger than replicationFactor. (cluster only) + * @return options + */ + public DatabaseUsersOptions writeConcern(final Integer writeConcern) { + this.writeConcern = writeConcern; + return this; + } + + public Boolean getActive() { + return active; + } + + /** + * @param active A flag indicating whether the user account should be activated or not. + * The default value is true. If set to false, the user won't be able to + * log into the database. + * @return options + */ + public DatabaseUsersOptions active(final Boolean active) { + this.active = active; + return this; + } + + public String getSharding() { + return sharding; + } + + /** + * @param sharding The sharding method to use for new collections in this database. Valid values + * are: "", "flexible", or "single". The first two are equivalent. (cluster only) + * @return options + */ + public DatabaseUsersOptions sharding(final String sharding) { + this.sharding = sharding; + return this; + } + +} diff --git a/src/test/java/com/arangodb/ArangoDBTest.java b/src/test/java/com/arangodb/ArangoDBTest.java index a43568983..85f227fa3 100644 --- a/src/test/java/com/arangodb/ArangoDBTest.java +++ b/src/test/java/com/arangodb/ArangoDBTest.java @@ -83,105 +83,138 @@ public static void shutdown() { @Parameters public static List builders() { - return BaseTest.arangos; - } + return BaseTest.arangos; + } - public ArangoDBTest(final ArangoDB arangoDB) { - super(); - this.arangoDB = arangoDB; - db1 = arangoDB.db(DB1); - db2 = arangoDB.db(DB2); - } + public ArangoDBTest(final ArangoDB arangoDB) { + super(); + this.arangoDB = arangoDB; + db1 = arangoDB.db(DB1); + db2 = arangoDB.db(DB2); + } - private boolean isEnterprise() { - return arangoDB.getVersion().getLicense() == License.ENTERPRISE; - } + private boolean isEnterprise() { + return arangoDB.getVersion().getLicense() == License.ENTERPRISE; + } - private boolean isCluster() { - return arangoDB.getRole() == ServerRole.COORDINATOR; - } + private boolean isCluster() { + return arangoDB.getRole() == ServerRole.COORDINATOR; + } - private boolean isAtLeastVersion(final int major, final int minor) { + private boolean isAtLeastVersion(final int major, final int minor) { return TestUtils.isAtLeastVersion(arangoDB.getVersion().getVersion(), major, minor, 0); - } - - @Test - public void getVersion() { - final ArangoDBVersion version = arangoDB.getVersion(); - assertThat(version, is(notNullValue())); - assertThat(version.getServer(), is(notNullValue())); - assertThat(version.getVersion(), is(notNullValue())); - } - - @Test - public void createAndDeleteDatabase() { - final String dbName = "testDB-" + UUID.randomUUID().toString(); - final Boolean resultCreate = arangoDB.createDatabase(dbName); - assertThat(resultCreate, is(true)); - final Boolean resultDelete = arangoDB.db(dbName).drop(); - assertThat(resultDelete, is(true)); - } - - @Test - public void createDatabaseWithOptions() { - assumeTrue(isCluster()); - assumeTrue(isAtLeastVersion(3, 6)); - final String dbName = "testDB-" + UUID.randomUUID().toString(); - final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions() - .name(dbName) - .options(new DatabaseOptions() - .writeConcern(2) - .replicationFactor(2) - .sharding("") - ) - ); - assertThat(resultCreate, is(true)); - - DatabaseEntity info = arangoDB.db(dbName).getInfo(); - assertThat(info.getReplicationFactor(), is(2)); - assertThat(info.getWriteConcern(), is(2)); - assertThat(info.getSharding(), is("")); - assertThat(info.getSatellite(), nullValue()); - - final Boolean resultDelete = arangoDB.db(dbName).drop(); - assertThat(resultDelete, is(true)); - } - - @Test + } + + @Test + public void getVersion() { + final ArangoDBVersion version = arangoDB.getVersion(); + assertThat(version, is(notNullValue())); + assertThat(version.getServer(), is(notNullValue())); + assertThat(version.getVersion(), is(notNullValue())); + } + + @Test + public void createAndDeleteDatabase() { + final String dbName = "testDB-" + UUID.randomUUID().toString(); + final Boolean resultCreate = arangoDB.createDatabase(dbName); + assertThat(resultCreate, is(true)); + final Boolean resultDelete = arangoDB.db(dbName).drop(); + assertThat(resultDelete, is(true)); + } + + @Test + public void createDatabaseWithOptions() { + assumeTrue(isCluster()); + assumeTrue(isAtLeastVersion(3, 6)); + final String dbName = "testDB-" + UUID.randomUUID().toString(); + final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions() + .name(dbName) + .options(new DatabaseOptions() + .writeConcern(2) + .replicationFactor(2) + .sharding("") + ) + ); + assertThat(resultCreate, is(true)); + + DatabaseEntity info = arangoDB.db(dbName).getInfo(); + assertThat(info.getReplicationFactor(), is(2)); + assertThat(info.getWriteConcern(), is(2)); + assertThat(info.getSharding(), is("")); + assertThat(info.getSatellite(), nullValue()); + + final Boolean resultDelete = arangoDB.db(dbName).drop(); + assertThat(resultDelete, is(true)); + } + + @Test public void createDatabaseWithOptionsSatellite() { - assumeTrue(isCluster()); - assumeTrue(isEnterprise()); - assumeTrue(isAtLeastVersion(3, 6)); - - final String dbName = "testDB-" + UUID.randomUUID().toString(); - final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions() - .name(dbName) - .options(new DatabaseOptions() - .writeConcern(2) - .satellite(true) - .sharding("") - ) - ); - assertThat(resultCreate, is(true)); - - DatabaseEntity info = arangoDB.db(dbName).getInfo(); - assertThat(info.getReplicationFactor(), nullValue()); - assertThat(info.getSatellite(), is(true)); - assertThat(info.getWriteConcern(), is(2)); - assertThat(info.getSharding(), is("")); - - final Boolean resultDelete = arangoDB.db(dbName).drop(); - assertThat(resultDelete, is(true)); - } - - @Test - public void getDatabases() { - Collection dbs = arangoDB.getDatabases(); - assertThat(dbs, is(notNullValue())); - assertThat(dbs.size(), is(greaterThan(0))); - assertThat(dbs.contains("_system"), is(true)); - assertThat(dbs, hasItem(DB1)); - } + assumeTrue(isCluster()); + assumeTrue(isEnterprise()); + assumeTrue(isAtLeastVersion(3, 6)); + + final String dbName = "testDB-" + UUID.randomUUID().toString(); + final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions() + .name(dbName) + .options(new DatabaseOptions() + .writeConcern(2) + .satellite(true) + .sharding("") + ) + ); + assertThat(resultCreate, is(true)); + + DatabaseEntity info = arangoDB.db(dbName).getInfo(); + assertThat(info.getReplicationFactor(), nullValue()); + assertThat(info.getSatellite(), is(true)); + assertThat(info.getWriteConcern(), is(2)); + assertThat(info.getSharding(), is("")); + + final Boolean resultDelete = arangoDB.db(dbName).drop(); + assertThat(resultDelete, is(true)); + } + + @Test + public void createDatabaseWithUsers() { + final String dbName = "testDB-" + UUID.randomUUID().toString(); + + final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions() + .name(dbName) + .users(Collections.singletonList(new DatabaseUsersOptions() + .active(true) + .username("testUser") + .passwd("testPasswd") + )) + ); + assertThat(resultCreate, is(true)); + + DatabaseEntity info = arangoDB.db(dbName).getInfo(); + assertThat(info.getName(), is(dbName)); + + assertThat(arangoDB.getUsers().stream().anyMatch(it -> it.getUser().equals("testUser")), is(true)); + + ArangoDB arangoDBTestUser = new ArangoDB.Builder() + .user("testUser") + .password("testPasswd") + .build(); + + // check if testUser has been created and can access the created db + ArangoCollection collection = arangoDBTestUser.db(dbName).collection("col-" + UUID.randomUUID().toString()); + collection.create(); + arangoDBTestUser.shutdown(); + + final Boolean resultDelete = arangoDB.db(dbName).drop(); + assertThat(resultDelete, is(true)); + } + + @Test + public void getDatabases() { + Collection dbs = arangoDB.getDatabases(); + assertThat(dbs, is(notNullValue())); + assertThat(dbs.size(), is(greaterThan(0))); + assertThat(dbs.contains("_system"), is(true)); + assertThat(dbs, hasItem(DB1)); + } @Test public void getAccessibleDatabases() { From afa0d07817c353c3b44fddc9d6b84ff4890d63cb Mon Sep 17 00:00:00 2001 From: Michele Rastelli Date: Tue, 14 Jul 2020 11:58:15 +0200 Subject: [PATCH 2/3] remove invalid parameters from DatabaseUsersOptions --- .../arangodb/model/DatabaseUsersOptions.java | 63 ------------------- 1 file changed, 63 deletions(-) diff --git a/src/main/java/com/arangodb/model/DatabaseUsersOptions.java b/src/main/java/com/arangodb/model/DatabaseUsersOptions.java index 0f4bfd97d..edc9175da 100644 --- a/src/main/java/com/arangodb/model/DatabaseUsersOptions.java +++ b/src/main/java/com/arangodb/model/DatabaseUsersOptions.java @@ -20,8 +20,6 @@ package com.arangodb.model; -import com.arangodb.entity.ReplicationFactor; - import java.util.Map; /** @@ -30,16 +28,9 @@ public class DatabaseUsersOptions { private String username; - private ReplicationFactor replicationFactor; private Map extra; private String passwd; - private Integer writeConcern; private Boolean active; - private String sharding; - - public DatabaseUsersOptions() { - replicationFactor = new ReplicationFactor(); - } public String getUsername() { return username; @@ -54,28 +45,6 @@ public DatabaseUsersOptions username(final String username) { return this; } - public Integer getReplicationFactor() { - return replicationFactor.getReplicationFactor(); - } - - /** - * @param replicationFactor Default replication factor for new collections created in this database. - * @return options - */ - public DatabaseUsersOptions replicationFactor(final Integer replicationFactor) { - this.replicationFactor.setReplicationFactor(replicationFactor); - return this; - } - - public Boolean getSatellite() { - return this.replicationFactor.getSatellite(); - } - - public DatabaseUsersOptions satellite(final Boolean satellite) { - this.replicationFactor.setSatellite(satellite); - return this; - } - public Map getExtra() { return extra; } @@ -103,24 +72,6 @@ public DatabaseUsersOptions passwd(final String passwd) { return this; } - public Integer getWriteConcern() { - return writeConcern; - } - - /** - * @param writeConcern Default write concern for new collections created in this database. - * It determines how many copies of each shard are required to be - * in sync on the different DBServers. If there are less then these many copies - * in the cluster a shard will refuse to write. Writes to shards with enough - * up-to-date copies will succeed at the same time however. The value of - * writeConcern can not be larger than replicationFactor. (cluster only) - * @return options - */ - public DatabaseUsersOptions writeConcern(final Integer writeConcern) { - this.writeConcern = writeConcern; - return this; - } - public Boolean getActive() { return active; } @@ -136,18 +87,4 @@ public DatabaseUsersOptions active(final Boolean active) { return this; } - public String getSharding() { - return sharding; - } - - /** - * @param sharding The sharding method to use for new collections in this database. Valid values - * are: "", "flexible", or "single". The first two are equivalent. (cluster only) - * @return options - */ - public DatabaseUsersOptions sharding(final String sharding) { - this.sharding = sharding; - return this; - } - } From a24075d03c36d4df8758940378b217736b2a28f2 Mon Sep 17 00:00:00 2001 From: Michele Rastelli Date: Fri, 14 Aug 2020 09:47:53 +0200 Subject: [PATCH 3/3] createDatabaseWithUsers test: added assertions on extra field --- src/test/java/com/arangodb/ArangoDBTest.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/arangodb/ArangoDBTest.java b/src/test/java/com/arangodb/ArangoDBTest.java index 85f227fa3..280b452ca 100644 --- a/src/test/java/com/arangodb/ArangoDBTest.java +++ b/src/test/java/com/arangodb/ArangoDBTest.java @@ -177,13 +177,14 @@ public void createDatabaseWithOptionsSatellite() { @Test public void createDatabaseWithUsers() { final String dbName = "testDB-" + UUID.randomUUID().toString(); - + final Map extra = Collections.singletonMap("key", "value"); final Boolean resultCreate = arangoDB.createDatabase(new DBCreateOptions() .name(dbName) .users(Collections.singletonList(new DatabaseUsersOptions() .active(true) .username("testUser") .passwd("testPasswd") + .extra(extra) )) ); assertThat(resultCreate, is(true)); @@ -191,7 +192,14 @@ public void createDatabaseWithUsers() { DatabaseEntity info = arangoDB.db(dbName).getInfo(); assertThat(info.getName(), is(dbName)); - assertThat(arangoDB.getUsers().stream().anyMatch(it -> it.getUser().equals("testUser")), is(true)); + Optional retrievedUserOptional = arangoDB.getUsers().stream() + .filter(it -> it.getUser().equals("testUser")) + .findFirst(); + assertThat(retrievedUserOptional.isPresent(), is(true)); + + UserEntity retrievedUser = retrievedUserOptional.get(); + assertThat(retrievedUser.getActive(), is(true)); + assertThat(retrievedUser.getExtra(), is(extra)); ArangoDB arangoDBTestUser = new ArangoDB.Builder() .user("testUser")