Blame view

test/TopicLengthTest.cpp 3.16 KB
68753efb   Peter M. Groen   [FIX] Setting up ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  /****************************************************************************
   * 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;
  
cf0166d6   Peter M. Groen   Added extra topic...
15
16
  static const std::string main_topic = "test/";
  
68753efb   Peter M. Groen   [FIX] Setting up ...
17
18
19
  /****************************************************************************
   *                      H E L P E R   C L A S S E S
   ****************************************************************************/
37906d07   Peter M. Groen   Added Topic Lengt...
20
  class Publisher
68753efb   Peter M. Groen   [FIX] Setting up ...
21
  {
37906d07   Peter M. Groen   Added Topic Lengt...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  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 ...
44
  
37906d07   Peter M. Groen   Added Topic Lengt...
45
46
47
48
49
      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 ...
50
  
37906d07   Peter M. Groen   Added Topic Lengt...
51
52
  private:
      osdev::components::mqtt::MqttClient     m_mqtt_client;
68753efb   Peter M. Groen   [FIX] Setting up ...
53
54
  };
  
37906d07   Peter M. Groen   Added Topic Lengt...
55
56
  /// @brief  class to generate a cumulative topic..
  class TopicTester
68753efb   Peter M. Groen   [FIX] Setting up ...
57
58
  {
      public:
37906d07   Peter M. Groen   Added Topic Lengt...
59
60
          TopicTester(std::shared_ptr<Publisher> publisher) : m_publisher(publisher){}
          virtual ~TopicTester(){}
68753efb   Peter M. Groen   [FIX] Setting up ...
61
  
37906d07   Peter M. Groen   Added Topic Lengt...
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');
cf0166d6   Peter M. Groen   Added extra topic...
67
68
                  std::string topic = std::string(main_topic + subtopic);
                  std::string message(std::to_string(topic.size()) + " (" + std::to_string(subtopic.size()) + ")");
68753efb   Peter M. Groen   [FIX] Setting up ...
69
  
37906d07   Peter M. Groen   Added Topic Lengt...
70
71
72
                  m_publisher->publish(topic, message);
              }
          }
68753efb   Peter M. Groen   [FIX] Setting up ...
73
74
  
      private:
37906d07   Peter M. Groen   Added Topic Lengt...
75
          std::shared_ptr<Publisher>  m_publisher;
68753efb   Peter M. Groen   [FIX] Setting up ...
76
  };
37906d07   Peter M. Groen   Added Topic Lengt...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  
  /*****************************************************************************
   *                      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);
  }