alert_config_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. require('models/alerts/alert_config');
  20. var model;
  21. describe('App.AlertConfigProperties', function () {
  22. describe('Parameter', function () {
  23. function getModel() {
  24. return App.AlertConfigProperties.Parameter.create();
  25. }
  26. App.TestAliases.testAsComputedAlias(getModel(), 'badge', 'threshold');
  27. });
  28. describe('App.AlertConfigProperties.Parameters', function () {
  29. describe('StringMixin', function () {
  30. var obj;
  31. beforeEach(function () {
  32. obj = App.AlertConfigProperties.Parameter.create(App.AlertConfigProperties.Parameters.StringMixin, {});
  33. });
  34. describe('#isValid', function () {
  35. Em.A([
  36. {value: '', expected: false},
  37. {value: '\t', expected: false},
  38. {value: ' ', expected: false},
  39. {value: '\n', expected: false},
  40. {value: '\r', expected: false},
  41. {value: 'some not empty string', expected: true}
  42. ]).forEach(function (test) {
  43. it('value: ' + JSON.stringify(test.value) + ' ;result - ' + test.expected, function () {
  44. obj.set('value', test.value);
  45. expect(obj.get('isValid')).to.be.equal(test.expected);
  46. });
  47. });
  48. });
  49. });
  50. describe('NumericMixin', function () {
  51. var obj;
  52. beforeEach(function () {
  53. obj = App.AlertConfigProperties.Parameter.create(App.AlertConfigProperties.Parameters.NumericMixin, {});
  54. });
  55. describe('#isValid', function () {
  56. Em.A([
  57. {value: '', expected: false},
  58. {value: 'abc', expected: false},
  59. {value: 'g1', expected: false},
  60. {value: '1g', expected: false},
  61. {value: '123', expected: true},
  62. {value: '123.8', expected: true},
  63. {value: 123, expected: true},
  64. {value: 123.8, expected: true},
  65. ]).forEach(function (test) {
  66. it('value: ' + JSON.stringify(test.value) + ' ;result - ' + test.expected, function () {
  67. obj.set('value', test.value);
  68. expect(obj.get('isValid')).to.be.equal(test.expected);
  69. });
  70. });
  71. });
  72. });
  73. describe('PercentageMixin', function () {
  74. var obj;
  75. beforeEach(function () {
  76. obj = App.AlertConfigProperties.Parameter.create(App.AlertConfigProperties.Parameters.PercentageMixin, {});
  77. });
  78. describe('#isValid', function () {
  79. Em.A([
  80. {value: '', expected: false},
  81. {value: 'abc', expected: false},
  82. {value: 'g1', expected: false},
  83. {value: '1g', expected: false},
  84. {value: '123', expected: false},
  85. {value: '23', expected: true},
  86. {value: '123.8', expected: false},
  87. {value: '5.8', expected: true},
  88. {value: 123, expected: false},
  89. {value: 23, expected: true},
  90. {value: 123.8, expected: false},
  91. {value: 5.8, expected: true}
  92. ]).forEach(function (test) {
  93. it('value: ' + JSON.stringify(test.value) + ' ;result - ' + test.expected, function () {
  94. obj.set('value', test.value);
  95. expect(obj.get('isValid')).to.be.equal(test.expected);
  96. });
  97. });
  98. });
  99. });
  100. });
  101. describe('Threshold', function () {
  102. beforeEach(function () {
  103. model = App.AlertConfigProperties.Threshold.create({});
  104. });
  105. describe('#apiFormattedValue', function () {
  106. it('should be based on showInputForValue and showInputForText', function () {
  107. model.setProperties({
  108. value: 'value',
  109. text: 'text',
  110. showInputForValue: false,
  111. showInputForText: false
  112. });
  113. expect(model.get('apiFormattedValue')).to.eql([]);
  114. model.set('showInputForValue', true);
  115. expect(model.get('apiFormattedValue')).to.eql(['value']);
  116. model.set('showInputForText', true);
  117. expect(model.get('apiFormattedValue')).to.eql(['value', 'text']);
  118. });
  119. });
  120. describe('#valueWasChanged', function () {
  121. it('value change should effect displayValue for AGGREGATE type', function () {
  122. model = App.AlertConfigProperties.Threshold.create(App.AlertConfigProperties.Thresholds.PercentageMixin, {
  123. value: '0.4',
  124. valueMetric: '%',
  125. text: 'text',
  126. showInputForValue: false,
  127. showInputForText: false
  128. });
  129. expect(model.get('displayValue')).to.be.equal('40');
  130. });
  131. it('value change should not effect displayValue for not AGGREGATE type', function () {
  132. model = App.AlertConfigProperties.Threshold.create({
  133. value: '0.4',
  134. valueMetric: '%',
  135. text: 'text',
  136. showInputForValue: false,
  137. showInputForText: false
  138. });
  139. expect(model.get('displayValue')).to.be.equal('0.4');
  140. });
  141. });
  142. describe('#badgeCssClass', function () {
  143. it ('should be based on badge', function () {
  144. model.set('badge', 'OK');
  145. expect(model.get('badgeCssClass')).to.equal('alert-state-OK');
  146. });
  147. });
  148. describe('#wasChanged', function () {
  149. Em.A([
  150. {
  151. p: {
  152. previousValue: null,
  153. previousText: null,
  154. value: '',
  155. text: ''
  156. },
  157. e: false
  158. },
  159. {
  160. p: {
  161. previousValue: 'not null',
  162. previousText: null,
  163. value: '',
  164. text: ''
  165. },
  166. e: true
  167. },
  168. {
  169. p: {
  170. previousValue: null,
  171. previousText: 'not null',
  172. value: '',
  173. text: ''
  174. },
  175. e: true
  176. },
  177. {
  178. p: {
  179. previousValue: 'not null',
  180. previousText: 'not null',
  181. value: '',
  182. text: ''
  183. },
  184. e: true
  185. }
  186. ]).forEach(function (test, i) {
  187. it('test #' + (i + 1), function () {
  188. model.setProperties(test.p);
  189. expect(model.get('wasChanged')).to.equal(test.e);
  190. });
  191. });
  192. });
  193. describe('#isValid', function () {
  194. it('should be true if showInputForValue is false', function () {
  195. model.set('showInputForValue', false);
  196. expect(model.get('isValid')).to.be.true;
  197. });
  198. it('should be false if displayValue is null', function () {
  199. model.set('displayValue', null);
  200. expect(model.get('isValid')).to.be.false;
  201. model.set('displayValue', undefined);
  202. expect(model.get('isValid')).to.be.false;
  203. });
  204. it('should be false if METRIC displayValue is not valid float', function () {
  205. model.set('displayValue', '$1234.444');
  206. expect(model.get('isValid')).to.be.false;
  207. model.set('displayValue', 'hello-world!');
  208. expect(model.get('isValid')).to.be.false;
  209. });
  210. it('should be true if METRIC displayValue is valid float with at most one decimal', function () {
  211. model.set('displayValue', '123.4');
  212. expect(model.get('isValid')).to.be.true;
  213. model.set('displayValue', '123.0');
  214. expect(model.get('isValid')).to.be.true;
  215. model.set('displayValue', '666');
  216. expect(model.get('isValid')).to.be.true;
  217. });
  218. it('should be false if METRIC displayValue is valid float with more than one decimal', function () {
  219. model.set('displayValue', '123.48');
  220. expect(model.get('isValid')).to.be.false;
  221. });
  222. it('should be true for AGGREGATE percentage with precision of 1', function () {
  223. model = Em.Object.create(App.AlertConfigProperties.Thresholds.PercentageMixin, {
  224. displayValue: '1',
  225. showInputForValue: true
  226. });
  227. expect(model.get('isValid')).to.be.true;
  228. model.set('displayValue', '88');
  229. expect(model.get('isValid')).to.be.true;
  230. });
  231. it('should be false for AGGREGATE percentage values with precision smaller than 1', function () {
  232. model = Em.Object.create(App.AlertConfigProperties.Thresholds.PercentageMixin, {
  233. displayValue: '70.01',
  234. showInputForValue: true
  235. });
  236. expect(model.get('isValid')).to.be.false;
  237. model.set('displayValue', '70.0');
  238. expect(model.get('isValid')).to.be.false;
  239. model.set('displayValue', '80.000');
  240. expect(model.get('isValid')).to.be.false;
  241. });
  242. it('should be true for PORT percentage values with precision of 1/10th', function () {
  243. model = App.AlertConfigProperties.Threshold.create({
  244. value: '0.4',
  245. showInputForValue: true
  246. })
  247. expect(model.get('isValid')).to.be.true;
  248. model.set('value', '3');
  249. expect(model.get('isValid')).to.be.true;
  250. model.set('value', '33.0');
  251. expect(model.get('isValid')).to.be.true;
  252. });
  253. it('should be false for PORT percentage values with precision greater than 1/10th', function() {
  254. model = App.AlertConfigProperties.Threshold.create({
  255. value: '4.234',
  256. showInputForValue: true
  257. });
  258. expect(model.get('isValid')).to.be.false;
  259. model.set('value', '44.001');
  260. expect(model.get('isValid')).to.be.false;
  261. model.set('value', '112.01');
  262. expect(model.get('isValid')).to.be.false;
  263. });
  264. });
  265. });
  266. describe('App.AlertConfigProperties.Thresholds', function () {
  267. describe('OkThreshold', function () {
  268. beforeEach(function () {
  269. model = App.AlertConfigProperties.Thresholds.OkThreshold.create();
  270. });
  271. describe('#apiProperty', function () {
  272. it('should be based on showInputForValue and showInputForText', function () {
  273. model.setProperties({
  274. showInputForValue: false,
  275. showInputForText: false
  276. });
  277. expect(model.get('apiProperty')).to.eql([]);
  278. model.set('showInputForValue', true);
  279. expect(model.get('apiProperty')).to.eql(['source.reporting.ok.value']);
  280. model.set('showInputForText', true);
  281. expect(model.get('apiProperty')).to.eql(['source.reporting.ok.value', 'source.reporting.ok.text']);
  282. });
  283. });
  284. });
  285. describe('WarningThreshold', function () {
  286. beforeEach(function () {
  287. model = App.AlertConfigProperties.Thresholds.WarningThreshold.create();
  288. });
  289. describe('#apiProperty', function () {
  290. it('should be based on showInputForValue and showInputForText', function () {
  291. model.setProperties({
  292. showInputForValue: false,
  293. showInputForText: false
  294. });
  295. expect(model.get('apiProperty')).to.eql([]);
  296. model.set('showInputForValue', true);
  297. expect(model.get('apiProperty')).to.eql(['source.reporting.warning.value']);
  298. model.set('showInputForText', true);
  299. expect(model.get('apiProperty')).to.eql(['source.reporting.warning.value', 'source.reporting.warning.text']);
  300. });
  301. });
  302. });
  303. describe('CriticalThreshold', function () {
  304. beforeEach(function () {
  305. model = App.AlertConfigProperties.Thresholds.CriticalThreshold.create();
  306. });
  307. describe('#apiProperty', function () {
  308. it('should be based on showInputForValue and showInputForText', function () {
  309. model.setProperties({
  310. showInputForValue: false,
  311. showInputForText: false
  312. });
  313. expect(model.get('apiProperty')).to.eql([]);
  314. model.set('showInputForValue', true);
  315. expect(model.get('apiProperty')).to.eql(['source.reporting.critical.value']);
  316. model.set('showInputForText', true);
  317. expect(model.get('apiProperty')).to.eql(['source.reporting.critical.value', 'source.reporting.critical.text']);
  318. });
  319. });
  320. });
  321. });
  322. });