ABAP RAP Unmanaged Transactional Apps - Part2
ABAP RAP Unmanaged Transactional Apps - Part2
Introduc on
Up for a challenge? Unmanaged transac onal scenarios in ABAP RAP can be tricky.
But fear not! This book offers a deep dive into this topic. Let’s discuss strategies
together!
I am wan ng to dig into complex scenario from an SAP press book that deals with SAP
ABAP RAP with unmanaged transac onal scenario. It is a new area for me and I’m
excited to explore it, even though I might encounter some bumps in the road along
the way (who doesn’t when learn something new).
Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024
Life (and German Classes) o en get in the way, but today I’m determined to get my
hands dirty and tackle this example head on.
This example caught my eye and piqued my curiosity because it presented a layer of
complexity that I haven’t encountered before.
For those of you who enjoy a good programming challenge, this scenario could be a
great way to push your skills…It is also a valuable exercise for anyone who preparing
for new job opportuni es.
Here’s what I found most intriguing.
@ObjectModel.representativeKey: 'PurchaseDocumentItem'
@ObjectModel.semanticKey: ['PurchaseDocumentItem','PurchaseDocument']
define view Z_I_PurchaseDocumentItem
as select from zpurchdocitem
association [1..1] to Z_I_PurchaseDocument as _PurchaseDocument on
$projection.PurchaseDocument = _PurchaseDocument.PurchaseDocument
association [0..1] to I_UnitOfMeasure as _QuantityUnitOfMeasure on $projection.QuantityUnit =
_QuantityUnitOfMeasure.UnitOfMeasure
association [0..1] to I_Currency as _Currency on $projection.Currency =
_Currency.Currency
{
@ObjectModel.text.element: ['Description']
key purchasedocumentitem as PurchaseDocumentItem,
@ObjectModel.foreignKey.association: '_PurchaseDocument'
key purchasedocument as PurchaseDocument,
@Semantics.text: true
description as Description,
vendor as Vendor,
vendortype as VendorType,
@Semantics.amount.currencyCode: 'Currency'
@DefaultAggregation: #NONE
price as Price,
Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024
@Semantics.currencyCode: true
@ObjectModel.foreignKey.association: '_Currency'
currency as Currency,
@Semantics.quantity.unitOfMeasure: 'QuantityUnit'
@DefaultAggregation: #NONE
quantity as Quantity,
@Semantics.unitOfMeasure: true
@ObjectModel.foreignKey.association: '_QuantityUnitOfMeasure'
quantityunit as QuantityUnit,
@Semantics.amount.currencyCode: 'Currency'
//Implementing Calculation in Purchase Document Item View
@DefaultAggregation: #NONE
quantity * price as OverallItemPrice,
@Semantics.imageUrl: true
purchasedocumentitemimageurl as PurchaseDocumentItemImageURL,
crea_date_time,
crea_uname,
lchg_date_time,
lchg_uname,
// Associations
_PurchaseDocument,
_QuantityUnitOfMeasure,
_Currency
}
@ObjectModel.text.element: ['Description']
key purchasedocumentitem as PurchaseDocumentItem,
@ObjectModel.foreignKey.association: '_PurchaseDocument'
key purchasedocument as PurchaseDocument,
@Semantics.text: true
description as Description,
vendor as Vendor,
/*
It is assumed that all quantity values shall be handled as measures by default. Quantity values that
shall not be handled as measures shall explicitly be annotated with @DefaultAggregation:#NONE.
*/
@Semantics.quantity.unitOfMeasure: 'QuantityUnit'
@DefaultAggregation: #NONE
quantity as Quantity,
@Semantics.unitOfMeasure: true
@ObjectModel.foreignKey.association: '_QuantityUnitOfMeasure'
quantityunit as QuantityUnit,
@Semantics.amount.currencyCode: 'Currency'
@DefaultAggregation: #NONE
quantity * price as OverallItemPrice,
@Semantics.imageUrl: true
purchasedocumentitemimageurl as PurchaseDocumentItemImageURL,
// Associations
_PurchaseDocument,
_QuantityUnitOfMeasure,
_Currency
/* Associations */
_PurchaseDocumentItem,
_Currency,
_Priority,
_Status,
_PurchasingOrganization
}
group by
PurchaseDocument,
_PurchaseDocumentItem.Currency,
PurchasingOrganization,
Description,
Status,
Priority,
PurchaseDocumentImageURL,
crea_date_time,
crea_uname,
lchg_date_time,
lchg_uname
Add the Unit Test Case for Overall Price of PO with 2 Items
Select the data from CDS view “Z_I_PURCHDOCOVERALLPRICE
This method will check whether selected data has expected calculated value.
Add the Unit Test Case for Overall Price of PO with no Items
CLASS-DATA:
environment TYPE REF TO if_cds_test_environment.
CLASS-METHODS:
"! In CLASS_SETUP, corresponding doubles and clone(s) for the CDS view under test and its
dependencies are created.
class_setup RAISING cx_static_check,
"! In CLASS_TEARDOWN, Generated database entities (doubles & clones) should be deleted at the
end of test class execution.
class_teardown.
DATA:
act_results TYPE STANDARD TABLE OF z_i_purchdocoverallprice WITH EMPTY KEY,
lt_z_i_purchasedocument TYPE STANDARD TABLE OF z_i_purchasedocument WITH EMPTY KEY,
lt_z_i_purchasedocumentitem TYPE STANDARD TABLE OF z_i_purchasedocumentitem WITH EMPTY
KEY.
METHODS:
"! SETUP method creates a common start state for each test method,
"! clear_doubles clears the test data for all the doubles used in the test method before each test
method execution.
setup RAISING cx_static_check,
prepare_testdata_set_no_items,
prepare_testdata_set,
"! In this method test data is inserted into the generated double(s) and the test is executed and
"! the results should be asserted with the actuals.
ENDCLASS.
METHOD class_setup.
environment = cl_cds_test_environment=>create( i_for_entity = 'Z_I_PURCHDOCOVERALLPRICE' ).
ENDMETHOD.
METHOD setup.
environment->clear_doubles( ).
ENDMETHOD.
METHOD class_teardown.
environment->destroy( ).
ENDMETHOD.
METHOD Overall_Price_No_Items.
prepare_testdata_set_no_items( ).
SELECT * FROM z_i_purchdocoverallprice INTO TABLE @act_results.
cl_abap_unit_assert=>assert_equals( exp = 1 act = lines( act_results ) ).
cl_abap_unit_assert=>assert_equals( exp = 0 act = act_results[ 1 ]-OverallPrice ).
ENDMETHOD.
METHOD overall_price.
prepare_testdata_set( ).
SELECT * FROM z_i_purchdocoverallprice INTO TABLE @act_results.
cl_abap_unit_assert=>assert_equals( exp = 1 act = lines( act_results ) ).
cl_abap_unit_assert=>assert_equals( exp = 20 act = act_results[ 1 ]-OverallPrice ).
ENDMETHOD.
METHOD prepare_testdata_set_no_items.
ENDMETHOD.
METHOD prepare_testdata_set.
ENDMETHOD.
ENDCLASS.
Looking forward to sharing more on this topic in my next post. Stay connected for
the continuation!