From 75ee24a9f9105ddbf0e1a39e87561f0fc3c01662 Mon Sep 17 00:00:00 2001
From: kongr45gpen <electrovesta@gmail.com>
Date: Sun, 18 Nov 2018 15:48:33 +0200
Subject: [PATCH] Add tests for ST[17]

---
 CMakeLists.txt                  |  2 +-
 tests/Services/ServiceTests.hpp | 53 +++++++++++++++++++++++++++++++++
 tests/Services/TestService.cpp  | 31 +++++++++++++++++++
 tests/TestPlatform.cpp          | 26 ++++++++++++++++
 4 files changed, 111 insertions(+), 1 deletion(-)
 create mode 100644 tests/Services/ServiceTests.hpp
 create mode 100644 tests/TestPlatform.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 764116df..a20f53f8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,6 +18,6 @@ add_executable(ecss_services src/main.cpp src/Message.cpp src/Service.cpp src/Se
 
 IF(EXISTS "${PROJECT_SOURCE_DIR}/lib/Catch2/CMakeLists.txt")
 add_subdirectory(lib/Catch2)
-add_executable(test src/Message.cpp src/Service.cpp src/Services/TestService.cpp tests/tests.cpp tests/Message.cpp)
+add_executable(test src/Message.cpp src/Services/TestService.cpp tests/tests.cpp tests/Message.cpp tests/TestPlatform.cpp tests/Services/TestService.cpp)
 target_link_libraries(test Catch2::Catch2)
 ENDIF()
diff --git a/tests/Services/ServiceTests.hpp b/tests/Services/ServiceTests.hpp
new file mode 100644
index 00000000..93c3740c
--- /dev/null
+++ b/tests/Services/ServiceTests.hpp
@@ -0,0 +1,53 @@
+#ifndef ECSS_SERVICES_TESTS_SERVICES_SERVICETESTS_HPP
+#define ECSS_SERVICES_TESTS_SERVICES_SERVICETESTS_HPP
+
+#include <vector>
+#include <Message.hpp>
+
+/**
+ * Supporting class for tests against ECSS services
+ *
+ * @todo See if members of this class can be made non-static
+ */
+class ServiceTests {
+	static std::vector<Message> queuedMessages;
+
+public:
+	/**
+	 * Get a message from the list of queued messages to send
+	 * @param number The number of the message, starting from 0 in chronological order
+	 */
+	static Message& get(unsigned long number) {
+		return queuedMessages.at(number);
+	}
+
+	/**
+	 * Add a message to the queue of messages to be sent
+	 */
+	static void queue(const Message &message) {
+		queuedMessages.push_back(message);
+	}
+
+	/**
+	 * Counts the number of messages in the queue
+	 */
+	static unsigned long count() {
+		return queuedMessages.size();
+	}
+
+	/**
+	 * Checks that there is *exactly* one message in the list of queued messages
+	 */
+	static bool hasOneMessage() {
+		return count() == 1;
+	}
+
+	/**
+	 * Remove all the queued messages from the list, starting over from 0 items again
+	 */
+	static void reset() {
+		queuedMessages.clear();
+	}
+};
+
+#endif //ECSS_SERVICES_TESTS_SERVICES_SERVICETESTS_HPP
diff --git a/tests/Services/TestService.cpp b/tests/Services/TestService.cpp
index 139597f9..0a5233a6 100644
--- a/tests/Services/TestService.cpp
+++ b/tests/Services/TestService.cpp
@@ -1,2 +1,33 @@
+#include <catch2/catch.hpp>
+#include <Services/TestService.hpp>
+#include <Message.hpp>
+#include "ServiceTests.hpp"
 
+TEST_CASE("TM[17,1]", "[service][st17]") {
+	TestService testService;
+
+	Message receivedPacket = Message(17, 1, Message::TC, 1);
+	testService.areYouAlive(receivedPacket);
+	REQUIRE(ServiceTests::hasOneMessage());
+
+	Message response = ServiceTests::get(0);
+	CHECK(response.serviceType == 17);
+	CHECK(response.messageType == 2);
+	REQUIRE(response.dataSize == 0);
+}
+
+TEST_CASE("TM[17,3]", "[service][st17]") {
+	TestService testService;
+
+	Message receivedPacket = Message(17, 3, Message::TC, 1);
+	receivedPacket.appendEnum16(40);
+	testService.onBoardConnection(receivedPacket);
+	REQUIRE(ServiceTests::hasOneMessage());
+
+	Message response = ServiceTests::get(0);
+	CHECK(response.serviceType == 17);
+	CHECK(response.messageType == 4);
+	REQUIRE(response.dataSize == 2);
+	CHECK(response.readEnum16() == 40);
+}
 
diff --git a/tests/TestPlatform.cpp b/tests/TestPlatform.cpp
new file mode 100644
index 00000000..08021c0b
--- /dev/null
+++ b/tests/TestPlatform.cpp
@@ -0,0 +1,26 @@
+#define CATCH_CONFIG_EXTERNAL_INTERFACES
+
+#include <catch2/catch.hpp>
+#include <Message.hpp>
+#include <Service.hpp>
+#include "Services/ServiceTests.hpp"
+
+std::vector<Message> ServiceTests::queuedMessages = std::vector<Message>();
+
+void Service::storeMessage(const Message &message) {
+	ServiceTests::queue(message);
+}
+
+struct ServiceTestsListener : Catch::TestEventListenerBase {
+	using TestEventListenerBase::TestEventListenerBase; // inherit constructor
+
+	void testCaseStarting( Catch::TestCaseInfo const& testInfo ) override {
+		// Perform some setup before a test case is run
+	}
+
+	void testCaseEnded( Catch::TestCaseStats const& testCaseStats ) override {
+		// Tear-down after a test case is run
+		ServiceTests::reset();
+	}
+};
+CATCH_REGISTER_LISTENER( ServiceTestsListener )
-- 
GitLab