NdmcClientTest.cpp
Go to the documentation of this file.
1 #include "NovaDAQMonitorClient/NdmcMetric.h"
2 #include "NovaDAQMonitorClient/NdmcClient.h"
3 #include "NovaDAQMonitorClient/NdmcClientService.h"
4 
5 #include "NdmcClientTest.h"
6 using namespace NDMC_NAMESPACE;
7 
8 /**
9  * Register the NdmcClientTest suite in the registry of suites in a "folder"
10  * of name "NovaDAQMonitorClient". Store the folder in the default registry path.
11  * $Id: NdmcClientTest.cpp,v 1.13 2014/02/03 02:21:31 kasahara Exp $
12  */
13 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NdmcClientTest, "NovaDAQMonitorClient" );
14 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaDAQMonitorClient");
15 
16 /**
17  * A public method to setUp the environment for the test. This will be invoked before
18  * every test in the fixture to get a fresh start.
19  */
21 
22  _configFile = getConfigFullFilePathName("unittest/gmond.conf");
24 
25 }
26 /**
27  * A public method to tearDown the environment for the test. This will be invoked after
28  * every test in the fixture to clean up allocated memory.
29  */
31  std::cout.flush();
32 }
33 
34 /**
35  * A test method to test that NdmcClient ctor works correctly.
36  */
38 
39  int32_t partition = 1;
40  std::string appName = "dcmapp";
41  std::string groupName = "dcmapplication";
42  uint32_t sendTimeInterval = 10;
43  NdmcClient client(partition,appName,groupName,sendTimeInterval);
44 
45  CPPUNIT_ASSERT(client.getPartition() == partition);
46  CPPUNIT_ASSERT(client.getApplicationName() == appName);
47  CPPUNIT_ASSERT(client.getGroupName() == "dcmapplication P1");
48  CPPUNIT_ASSERT(client.getSendTimeInterval() == sendTimeInterval);
49  CPPUNIT_ASSERT(client.getNumMetric() == 0);
50 
51 }
52 
53 /**
54  * A test method to test that NdmcClient setSendTimeInterval works correctly.
55  */
57 
58  int32_t partition = 0;
59  std::string appName = "";
60  std::string groupName = "dcmapp";
61  uint32_t sendTimeInterval = 10;
62  NdmcClient client(partition,appName,groupName,sendTimeInterval);
63  CPPUNIT_ASSERT(client.getSendTimeInterval() == sendTimeInterval);
64  sendTimeInterval = 5;
65  client.setSendTimeInterval(sendTimeInterval);
66  CPPUNIT_ASSERT(client.getSendTimeInterval() == sendTimeInterval);
67 
68 }
69 
70 /**
71  * A test method to test that NdmcClient setPartition works correctly.
72  */
74 
75  int32_t partition = 1;
76  NdmcClient client(partition);
77  CPPUNIT_ASSERT(client.getPartition() == partition);
78  partition = 3;
79  client.setPartition(partition);
80  CPPUNIT_ASSERT(client.getPartition() == partition);
81 
82 }
83 
84 /**
85  * A test method to test that NdmcClient setApplicationName works correctly.
86  */
88 
89  int32_t partition = 0;
90  std::string appName = "dcmapp";
91  NdmcClient client(partition, appName);
92  CPPUNIT_ASSERT(client.getApplicationName() == appName);
93 
94  appName = "";
95  client.setApplicationName(appName);
96  CPPUNIT_ASSERT(client.getApplicationName() == appName);
97 
98 }
99 
100 /**
101  * A test method to test that NdmcClient setGroupName works correctly.
102  */
104 
105  int32_t partition = 0;
106  std::string appName = "dcmapp";
107  NdmcClient client(partition,appName);
108  CPPUNIT_ASSERT(client.getGroupName() == "dcmapp P0");
109 
110  partition = -1;
111  client.setPartition(partition);
112  CPPUNIT_ASSERT(client.getGroupName() == "dcmapp");
113 
114  appName = "";
115  client.setApplicationName(appName);
116  CPPUNIT_ASSERT(client.getGroupName() == "");
117 
118  partition = 2;
119  client.setPartition(partition);
120  CPPUNIT_ASSERT(client.getGroupName() == "P2");
121 
122  std::string groupName = "GT";
123  client.setGroupName(groupName);
124  CPPUNIT_ASSERT(client.getGroupName() == "GT P2");
125 
126  groupName = "";
127  client.setGroupName(groupName);
128  CPPUNIT_ASSERT(client.getGroupName() == "P2");
129 
130 }
131 
132 /**
133  * A test method to test that NdmcClient will adopt a metric correctly.
134  */
136 
137  int32_t partition = 0;
138  std::string appName = "dcmapp";
139  std::string groupName = "";
140  uint32_t sendTimeInterval = 10;
141  NdmcClient client(partition,appName,groupName,sendTimeInterval);
142 
143  CPPUNIT_ASSERT(client.getNumMetric()==0);
144 
145  std::string name1 = "If you thought science was certain";
146  CPPUNIT_ASSERT(client._adoptMetric(new NdmcMetric<uint32_t>(name1))==0);
147  CPPUNIT_ASSERT(client.getNumMetric() == 1);
148 
149  std::string name2 = "well that's just";
150  CPPUNIT_ASSERT(client._adoptMetric(new NdmcMetric<double>(name2))==1);
151  CPPUNIT_ASSERT(client.getNumMetric() == 2);
152 
153  std::string name3 = "an error on your part";
154  CPPUNIT_ASSERT(client._adoptMetric(new NdmcMetric<int32_t>(name3))==2);
155  CPPUNIT_ASSERT(client.getNumMetric() == 3);
156 
157  std::string name4 = "an error on your part";
158  CPPUNIT_ASSERT(client._adoptMetric(new NdmcMetric<int32_t>(name4))==-1);
159  CPPUNIT_ASSERT(client.getNumMetric() == 3);
160 
161 }
162 
163 /**
164  * A test method to test that NdmcClient can retrieve a metric correctly by metricId
165  */
167 
168  int32_t partition = 0;
169  std::string appName = "dcmapp";
170  std::string groupName = "";
171  uint32_t sendTimeInterval = 10;
172  NdmcClient client(partition,appName,groupName,sendTimeInterval);
173 
174  CPPUNIT_ASSERT(client.getNumMetric()==0);
175 
176  std::string name1 = "I,";
177  CPPUNIT_ASSERT(client._adoptMetric(new NdmcMetric<uint32_t>(name1))==0);
178  CPPUNIT_ASSERT(client.getNumMetric() == 1);
179 
180  std::string name2 = "a universe of atoms,";
181  CPPUNIT_ASSERT(client._adoptMetric(new NdmcMetric<double>(name2))==1);
182  CPPUNIT_ASSERT(client.getNumMetric() == 2);
183 
184  std::string name3 = "an atom in the universe";
185  CPPUNIT_ASSERT(client._adoptMetric(new NdmcMetric<int32_t>(name3))==2);
186  CPPUNIT_ASSERT(client.getNumMetric() == 3);
187 
188  NdmcMetricABC* metric = NULL;
189  metric = client._getMetric(2);
190  CPPUNIT_ASSERT(metric);
191  CPPUNIT_ASSERT(metric->getName() == name3);
192  CPPUNIT_ASSERT(client.getNumMetric() == 3);
193 ;
194  metric = client._getMetric(0);
195  CPPUNIT_ASSERT(metric);
196  CPPUNIT_ASSERT(metric->getName() == name1);
197  CPPUNIT_ASSERT(client.getNumMetric() == 3);
198 
199  metric = client._getMetric(1);
200  CPPUNIT_ASSERT(metric);
201  CPPUNIT_ASSERT(metric->getName() == name2);
202  CPPUNIT_ASSERT(client.getNumMetric() == 3);
203 
204  metric = client._getMetric(3);
205  CPPUNIT_ASSERT(!metric);
206  CPPUNIT_ASSERT(client.getNumMetric() == 3);
207 
208 }
209 
210 /**
211  * A test method to test that NdmcClient will create a metric correctly.
212  */
214 
215  int32_t partition = 0;
216  std::string appName = "dcmapp";
217  std::string groupName = "";
218  uint32_t sendTimeInterval = 10;
219  NdmcClient client(partition,appName,groupName,sendTimeInterval);
220 
221  CPPUNIT_ASSERT(client.getNumMetric()==0);
222 
223  std::string name1 = "If you thought science was certain";
224  CPPUNIT_ASSERT(client.createMetric<uint32_t>(name1,"your",VALUE)==0);
225  CPPUNIT_ASSERT(client.getNumMetric() == 1);
226 
227  std::string name2 = "well that's just";
228  CPPUNIT_ASSERT(client.createMetric<double>(name2,"tax dollars",COUNTER)==1);
229  CPPUNIT_ASSERT(client.getNumMetric() == 2);
230 
231  std::string name3 = "an error on your part";
232  CPPUNIT_ASSERT(client.createMetric<int32_t>(name3,"at work",RATE)==2);
233  CPPUNIT_ASSERT(client.getNumMetric() == 3);
234 
235  std::string name4 = "an error on your part";
236  CPPUNIT_ASSERT(client.createMetric<int32_t>(name4,"for you",AVERAGE)==-1);
237  CPPUNIT_ASSERT(client.getNumMetric() == 3);
238 
239 }
240 
241 /**
242  * A test method to test that NdmcClient will create a metric correctly.
243  */
245 
246  int32_t partition = 0;
247  std::string appName = "dcmapp";
248  std::string groupName = "";
249  uint32_t sendTimeInterval = 10;
250  NdmcClient client(partition,appName,groupName,sendTimeInterval);
251 
252  CPPUNIT_ASSERT(client.getNumMetric()==0);
253 
254  std::string name1 = "If you thought science was certain";
255  CPPUNIT_ASSERT(client.createMetric<uint32_t>(name1,"your",VALUE)==0);
256  CPPUNIT_ASSERT(client.getNumMetric() == 1);
258  metric = dynamic_cast<NdmcMetric<uint32_t>*>(client._getMetric(0));
259  CPPUNIT_ASSERT(metric);
260  CPPUNIT_ASSERT(metric->getName() == name1);
261  uint32_t lastValueSent=0;
262  metric->_lastValueSent = 10;
263  metric->_valueHasBeenSent = 1;
264  CPPUNIT_ASSERT(client.getLastValueSent(0,lastValueSent)==true);
265  CPPUNIT_ASSERT(lastValueSent==10);
266 
267  std::string name2 = "well that's just";
268  CPPUNIT_ASSERT(client.createMetric<double>(name2,"tax dollars",COUNTER)==1);
269  CPPUNIT_ASSERT(client.getNumMetric() == 2);
270  NdmcMetric<double>* metricd = dynamic_cast<NdmcMetric<double>*>(client._getMetric(1));
271  CPPUNIT_ASSERT(metricd);
272  CPPUNIT_ASSERT(metricd->getName() == name2);
273  metricd->_lastValueSent = 20.5;
274  metricd->_valueHasBeenSent = 1;
275  double lastValueSentDouble;
276  CPPUNIT_ASSERT(client.getLastValueSent(1,lastValueSentDouble)==true);
277  CPPUNIT_ASSERT(lastValueSentDouble==20.5);
278 
279 }
280 
281 /**
282  * A test method to test that NdmcClient::destroyMetric will destroy a metric correctly.
283  */
285 
286  int32_t partition = 0;
287  std::string appName = "dcmapp";
288  std::string groupName = "";
289  uint32_t sendTimeInterval = 10;
290  NdmcClient client(partition,appName,groupName,sendTimeInterval);
291 
292  CPPUNIT_ASSERT(client.getNumMetric()==0);
293 
294  std::string name1 = "If you thought science was certain";
295  CPPUNIT_ASSERT(client.createMetric<uint32_t>(name1,"your",VALUE)==0);
296  CPPUNIT_ASSERT(client.getNumMetric() == 1);
297  client.destroyMetric(0);
298  CPPUNIT_ASSERT(client.getNumMetric() == 0);
299 
300  std::string name2 = "well that's just";
301  CPPUNIT_ASSERT(client.createMetric<double>(name2,"tax dollars",COUNTER)==0);
302  CPPUNIT_ASSERT(client.getNumMetric() == 1);
303 
304  std::string name3 = "an error on your part";
305  CPPUNIT_ASSERT(client.createMetric<int32_t>(name3,"at work",RATE)==1);
306  CPPUNIT_ASSERT(client.getNumMetric() == 2);
307 
308  client.destroyMetric(2);
309  CPPUNIT_ASSERT(client.getNumMetric() == 2);
310  client.destroyMetric(-1);
311  CPPUNIT_ASSERT(client.getNumMetric() == 2);
312 
313  client.destroyMetric(0);
314  CPPUNIT_ASSERT(client.getNumMetric() == 1);
315  CPPUNIT_ASSERT(client._getMetric(1));
316 
317  CPPUNIT_ASSERT(client.createMetric<double>(name2,"tax dollars",COUNTER) == 2);
318  CPPUNIT_ASSERT(client.getNumMetric() == 2);
319  CPPUNIT_ASSERT(!client._getMetric(0));
320  CPPUNIT_ASSERT(client._getMetric(1));
321  CPPUNIT_ASSERT(client._getMetric(2));
322  CPPUNIT_ASSERT(!client._getMetric(3));
323 
324 
325 }
NdmcMetricABC * _getMetric(int32_t metricId) const
Definition: NdmcClient.cpp:232
int32_t _adoptMetric(NdmcMetricABC *metric)
Definition: NdmcClient.cpp:167
void setSendTimeInterval(uint32_t sendTimeInterval)
Definition: NdmcClient.cpp:155
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(NdmcClientTest,"NovaDAQMonitorClient")
void setGroupName(std::string groupName)
Definition: NdmcClient.cpp:85
Definition: Ndmc.h:37
static void setCONFIGFILE(std::string configfile)
Definition: Ndmc.h:38
int32_t createMetric(std::string metricName, std::string units, MetricType_t metricType=VALUE, std::string title="", std::string description="")
Definition: NdmcClient.h:110
T _lastValueSent
Definition: NdmcMetric.h:103
Definition: Ndmc.h:36
NDMC_NAMESPACE_BEGIN std::string getConfigFullFilePathName(std::string configFile)
void destroyMetric(int32_t metricID)
Definition: NdmcClient.cpp:193
uint32_t getNumMetric()
Definition: NdmcClient.cpp:245
void testSetSendTimeInterval()
uint32_t getSendTimeInterval() const
Definition: NdmcClient.h:51
OStream cout
Definition: OStream.cxx:6
bool getLastValueSent(int32_t metricId, T &lastValueSent)
Definition: NdmcClient.h:214
void setApplicationName(std::string appName)
Definition: NdmcClient.cpp:73
void testSetApplicationName()
bool setPartition(int32_t partition)
Definition: NdmcClient.cpp:60
int32_t getPartition() const
Definition: NdmcClient.h:48
std::string getApplicationName() const
Definition: NdmcClient.h:49
std::string getGroupName() const
Definition: NdmcClient.h:50
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaDAQMonitorClient")
void testGetLastValueSent()
Definition: Ndmc.h:35
enum BeamMode string