From b647b95891e46964e70e406158af24bd3e5ee9b1 Mon Sep 17 00:00:00 2001
From: athatheocsd <athatheo@csd.auth.gr>
Date: Thu, 29 Nov 2018 18:29:32 +0200
Subject: [PATCH] Tests created and passed

---
 CMakeLists.txt                       |  2 +-
 src/Services/EventReportService.cpp  |  8 ---
 test/Services/EventReportService.cpp | 85 ++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 9 deletions(-)
 create mode 100644 test/Services/EventReportService.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9329ae3a..17504fda 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -39,7 +39,7 @@ IF (EXISTS "${PROJECT_SOURCE_DIR}/lib/Catch2/CMakeLists.txt")
     add_executable(tests
             $<TARGET_OBJECTS:common>
             ${test_main_SRC}
-            ${test_SRC})
+            ${test_SRC} test/Services/EventReportService.cpp)
 
     target_link_libraries(tests Catch2::Catch2)
 ENDIF()
diff --git a/src/Services/EventReportService.cpp b/src/Services/EventReportService.cpp
index 45063616..bb3b68e3 100644
--- a/src/Services/EventReportService.cpp
+++ b/src/Services/EventReportService.cpp
@@ -1,14 +1,6 @@
 #include "Services/EventReportService.hpp"
 #include "Message.hpp"
 
-/**
- * Note for the funtions informativeEventReport, lowSeverityReport, mediumSeverityReport,
- * highSeverityReport: I could use
- * for (int i=0; i<length; i++ {
- * 		report.appendByte(data[i]);
- * }
- * instead of reinterpret_cast as it is a dangerous cast
- */
 
 void EventReportService::informativeEventReport(uint16_t eventID, const uint8_t *data,
                                                 uint8_t length) {
diff --git a/test/Services/EventReportService.cpp b/test/Services/EventReportService.cpp
new file mode 100644
index 00000000..6d5fa141
--- /dev/null
+++ b/test/Services/EventReportService.cpp
@@ -0,0 +1,85 @@
+#include <catch2/catch.hpp>
+#include <Services/EventReportService.hpp>
+#include <Message.hpp>
+#include "ServiceTests.hpp"
+#include <cstring>
+
+/*
+ * @todo Change the reinterpret_cast
+ */
+TEST_CASE("Informative Event Report TM[5,1]","[service][st05]") {
+	EventReportService eventReportService;
+	const unsigned char eventReportData[]="HelloWorld";
+	char checkString[255];
+	eventReportService.informativeEventReport(1, eventReportData,10);
+	REQUIRE(ServiceTests::hasOneMessage());
+
+	Message report = ServiceTests::get(0);
+	// Checks for the data-members of the report Message created
+	CHECK(report.serviceType == 5);
+	CHECK(report.messageType == 1);
+	CHECK(report.packetType == 0); // packet type(TM = 0, TC = 1)
+	REQUIRE(report.dataSize == 12);
+	// Check for the value that is stored in <<data>> array(data-member of object response)
+	CHECK(report.readEnum16() == 1);
+	report.readString(checkString,10);
+	CHECK(strcmp(checkString, reinterpret_cast<const char*>(eventReportData)) == 0);
+}
+
+TEST_CASE("Loww Severity Anomaly Report TM[5,2]","[service][st05]") {
+	EventReportService eventReportService;
+	const unsigned char eventReportData[]="HelloWorld";
+	char checkString[255];
+	eventReportService.lowSeverityAnomalyReport(2, eventReportData,10);
+	REQUIRE(ServiceTests::hasOneMessage());
+
+	Message report = ServiceTests::get(0);
+	// Checks for the data-members of the report Message created
+	CHECK(report.serviceType == 5);
+	CHECK(report.messageType == 2);
+	CHECK(report.packetType == 0); // packet type(TM = 0, TC = 1)
+	REQUIRE(report.dataSize == 12);
+	// Check for the value that is stored in <<data>> array(data-member of object response)
+	CHECK(report.readEnum16() == 2);
+	report.readString(checkString,10);
+	CHECK(strcmp(checkString, reinterpret_cast<const char*>(eventReportData)) == 0);
+}
+
+TEST_CASE("Medium Severity Anomaly Report TM[5,3]","[service][st05]") {
+	EventReportService eventReportService;
+	const unsigned char eventReportData[]="HelloWorld";
+	char checkString[255];
+	eventReportService.mediumSeverityAnomalyReport(3, eventReportData,10);
+	REQUIRE(ServiceTests::hasOneMessage());
+
+	Message report = ServiceTests::get(0);
+	// Checks for the data-members of the report Message created
+	CHECK(report.serviceType == 5);
+	CHECK(report.messageType == 3);
+	CHECK(report.packetType == 0); // packet type(TM = 0, TC = 1)
+	REQUIRE(report.dataSize == 12);
+	// Check for the value that is stored in <<data>> array(data-member of object response)
+	CHECK(report.readEnum16() == 3);
+	report.readString(checkString,10);
+	CHECK(strcmp(checkString, reinterpret_cast<const char*>(eventReportData)) == 0);
+}
+
+TEST_CASE("High Severity Anomaly Report TM[5,4]","[service][st05]") {
+	EventReportService eventReportService;
+	const unsigned char eventReportData[]="HelloWorld";
+	char checkString[255];
+	eventReportService.highSeverityAnomalyReport(4, eventReportData,10);
+	REQUIRE(ServiceTests::hasOneMessage());
+
+	Message report = ServiceTests::get(0);
+	// Checks for the data-members of the report Message created
+	CHECK(report.serviceType == 5);
+	CHECK(report.messageType == 4);
+	CHECK(report.packetType == 0); // packet type(TM = 0, TC = 1)
+	REQUIRE(report.dataSize == 12);
+	// Check for the value that is stored in <<data>> array(data-member of object response)
+	CHECK(report.readEnum16() == 4);
+	report.readString(checkString,10);
+	CHECK(strcmp(checkString, reinterpret_cast<const char*>(eventReportData)) == 0);
+}
+
-- 
GitLab