-
Notifications
You must be signed in to change notification settings - Fork 4
/
unitTest2.rpgle
167 lines (129 loc) · 4.38 KB
/
unitTest2.rpgle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<%@ language="RPGLE" runasowner="*YES" owner="QPGMR"%>
<%
ctl-opt copyright('System & Method (C), 2019');
ctl-opt decEdit('0,') datEdit(*YMD.) main(main);
ctl-opt bndDir('NOXDB':'ICEUTILITY':'QC2LE');
/* -----------------------------------------------------------------------------
Service . . . : microservice router
Author . . . : Niels Liisberg
Company . . . : System & Method A/S
CRTICEPGM STMF('/www/IceBreak-Samples/unittest2.rpgle') SVRID(samples)
By Date PTF Description
------ ---------- ------- ---------------------------------------------------
NLI 10.05.2019 New program
----------------------------------------------------------------------------- */
/include noxdb
/include qasphdr,iceutility
// --------------------------------------------------------------------
// Main line:
// --------------------------------------------------------------------
dcl-proc main;
dcl-s pInput pointer;
dcl-s pOutput pointer;
dcl-s errCount int(10);
dcl-s msg varchar(256);
pInput = json_parseString ('{ -
"action": "msXlate.translate", -
"source": "en", -
"target": "es", -
"text" : "Good afternoon my friends" -
}');
pOutput = runService (pInput);
// required output:
msg = json_getStr(pOutput : 'translations[0].translation');
logText(msg);
json_delete(pInput);
json_delete(pOutput);
end-proc;
/* -------------------------------------------------------------------- *\
log if problems
\* -------------------------------------------------------------------- */
dcl-proc fail ;
dcl-pi *n;
errCount int(10);
text varchar(256) value;
pIn pointer value;
pOut pointer value;
end-pi;
errCount += 1;
logText ('Error in' + text);
logJson (pIn);
logJson (pOut);
end-proc;
/* -------------------------------------------------------------------- *\
run a a microservice call
\* -------------------------------------------------------------------- */
dcl-proc runService export;
dcl-pi *n pointer;
pActionIn pointer value options (*string);
end-pi;
dcl-pr ActionProc pointer extproc(pProc);
payload pointer value;
end-pr;
dcl-s Action varchar(128);
dcl-s prevAction varchar(128) static;
dcl-s pgmName char(10);
dcl-s procName varchar(128);
dcl-s pProc pointer (*PROC) static;
dcl-s pAction pointer;
dcl-s pResponse pointer;
dcl-s errText char(128);
dcl-s errPgm char(64);
dcl-s errList char(4096);
// will return the same pointer id action is already a parse object
pAction = json_parseString(pActionIn);
action = strUpper(json_GetStr(pAction:'action'));
//if action <> prevAction;
// prevAction = action;
pgmName = word (action:1:'.');
procName = word (action:2:'.');
pProc = loadServiceProgramProc ('*LIBL': pgmName : procName);
//endif;
if (pProc = *NULL);
pResponse= FormatError (
'Invalid action: ' + action + ' or service not found'
);
else;
monitor;
pResponse = ActionProc(pAction);
on-error;
soap_Fault(errText:errPgm:errList);
pResponse = FormatError (
'Error in service ' + action + ', ' + errText
);
endmon;
endif;
// if my input was a jsonstring, i did the parse and i have to cleanup
if pAction <> pActionIn;
json_delete (pAction);
endif;
return pResponse;
end-proc;
/* -------------------------------------------------------------------- *\
JSON error monitor
\* -------------------------------------------------------------------- */
dcl-proc FormatError export;
dcl-pi *n pointer;
description varchar(256) const options(*VARSIZE);
end-pi;
dcl-s msg varchar(4096);
dcl-s pMsg pointer;
msg = json_message(*NULL);
pMsg = json_parseString (' -
{ -
"success": false, -
"description":"' + description + '", -
"message": "' + msg + '"-
} -
');
consoleLog(msg);
return pMsg;
end-proc;
/* -------------------------------------------------------------------- *\
JSON error monitor
\* -------------------------------------------------------------------- */
dcl-proc successTrue export;
dcl-pi *n pointer;
end-pi;
return json_parseString ('{"success": true}');
end-proc;