alert_config_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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: true},
  85. {value: '23', expected: true},
  86. {value: '123.8', expected: true},
  87. {value: '5.8', expected: true},
  88. {value: 123, expected: true},
  89. {value: 23, expected: true},
  90. {value: 123.8, expected: true},
  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('#badgeCssClass', function () {
  121. it ('should be based on badge', function () {
  122. model.set('badge', 'OK');
  123. expect(model.get('badgeCssClass')).to.equal('alert-state-OK');
  124. });
  125. });
  126. describe('#wasChanged', function () {
  127. Em.A([
  128. {
  129. p: {
  130. previousValue: null,
  131. previousText: null,
  132. value: '',
  133. text: ''
  134. },
  135. e: false
  136. },
  137. {
  138. p: {
  139. previousValue: 'not null',
  140. previousText: null,
  141. value: '',
  142. text: ''
  143. },
  144. e: true
  145. },
  146. {
  147. p: {
  148. previousValue: null,
  149. previousText: 'not null',
  150. value: '',
  151. text: ''
  152. },
  153. e: true
  154. },
  155. {
  156. p: {
  157. previousValue: 'not null',
  158. previousText: 'not null',
  159. value: '',
  160. text: ''
  161. },
  162. e: true
  163. }
  164. ]).forEach(function (test, i) {
  165. it('test #' + (i + 1), function () {
  166. model.setProperties(test.p);
  167. expect(model.get('wasChanged')).to.equal(test.e);
  168. });
  169. });
  170. });
  171. describe('#isValid', function () {
  172. it('should be true if showInputForValue is false', function () {
  173. model.set('showInputForValue', false);
  174. expect(model.get('isValid')).to.be.true;
  175. });
  176. it('should be false if displayValue is null', function () {
  177. model.set('displayValue', null);
  178. expect(model.get('isValid')).to.be.false;
  179. model.set('displayValue', undefined);
  180. expect(model.get('isValid')).to.be.false;
  181. });
  182. it('should be false if METRIC displayValue is not valid float', function () {
  183. model.set('displayValue', '$1234.444');
  184. expect(model.get('isValid')).to.be.false;
  185. model.set('displayValue', 'hello-world!');
  186. expect(model.get('isValid')).to.be.false;
  187. });
  188. it('should be true if METRIC displayValue is valid float with at most one decimal', function () {
  189. model.set('displayValue', '123.4');
  190. expect(model.get('isValid')).to.be.true;
  191. model.set('displayValue', '123.0');
  192. expect(model.get('isValid')).to.be.true;
  193. model.set('displayValue', '666');
  194. expect(model.get('isValid')).to.be.true;
  195. });
  196. it('should be false if METRIC displayValue is valid float with more than one decimal', function () {
  197. model.set('displayValue', '123.48');
  198. expect(model.get('isValid')).to.be.false;
  199. });
  200. it('should be true for AGGREGATE percentage with precision of 1', function () {
  201. model = Em.Object.create(App.AlertConfigProperties.Thresholds.PercentageMixin, {
  202. displayValue: '1',
  203. showInputForValue: true
  204. });
  205. expect(model.get('isValid')).to.be.true;
  206. model.set('displayValue', '88');
  207. expect(model.get('isValid')).to.be.true;
  208. });
  209. it('should be false for AGGREGATE percentage values with precision smaller than 1', function () {
  210. model = Em.Object.create(App.AlertConfigProperties.Thresholds.PercentageMixin, {
  211. displayValue: '70.01',
  212. showInputForValue: true
  213. });
  214. expect(model.get('isValid')).to.be.false;
  215. model.set('displayValue', '70.0');
  216. expect(model.get('isValid')).to.be.false;
  217. model.set('displayValue', '80.000');
  218. expect(model.get('isValid')).to.be.false;
  219. });
  220. it('should be true for PORT percentage values with precision of 1/10th', function () {
  221. model = App.AlertConfigProperties.Threshold.create({
  222. value: '0.4',
  223. showInputForValue: true
  224. })
  225. expect(model.get('isValid')).to.be.true;
  226. model.set('value', '3');
  227. expect(model.get('isValid')).to.be.true;
  228. model.set('value', '33.0');
  229. expect(model.get('isValid')).to.be.true;
  230. });
  231. it('should be false for PORT percentage values with precision greater than 1/10th', function() {
  232. model = App.AlertConfigProperties.Threshold.create({
  233. value: '4.234',
  234. showInputForValue: true
  235. });
  236. expect(model.get('isValid')).to.be.false;
  237. model.set('value', '44.001');
  238. expect(model.get('isValid')).to.be.false;
  239. model.set('value', '112.01');
  240. expect(model.get('isValid')).to.be.false;
  241. });
  242. });
  243. });
  244. describe('App.AlertConfigProperties.Thresholds', function () {
  245. describe('OkThreshold', function () {
  246. beforeEach(function () {
  247. model = App.AlertConfigProperties.Thresholds.OkThreshold.create();
  248. });
  249. describe('#apiProperty', function () {
  250. it('should be based on showInputForValue and showInputForText', function () {
  251. model.setProperties({
  252. showInputForValue: false,
  253. showInputForText: false
  254. });
  255. expect(model.get('apiProperty')).to.eql([]);
  256. model.set('showInputForValue', true);
  257. expect(model.get('apiProperty')).to.eql(['source.reporting.ok.value']);
  258. model.set('showInputForText', true);
  259. expect(model.get('apiProperty')).to.eql(['source.reporting.ok.value', 'source.reporting.ok.text']);
  260. });
  261. });
  262. });
  263. describe('WarningThreshold', function () {
  264. beforeEach(function () {
  265. model = App.AlertConfigProperties.Thresholds.WarningThreshold.create();
  266. });
  267. describe('#apiProperty', function () {
  268. it('should be based on showInputForValue and showInputForText', function () {
  269. model.setProperties({
  270. showInputForValue: false,
  271. showInputForText: false
  272. });
  273. expect(model.get('apiProperty')).to.eql([]);
  274. model.set('showInputForValue', true);
  275. expect(model.get('apiProperty')).to.eql(['source.reporting.warning.value']);
  276. model.set('showInputForText', true);
  277. expect(model.get('apiProperty')).to.eql(['source.reporting.warning.value', 'source.reporting.warning.text']);
  278. });
  279. });
  280. });
  281. describe('CriticalThreshold', function () {
  282. beforeEach(function () {
  283. model = App.AlertConfigProperties.Thresholds.CriticalThreshold.create();
  284. });
  285. describe('#apiProperty', function () {
  286. it('should be based on showInputForValue and showInputForText', function () {
  287. model.setProperties({
  288. showInputForValue: false,
  289. showInputForText: false
  290. });
  291. expect(model.get('apiProperty')).to.eql([]);
  292. model.set('showInputForValue', true);
  293. expect(model.get('apiProperty')).to.eql(['source.reporting.critical.value']);
  294. model.set('showInputForText', true);
  295. expect(model.get('apiProperty')).to.eql(['source.reporting.critical.value', 'source.reporting.critical.text']);
  296. });
  297. });
  298. });
  299. });
  300. });