Blame view

test/TopicLengthTest.cpp 3.07 KB
68753efb   Peter M. Groen   [FIX] Setting up ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /****************************************************************************
   * COpyright (c) 2023 Open Systems Development B.V.
   ****************************************************************************/
  
  #include <gmock/gmock.h>
  #include <gtest/gtest.h>
  #include <string>
  #include <memory>
  
  #include "mqttclient.h"
  
  using namespace osdev::components::mqtt;
  using namespace osdev::components::log;
  
  /****************************************************************************
   *                      H E L P E R   C L A S S E S
   ****************************************************************************/
37906d07   Peter M. Groen   Added Topic Lengt...
18
  class Publisher
68753efb   Peter M. Groen   [FIX] Setting up ...
19
  {
37906d07   Peter M. Groen   Added Topic Lengt...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  public:
      Publisher() : m_mqtt_client("TopicTester"){}
      virtual ~Publisher() {}
  
      void    connect(const std::string &hostname,
                   int portnumber = 1883,
                   const std::string &username = std::string(),
                   const std::string &password = std::string(),
                   const std::string &lwt_topic = std::string(),
                   const std::string &lwt_message = std::string()
                   )
      {
          m_mqtt_client.connect(hostname, portnumber,
                                osdev::components::mqtt::Credentials(username, password),
                                osdev::components::mqtt::mqtt_LWT(lwt_topic, lwt_message),
                                true,
                                osdev::components::log::LogSettings
                                {
                                    osdev::components::log::LogLevel::Debug,
                                    osdev::components::log::LogMask::None
                                });
      }
68753efb   Peter M. Groen   [FIX] Setting up ...
42
  
37906d07   Peter M. Groen   Added Topic Lengt...
43
44
45
46
47
      void    publish(const std::string &message_topic, const std::string &message_payload)
      {
          osdev::components::mqtt::MqttMessage message(message_topic, true, false, message_payload);
          osdev::components::mqtt::Token t_result = m_mqtt_client.publish(message, 0);
      }
68753efb   Peter M. Groen   [FIX] Setting up ...
48
  
37906d07   Peter M. Groen   Added Topic Lengt...
49
50
  private:
      osdev::components::mqtt::MqttClient     m_mqtt_client;
68753efb   Peter M. Groen   [FIX] Setting up ...
51
52
  };
  
37906d07   Peter M. Groen   Added Topic Lengt...
53
54
  /// @brief  class to generate a cumulative topic..
  class TopicTester
68753efb   Peter M. Groen   [FIX] Setting up ...
55
56
  {
      public:
37906d07   Peter M. Groen   Added Topic Lengt...
57
58
          TopicTester(std::shared_ptr<Publisher> publisher) : m_publisher(publisher){}
          virtual ~TopicTester(){}
68753efb   Peter M. Groen   [FIX] Setting up ...
59
  
37906d07   Peter M. Groen   Added Topic Lengt...
60
61
62
63
64
65
66
          void    RunTopicTester(int max_number_of_chars)
          {
              for(int nCount = 1; nCount < max_number_of_chars; nCount++)
              {
                  std::string subtopic(nCount, 'a');
                  std::string topic = std::string("topics/" + subtopic);
                  std::string message(std::to_string(topic.size()));
68753efb   Peter M. Groen   [FIX] Setting up ...
67
  
37906d07   Peter M. Groen   Added Topic Lengt...
68
69
70
                  m_publisher->publish(topic, message);
              }
          }
68753efb   Peter M. Groen   [FIX] Setting up ...
71
72
  
      private:
37906d07   Peter M. Groen   Added Topic Lengt...
73
          std::shared_ptr<Publisher>  m_publisher;
68753efb   Peter M. Groen   [FIX] Setting up ...
74
  };
37906d07   Peter M. Groen   Added Topic Lengt...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  
  /*****************************************************************************
   *                      T H E   A C T U A L   T E S T S
   *****************************************************************************/
  /// TopicTester
  TEST(topictest, TopicLengthTest)
  {
      std::shared_ptr<Publisher> pPublisher = std::make_shared<Publisher>();
      pPublisher->connect("127.0.0.1", 1883);
  
      TopicTester oTester(pPublisher);
  
      oTester.RunTopicTester(101);
  }