Commit d6a87f3d authored by Steven Murray's avatar Steven Murray
Browse files

Refactored the mock database

parent cc43d0ee
......@@ -17,11 +17,16 @@ set (MIDDLE_TIER_LIB_SRC_FILES
MiddleTierUser.cpp
MigrationRoute.cpp
MigrationRouteId.cpp
MigrationRoutes.cpp
MockMiddleTierDatabase.cpp
MockAdminHostDatabase.cpp
MockAdminUserDatabase.cpp
MockDatabase.cpp
MockLogicalLibraryDatabase.cpp
MockMiddleTierAdmin.cpp
MockMiddleTierUser.cpp
MockMiddleTierUser.cpp
MockMigrationRouteDatabase.cpp
MockTapeDatabase.cpp
MockTapePoolDatabase.cpp
SecurityIdentity.cpp
StorageClass.cpp
Tape.cpp
......@@ -33,8 +38,13 @@ add_library (ctamiddletier SHARED
${MIDDLE_TIER_LIB_SRC_FILES})
set (MIDDLE_TIER_UNIT_TESTS_LIB_SRC_FILES
MockAdminHostDatabaseTest.cpp
MockAdminUserDatabaseTest.cpp
MockLogicalLibraryDatabaseTest.cpp
MockMiddleTierAdminTest.cpp
MockMiddleTierUserTest.cpp
MockTapeDatabaseTest.cpp
MockTapePoolDatabaseTest.cpp
UtilsTest.cpp)
add_library (ctamiddletierunittests SHARED
......
#include "Exception.hpp"
#include "MockAdminHostDatabase.hpp"
#include <sstream>
//------------------------------------------------------------------------------
// createAdminHost
//------------------------------------------------------------------------------
void cta::MockAdminHostDatabase::createAdminHost(
const SecurityIdentity &requester,
const std::string &hostName,
const std::string &comment) {
checkAdminHostDoesNotAlreadyExist(hostName);
AdminHost adminHost(hostName, requester.user, comment);
m_adminHosts[hostName] = adminHost;
}
//------------------------------------------------------------------------------
// checkAdminHostDoesNotAlreadyExist
//------------------------------------------------------------------------------
void cta::MockAdminHostDatabase::checkAdminHostDoesNotAlreadyExist(
const std::string &hostName) const {
std::map<std::string, AdminHost>::const_iterator itor =
m_adminHosts.find(hostName);
if(itor != m_adminHosts.end()) {
std::ostringstream message;
message << "Administration host " << hostName << " already exists";
throw(Exception(message.str()));
}
}
//------------------------------------------------------------------------------
// deleteAdminHost
//------------------------------------------------------------------------------
void cta::MockAdminHostDatabase::deleteAdminHost(
const SecurityIdentity &requester,
const std::string &hostName) {
for(std::map<std::string, AdminHost>::iterator itor = m_adminHosts.begin();
itor != m_adminHosts.end(); itor++) {
if(hostName == itor->first) {
m_adminHosts.erase(itor);
return;
}
}
// Reaching this point means the administration host to be deleted does not
// exist
std::ostringstream message;
message << "Administration host " << hostName << " does not exist";
throw Exception(message.str());
}
//------------------------------------------------------------------------------
// getAdminHosts
//------------------------------------------------------------------------------
std::list<cta::AdminHost> cta::MockAdminHostDatabase::getAdminHosts(
const SecurityIdentity &requester) const {
std::list<cta::AdminHost> hosts;
for(std::map<std::string, AdminHost>::const_iterator itor =
m_adminHosts.begin(); itor != m_adminHosts.end(); itor++) {
hosts.push_back(itor->second);
}
return hosts;
}
#pragma once
#include "AdminHost.hpp"
#include "SecurityIdentity.hpp"
#include <list>
#include <map>
#include <string>
namespace cta {
/**
* Mock database of administration hosts.
*/
class MockAdminHostDatabase {
public:
/**
* Creates the specified administration host.
*
* @param requester The identity of the user requesting the creation of the
* administration host.
* @param hostName The network name of the administration host.
* @param comment The comment describing the administration host.
*/
void createAdminHost(
const SecurityIdentity &requester,
const std::string &hostName,
const std::string &comment);
/**
* Throws an exception if the specified administration host already exists.
*
* @param hostName The network name of the administration host.
*/
void checkAdminHostDoesNotAlreadyExist(const std::string &hostName) const;
/**
* Deletes the specified administration host.
*
* @param requester The identity of the user requesting the deletion of the
* administration host.
* @param hostName The network name of the administration host.
* @param comment The comment describing the administration host.
*/
void deleteAdminHost(
const SecurityIdentity &requester,
const std::string &hostName);
/**
* Returns the current list of administration hosts.
*
* @param requester The identity of the user requesting the list.
*/
std::list<AdminHost> getAdminHosts(const SecurityIdentity &requester) const;
private:
/**
* Mapping from administration host-name to administration host.
*/
std::map<std::string, AdminHost> m_adminHosts;
}; // class MockAdminHostDatabase
} // namespace cta
#include "MockAdminHostDatabase.hpp"
#include <gtest/gtest.h>
namespace unitTests {
class cta_client_MockAdminHostDatabaseTest: public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(cta_client_MockAdminHostDatabaseTest, createAdminHost_new) {
using namespace cta;
MockAdminHostDatabase db;
const SecurityIdentity requester;
{
std::list<AdminHost> adminHosts;
ASSERT_NO_THROW(adminHosts = db.getAdminHosts(requester));
ASSERT_TRUE(adminHosts.empty());
}
const std::string adminHost1 = "adminHost1";
const std::string comment = "Comment";
ASSERT_NO_THROW(db.createAdminHost(requester, adminHost1, comment));
{
std::list<AdminHost> adminHosts;
ASSERT_NO_THROW(adminHosts = db.getAdminHosts(requester));
ASSERT_EQ(1, adminHosts.size());
ASSERT_EQ(adminHost1, adminHosts.front().getName());
ASSERT_EQ(comment, adminHosts.front().getComment());
}
}
TEST_F(cta_client_MockAdminHostDatabaseTest, deleteAdminHost_existing) {
using namespace cta;
MockAdminHostDatabase db;
const SecurityIdentity requester;
{
std::list<AdminHost> adminHosts;
ASSERT_NO_THROW(adminHosts = db.getAdminHosts(requester));
ASSERT_TRUE(adminHosts.empty());
}
const std::string adminHost1 = "adminHost1";
const std::string comment = "Comment";
ASSERT_NO_THROW(db.createAdminHost(requester, adminHost1, comment));
{
std::list<AdminHost> adminHosts;
ASSERT_NO_THROW(adminHosts = db.getAdminHosts(requester));
ASSERT_EQ(1, adminHosts.size());
ASSERT_EQ(adminHost1, adminHosts.front().getName());
ASSERT_EQ(comment, adminHosts.front().getComment());
}
ASSERT_NO_THROW(db.deleteAdminHost(requester, adminHost1));
{
std::list<AdminHost> adminHosts;
ASSERT_NO_THROW(adminHosts = db.getAdminHosts(requester));
ASSERT_TRUE(adminHosts.empty());
}
}
TEST_F(cta_client_MockAdminHostDatabaseTest, deleteAdminHost_non_existing) {
using namespace cta;
MockAdminHostDatabase db;
const SecurityIdentity requester;
{
std::list<AdminHost> adminHosts;
ASSERT_NO_THROW(adminHosts = db.getAdminHosts(requester));
ASSERT_TRUE(adminHosts.empty());
}
const std::string adminHost1 = "adminHost1";
ASSERT_THROW(db.deleteAdminHost(requester, adminHost1), std::exception);
{
std::list<AdminHost> adminHosts;
ASSERT_NO_THROW(adminHosts = db.getAdminHosts(requester));
ASSERT_TRUE(adminHosts.empty());
}
}
} // namespace unitTests
#include "Exception.hpp"
#include "MockAdminUserDatabase.hpp"
#include <iostream>
#include <memory>
#include <sstream>
//------------------------------------------------------------------------------
// createAdminUser
//------------------------------------------------------------------------------
void cta::MockAdminUserDatabase::createAdminUser(
const SecurityIdentity &requester,
const UserIdentity &user,
const std::string &comment) {
checkAdminUserDoesNotAlreadyExist(user);
AdminUser adminUser(user, requester.user, comment);
m_adminUsers[user.getUid()] = adminUser;
}
//------------------------------------------------------------------------------
// checkAdminUserDoesNotAlreadyExist
//------------------------------------------------------------------------------
void cta::MockAdminUserDatabase::checkAdminUserDoesNotAlreadyExist(
const UserIdentity &user) const {
std::map<uint32_t, AdminUser>::const_iterator itor =
m_adminUsers.find(user.getUid());
if(itor != m_adminUsers.end()) {
std::ostringstream message;
message << "Administrator with uid " << user.getUid() <<
" already exists";
throw(Exception(message.str()));
}
}
//------------------------------------------------------------------------------
// deleteAdminUser
//------------------------------------------------------------------------------
void cta::MockAdminUserDatabase::deleteAdminUser(
const SecurityIdentity &requester,
const UserIdentity &user) {
for(std::map<uint32_t, AdminUser>::iterator itor = m_adminUsers.begin();
itor != m_adminUsers.end(); itor++) {
if(user.getUid() == itor->first) {
m_adminUsers.erase(itor);
return;
}
}
// Reaching this point means the administrator to be deleted does not exist
std::ostringstream message;
message << "Administration with uid " << user.getUid() << " does not exist";
throw Exception(message.str());
}
//------------------------------------------------------------------------------
// getAdminUsers
//------------------------------------------------------------------------------
std::list<cta::AdminUser> cta::MockAdminUserDatabase::getAdminUsers(
const SecurityIdentity &requester) const {
std::list<cta::AdminUser> users;
for(std::map<uint32_t, AdminUser>::const_iterator itor =
m_adminUsers.begin(); itor != m_adminUsers.end(); itor++) {
users.push_back(itor->second);
}
return users;
}
#pragma once
#include "AdminUser.hpp"
#include "SecurityIdentity.hpp"
#include "UserIdentity.hpp"
#include <list>
#include <map>
namespace cta {
/**
* Mock database of admin users.
*/
class MockAdminUserDatabase {
public:
/**
* Creates the specified administrator.
*
* @param requester The identity of the user requesting the creation of the
* administrator.
* @param user The identity of the administrator.
* @param comment The comment describing the sministrator.
*/
void createAdminUser(
const SecurityIdentity &requester,
const UserIdentity &user,
const std::string &comment);
/**
* Deletes the specified administrator.
*
* @param requester The identity of the user requesting the deletion of the
* administrator.
* @param user The identity of the administrator.
*/
void deleteAdminUser(
const SecurityIdentity &requester,
const UserIdentity &user);
/**
* Returns the current list of administrators in lexicographical order.
*
* @param requester The identity of the user requesting the list.
* @return The current list of administrators in lexicographical order.
*/
std::list<AdminUser> getAdminUsers(const SecurityIdentity &requester) const;
/**
* Throws an exception if the specified administrator already exists.
*
* @param user The identity of the administrator.
*/
void checkAdminUserDoesNotAlreadyExist(const UserIdentity &user) const;
private:
/**
* Mapping from administrator uid to administrator.
*/
std::map<uint32_t, AdminUser> m_adminUsers;
}; // MockAdminUserDatabase
} // namespace cta
#include "MockAdminUserDatabase.hpp"
#include "MockMiddleTierUser.hpp"
#include <gtest/gtest.h>
namespace unitTests {
class cta_client_MockAdminUserDatabaseTest: public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(cta_client_MockAdminUserDatabaseTest, createAdminUser_new) {
using namespace cta;
MockAdminUserDatabase db;
const SecurityIdentity requester;
{
std::list<AdminUser> adminUsers;
ASSERT_NO_THROW(adminUsers = db.getAdminUsers(requester));
ASSERT_TRUE(adminUsers.empty());
}
const uint16_t adminUser1Uid = 1234;
const uint16_t adminUser1Gid = 5678;
const UserIdentity adminUser1(adminUser1Uid, adminUser1Gid);
const std::string comment = "Comment";
ASSERT_NO_THROW(db.createAdminUser(requester, adminUser1, comment));
{
std::list<AdminUser> adminUsers;
ASSERT_NO_THROW(adminUsers = db.getAdminUsers(requester));
ASSERT_EQ(1, adminUsers.size());
ASSERT_EQ(adminUser1Uid, adminUsers.front().getUser().getUid());
ASSERT_EQ(adminUser1Gid, adminUsers.front().getUser().getGid());
ASSERT_EQ(comment, adminUsers.front().getComment());
}
}
TEST_F(cta_client_MockAdminUserDatabaseTest, createAdminUser_already_existing) {
using namespace cta;
MockAdminUserDatabase db;
const SecurityIdentity requester;
{
std::list<AdminUser> adminUsers;
ASSERT_NO_THROW(adminUsers = db.getAdminUsers(requester));
ASSERT_TRUE(adminUsers.empty());
}
const uint16_t adminUser1Uid = 1234;
const uint16_t adminUser1Gid = 5678;
const UserIdentity adminUser1(adminUser1Uid, adminUser1Gid);
const std::string comment = "Comment";
ASSERT_NO_THROW(db.createAdminUser(requester, adminUser1, comment));
{
std::list<AdminUser> adminUsers;
ASSERT_NO_THROW(adminUsers = db.getAdminUsers(requester));
ASSERT_EQ(1, adminUsers.size());
ASSERT_EQ(adminUser1Uid, adminUsers.front().getUser().getUid());
ASSERT_EQ(adminUser1Gid, adminUsers.front().getUser().getGid());
ASSERT_EQ(comment, adminUsers.front().getComment());
}
ASSERT_THROW(db.createAdminUser(requester, adminUser1, comment),
std::exception);
{
std::list<AdminUser> adminUsers;
ASSERT_NO_THROW(adminUsers = db.getAdminUsers(requester));
ASSERT_EQ(1, adminUsers.size());
}
}
TEST_F(cta_client_MockAdminUserDatabaseTest, deleteAdminUser_existing) {
using namespace cta;
MockAdminUserDatabase db;
const SecurityIdentity requester;
{
std::list<AdminUser> adminUsers;
ASSERT_NO_THROW(adminUsers = db.getAdminUsers(requester));
ASSERT_TRUE(adminUsers.empty());
}
const uint16_t adminUser1Uid = 1234;
const uint16_t adminUser1Gid = 5678;
const UserIdentity adminUser1(adminUser1Uid, adminUser1Gid);
const std::string comment = "Comment";
ASSERT_NO_THROW(db.createAdminUser(requester, adminUser1, comment));
{
std::list<AdminUser> adminUsers;
ASSERT_NO_THROW(adminUsers = db.getAdminUsers(requester));
ASSERT_EQ(1, adminUsers.size());
ASSERT_EQ(adminUser1Uid, adminUsers.front().getUser().getUid());
ASSERT_EQ(adminUser1Gid, adminUsers.front().getUser().getGid());
ASSERT_EQ(comment, adminUsers.front().getComment());
}
ASSERT_NO_THROW(db.deleteAdminUser(requester, adminUser1));
{
std::list<AdminUser> adminUsers;
ASSERT_NO_THROW(adminUsers = db.getAdminUsers(requester));
ASSERT_TRUE(adminUsers.empty());
}
}
TEST_F(cta_client_MockAdminUserDatabaseTest, deleteAdminUser_non_existing) {
using namespace cta;
MockAdminUserDatabase db;
const SecurityIdentity requester;
{
std::list<AdminUser> adminUsers;
ASSERT_NO_THROW(adminUsers = db.getAdminUsers(requester));
ASSERT_TRUE(adminUsers.empty());
}
const uint16_t adminUser1Uid = 1234;
const uint16_t adminUser1Gid = 5678;
const UserIdentity adminUser1(adminUser1Uid, adminUser1Gid);
ASSERT_THROW(db.deleteAdminUser(requester, adminUser1), std::exception);
{
std::list<AdminUser> adminUsers;
ASSERT_NO_THROW(adminUsers = db.getAdminUsers(requester));
ASSERT_TRUE(adminUsers.empty());
}
}
} // namespace unitTests
#include "Exception.hpp"
#include "MockMiddleTierDatabase.hpp"
#include "MockDatabase.hpp"
#include <iostream>
#include <memory>
......@@ -8,7 +8,7 @@
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::MockMiddleTierDatabase::MockMiddleTierDatabase():
cta::MockDatabase::MockDatabase():
fileSystemRoot(storageClasses,
DirectoryEntry(DirectoryEntry::ENTRYTYPE_DIRECTORY, "/", "")) {
}
......@@ -16,5 +16,5 @@ cta::MockMiddleTierDatabase::MockMiddleTierDatabase():
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::MockMiddleTierDatabase::~MockMiddleTierDatabase() throw() {
cta::MockDatabase::~MockDatabase() throw() {
}
#pragma once
#include "AdminHost.hpp"
#include "AdminUser.hpp"
#include "FileSystemNode.hpp"
#include "FileSystemStorageClasses.hpp"
#include "LogicalLibrary.hpp"
#include "MiddleTierUser.hpp"
#include "MigrationRoutes.hpp"
#include "StorageClass.hpp"
#include "Tape.hpp"
#include <stdint.h>
#include <string>
#include <vector>
#include "MockAdminHostDatabase.hpp"
#include "MockAdminUserDatabase.hpp"
#include "MockLogicalLibraryDatabase.hpp"
#include "MockMigrationRouteDatabase.hpp"
#include "MockTapeDatabase.hpp"
#include "MockTapePoolDatabase.hpp"
namespace cta {
/**
* Mock middle-tier.
* Mock database.
*/
class MockMiddleTierDatabase {
public:
struct MockDatabase {
/**
* Constructor.
*
* Creates the root directory "/" owned by user root and with no file
* attributes or permissions.
*/