Add unit tests for Job scheduler API 09/279409/2
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 9 Aug 2022 03:24:51 +0000 (12:24 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Tue, 9 Aug 2022 03:25:30 +0000 (03:25 +0000)
To refactor job scheduler API, unit tests are added.

Change-Id: I865766786bb7694eb1b3b0b70088742cc4ff6643
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
test/unit_tests/test_job_scheduler.cc [new file with mode: 0644]

diff --git a/test/unit_tests/test_job_scheduler.cc b/test/unit_tests/test_job_scheduler.cc
new file mode 100644 (file)
index 0000000..873f4d3
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <aul.h>
+#include <aul_cmd.h>
+#include <aul_job_scheduler.h>
+#include <aul_sock.h>
+#include <gtest/gtest.h>
+#include <stdlib.h>
+
+#include <bundle_cpp.h>
+
+#include <iostream>
+#include <memory>
+
+#include "mock/mock_hook.h"
+#include "mock/socket_mock.h"
+#include "mock/test_fixture.h"
+#include "src/launch.h"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::Invoke;
+using ::testing::SaveArg;
+
+namespace {
+
+constexpr const char kJobId[] = "TestJob";
+
+class Mocks : virtual public ::testing::NiceMock<SocketMock> {};
+
+}  // namespace
+
+class JobSchedulerTest : public TestFixture {
+ public:
+  JobSchedulerTest() : TestFixture(std::make_unique<::Mocks>()) {}
+
+  virtual void SetUp() {
+    if (aul_is_initialized())
+      return;
+
+    aul_launch_init(nullptr, nullptr);
+  }
+
+  virtual void TearDown() {
+    aul_launch_fini();
+  }
+};
+
+TEST_F(JobSchedulerTest, aul_job_scheduler_update_job_status_P) {
+  int cmd = -1;
+  int opt = -1;
+  bundle* kb = nullptr;
+
+  EXPECT_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+      .Times(1)
+      .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags)
+          -> ssize_t {
+        const app_pkt_t* pkt = reinterpret_cast<const app_pkt_t*>(buf);
+        cmd = pkt->cmd;
+        opt = pkt->opt;
+        kb = bundle_decode(pkt->data, pkt->len);
+        return n;
+      }));
+
+  int ret = aul_job_scheduler_update_job_status(kJobId, JOB_STATUS_START);
+  EXPECT_NE(kb, nullptr);
+  tizen_base::Bundle b(kb, false, true);
+
+  EXPECT_EQ(ret, AUL_R_OK);
+  EXPECT_EQ(cmd, JOB_STATUS_UPDATE);
+  EXPECT_EQ(opt, (AUL_SOCK_NOREPLY | AUL_SOCK_BUNDLE));
+  EXPECT_EQ(b.GetString(AUL_K_JOB_ID), kJobId);
+  EXPECT_EQ(b.GetString(AUL_K_JOB_STATUS), std::to_string(JOB_STATUS_START));
+}
+
+TEST_F(JobSchedulerTest, aul_job_scheduler_update_job_status_N) {
+  int ret = aul_job_scheduler_update_job_status(kJobId,
+      static_cast<aul_job_status_e>(JOB_STATUS_FINISHED + 1));
+  EXPECT_EQ(ret, AUL_R_EINVAL);
+
+  ret = aul_job_scheduler_update_job_status(nullptr, JOB_STATUS_STOPPED);
+  EXPECT_EQ(ret, AUL_R_EINVAL);
+}