Skip to content

Commit cf8f760

Browse files
authored
Merge pull request #24 from snandasena/dev
Improved
2 parents 392fb4b + 8b0dd4b commit cf8f760

File tree

13 files changed

+298
-1
lines changed

13 files changed

+298
-1
lines changed

app/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ add_subdirectory(interfaces)
88
add_subdirectory(parsers)
99
add_subdirectory(configs)
1010
add_subdirectory(auth)
11-
add_subdirectory(servers)
11+
add_subdirectory(servers)
12+
add_subdirectory(services)
13+
add_subdirectory(storage)

app/services/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
project(Services)
2+
3+
add_subdirectory(tests)
4+
5+
include_directories(${PROJECT_SOURCE_DIR}/include)
6+
7+
set(HEADERS
8+
${PROJECT_SOURCE_DIR}/include/services/TaxService.h
9+
)
10+
11+
set(SOURCES
12+
${PROJECT_SOURCE_DIR}/src/TaxServiceFactory.cpp
13+
${PROJECT_SOURCE_DIR}/src/TaxService.cpp
14+
)
15+
16+
add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS})
17+
18+
target_link_libraries( ${PROJECT_NAME} PRIVATE
19+
Interfaces
20+
Common
21+
Storage
22+
Servers
23+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Created by sajith on 7/4/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_TAXSERVICE_H
6+
#define DESIGN_PATTERNS_TAXSERVICE_H
7+
8+
#include "services/ITaxService.h"
9+
10+
#include <memory>
11+
12+
#include "auth/IAuthorization.h"
13+
#include "parsers/IReportParser.h"
14+
#include "storage/ReportsStorage.h"
15+
#include "types/User.h"
16+
17+
namespace services
18+
{
19+
class TaxService : public ITaxService
20+
{
21+
public:
22+
23+
TaxService(const types::User &, const auth::IAuthorization &, const parsers::IReportParser &);
24+
25+
ReportStatus onReportRequest(const std::string &) override;
26+
27+
private:
28+
const types::User &user;
29+
const auth::IAuthorization &authManager;
30+
const parsers::IReportParser &reportParser;
31+
storage::ReportsStorage storage;
32+
};
33+
}
34+
35+
#endif //DESIGN_PATTERNS_TAXSERVICE_H
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Created by sajith on 7/4/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_TAXSERVICEFACTORY_H
6+
#define DESIGN_PATTERNS_TAXSERVICEFACTORY_H
7+
8+
#include "auth/IAuthorization.h"
9+
#include "parsers/IReportParser.h"
10+
#include "services/ITaxServiceFactory.h"
11+
12+
namespace services
13+
{
14+
class TaxServiceFactory : public ITaxServiceFactory
15+
{
16+
public:
17+
18+
TaxServiceFactory(const auth::IAuthorization &, const parsers::IReportParser &);
19+
20+
std::unique_ptr<ITaxService> create(const types::User &) const override;
21+
22+
private:
23+
const auth::IAuthorization &authManager;
24+
const parsers::IReportParser &reportParser;
25+
};
26+
}
27+
28+
#endif //DESIGN_PATTERNS_TAXSERVICEFACTORY_H

app/services/src/TaxService.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Created by sajith on 7/4/22.
3+
//
4+
5+
#include "constants/Constants.h"
6+
#include "services/TaxService.h"
7+
8+
services::TaxService::TaxService(const types::User &u, const auth::IAuthorization &auth,
9+
const parsers::IReportParser &parsr) :
10+
user{u}, authManager{auth}, reportParser{parsr}
11+
{
12+
13+
}
14+
services::ReportStatus services::TaxService::onReportRequest(const std::string &request)
15+
{
16+
const auto report = reportParser.parseReport(request);
17+
if (report != std::nullopt && authManager.isAuthorized(user.login, report->payer))
18+
{
19+
storage.storeReport(*report);
20+
return constants::OK;
21+
}
22+
return constants::NOK;
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Created by sajith on 7/4/22.
3+
//
4+
5+
6+
#include "services/TaxServiceFactory.h"
7+
#include "services/TaxService.h"
8+
9+
namespace services
10+
{
11+
TaxServiceFactory::TaxServiceFactory(const auth::IAuthorization &auth, const parsers::IReportParser &parser) :
12+
authManager{auth}, reportParser{parser}
13+
{
14+
15+
}
16+
std::unique_ptr<ITaxService> services::TaxServiceFactory::create(const types::User &user) const
17+
{
18+
return std::make_unique<TaxService>(user, authManager, reportParser);
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Created by sajith on 7/4/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_AUTHORIZATIONMOCK_H
6+
#define DESIGN_PATTERNS_AUTHORIZATIONMOCK_H
7+
8+
#include "auth/IAuthorization.h"
9+
10+
#include <gmock/gmock.h>
11+
12+
13+
namespace auth
14+
{
15+
class AuthorizationMock : public IAuthorization
16+
{
17+
public:
18+
MOCK_CONST_METHOD2(isAuthorized, bool(const types::Login &, std::uint32_t));
19+
};
20+
}
21+
#endif //DESIGN_PATTERNS_AUTHORIZATIONMOCK_H

app/services/tests/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
project(ServicesTests)
2+
3+
find_package(GTest REQUIRED)
4+
5+
6+
include_directories(
7+
${GTEST_INCLUDE_DIRS}
8+
${GMOCK_INCLUDE_DIRS}
9+
)
10+
11+
set(SOURCES
12+
TaxService_tests.cpp)
13+
14+
add_executable(${PROJECT_NAME} ${SOURCES})
15+
16+
target_link_libraries(${PROJECT_NAME} PRIVATE
17+
GTest::GTest
18+
GTest::Main
19+
gmock
20+
gmock_main
21+
Interfaces
22+
Servers
23+
Services
24+
Storage
25+
Common
26+
)
27+
28+
add_test(${PROJECT_NAME} ${PROJECT_NAME})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Created by sajith on 7/4/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_REPORTPARSERMOCK_H
6+
#define DESIGN_PATTERNS_REPORTPARSERMOCK_H
7+
8+
#include "parsers/IReportParser.h"
9+
10+
#include <gmock/gmock.h>
11+
12+
namespace parsers
13+
{
14+
class ReportParserMock : public IReportParser
15+
{
16+
public:
17+
MOCK_CONST_METHOD1(parseReport, std::optional<types::Report>(const std::string&));
18+
};
19+
}
20+
21+
#endif //DESIGN_PATTERNS_REPORTPARSERMOCK_H
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Created by sajith on 7/4/22.
3+
//
4+
5+
6+
7+
#include <gtest/gtest.h>
8+
#include <gmock/gmock.h>
9+
10+
#include <optional>
11+
#include <string>
12+
13+
#include "constants/Constants.h"
14+
#include "AuthorizationMock.h"
15+
#include "ReportParserMock.h"
16+
#include "types/Report.h"
17+
#include "types/User.h"
18+
#include "../include/services/TaxService.h"
19+
20+
21+
using namespace constants;
22+
23+
using ::testing::Return;
24+
using ::testing::StrictMock;
25+
26+
struct TaxServiceTests : testing::Test
27+
{
28+
TaxServiceTests() : sut(user, authMock, parserMock)
29+
{}
30+
31+
32+
types::User user;
33+
StrictMock<auth::AuthorizationMock> authMock;
34+
StrictMock<parsers::ReportParserMock> parserMock;
35+
services::TaxService sut;
36+
37+
const std::string rawReport = "{}";
38+
const types::Report report{10, "VAT", 20, 2020};
39+
40+
};
41+
42+
TEST_F(TaxServiceTests, whenReportParsingAndAuthorizationSucceed_returnOK)
43+
{
44+
EXPECT_CALL(authMock, isAuthorized(user.login, report.payer)).WillOnce(Return(true));
45+
EXPECT_CALL(parserMock, parseReport(rawReport)).WillOnce(Return(report));
46+
ASSERT_EQ(sut.onReportRequest(rawReport),OK);
47+
}

0 commit comments

Comments
 (0)