Commit 5d7d9426333e9892ed997783fb5aa0221c06c302

Authored by Adhidarma Hadiwinoto
1 parent 3e149fe8c6
Exists in master

reverse-parser

Showing 3 changed files with 126 additions and 36 deletions Side-by-side Diff

... ... @@ -41,5 +41,8 @@
41 41 "xmlrpc": "~1.3.1",
42 42 "xmlserializer": "~0.3.3",
43 43 "xpath": "0.0.21"
  44 + },
  45 + "devDependencies": {
  46 + "should": "^11.2.0"
44 47 }
45 48 }
... ... @@ -0,0 +1,77 @@
  1 +"use strict";
  2 +
  3 +function parseMessage(msg) {
  4 + const splited = msg.split(',');
  5 +
  6 + return {
  7 + namapel: _getNamaPel(splited),
  8 + tarifdaya: _getTarifDaya(splited),
  9 + tarif: _getTarif(splited),
  10 + daya: _getDaya(splited),
  11 + jumlahkwh: _getJumlahKwh(splited)
  12 + }
  13 +}
  14 +
  15 +function _getNamaPel(splited) {
  16 + try {
  17 + return splited[2];
  18 + }
  19 + catch(e) {
  20 + console.trace('Exception on _getNamaPel:', e);
  21 + return;
  22 + }
  23 +}
  24 +
  25 +function _getTarifDaya(splited) {
  26 + try {
  27 + let value = splited[5];
  28 +
  29 + if (value.indexOf('VA') < 0) {
  30 + value += 'VA';
  31 + }
  32 +
  33 + return value;
  34 + }
  35 + catch(e) {
  36 + console.trace('Exception on _getTarifDaya:', e);
  37 + return;
  38 + }
  39 +}
  40 +
  41 +function _getJumlahKwh(splited) {
  42 + try {
  43 + let value = splited[7];
  44 + value = value.replace(/^kWh\s*/, '');
  45 + return value;
  46 + }
  47 + catch(e) {
  48 + console.trace('Exception on _getJumlahKwh:', e);
  49 + return;
  50 + }
  51 +}
  52 +
  53 +function _getTarif(splited) {
  54 + const tarifdaya = _getTarifDaya(splited);
  55 +
  56 + try {
  57 + return tarifdaya.split('/')[0];
  58 + }
  59 + catch(e) {
  60 + console.trace('Exception on _getTarif:', e);
  61 + return;
  62 + }
  63 +}
  64 +
  65 +function _getDaya(splited) {
  66 + const tarifdaya = _getTarifDaya(splited);
  67 +
  68 + try {
  69 + return tarifdaya.split('/')[1];
  70 + }
  71 + catch(e) {
  72 + console.trace('Exception on _getDaya:', e);
  73 + return;
  74 + }
  75 +}
  76 +
  77 +exports.parseMessage = parseMessage;
1   -var assert = require("assert");
  1 +"use strict";
  2 +const should = require("should");
  3 +const reverseParser = require('./reverse-parser');
2 4  
  5 +describe('#reverse-parser', function() {
  6 + describe('#parseMessage', function() {
3 7  
4   -describe('aaa', function() {
5   - var aaa = require('sate24/aaa');
  8 + describe('test with valid message', function () {
  9 + const msg = 'IDPEL:2017-01-30 16:58:19,1BNS2550C539F5B40545,ERRY-FEBRIANTO,32024560198,32024560198,R1M/900,Rp ,kWh 24.40,Stroom/Token 4260-5779-4249-2925-9618,SUKSES';
  10 + const data = reverseParser.parseMessage(msg);
6 11  
7   - describe("#unaliasResponseCode()", function() {
8   - it('should return 68', function() {
9   - assert.equal('68', aaa.unaliasResponseCode('01', '01:68'));
10   - });
  12 + it('should to have correct nama pelanggan', function() {
  13 + data.namapel.should.equal('ERRY-FEBRIANTO');
  14 + })
11 15  
12   - it('should return 68', function() {
13   - assert.equal('68', aaa.unaliasResponseCode('68', '01:68'));
14   - });
  16 + it('should to have correct tarif daya', function() {
  17 + data.tarifdaya.should.equal('R1M/900VA');
  18 + })
15 19  
16   - it('should return 00', function() {
17   - assert.equal('00', aaa.unaliasResponseCode('00', '01:68'));
18   - });
  20 + it('should to have correct tarif', function() {
  21 + data.tarif.should.equal('R1M');
  22 + })
19 23  
20   - it('should return 40', function() {
21   - assert.equal('40', aaa.unaliasResponseCode('40', ''));
22   - });
  24 + it('should to have correct daya', function() {
  25 + data.daya.should.equal('900VA');
  26 + })
23 27  
24   - it('should return 40', function() {
25   - assert.equal('40', aaa.unaliasResponseCode('40', ''));
26   - });
  28 + it('should to have correct jumlah kwh', function() {
  29 + data.jumlahkwh.should.equal('24.40');
  30 + })
  31 + })
27 32  
28   - it('should return 40', function() {
29   - assert.equal('40', aaa.unaliasResponseCode('40'));
30   - });
  33 + describe('test with invalid message', function () {
  34 + const msg = '';
  35 + const data = reverseParser.parseMessage(msg);
31 36  
32   - });
33   -});
  37 + it('should to have correct nama pelanggan', function() {
  38 + should(data.namapel).not.be.ok();
  39 + })
34 40  
35   -describe('aaa', function() {
36   - var partner = require('./httppulsakita');
  41 + it('should to have correct tarif daya', function() {
  42 + should(data.tarifdaya).not.be.ok();
  43 + })
37 44  
38   - describe("#parseResult()", function() {
39   - message = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><respon><tanggal>2015/6/16 15:43:35</tanggal><idagen>P0039</idagen><refid>AEE15B32987941D89FFF4BC7EF676C13</refid><produk>PLN20</produk><tujuan>14204279369</tujuan><rc>0000</rc><data> </data><token> </token><pesan>#14836 PLN20 ke:14204279369 SUKSES. SN:3520-2887-6627-6699-4826/TestDummyPanjang6955555/P1/7000VA/32,4. \
40   -Sisa saldo Rp. 5,000,000 - Rp. 18,700 = Rp. 4,981,300</pesan></respon>';
  45 + it('should to have correct tarif', function() {
  46 + should(data.tarif).not.be.ok();
  47 + })
41 48  
42   - data = partner.parseResult(message);
43   - console.log(data);
  49 + it('should to have correct daya', function() {
  50 + should(data.daya).not.be.ok();
  51 + })
44 52  
45   - it('should return 2015/6/16 15:43:35', function() {
46   - assert.equal('2015/6/16 15:43:35', data.respon.tanggal);
47   - });
48   - });
49   -});
  53 + it('should to have correct jumlah kwh', function() {
  54 + should(data.jumlahkwh).not.be.ok();
  55 + })
  56 + })
  57 +
  58 + })
  59 +})