0% found this document useful (0 votes)
423 views

ABAP RAP Unmanaged Transactional Apps - Part2

Uploaded by

Star
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
423 views

ABAP RAP Unmanaged Transactional Apps - Part2

Uploaded by

Star
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Inhaltsverzeichnis

DEVELOPING AN UNMANAGED TRANSACTIONAL APPLICATION USING THE ABAP RESTFUL PROGRAMMING


MODEL:PART 1 ................................................................................................................................... 1
REFERENCE MATERIAL .................................................................................................................................. 1
INTRODUCTION ............................................................................................................................................ 2
CREATE PURCHASE DOCUMENT TABLE FOR HEADER............................................................................................ 3
CREATE PURCHASE DOCUMENT TABLE FOR ITEM................................................................................................ 3
CREATING BASIC INTERFACE CDS VIEW FOR PO HEADER .................................................................................... 4
CREATING BASIC INTERFACE CDS VIEW FOR PO ITEM:VERSION 0 ........................................................................ 5
CREATING BASIC INTERFACE CDS FOR PURCHASE ORGANIZATION......................................................................... 6
CREATING BASIC INTERFACE CDS FOR PURCHASE DOCUMENT STATUS ................................................................... 6
CREATING BASIC INTERFACE CDS FOR PURCHASE DOCUMENT PRIORITY ................................................................ 7
DEVELOPING AN UNMANAGED TRANSACTIONAL APPLICATION USING THE ABAP RESTFUL PROGRAMMING
MODEL:PART 2 ................................................................................................................................... 7
CREATING BASIC INTERFACE CDS VIEW FOR PO ITEM -VERSION 1: ...................................................................... 7
CREATE CDS VIEW FOR OVERALL ITEM PRICE................................................................................................... 8
CREATING TEST CLASS FOR OVERALLPRICE CDS VIEW........................................................................................ 9
ADD THE UNIT TEST CASE FOR OVERALL PRICE OF PO WITH 2 ITEMS .................................................................. 12
ADD THE UNIT TEST CASE FOR OVERALL PRICE OF PO WITH NO ITEMS ............................................................... 12
PREPARE THE UNIT TEST FOR PO WITH 2 ITEMS .............................................................................................. 13
FINAL UNIT TEST CLASS............................................................................................................................... 13

Developing an Unmanaged Transac onal Applica on Using the ABAP


RESTful Programming Model:Part 1
Reference Material
I recommend the book I men oned below for further insight, if you have the chance
to check it out.
h ps://www.sap-press.com/abap-res ul-applica on-programming-model_5647/

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024


Older Version

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.

Create Purchase Document table for header


@EndUserText.label : 'Purchase Document'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zpurchdocument {

key client : abap.clnt not null;


key purchasedocument : zpurchasedocumentdtel not null;
@EndUserText.label : 'Description'
description : abap.sstring(128);
@EndUserText.label : 'Approval Status'
status : abap.char(2);
@EndUserText.label : 'Priority'
priority : abap.char(2);
@EndUserText.label : 'Purchasing Organization'
purchasingorganization : abap.char(4);
@EndUserText.label : 'Purchase Document Image URL'
purchasedocumentimageurl : abap.sstring(255);
crea_date_time : timestampl;
crea_uname : syuname;
lchg_date_time : timestampl;
lchg_uname : syuname;

Create Purchase Document table for item

@EndUserText.label : 'Purchase Document Item'


@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zpurchdocitem {

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024


key client : abap.clnt not null;
@EndUserText.label : 'Purchase Document Item'
key purchasedocumentitem : abap.char(10) not null;
@AbapCatalog.foreignKey.keyType : #KEY
@AbapCatalog.foreignKey.screenCheck : true
key purchasedocument : zpurchasedocumentdtel not null
with foreign key [1..*,1] zpurchdocument
where client = zpurchdocitem.client
and purchasedocument = zpurchdocitem.purchasedocument;
@EndUserText.label : 'Description'
description : abap.sstring(128);
@EndUserText.label : 'Price'
@Semantics.amount.currencyCode : 'zpurchdocitem.currency'
price : abap.curr(13,2);
@EndUserText.label : 'Currency'
currency : abap.cuky;
@EndUserText.label : 'Quantity'
@Semantics.quantity.unitOfMeasure : 'zpurchdocitem.quantityunit'
quantity : abap.quan(13,2);
@EndUserText.label : 'Unit'
quantityunit : abap.unit(3);
@EndUserText.label : 'Vendor'
vendor : abap.sstring(32);
@EndUserText.label : 'Vendor Type'
vendortype : abap.sstring(32);
@EndUserText.label : 'Purchase Document Item Image URL'
purchasedocumentitemimageurl : abap.sstring(255);
crea_date_time : timestampl;
crea_uname : uname;
lchg_date_time : timestampl;
lchg_uname : uname;

Crea ng Basic Interface CDS View for PO Header


@AbapCatalog.sqlViewName: 'ZIPURCHDOC'
@EndUserText.label: 'Purchase Document'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@ObjectModel.representativeKey: 'PurchaseDocument'
@ObjectModel.semanticKey: ['PurchaseDocument']
define view Z_I_PurchaseDocument
as select from zpurchdocument
association [0..*] to Z_I_PurchaseDocumentItem as _PurchaseDocumentItem on
$projection.PurchaseDocument = _PurchaseDocumentItem.PurchaseDocument
association [0..1] to ZI_PurchaseDocumentPriority as _Priority on $projection.Priority =
_Priority.Priority
association [0..1] to zi_purchasingdocstatus as _Status on $projection.Status =
_Status.Status
association [0..1] to ZI_PurchasingOrganization as _PurchasingOrganization on
$projection.PurchasingOrganization = _PurchasingOrganization.Purchasingorganization
Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024
{
@ObjectModel.text.element: ['Description']
key purchasedocument as PurchaseDocument,
@Semantics.text: true
description as Description,
@ObjectModel.foreignKey.association: '_Status'
status as Status,
@ObjectModel.foreignKey.association: '_Priority'
priority as Priority,
@ObjectModel.foreignKey.association: '_PurchasingOrganization'
purchasingorganization as PurchasingOrganization,
@Semantics.imageUrl: true
purchasedocumentimageurl as PurchaseDocumentImageURL,
crea_date_time,
crea_uname,
lchg_date_time,
lchg_uname,
// Associations
_PurchaseDocumentItem,
_Priority,
_Status,
_PurchasingOrganization
}

Crea ng Basic Interface CDS View for PO Item:Version 0


@AbapCatalog.sqlViewName: 'ZIPURCHDOCITEM'
@EndUserText.label: 'Purchase Document Item'
@AccessControl.authorizationCheck: #NOT_REQUIRED

@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
}

Crea ng Basic Interface CDS for Purchase Organiza on


@AbapCatalog.sqlViewName: 'ZCPURCHORG'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Purchasing Organization'
define view ZI_PurchasingOrganization as select from zpurchorg
{
key purchasingorganization as Purchasingorganization,
organization_name as OrganizationName,
organization_address as OrganizationAddress

Crea ng Basic Interface CDS for Purchase document Status


@AbapCatalog.sqlViewName: 'ZCPURDOCSTAT'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Purchasing Document Status'
define view zi_purchasingdocstatus
as select from zpurchdocstatus
{
key status as Status,

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024


status_description as StatusDescription
}

Crea ng Basic Interface CDS for Purchase document Priority


@AbapCatalog.sqlViewName: 'ZCPODOCPRIO'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Purchase Document Priority'
define view ZI_PurchaseDocumentPriority as select from zpurchpriority
{
key priority as Priority,
priority_description as PriorityDescription
}

Developing an Unmanaged Transac onal Applica on Using the ABAP


RESTful Programming Model:Part 2

Crea ng Basic Interface CDS View for PO Item -Version 1:


@AbapCatalog.sqlViewName: 'ZIPURCHDOCITEM'
@EndUserText.label: 'Purchase Document Item'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@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,

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024


/*
It is assumed that all amount values shall be handled as measures by default. Amount values that
shall not be handled as measures shall explicitly be annotated with @DefaultAggregation:#NONE.
*/
@Semantics.amount.currencyCode: 'Currency'
@DefaultAggregation: #NONE
price as Price,
@Semantics.currencyCode: true
@ObjectModel.foreignKey.association: '_Currency'
currency as Currency,

/*
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,

// BOPF Admin Data


crea_date_time,
crea_uname,
lchg_date_time,
lchg_uname,

// Associations
_PurchaseDocument,
_QuantityUnitOfMeasure,
_Currency

Create CDS View for Overall Item Price


@AbapCatalog.sqlViewName: 'ZIPURCHDOCPRICE'
@EndUserText.label: 'Purchase Document Item'
@AccessControl.authorizationCheck: #CHECK
@ObjectModel.representativeKey: 'PurchaseDocument'
@ObjectModel.semanticKey: [ 'PurchaseDocument' ]

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024


define view Z_I_PurchDocOverallPrice
as select from Z_I_PurchaseDocument
association [0..1] to I_Currency as _Currency on $projection.currency = _Currency.Currency
{
key PurchaseDocument,
@Semantics.amount.currencyCode: 'Currency'
@DefaultAggregation: #NONE
sum( _PurchaseDocumentItem.OverallItemPrice ) as OverallPrice,
@ObjectModel.foreignKey.association: '_Currency'
@Semantics.currencyCode: true
_PurchaseDocumentItem.Currency,
PurchasingOrganization,
Description,
Status,
Priority,
PurchaseDocumentImageURL,
crea_date_time,
crea_uname,
lchg_date_time,
lchg_uname,

/* 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

Crea ng Test Class for Overallprice CDS View


Please create Test class for the CDS view Z_I_PurchDocOverallPrice

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024


Please provide the test class as given below

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024


Please provide test method name as given below

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024


A er a crea ng test class , you can find the below generated code.

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

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024


Prepare the Unit Test for PO with 2 items

Final Unit Test Class


"!@testing Z_I_PURCHDOCOVERALLPRICE
CLASS ltc_Z_I_PURCHDOCOVERALLPRICE
DEFINITION FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.

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.

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024


Overall_Price_No_Items FOR TESTING RAISING cx_static_check,
overall_price FOR TESTING RAISING cx_static_check.

ENDCLASS.

CLASS ltc_Z_I_PURCHDOCOVERALLPRICE IMPLEMENTATION.

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.

"Prepare test data for 'z_i_purchasedocument'


lt_z_i_purchasedocument = VALUE #(
(
purchasedocument = '1'
description = 'Doc with no items'
) ).
environment->insert_test_data( i_data = lt_z_i_purchasedocument ).

ENDMETHOD.
METHOD prepare_testdata_set.

"Prepare test data for 'z_i_purchasedocument'


lt_z_i_purchasedocument = VALUE #(
(
purchasedocument = '1'
Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024
description = 'Doc with no items'
) ).
environment->insert_test_data( i_data = lt_z_i_purchasedocument ).

"Prepare test data for 'z_i_purchasedocumentitem'


"TODO: Provide the test data here
lt_z_i_purchasedocumentitem = VALUE #( ( PurchaseDocument = '1'
PurchaseDocumentItem = '1' OverallItemPrice = '10' )
( PurchaseDocument = '1'
PurchaseDocumentItem = '2' OverallItemPrice = '10' ) ).
environment->insert_test_data( i_data = lt_z_i_purchasedocumentitem ).

ENDMETHOD.
ENDCLASS.

Looking forward to sharing more on this topic in my next post. Stay connected for
the continuation!

Prepared by Maheswari Angamuthu([email protected]) 07-Mar-2024

You might also like