At the end of this exercise you'll have some test data that will be served for your external service, when you run the CAP server with mocking turned on.
You'll have noticed by now in the log output to cds run
and cds watch
that at the persistence layer, CSV data is loaded automatically, as long as the location and filenames are as the CAP server expects (remember, convention over configuration is key with CAP). The filenames are made up from the namespace and entity names - see the link in the Further reading section below on providing initial data.
Relevant log output lines for entities in the context of the local (incidents) service look like this:
> init from db/data/acme.incmgt-Appointments.csv
> init from db/data/acme.incmgt-Incidents.conversation.csv
> init from db/data/acme.incmgt-Incidents.csv
> init from db/data/acme.incmgt-Incidents_status.csv
> init from db/data/acme.incmgt-Incidents_urgency.csv
> init from db/data/acme.incmgt-Incidents_urgency.texts.csv
> init from db/data/acme.incmgt-ServiceWorkers.csv
> init from db/data/acme.incmgt-TeamCalendar.csv
Test data in the form of CSV records can be supplied for mocked external services too. Let's add a CSV file to have some actual data served for our mocked API_BUSINESS_PARTNER
external service.
👉 Create a new data/
directory within the srv/external/
directory, and within that, create a file called API_BUSINESS_PARTNER-A_BusinessPartner.csv
. One way to do this is on the command line, in the incidents/
directory:
mkdir srv/external/data/
touch srv/external/data/API_BUSINESS_PARTNER-A_BusinessPartner.csv
At this point you should have a new file ready to add CSV records too.
👉 Do that now; add these records (including the CSV header record) to the file:
BusinessPartner;BusinessPartnerFullName
Z100001;Harry Potter
Z100002;Sherlock Holmes
Z100003;Sunny Sunshine
Yes, this is not strictly CSV (it's semi-colon separated), but it's fine for now.
👉 At this point, start or restart the CAP server with cds watch
:
cds watch
You should now see an additional "init from" line in the log output showing that data has been loaded for the mocked external service, specifically for the A_BusinessPartner
entity in the API_BUSINESS_PARTNER
namespace:
> init from srv/external/data/API_BUSINESS_PARTNER-A_BusinessPartner.CSV
👉 Head back to the browser, open http://localhost:4004 and select the A_BusinessPartner
endpoint at http://localhost:4004/api-business-partner/A_BusinessPartner. This represents the entity set from the original imported OData service, and is now returned not empty, as before, but with data.
👉 To confirm that there are indeed just three records (from the CSV file), add the OData system query option $count=true
as a query string parameter to the URL you just used, and you should see output like this:
{
"@odata.context": "$metadata#A_BusinessPartner",
"@odata.count": 3,
"value": [
{
"BusinessPartner": "Z100001",
"Customer": null,
"Supplier": null,
"AcademicTitle": null,
"AuthorizationGroup": null,
"BusinessPartnerCategory": null,
"BusinessPartnerFullName": "Harry Potter",
"TradingPartner": null
},
{
"BusinessPartner": "Z100002",
"Customer": null,
"Supplier": null,
"AcademicTitle": null,
"AuthorizationGroup": null,
"BusinessPartnerCategory": null,
"BusinessPartnerFullName": "Sherlock Holmes",
"TradingPartner": null
},
{
"BusinessPartner": "Z100003",
"Customer": null,
"Supplier": null,
"AcademicTitle": null,
"AuthorizationGroup": null,
"BusinessPartnerCategory": null,
"BusinessPartnerFullName": "Sunny Sunshine",
"TradingPartner": null
}
]
}
Many of the properties in the
A_BusinessPartner
entity type have been omitted here for brevity (in fact you could just request the single (non-key) property for which we have values in the CSV file like this: http://localhost:4004/api-business-partner/A_BusinessPartner?$count=true&$select=BusinessPartnerFullName). But however you want to view the data right now, it is a good time to point out that any properties which haven't been supplied with data via CSV are still presented, withnull
as their values.
At this point the external service that you imported is now not only mocked, but has data too.
- Providing initial data
- $count as a system query option in OData V4
- Adding data for our SAP Cloud Application Programming Model service (video)
If you finish earlier than your fellow participants, you might like to ponder these questions. There isn't always a single correct answer and there are no prizes - they're just to give you something else to think about.
-
Is CSV data supplied for the entire external service, or just a single entity within that? What about at the property level within the entity?
-
Normally, one might expect data files to be provided at the persistence layer, i.e. somewhere within the
db/
directory. But the data we provided here was not in there. Where was it, and why? Would it work if you put it intodb/data/
?