
Folder Structure.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mock Server Tutorial</title>
<script id="sap-ui-bootstrap"
src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
data-sap-ui-theme="sap_belize"
data-sap-ui-resourceroots='{
"project4": "../"
}'
data-sap-ui-oninit="module:project4/test/initMockServer"
data-sap-ui-compatVersion="edge"
data-sap-ui-async="true">
</script>
</head>
<body class="sapUiBody">
<div data-sap-ui-component data-name="project4" data-id="container" data-settings='{"id" : "MockServer"}'></div>
</body>
</html>
sap.ui.define([
"project4/localService/mockServer"
], function (mockserver) {
"use strict";
debugger
// initialize the mock server
mockserver.init();
// initialize the embedded component on the HTML page
sap.ui.require(["sap/ui/core/ComponentSupport"]);
});
loading metadata in browser
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
<Schema Namespace="NorthwindModel" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityType Name="Order">
<Key>
<PropertyRef Name="OrderID" />
</Key>
<Property Name="OrderID" Type="Edm.Int32" Nullable="false" p8:StoreGeneratedPattern="Identity" xmlns:p8="http://schemas.microsoft.com/ado/2009/02/edm/annotation" />
<Property Name="CustomerID" Type="Edm.String" Nullable="true" MaxLength="5" Unicode="true" FixedLength="true" />
<Property Name="EmployeeID" Type="Edm.Int32" Nullable="true" />
<Property Name="OrderDate" Type="Edm.DateTime" Nullable="true" />
<Property Name="RequiredDate" Type="Edm.DateTime" Nullable="true" />
<Property Name="ShippedDate" Type="Edm.DateTime" Nullable="true" />
<Property Name="ShipVia" Type="Edm.Int32" Nullable="true" />
<Property Name="Freight" Type="Edm.Decimal" Nullable="true" Precision="19" Scale="4" />
<Property Name="ShipName" Type="Edm.String" Nullable="true" MaxLength="40" Unicode="true" FixedLength="false" />
<Property Name="ShipAddress" Type="Edm.String" Nullable="true" MaxLength="60" Unicode="true" FixedLength="false" />
<Property Name="ShipCity" Type="Edm.String" Nullable="true" MaxLength="15" Unicode="true" FixedLength="false" />
<Property Name="ShipRegion" Type="Edm.String" Nullable="true" MaxLength="15" Unicode="true" FixedLength="false" />
<Property Name="ShipPostalCode" Type="Edm.String" Nullable="true" MaxLength="10" Unicode="true" FixedLength="false" />
<Property Name="ShipCountry" Type="Edm.String" Nullable="true" MaxLength="15" Unicode="true" FixedLength="false" />
<NavigationProperty Name="Customer" Relationship="NorthwindModel.FK_Orders_Customers" FromRole="Orders" ToRole="Customers" />
<NavigationProperty Name="Employee" Relationship="NorthwindModel.FK_Orders_Employees" FromRole="Orders" ToRole="Employees" />
<NavigationProperty Name="Order_Details" Relationship="NorthwindModel.FK_Order_Details_Orders" FromRole="Orders" ToRole="Order_Details" />
<NavigationProperty Name="Shipper" Relationship="NorthwindModel.FK_Orders_Shippers" FromRole="Orders" ToRole="Shippers" />
</EntityType>
</Schema>
<Schema Namespace="ODataWeb.Northwind.Model" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="NorthwindEntities" p7:LazyLoadingEnabled="true" m:IsDefaultEntityContainer="true" xmlns:p7="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
<EntitySet Name="Orders" EntityType="NorthwindModel.Order" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
loading the data in json format
{
"d" : {
"results": [
{
"__metadata": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10443)", "type": "NorthwindModel.Order"
}, "OrderID": 10443, "CustomerID": "REGGC", "EmployeeID": 8, "OrderDate": "\/Date(855705600000)\/", "RequiredDate": "\/Date(858124800000)\/", "ShippedDate": "\/Date(855878400000)\/", "ShipVia": 1, "Freight": "13.9500", "ShipName": "Reggiani Caseifici", "ShipAddress": "Strada Provinciale 124", "ShipCity": "Reggio Emilia", "ShipRegion": null, "ShipPostalCode": "42100", "ShipCountry": "Italy", "Customer": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10443)/Customer"
}
}, "Employee": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10443)/Employee"
}
}, "Order_Details": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10443)/Order_Details"
}
}, "Shipper": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10443)/Shipper"
}
}
}, {
"__metadata": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10444)", "type": "NorthwindModel.Order"
}, "OrderID": 10444, "CustomerID": "BERGS", "EmployeeID": 3, "OrderDate": "\/Date(855705600000)\/", "RequiredDate": "\/Date(858124800000)\/", "ShippedDate": "\/Date(856483200000)\/", "ShipVia": 3, "Freight": "3.5000", "ShipName": "Berglunds snabbk\u00f6p", "ShipAddress": "Berguvsv\u00e4gen 8", "ShipCity": "Lule\u00e5", "ShipRegion": null, "ShipPostalCode": "S-958 22", "ShipCountry": "Sweden", "Customer": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10444)/Customer"
}
}, "Employee": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10444)/Employee"
}
}, "Order_Details": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10444)/Order_Details"
}
}, "Shipper": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10444)/Shipper"
}
}
}, {
"__metadata": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10445)", "type": "NorthwindModel.Order"
}, "OrderID": 10445, "CustomerID": "BERGS", "EmployeeID": 3, "OrderDate": "\/Date(855792000000)\/", "RequiredDate": "\/Date(858211200000)\/", "ShippedDate": "\/Date(856396800000)\/", "ShipVia": 1, "Freight": "9.3000", "ShipName": "Berglunds snabbk\u00f6p", "ShipAddress": "Berguvsv\u00e4gen 8", "ShipCity": "Lule\u00e5", "ShipRegion": null, "ShipPostalCode": "S-958 22", "ShipCountry": "Sweden", "Customer": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10445)/Customer"
}
}, "Employee": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10445)/Employee"
}
}, "Order_Details": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10445)/Order_Details"
}
}, "Shipper": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10445)/Shipper"
}
}
}, {
"__metadata": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10446)", "type": "NorthwindModel.Order"
}, "OrderID": 10446, "CustomerID": "TOMSP", "EmployeeID": 6, "OrderDate": "\/Date(855878400000)\/", "RequiredDate": "\/Date(858297600000)\/", "ShippedDate": "\/Date(856310400000)\/", "ShipVia": 1, "Freight": "14.6800", "ShipName": "Toms Spezialit\u00e4ten", "ShipAddress": "Luisenstr. 48", "ShipCity": "M\u00fcnster", "ShipRegion": null, "ShipPostalCode": "44087", "ShipCountry": "Germany", "Customer": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10446)/Customer"
}
}, "Employee": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10446)/Employee"
}
}, "Order_Details": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10446)/Order_Details"
}
}, "Shipper": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10446)/Shipper"
}
}
}, {
"__metadata": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10447)", "type": "NorthwindModel.Order"
}, "OrderID": 10447, "CustomerID": "RICAR", "EmployeeID": 4, "OrderDate": "\/Date(855878400000)\/", "RequiredDate": "\/Date(858297600000)\/", "ShippedDate": "\/Date(857692800000)\/", "ShipVia": 2, "Freight": "68.6600", "ShipName": "Ricardo Adocicados", "ShipAddress": "Av. Copacabana, 267", "ShipCity": "Rio de Janeiro", "ShipRegion": "RJ", "ShipPostalCode": "02389-890", "ShipCountry": "Brazil", "Customer": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10447)/Customer"
}
}, "Employee": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10447)/Employee"
}
}, "Order_Details": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10447)/Order_Details"
}
}, "Shipper": {
"__deferred": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders(10447)/Shipper"
}
}
}
], "__next": "https://services.odata.org/V2/Northwind/Northwind.svc/Orders?$skiptoken=10447"
}
}
sap.ui.define([
"sap/ui/core/util/MockServer"
], (MockServer) => {
"use strict";
return {
init: function () {
// create
const oMockServer = new MockServer({
rootUri: "/V2/Northwind/Northwind.svc/"
});
const oUrlParams = new URLSearchParams(window.location.search);
// configure mock server with a delay
MockServer.config({
autoRespond: true,
autoRespondAfter: oUrlParams.get("serverDelay") || 999
});
// simulate
oMockServer.simulate("../localService/metadata.xml", {
sMockdataBaseUrl: "../localService/mockdata",
bGenerateMissingMockData: true
});
// const sPath = sap.ui.require.toUrl("project4/localService");
// oMockServer.simulate(sPath + "/metadata.xml", sPath + "/mockdata");
// start
oMockServer.start();
}
};
});
service uri in the manifest.json file
entry of backend server in ui5-mock.yaml file
removed entry of real service in ui5-mock.yaml file
package.json configuration
<mvc:View controllerName="project4.controller.View1"
xmlns:mvc="sap.ui.core.mvc" displayBlock="true"
xmlns="sap.m">
<Page id="page" title="{i18n>title}">
<content>
<Button text="hello" press="onPress"></Button>
<Table id="_IDGenTable1"
items="{/Orders}">
<headerToolbar>
<OverflowToolbar id="_IDGenOverflowToolbar1">
<Title id="_IDGenTitle1" text="Employee Details" level="H2"/>
<ToolbarSpacer id="_IDGenToolbarSpacer1"/>
<Button id="_IDGenButton1"
tooltip="Sort"
icon="sap-icon://sort"
press="handleSortButtonPressed"/>
<Button id="_IDGenButton2"
tooltip="Filter"
icon="sap-icon://filter"
press="handleFilterButtonPressed"/>
<Button id="_IDGenButton3"
tooltip="Group"
icon="sap-icon://group-2"
press="handleGroupButtonPressed"/>
<ToggleButton id="_IDGenToggleButton1" icon="sap-icon://menu" tooltip="Enable Custom Context Menu" press="onToggleContextMenu" />
</OverflowToolbar>
</headerToolbar>
<infoToolbar>
<OverflowToolbar id="vsdFilterBar" visible="false">
<Text id="vsdFilterLabel" />
</OverflowToolbar>
</infoToolbar>
<columns>
<Column id="_IDGenColumn1" width="12em" headerMenu="columnHeaderMenu"><Text id="_IDGenText1" text="Employee Name" /></Column>
<Column id="_IDGenColumn2" minScreenWidth="Tablet" demandPopin="true"><Text id="_IDGenText2" text="Working City" /></Column>
<Column id="_IDGenColumn3" minScreenWidth="Tablet" demandPopin="true" ><Text id="_IDGenText3" text="Age " /></Column>
<Column id="_IDGenColumn4" minScreenWidth="Tablet" demandPopin="true" ><Text id="_IDGenText4" text="Designation " /></Column>
</columns>
<items>
<ColumnListItem id="_IDGenColumnListItem1" vAlign="Middle">
<cells>
<ObjectIdentifier id="_IDGenObjectIdentifier1" title="{CustomerID}" text="{OrderID}" />
<Text id="_IDGenText6" text="{ShipCity}" />
<Text id="_IDGenText7" text="{CustomerID}" />
<ObjectNumber id="_IDGenObjectNumber1" number="{ShipAddress}"/>
</cells>
</ColumnListItem>
</items>
</Table>
</content>
</Page>
</mvc:View>
data loaded from real service
imitating the real server by showing busy symbol by server delay.
loading the mockdata
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
7 | |
6 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |