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

Added MockArchivalJobTableTest

parent 235e64ef
......@@ -45,6 +45,7 @@ add_library (ctamiddletier SHARED
set (MIDDLE_TIER_UNIT_TESTS_LIB_SRC_FILES
MockAdminHostTableTest.cpp
MockAdminUserTableTest.cpp
MockArchivalJobTableTest.cpp
MockLogicalLibraryTableTest.cpp
MockMiddleTierAdminTest.cpp
MockMiddleTierUserTest.cpp
......
#include "Exception.hpp"
#include "MockArchivalJobTable.hpp"
#include "Exception.hpp"
#include <sstream>
//------------------------------------------------------------------------------
// createArchivalJob
//------------------------------------------------------------------------------
void cta::MockArchivalJobTable::createArchivalJob(
const SecurityIdentity &requester,
const std::string &tapePoolName,
const std::string &srcUrl,
const std::string &dstPath) {
checkArchivalJobDoesNotAlreadyExist(dstPath);
ArchivalJob job(ArchivalJobState::NONE, srcUrl, dstPath, requester.user);
std::map<std::string, std::map<time_t, ArchivalJob> >::iterator poolItor =
m_jobsTree.find(tapePoolName);
if(poolItor == m_jobsTree.end()) {
std::map<time_t, ArchivalJob> jobs;
jobs[job.getCreationTime()] = job;
m_jobsTree[tapePoolName] = jobs;
} else {
poolItor->second[job.getCreationTime()] = job;
}
}
//------------------------------------------------------------------------------
......@@ -16,6 +32,20 @@ void cta::MockArchivalJobTable::createArchivalJob(
//------------------------------------------------------------------------------
void cta::MockArchivalJobTable::checkArchivalJobDoesNotAlreadyExist(
const std::string &dstPath) const {
for(std::map<std::string, std::map<time_t, ArchivalJob> >::const_iterator
poolItor = m_jobsTree.begin(); poolItor != m_jobsTree.end(); poolItor++) {
const std::map<time_t, ArchivalJob> &jobs = poolItor->second;
for(std::map<time_t, ArchivalJob>::const_iterator jobItor =
jobs.begin(); jobItor != jobs.end(); jobItor++) {
const ArchivalJob &job = jobItor->second;
if(dstPath == job.getDstPath()) {
std::ostringstream message;
message << "An archival job for destination path " << dstPath <<
" already exists";
throw(Exception(message.str()));
}
}
}
}
//------------------------------------------------------------------------------
......@@ -24,16 +54,35 @@ void cta::MockArchivalJobTable::checkArchivalJobDoesNotAlreadyExist(
void cta::MockArchivalJobTable::deleteArchivalJob(
const SecurityIdentity &requester,
const std::string &dstPath) {
for(std::map<std::string, std::map<time_t, ArchivalJob> >::iterator
poolItor = m_jobsTree.begin(); poolItor != m_jobsTree.end(); poolItor++) {
std::map<time_t, ArchivalJob> &jobs = poolItor->second;
for(std::map<time_t, ArchivalJob>::iterator jobItor =
jobs.begin(); jobItor != jobs.end(); jobItor++) {
const ArchivalJob &job = jobItor->second;
if(dstPath == job.getDstPath()) {
jobs.erase(jobItor);
if(jobs.empty()) {
m_jobsTree.erase(poolItor);
}
return;
}
}
}
std::ostringstream message;
message << "An archival job for destination path " << dstPath <<
" does not exist";
throw(Exception(message.str()));
}
//------------------------------------------------------------------------------
// getArchivalJobs
//------------------------------------------------------------------------------
const std::map<cta::TapePool, std::list<cta::ArchivalJob> >
cta::MockArchivalJobTable::getArchivalJobs(
const std::map<std::string, std::map<time_t, cta::ArchivalJob> >
&cta::MockArchivalJobTable::getArchivalJobs(
const SecurityIdentity &requester) const {
std::map<cta::TapePool, std::list<cta::ArchivalJob> > jobs;
return jobs;
return m_jobsTree;
}
//------------------------------------------------------------------------------
......@@ -43,5 +92,16 @@ std::list<cta::ArchivalJob> cta::MockArchivalJobTable::getArchivalJobs(
const SecurityIdentity &requester,
const std::string &tapePoolName) const {
std::list<cta::ArchivalJob> jobs;
const std::map<std::string, std::map<time_t, ArchivalJob> >::const_iterator
poolItor = m_jobsTree.find(tapePoolName);
if(poolItor != m_jobsTree.end()) {
const std::map<time_t, ArchivalJob> &jobMap = poolItor->second;
for(std::map<time_t, ArchivalJob>::const_iterator jobItor =
jobMap.begin(); jobItor != jobMap.end(); jobItor++) {
jobs.push_back(jobItor->second);
}
}
return jobs;
}
......@@ -21,11 +21,13 @@ public:
*
* @param requester The identity of the user requesting the creation of the
* job.
* @param tapePoolName The name of the destination tape pool.
* @param srcUrl The URL of the source file.
* @param dstPath The full path of the destination file within the archive.
*/
void createArchivalJob(
const SecurityIdentity &requester,
const std::string &tapePoolName,
const std::string &srcUrl,
const std::string &dstPath);
......@@ -48,15 +50,15 @@ public:
const std::string &dstPath);
/**
* Returns all of the existing archival jobs grouped by tape pool and then
* sorted by creation time in ascending order (oldest first).
* Returns all of the existing archival jobs grouped by tape pool name and
* then sorted by creation time in ascending order (oldest first).
*
* @param requester The identity of the user requesting the list.
* @return All of the existing archival jobs grouped by tape pool and then
* sorted by creation time in ascending order (oldest first).
*/
const std::map<TapePool, std::list<ArchivalJob> > getArchivalJobs(
const SecurityIdentity &requester) const;
const std::map<std::string, std::map<time_t, ArchivalJob> >
&getArchivalJobs(const SecurityIdentity &requester) const;
/**
* Returns the list of archival jobs associated with the specified tape pool
......@@ -74,10 +76,10 @@ public:
private:
/**
* All of the existing archival jobs grouped by tape pool and then
* All of the existing archival jobs grouped by tape pool name and then
* sorted by creation time in ascending order (oldest first).
*/
std::map<TapePool, std::map<time_t, ArchivalJob> > m_jobs;
std::map<std::string, std::map<time_t, ArchivalJob> > m_jobsTree;
}; // class MockArchivalJobTable
......
#include "MockArchivalJobTable.hpp"
#include <gtest/gtest.h>
namespace unitTests {
class cta_client_MockArchivalJobTableTest: public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(cta_client_MockArchivalJobTableTest, createArchivalJob_new) {
using namespace cta;
MockArchivalJobTable table;
const SecurityIdentity requester;
{
std::map<std::string, std::map<time_t, ArchivalJob> > pools;
ASSERT_NO_THROW(pools = table.getArchivalJobs(requester));
ASSERT_TRUE(pools.empty());
}
const std::string tapePoolName = "tapePool";
const std::string srcUrl = "diskUrl";
const std::string dstPath = "/tapeFilePath";
ASSERT_NO_THROW(table.createArchivalJob(requester, tapePoolName, srcUrl,
dstPath));
{
std::map<std::string, std::map<time_t, ArchivalJob> > pools;
ASSERT_NO_THROW(pools = table.getArchivalJobs(requester));
ASSERT_EQ(1, pools.size());
std::map<std::string, std::map<time_t, ArchivalJob> >::const_iterator
poolItor = pools.find(tapePoolName);
ASSERT_FALSE(poolItor == pools.end());
const std::map<time_t, ArchivalJob> &jobs = poolItor->second;
ASSERT_EQ(1, jobs.size());
const std::map<time_t, ArchivalJob>::const_iterator jobItor = jobs.begin();
ASSERT_FALSE(jobItor == jobs.end());
const ArchivalJob &job = jobItor->second;
ASSERT_EQ(srcUrl, job.getSrcUrl());
ASSERT_EQ(dstPath, job.getDstPath());
}
{
const std::list<ArchivalJob> &jobs = table.getArchivalJobs(requester,
tapePoolName);
ASSERT_EQ(1, jobs.size());
const ArchivalJob &job = jobs.front();
ASSERT_EQ(srcUrl, job.getSrcUrl());
ASSERT_EQ(dstPath, job.getDstPath());
}
}
TEST_F(cta_client_MockArchivalJobTableTest, createArchivalJob_already_existing) {
using namespace cta;
MockArchivalJobTable table;
const SecurityIdentity requester;
{
std::map<std::string, std::map<time_t, ArchivalJob> > pools;
ASSERT_NO_THROW(pools = table.getArchivalJobs(requester));
ASSERT_TRUE(pools.empty());
}
const std::string tapePoolName = "tapePool";
const std::string srcUrl = "diskUrl";
const std::string dstPath = "/tapeFilePath";
ASSERT_NO_THROW(table.createArchivalJob(requester, tapePoolName, srcUrl,
dstPath));
{
std::map<std::string, std::map<time_t, ArchivalJob> > pools;
ASSERT_NO_THROW(pools = table.getArchivalJobs(requester));
ASSERT_EQ(1, pools.size());
std::map<std::string, std::map<time_t, ArchivalJob> >::const_iterator
poolItor = pools.find(tapePoolName);
ASSERT_FALSE(poolItor == pools.end());
const std::map<time_t, ArchivalJob> &jobs = poolItor->second;
ASSERT_EQ(1, jobs.size());
const std::map<time_t, ArchivalJob>::const_iterator jobItor = jobs.begin();
ASSERT_FALSE(jobItor == jobs.end());
const ArchivalJob &job = jobItor->second;
ASSERT_EQ(srcUrl, job.getSrcUrl());
ASSERT_EQ(dstPath, job.getDstPath());
}
{
const std::list<ArchivalJob> &jobs = table.getArchivalJobs(requester,
tapePoolName);
ASSERT_EQ(1, jobs.size());
const ArchivalJob &job = jobs.front();
ASSERT_EQ(srcUrl, job.getSrcUrl());
ASSERT_EQ(dstPath, job.getDstPath());
}
ASSERT_THROW(table.createArchivalJob(requester, tapePoolName, srcUrl,
dstPath), std::exception);
}
TEST_F(cta_client_MockArchivalJobTableTest, deleteTape_existing) {
using namespace cta;
MockArchivalJobTable table;
const SecurityIdentity requester;
{
std::map<std::string, std::map<time_t, ArchivalJob> > pools;
ASSERT_NO_THROW(pools = table.getArchivalJobs(requester));
ASSERT_TRUE(pools.empty());
}
const std::string tapePoolName = "tapePool";
const std::string srcUrl = "diskUrl";
const std::string dstPath = "/tapeFilePath";
ASSERT_NO_THROW(table.createArchivalJob(requester, tapePoolName, srcUrl,
dstPath));
{
std::map<std::string, std::map<time_t, ArchivalJob> > pools;
ASSERT_NO_THROW(pools = table.getArchivalJobs(requester));
ASSERT_EQ(1, pools.size());
std::map<std::string, std::map<time_t, ArchivalJob> >::const_iterator
poolItor = pools.find(tapePoolName);
ASSERT_FALSE(poolItor == pools.end());
const std::map<time_t, ArchivalJob> &jobs = poolItor->second;
ASSERT_EQ(1, jobs.size());
const std::map<time_t, ArchivalJob>::const_iterator jobItor = jobs.begin();
ASSERT_FALSE(jobItor == jobs.end());
const ArchivalJob &job = jobItor->second;
ASSERT_EQ(srcUrl, job.getSrcUrl());
ASSERT_EQ(dstPath, job.getDstPath());
}
{
const std::list<ArchivalJob> &jobs = table.getArchivalJobs(requester,
tapePoolName);
ASSERT_EQ(1, jobs.size());
const ArchivalJob &job = jobs.front();
ASSERT_EQ(srcUrl, job.getSrcUrl());
ASSERT_EQ(dstPath, job.getDstPath());
}
ASSERT_NO_THROW(table.deleteArchivalJob(requester, dstPath));
{
std::map<std::string, std::map<time_t, ArchivalJob> > pools;
ASSERT_NO_THROW(pools = table.getArchivalJobs(requester));
ASSERT_TRUE(pools.empty());
}
}
TEST_F(cta_client_MockArchivalJobTableTest, deleteTape_non_existing) {
using namespace cta;
MockArchivalJobTable table;
const SecurityIdentity requester;
const std::string dstPath = "/tapeFilePath";
ASSERT_THROW(table.deleteArchivalJob(requester, dstPath), std::exception);
}
} // namespace unitTests
......@@ -277,7 +277,8 @@ void cta::MockMiddleTierUser::checkUserIsAuthorisedToArchive(
std::map<cta::TapePool, std::list<cta::ArchivalJob> >
cta::MockMiddleTierUser::getArchivalJobs(
const SecurityIdentity &requester) const {
return m_db.archivalJobs.getArchivalJobs(requester);
std::map<cta::TapePool, std::list<cta::ArchivalJob> > jobs;
return jobs;
}
//------------------------------------------------------------------------------
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment