Skip to content

Commit 40cf308

Browse files
Xsodata with xsjs handlers application test (SAP-archive#1776)
* Added xsodata with xsjs handlers application test * Added some logs for health checks * Modified the application events xsjslib Co-authored-by: Dimitar Panayotov <[email protected]>
1 parent 78477c6 commit 40cf308

File tree

14 files changed

+553
-0
lines changed

14 files changed

+553
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
* Copyright (c) 2022 SAP SE or an SAP affiliate company and XSK contributors
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Apache License, v2.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and XSK contributors
10+
* SPDX-License-Identifier: Apache-2.0
11+
*/
12+
package com.sap.xsk.integration.tests.applications;
13+
14+
import com.google.gson.Gson;
15+
import com.google.gson.JsonElement;
16+
import com.google.gson.JsonParser;
17+
import com.sap.xsk.integration.tests.applications.deployment.ProjectDeploymentRule;
18+
import com.sap.xsk.integration.tests.applications.status.ProjectHealthCheckRule;
19+
import com.sap.xsk.integration.tests.applications.status.ProjectHttpCheck;
20+
import com.sap.xsk.integration.tests.applications.status.ProjectSqlCheck;
21+
import com.sap.xsk.integration.tests.applications.utils.HanaDataSourceFactory;
22+
import com.sap.xsk.integration.tests.applications.utils.HttpClientFactory;
23+
import com.sap.xsk.integration.tests.core.client.http.XSKHttpClient;
24+
import com.sap.xsk.integration.tests.core.hdb.utils.HanaITestUtils;
25+
import org.apache.commons.io.IOUtils;
26+
import org.apache.http.HttpEntity;
27+
import org.apache.http.HttpResponse;
28+
import org.apache.http.client.methods.HttpUriRequest;
29+
import org.apache.http.client.methods.RequestBuilder;
30+
import org.apache.http.entity.ContentType;
31+
import org.apache.http.entity.StringEntity;
32+
import org.junit.AfterClass;
33+
import org.junit.BeforeClass;
34+
import org.junit.ClassRule;
35+
import org.junit.Test;
36+
import org.junit.rules.RuleChain;
37+
import org.junit.rules.TestRule;
38+
import javax.sql.DataSource;
39+
import java.io.IOException;
40+
import java.net.MalformedURLException;
41+
import java.net.URISyntaxException;
42+
import java.net.URL;
43+
import java.nio.charset.StandardCharsets;
44+
import java.sql.Connection;
45+
import java.sql.SQLException;
46+
import java.sql.Statement;
47+
import java.util.Arrays;
48+
import java.util.List;
49+
import java.util.concurrent.ExecutionException;
50+
51+
import static com.sap.xsk.integration.tests.applications.deployment.ProjectType.CUSTOM;
52+
import static org.junit.Assert.assertEquals;
53+
54+
public class XsodataWithXsjsHandlersTest {
55+
56+
private static DataSource dataSource;
57+
private static HttpClientFactory httpClientFactory = new HttpClientFactory();
58+
59+
private static final String APPLICATION_NAME = "xsodata-with-xsjs-handlers";
60+
private static final String APPLICATION_SCHEMA = "TEST_SCHEMA";
61+
62+
private static final String EMPLOYEE_TABLE = "xsodata-with-xsjs-handlers::Entities.Employee";
63+
private static final String STATUS_TABLE = "xsodata-with-xsjs-handlers::Entities.Status";
64+
private static final String SALARY_TABLE = "xsodata-with-xsjs-handlers::Entities.Salary";
65+
66+
private static final String XSODATA_SERVICE_PATH = "/services/v4/web/xsodata-with-xsjs-handlers/Service.xsodata";
67+
68+
private static final String XSODATA_EMPLOYEE_PATH = XSODATA_SERVICE_PATH + "/Employee?$format=json";
69+
private static final String XSODATA_STATUS_PATH = XSODATA_SERVICE_PATH + "/Status?$format=json";
70+
private static final String XSODATA_SALARY_PATH = XSODATA_SERVICE_PATH + "/Salary?$format=json";
71+
72+
private static final List<ProjectHttpCheck> HTTP_CHECKS = Arrays.asList(
73+
new ProjectHttpCheck(XSODATA_SERVICE_PATH + "/Employee", 200),
74+
new ProjectHttpCheck(XSODATA_SERVICE_PATH + "/Status", 200),
75+
new ProjectHttpCheck(XSODATA_SERVICE_PATH + "/Salary", 200)
76+
);
77+
78+
private static final List<ProjectSqlCheck> SQL_CHECKS = Arrays.asList(
79+
new ProjectSqlCheck(APPLICATION_SCHEMA, EMPLOYEE_TABLE, true, false),
80+
new ProjectSqlCheck(APPLICATION_SCHEMA, STATUS_TABLE, true, false),
81+
new ProjectSqlCheck(APPLICATION_SCHEMA, SALARY_TABLE, true, false)
82+
);
83+
84+
public static final ProjectDeploymentRule projectDeploymentRule = new ProjectDeploymentRule(APPLICATION_NAME, CUSTOM);
85+
86+
public static final ProjectHealthCheckRule projectHealthCheckRule = new ProjectHealthCheckRule(HTTP_CHECKS, SQL_CHECKS);
87+
88+
@ClassRule
89+
public static TestRule chain = RuleChain.outerRule(projectDeploymentRule).around(projectHealthCheckRule);
90+
91+
@BeforeClass
92+
public static void getDataSource() {
93+
dataSource = HanaDataSourceFactory.getDataSource();
94+
}
95+
96+
class Employee {
97+
98+
private Integer Id;
99+
private String Name;
100+
private Integer Age;
101+
private String Country;
102+
103+
public Employee(String name, Integer age, String country) {
104+
Name = name;
105+
Age = age;
106+
Country = country;
107+
}
108+
109+
public Employee(Integer id, String name, Integer age, String country) {
110+
Id = id;
111+
Name = name;
112+
Age = age;
113+
Country = country;
114+
}
115+
}
116+
117+
@Test
118+
public void testXsodataXsjsEventHandlers() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
119+
120+
XSKHttpClient xskHttpClient = httpClientFactory.createXSKHttpClient();
121+
122+
URL xsodataEmployeeGetUrl = new URL(xskHttpClient.getBaseHost() + XSODATA_EMPLOYEE_PATH);
123+
URL xsodataStatusGetUrl = new URL(xskHttpClient.getBaseHost() + XSODATA_STATUS_PATH);
124+
URL xsodataSalaryGetUrl = new URL(xskHttpClient.getBaseHost() + XSODATA_SALARY_PATH);
125+
126+
makeCreateRequest(xskHttpClient);
127+
128+
assertHandlerResults(xskHttpClient, xsodataStatusGetUrl, "/expected-results/xsodata-with-xsjs-handlers/EmployeeStatusInserted.json");
129+
assertHandlerResults(xskHttpClient, xsodataEmployeeGetUrl, "/expected-results/xsodata-with-xsjs-handlers/EmployeeInserted.json");
130+
assertHandlerResults(xskHttpClient, xsodataSalaryGetUrl, "/expected-results/xsodata-with-xsjs-handlers/EmployeeSalaryInserted.json");
131+
132+
makeUpdateRequest(xskHttpClient);
133+
134+
assertHandlerResults(xskHttpClient, xsodataStatusGetUrl, "/expected-results/xsodata-with-xsjs-handlers/EmployeeStatusUpdated.json");
135+
assertHandlerResults(xskHttpClient, xsodataEmployeeGetUrl, "/expected-results/xsodata-with-xsjs-handlers/EmployeeUpdated.json");
136+
assertHandlerResults(xskHttpClient, xsodataSalaryGetUrl, "/expected-results/xsodata-with-xsjs-handlers/EmployeeSalaryUpdated.json");
137+
138+
makeDeleteRequest(xskHttpClient);
139+
140+
assertHandlerResults(xskHttpClient, xsodataStatusGetUrl, "/expected-results/xsodata-with-xsjs-handlers/EmployeeStatusDeleted.json");
141+
assertHandlerResults(xskHttpClient, xsodataEmployeeGetUrl, "/expected-results/xsodata-with-xsjs-handlers/EmployeeDeleted.json");
142+
assertHandlerResults(xskHttpClient, xsodataSalaryGetUrl, "/expected-results/xsodata-with-xsjs-handlers/EmployeeSalaryDeleted.json");
143+
}
144+
145+
private void makeCreateRequest(XSKHttpClient xskHttpClient)
146+
throws IOException, URISyntaxException, ExecutionException, InterruptedException {
147+
URL xsodataEmployeeCreateUrl = new URL(xskHttpClient.getBaseHost() + XSODATA_SERVICE_PATH + "/Employee");
148+
149+
Employee employee = new Employee(1, "Ben", 22, "Bulgaria");
150+
Gson gson = new Gson();
151+
StringEntity body = new StringEntity(gson.toJson(employee), ContentType.APPLICATION_JSON);
152+
153+
HttpUriRequest xsodataRequest = RequestBuilder.post(xsodataEmployeeCreateUrl.toURI()).setEntity(body).build();
154+
xskHttpClient.executeRequestAsync(xsodataRequest).get();
155+
}
156+
157+
private void makeUpdateRequest(XSKHttpClient xskHttpClient)
158+
throws MalformedURLException, URISyntaxException, ExecutionException, InterruptedException {
159+
URL xsodataEmployeeCreateUrl = new URL(xskHttpClient.getBaseHost() + XSODATA_SERVICE_PATH + "/Employee(1)");
160+
161+
Employee employee = new Employee("John", 30, "USA");
162+
Gson gson = new Gson();
163+
StringEntity body = new StringEntity(gson.toJson(employee), ContentType.APPLICATION_JSON);
164+
165+
HttpUriRequest xsodataRequest = RequestBuilder.put(xsodataEmployeeCreateUrl.toURI()).setEntity(body).build();
166+
xskHttpClient.executeRequestAsync(xsodataRequest).get();
167+
}
168+
169+
private void makeDeleteRequest(XSKHttpClient xskHttpClient)
170+
throws MalformedURLException, URISyntaxException, ExecutionException, InterruptedException {
171+
URL xsodataEmployeeCreateUrl = new URL(xskHttpClient.getBaseHost() + XSODATA_SERVICE_PATH + "/Employee(1)");
172+
173+
HttpUriRequest xsodataRequest = RequestBuilder.delete(xsodataEmployeeCreateUrl.toURI()).build();
174+
xskHttpClient.executeRequestAsync(xsodataRequest).get();
175+
}
176+
177+
private String makeGetRequest(XSKHttpClient xskHttpClient, URL url)
178+
throws IOException, URISyntaxException, ExecutionException, InterruptedException {
179+
HttpUriRequest xsodataRequest = RequestBuilder.get(url.toURI()).build();
180+
181+
HttpResponse xsodataHttpResponse = xskHttpClient.executeRequestAsync(xsodataRequest).get();
182+
HttpEntity xsodataEntity = xsodataHttpResponse.getEntity();
183+
return IOUtils.toString(xsodataEntity.getContent(), StandardCharsets.UTF_8);
184+
}
185+
186+
private void assertHandlerResults(XSKHttpClient xskHttpClient, URL xsodataStatusGetUrl, String expectedXsodataResultPath)
187+
throws IOException, URISyntaxException, ExecutionException, InterruptedException {
188+
String xsodataResult = makeGetRequest(xskHttpClient, xsodataStatusGetUrl);
189+
190+
String expectedXsodataResult = IOUtils.toString(
191+
XsodataWithXsjsHandlersTest.class.getResourceAsStream(expectedXsodataResultPath),
192+
StandardCharsets.UTF_8);
193+
194+
JsonElement xsodataJson = JsonParser.parseString(xsodataResult);
195+
JsonElement expectedXsodataJson = JsonParser.parseString(expectedXsodataResult);
196+
197+
assertEquals("The xsodata request response did not match the expected result!", expectedXsodataJson, xsodataJson);
198+
}
199+
200+
@AfterClass
201+
public static void dropSchema() throws SQLException {
202+
try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) {
203+
HanaITestUtils.dropSchema(statement, APPLICATION_SCHEMA);
204+
}
205+
}
206+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace xsodata-with-xsjs-handlers;
2+
3+
@Schema: 'TEST_SCHEMA'
4+
5+
context Entities {
6+
7+
entity Employee {
8+
key Id : Integer;
9+
Name : String(500);
10+
Age : Integer;
11+
Country : String(100);
12+
};
13+
14+
entity Status {
15+
key Id : Integer;
16+
Active : Boolean;
17+
}
18+
19+
entity Salary {
20+
key Id : Integer;
21+
Amount : Integer;
22+
}
23+
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
service namespace "xsodata-with-xsjs-handlers" {
2+
"TEST_SCHEMA"."xsodata-with-xsjs-handlers::Entities.Employee" as "Employee" with ("Id", "Name", "Country", "Age")
3+
create using "xsodata-with-xsjs-handlers.lib:Events.xsjslib::onCreate" create events (before "xsodata-with-xsjs-handlers.lib:Events.xsjslib::beforeCreate", after "xsodata-with-xsjs-handlers.lib:Events.xsjslib::afterCreate")
4+
update using "xsodata-with-xsjs-handlers.lib:Events.xsjslib::onUpdate" update events (before "xsodata-with-xsjs-handlers.lib:Events.xsjslib::beforeUpdate", after "xsodata-with-xsjs-handlers.lib:Events.xsjslib::afterUpdate")
5+
delete using "xsodata-with-xsjs-handlers.lib:Events.xsjslib::onDelete" delete events (before "xsodata-with-xsjs-handlers.lib:Events.xsjslib::beforeDelete", after "xsodata-with-xsjs-handlers.lib:Events.xsjslib::afterDelete");
6+
7+
"TEST_SCHEMA"."xsodata-with-xsjs-handlers::Entities.Status" as "Status";
8+
"TEST_SCHEMA"."xsodata-with-xsjs-handlers::Entities.Salary" as "Salary";
9+
10+
}
11+
12+
13+
annotations {
14+
enable OData4SAP;
15+
}
16+
17+
settings {
18+
support null;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
schema_name="TEST_SCHEMA";

0 commit comments

Comments
 (0)