Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

http json converter escaping and valid empty json conversion added #130

Merged
merged 2 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/zamp_converter/zcl_amp_conv_http_json.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ CLASS zcl_amp_conv_http_json DEFINITION

PUBLIC SECTION.

INTERFACES zif_amp_converter.

INTERFACES zif_amp_converter .
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
Expand All @@ -15,6 +14,7 @@ ENDCLASS.

CLASS zcl_amp_conv_http_json IMPLEMENTATION.


METHOD zif_amp_converter~convert.

DATA(metric_store_groups) = metric_store.
Expand All @@ -34,7 +34,11 @@ CLASS zcl_amp_conv_http_json IMPLEMENTATION.

LOOP AT metric_store ASSIGNING FIELD-SYMBOL(<metric>) WHERE metric_group = <metric_store_group>-metric_group.

converted_metrics = converted_metrics && |"{ <metric>-metric_key }" : { <metric>-metric_value },|.
DATA(metric_key) = <metric>-metric_key.

REPLACE ALL OCCURRENCES OF REGEX `(["\\\/])` IN metric_key WITH `\\$0`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good hint. Maybe we can raise a good first issue to do that afterwards?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created: #133


converted_metrics = converted_metrics && |"{ metric_key }" : { <metric>-metric_value },|.

ENDLOOP.

Expand All @@ -43,8 +47,9 @@ CLASS zcl_amp_conv_http_json IMPLEMENTATION.
converted_metrics = converted_metrics && |\},|.

ENDLOOP.

converted_metrics = substring( val = converted_metrics off = 0 len = strlen( converted_metrics ) - 1 ).
IF sy-subrc = 0.
converted_metrics = substring( val = converted_metrics off = 0 len = strlen( converted_metrics ) - 1 ).
ENDIF.

converted_metrics = converted_metrics && |\}|.

Expand Down
110 changes: 110 additions & 0 deletions src/zamp_converter/zcl_amp_conv_http_json.clas.testclasses.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
CLASS ltcl_test DEFINITION FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.

PRIVATE SECTION.
DATA cut TYPE REF TO zcl_amp_conv_http_json.

METHODS setup.
METHODS test_content_type FOR TESTING.
METHODS test_without_metrics FOR TESTING.
METHODS test_only_allowed_characters FOR TESTING.
"! not allowed characters: https://www.tutorialspoint.com/json_simple/json_simple_escape_characters.htm
METHODS test_special_characters FOR TESTING.

ENDCLASS.

CLASS ltcl_test IMPLEMENTATION.


METHOD test_only_allowed_characters.

DATA metric_list TYPE zif_amp_converter=>metric_store.

metric_list = VALUE #( ( metric_scenario = 's1' metric_group = 'g1' metric_key = 'k1' metric_value = 1 )
( metric_scenario = 's1' metric_group = 'g1' metric_key = 'k2' metric_value = 2 )
( metric_scenario = 's1' metric_group = 'g1' metric_key = 'k3' metric_value = 3 ) ).

DATA(act_converted_metrics) = cut->zif_amp_converter~convert( metric_store = metric_list ).

DATA(exp_converted_metrics) = |\{"NPL_001_s1_g1": \{"k1": 1, "k2": 2, "k3": 3\}\}|.

CONDENSE act_converted_metrics NO-GAPS.
CONDENSE exp_converted_metrics NO-GAPS.

cl_aunit_assert=>assert_equals( EXPORTING
exp = exp_converted_metrics
act = act_converted_metrics
msg = |wrong json with only allowed characters| ).

ENDMETHOD.

METHOD test_special_characters.

DATA metric_list TYPE zif_amp_converter=>metric_store.

metric_list = VALUE #( ( metric_scenario = 's1' metric_group = 'g1' metric_key = 'k"1"' metric_value = 1 )
( metric_scenario = 's1' metric_group = 'g1' metric_key = 'k\b2' metric_value = 2 )
( metric_scenario = 's1' metric_group = 'g1' metric_key = 'k\f3' metric_value = 3 )
( metric_scenario = 's1' metric_group = 'g1' metric_key = 'k\n4' metric_value = 4 )
( metric_scenario = 's1' metric_group = 'g1' metric_key = 'k\r5' metric_value = 5 )
( metric_scenario = 's1' metric_group = 'g1' metric_key = 'k\t6' metric_value = 6 )
( metric_scenario = 's1' metric_group = 'g1' metric_key = 'k\7' metric_value = 7 ) ).

DATA(act_converted_metrics) = cut->zif_amp_converter~convert( metric_store = metric_list ).

DATA(exp_converted_metrics) = |\{"NPL_001_s1_g1": \{"k\\"1\\"": 1,| &&
|"k\\\\b2": 2,| &&
|"k\\\\f3": 3,| &&
|"k\\\\n4": 4,| &&
|"k\\\\r5": 5,| &&
|"k\\\\t6": 6,| &&
|"k\\\\7": 7\}\}|.

CONDENSE act_converted_metrics NO-GAPS.
CONDENSE exp_converted_metrics NO-GAPS.

cl_aunit_assert=>assert_equals( EXPORTING
exp = exp_converted_metrics
act = act_converted_metrics
msg = |wrong json with special metrics| ).

ENDMETHOD.

METHOD setup.
cut = NEW zcl_amp_conv_http_json( ).
ENDMETHOD.

METHOD test_content_type.

DATA(exp_content_type) = |application/json|.

cut->zif_amp_converter~convert( EXPORTING
metric_store = VALUE #( ( ) )
IMPORTING
content_type = DATA(act_content_type) ).

cl_aunit_assert=>assert_equals( EXPORTING
exp = exp_content_type
act = act_content_type
msg = |wrong content type| ).

ENDMETHOD.

METHOD test_without_metrics.

DATA(act_converted_metrics) = cut->zif_amp_converter~convert( metric_store = VALUE #( ) ).

DATA(exp_converted_metrics) = |\{\}|.

CONDENSE act_converted_metrics NO-GAPS.
CONDENSE exp_converted_metrics NO-GAPS.

cl_aunit_assert=>assert_equals( EXPORTING
exp = exp_converted_metrics
act = act_converted_metrics
msg = |wrong empty json| ).

ENDMETHOD.

ENDCLASS.
1 change: 1 addition & 0 deletions src/zamp_converter/zcl_amp_conv_http_json.clas.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<CLSCCINCL>X</CLSCCINCL>
<FIXPT>X</FIXPT>
<UNICODE>X</UNICODE>
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
</VSEOCLASS>
</asx:values>
</asx:abap>
Expand Down