-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathtest_server.py
executable file
·254 lines (211 loc) · 8.32 KB
/
test_server.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# SPDX-FileCopyrightText: 2013 SAP SE Srdjan Boskovic <srdjan.boskovic@sap.com>
#
# SPDX-License-Identifier: Apache-2.0
import os
import sys
from time import sleep
import pytest
from pyrfc import (
ABAPApplicationError,
Connection,
RCStatus,
RFCError,
Server,
set_ini_file_directory,
)
sys.path.append(os.path.dirname(__file__))
from data.func_desc_BAPISDORDER_GETDETAILEDLIST import (
FUNC_DESC_BAPISDORDER_GETDETAILEDLIST,
)
from data.func_desc_BS01_SALESORDER_GETDETAIL import (
FUNC_DESC_BS01_SALESORDER_GETDETAIL,
)
from data.func_desc_STFC_STRUCTURE import FUNC_DESC_STFC_STRUCTURE
from function_description_utils import compare_function_description
def my_stfc_connection(request_context=None, REQUTEXT=""):
print("stfc invoked")
print(
"request_context",
request_context,
)
print(f"REQUTEXT: {REQUTEXT}")
return {
"ECHOTEXT": REQUTEXT,
"RESPTEXT": "Python server here",
}
dir_path = os.path.dirname(os.path.realpath(__file__))
set_ini_file_directory(dir_path)
server = Server(
server_params={"dest": "gateway"},
client_params={"dest": "MME"},
)
client = Connection(dest="MME")
@pytest.mark.skipif(
not sys.platform.startswith("darwin"), reason="Manual server test on Darwin only"
)
class TestServer:
def test_add_wrong_function(self):
with pytest.raises(ABAPApplicationError) as ex:
server.add_function("STFC_CONNECTION1", my_stfc_connection)
error = ex.value
assert error.code == 5
assert error.key == "FU_NOT_FOUND"
assert error.message == "ID:FL Type:E Number:046 STFC_CONNECTION1"
def test_add_function_twice(self):
with pytest.raises(RFCError) as ex:
server.add_function("STFC_CONNECTION", my_stfc_connection)
server.add_function("STFC_CONNECTION", my_stfc_connection)
error = ex.value
assert error.args[0] == "Server function 'STFC_CONNECTION' already installed."
def test_function_description_STFC_STRUCTURE(self):
func_name = "STFC_STRUCTURE"
func_desc = client.get_function_description(func_name)
compare_function_description(
func_desc,
FUNC_DESC_STFC_STRUCTURE,
)
def test_function_description_BAPISDORDER_GETDETAILEDLIST(self):
func_name = "BAPISDORDER_GETDETAILEDLIST"
func_desc = client.get_function_description(func_name)
compare_function_description(
func_desc,
FUNC_DESC_BAPISDORDER_GETDETAILEDLIST,
)
def test_function_description_BS01_SALESORDER_GETDETAIL(self):
func_name = "BS01_SALESORDER_GETDETAIL"
func_desc = client.get_function_description(func_name)
compare_function_description(
func_desc,
FUNC_DESC_BS01_SALESORDER_GETDETAIL,
)
def test_stfc_structure(self):
def my_stfc_structure(request_context=None, IMPORTSTRUCT=None, RFCTABLE=None):
"""Server function my_stfc_structure with the signature
of ABAP function module STFC_STRUCTURE."""
print("stfc structure invoked")
print("request_context", request_context)
if IMPORTSTRUCT is None:
IMPORTSTRUCT = {}
if RFCTABLE is None:
RFCTABLE = []
ECHOSTRUCT = IMPORTSTRUCT.copy()
ECHOSTRUCT["RFCINT1"] += 1
ECHOSTRUCT["RFCINT2"] += 1
ECHOSTRUCT["RFCINT4"] += 1
if len(RFCTABLE) == 0:
RFCTABLE = [ECHOSTRUCT]
RESPTEXT = f"Python server sends {len(RFCTABLE)} table rows"
print(f"ECHOSTRUCT: {ECHOSTRUCT}")
print(f"RFCTABLE: {RFCTABLE}")
print(f"RESPTEXT: {RESPTEXT}")
return {
"ECHOSTRUCT": ECHOSTRUCT,
"RFCTABLE": RFCTABLE,
"RESPTEXT": RESPTEXT,
}
def authentication_check(func_name=False, request_context=None):
if request_context is None:
request_context = {}
print(
f"[js] authorization '{func_name}' request_context",
request_context,
)
return RCStatus.OK
def authorization_check(rfcHandle=None, securityAttributes=None):
print(f"[js] authorization for {rfcHandle}, attr {securityAttributes}")
return RCStatus.OK
# create server
server = Server(
server_params={"dest": "MME_GATEWAY"},
client_params={"dest": "MME"},
config={
"authentication_check": authentication_check,
"authorization_check": authorization_check,
"check_date": False,
"check_time": False,
"server_log": True,
},
)
# expose python function my_stfc_structure as ABAP function STFC_STRUCTURE,
server.add_function("STFC_STRUCTURE", my_stfc_structure)
# start server
server.start()
# call ABAP function module which will call Python server
# and return the server response
client = Connection(dest="MME")
result = client.call("ZSERVER_TEST_STFC_STRUCTURE")
# shutdown server
server.close()
# check the server response
assert result["RESPTEXT"] == "Python server sends 1 table rows"
assert "ECHOSTRUCT" in result
assert result["ECHOSTRUCT"]["RFCINT1"] == 2
assert result["ECHOSTRUCT"]["RFCINT2"] == 3
assert result["ECHOSTRUCT"]["RFCINT4"] == 5
assert result["ECHOSTRUCT"]["RFCDATE"] == "20230928"
assert result["ECHOSTRUCT"]["RFCTIME"] == "240000"
def test_trfc(self):
def stfc_write_to_tcpic(request_context=None, RESTART_QNAME="", TCPICDAT=[]):
context = (
{"error": "No request context"}
if request_context is None
else request_context["server_context"]
)
print("[js] python function: stfc_write_to_tcpic", f"context {context}")
return {"TCPICDAT": TCPICDAT}
def onCheckTransaction(rfcHandle, tid):
print("[js] onCheckTransaction", rfcHandle, tid)
return RCStatus.OK
def onCommitTransaction(rfcHandle, tid):
print("[js] onCommitTransaction", rfcHandle, tid)
return RCStatus.OK
def onConfirmTransaction(rfcHandle, tid):
print("[js] onConfirmTransaction", rfcHandle, tid)
return RCStatus.OK
def onRollbackTransaction(rfcHandle, tid):
print("[js] onRollbackTransaction", rfcHandle, tid)
return RCStatus.OK
def authenticationHandler(function_name, server_context=None):
print(f"[js] authentication for {function_name}, context {server_context}")
return RCStatus.OK
def authorizationHandler(rfcHandle=None, securityAttributes=None):
print(
f"[js] authorization for {rfcHandle}, security attr {securityAttributes}"
)
return RCStatus.OK
try:
# Create server
server = Server(
server_params={"dest": "MME_GATEWAY"},
client_params={"dest": "MME"},
config={"check_date": False, "check_time": False, "server_log": False},
)
# ABAP function used to send IDocs via tRFC/qRFC
server.add_function("STFC_WRITE_TO_TCPIC", stfc_write_to_tcpic)
# Register the RFC transaction handlers.
server.transaction_rfc_init(
sysId="MME",
transactionHandler={
"check": onCheckTransaction,
"commit": onCommitTransaction,
"rollback": onRollbackTransaction,
"confirm": onConfirmTransaction,
},
)
# Start server
server.start()
# call RSARFCT0
client = Connection(dest="MME")
tcall = "00001"
ncall = client.call("ZSERVER_TEST_TRFC", NCALL=tcall)["EV_NCALL"]
client.close()
assert ncall == tcall
# receive queues from abap system
sleep(5)
except Exception as ex:
print(ex)
finally:
# Shutdown server
server.close()
def teardown():
server.close()