diff --git a/test/ServicesTest/Makefile b/test/ServicesTest/Makefile new file mode 100644 index 0000000..7726cec --- /dev/null +++ b/test/ServicesTest/Makefile @@ -0,0 +1,20 @@ +ECHO = @echo +GCC = g++ +CCFLAGS = -c -O3 -g -std=c++11 -pedantic -Wall +SRC = $(wildcard *.cpp) +OBJECTS = $(SRC:.cpp=.o) +EXE = test + +$(EXE) : $(OBJECTS) + $(ECHO) "-Linking $(EXE)-" + $(GCC) -o $@ $^ + +$(OBJECTS):$(SRC) + $(ECHO) "-Compilation $<- " + $(GCC) $(CCFLAGS) -o $@ $< + +.PHONY: clean +clean: + $(ECHO) "-Cleaning-" + $(RM) $(OBJECTS) $(EXE) + diff --git a/test/ServicesTest/Service.cpp b/test/ServicesTest/Service.cpp new file mode 100644 index 0000000..520536f --- /dev/null +++ b/test/ServicesTest/Service.cpp @@ -0,0 +1,41 @@ +//deployer Service implementation +// Copyright (C) 2024 Jean-Cloud +// GNU General Public License v3 + +#include "Service.h" + +//constructor and destructor defined inline + +//public methods +int Service::getUserID () const +{ + return userID; +} +string Service::getUsername() const +{ + return username; +} +list Service::getServers() const +{ + return servers; +} + +//operator == override +bool Service::operator == (const Service & service) const +{ + if (service.getServers()==servers && service.getUserID()==userID && service.getUsername()==username){ + return true; + }else{ + return false; + } +} + +//operator << override +ostream & operator << (ostream & out, const Service & s) +{ + out << "userID: " << s.userID << endl << "username: " << s.username << endl << "servers: "; + for (string server : s.servers){ + out << server << " " << endl; + } + return out; +} \ No newline at end of file diff --git a/test/ServicesTest/Service.h b/test/ServicesTest/Service.h new file mode 100644 index 0000000..fc081af --- /dev/null +++ b/test/ServicesTest/Service.h @@ -0,0 +1,29 @@ +//deployer Service header +// Copyright (C) 2024 Jean-Cloud +// GNU General Public License v3 + +#if !defined(SERVICE_H) +#define SERVICE_H + +#include +#include +#include +#include + +using namespace std; + +class Service { + public: + Service(int aUserID, string aUsername, list aServers):userID(aUserID),username(aUsername),servers(aServers){} + ~Service(){} + int getUserID () const; + string getUsername() const; + list getServers() const; + bool operator == (const Service & service) const; + friend ostream & operator<<(ostream & out, const Service & s); + private: + int userID; + string username; + list servers; +}; +#endif \ No newline at end of file diff --git a/test/ServicesTest/Services.cpp b/test/ServicesTest/Services.cpp new file mode 100644 index 0000000..07b2502 --- /dev/null +++ b/test/ServicesTest/Services.cpp @@ -0,0 +1,92 @@ +// deployer Services implementation +// Copyright (C) 2024 Jean-Cloud +// GNU General Public License v3 + +#include +#include +#include +#include +#include +#include "Services.h" + +//constructor +Services::Services(string ServicesCSV) +{ + services=readServicesFromCSV(ServicesCSV); +} +//destructor +Services::~Services(){} + +//public methods +vector Services::getServices()const +{ + return services; +} + +const Service* Services::findByUsername(string aUsername) const +{ + for (const Service & service : services){ + if (service.getUsername().compare(aUsername)==0){ + return &service; + } + } + return nullptr; +} + +const Service * Services::findByID(int aUserID) const +{ + for (const Service & service : services){ + if (service.getUserID()==aUserID){ + return &service; + } + } + return nullptr; +} + +list Services::findByServer(string aServer) const +{ + list result; + for (const Service & service : services){ + for (string server : service.getServers()){ + if(server.compare(aServer)==0){ + result.push_back(&service); + } + } + } + return result; +} + +//private methods +vector Services::readServicesFromCSV (string CSV) const +{ + //this method extracts the list of uid|username|servers from the services.csv file + //and returns them in a vector , with Service a structure defined in the header + vector result; + ifstream streamServices(CSV); + if (!streamServices){ + cout << "Invalid services.csv file." << endl; + }else{ + string line; + string tmpUserID; //used before converting to int + int userID; + string username; + string server; + list servers; + while(getline(streamServices,line)){ + if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account + continue; + } + stringstream streamLine(line); + getline(streamLine,tmpUserID,';'); //extracting the userID + userID=stoi(tmpUserID); + getline(streamLine,username,';'); //extracting the username + while(getline(streamLine,server,';')){ //extracting the server(s) + servers.push_back(server); + } + Service entry = {userID,username,servers}; + result.push_back(entry); + } + } + return result; +} + diff --git a/test/ServicesTest/Services.h b/test/ServicesTest/Services.h new file mode 100644 index 0000000..0d21a23 --- /dev/null +++ b/test/ServicesTest/Services.h @@ -0,0 +1,31 @@ +// deployer Services header +// Copyright (C) 2024 Jean-Cloud +// GNU General Public License v3 + +#if !defined(SERVICES_H) +#define SERVICES_H + +#include +#include +#include +#include +#include "Service.h" + +using namespace std; + + +class Services +//extracts the list of uid|username|service from the services.csv file +{ + public: + Services(string servicesCSV="../src/services.csv"); + vector getServices() const; + const Service * findByUsername(string aUsername) const; + const Service * findByID(int aUserID) const; + list findByServer(string aServer) const; + ~Services(); + private: + vector readServicesFromCSV (string CSV) const; + vector services; +}; +#endif \ No newline at end of file diff --git a/test/ServicesTest/main.cpp b/test/ServicesTest/main.cpp new file mode 100644 index 0000000..d12bdf0 --- /dev/null +++ b/test/ServicesTest/main.cpp @@ -0,0 +1,35 @@ +//test file for class Services + +#include "Services.h" + +int main() +{ + Services aServices = Services("testSample.csv"); + vector result = aServices.getServices(); + + vector expectedResult; + list s1={"server1","server1bis"}; + list s3={"server3"}; + list s4={""}; + list s5={""}; + list s6={""}; + list s7={""}; + list s8={"server8","server8bis","server8ter"}; + + expectedResult.push_back(Service(1,"username1",s1)); + expectedResult.push_back(Service(3,"username3",s3)); + expectedResult.push_back(Service(4,"username4",s4)); + expectedResult.push_back(Service(5,"username5",s5)); + expectedResult.push_back(Service(6,"",s6)); + expectedResult.push_back(Service(7,"username7",s7)); + + if(expectedResult==result){ + cout << "Services test: OK" << endl; + return 0; + }else { + cout << "Services test: FAIL" << endl; + return 1; + } +} + + diff --git a/test/ServicesTest/testSample.csv b/test/ServicesTest/testSample.csv new file mode 100644 index 0000000..4019acc --- /dev/null +++ b/test/ServicesTest/testSample.csv @@ -0,0 +1,9 @@ +1;username1;server1;server1bis +#2;comment;comment +3;username3;server3 +4;username4 +5;username5; +6;;; +7;username7 + + diff --git a/test/deployer b/test/deployer new file mode 100755 index 0000000..e9887d3 Binary files /dev/null and b/test/deployer differ