Skip to content

Commit 3e990c8

Browse files
committed
int_test with suggested change in Api class
1 parent f920d7d commit 3e990c8

3 files changed

Lines changed: 16 additions & 340 deletions

File tree

sdm/src/test/java/integration/com/sap/cds/sdm/Api.java

Lines changed: 1 addition & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -224,151 +224,8 @@ public List<String> createAttachment(
224224
String ID;
225225
String error = "";
226226

227-
// Creating empty attachments
228227
String fileName = file.getName();
229-
230-
MediaType mediaType = MediaType.parse("application/json");
231-
RequestBody body =
232-
RequestBody.create(
233-
mediaType, ByteString.encodeUtf8("{\n \"fileName\" : \"" + fileName + "\"\n}"));
234-
Request postRequest =
235-
new Request.Builder()
236-
.url(
237-
"https://"
238-
+ appUrl
239-
+ "/odata/v4/"
240-
+ serviceName
241-
+ "/"
242-
+ entityName
243-
+ "(ID="
244-
+ entityID
245-
+ ",IsActiveEntity=false)/"
246-
+ facetName)
247-
.method("POST", body)
248-
.addHeader("Content-Type", "application/json")
249-
.addHeader("Authorization", token)
250-
.build();
251-
252-
try (Response response = httpClient.newCall(postRequest).execute()) {
253-
if (response.code() != 201) {
254-
System.out.println(
255-
"Create Attachment in the section: "
256-
+ facetName
257-
+ " failed. Error : "
258-
+ response.body().string());
259-
throw new IOException("Could not read Attachment");
260-
}
261-
Map<String, Object> responseMap = objectMapper.readValue(response.body().string(), Map.class);
262-
ID = (String) responseMap.get("ID");
263-
264-
long startTime = System.nanoTime();
265-
// Upload file content into the empty attachment
266-
RequestBody fileBody = RequestBody.create(file, MediaType.parse("application/octet-stream"));
267-
Request fileRequest =
268-
new Request.Builder()
269-
.url(
270-
"https://"
271-
+ appUrl
272-
+ "/odata/v4/"
273-
+ serviceName
274-
+ "/"
275-
+ entityName
276-
+ "_"
277-
+ facetName
278-
+ "(up__ID="
279-
+ entityID
280-
+ ",ID="
281-
+ ID
282-
+ ",IsActiveEntity=false)/content")
283-
.put(fileBody)
284-
.addHeader("Authorization", token)
285-
.build();
286-
287-
try (Response fileResponse = httpClient.newCall(fileRequest).execute()) {
288-
if (fileResponse.code() != 204) {
289-
String responseBodyString = fileResponse.body().string();
290-
System.out.println(
291-
"Create Attachment in the section: "
292-
+ facetName
293-
+ " failed. Error : "
294-
+ responseBodyString);
295-
error = responseBodyString;
296-
Request request =
297-
new Request.Builder()
298-
.url(
299-
"https://"
300-
+ appUrl
301-
+ "/odata/v4/"
302-
+ serviceName
303-
+ "/"
304-
+ entityName
305-
+ "_"
306-
+ facetName
307-
+ "(up__ID="
308-
+ entityID
309-
+ ",ID="
310-
+ ID
311-
+ ",IsActiveEntity=false)")
312-
.delete()
313-
.addHeader("Authorization", token)
314-
.build();
315-
316-
try (Response deleteResponse = httpClient.newCall(request).execute()) {
317-
if (deleteResponse.code() != 204) {
318-
System.out.println(
319-
"Delete Attachment in section :"
320-
+ facetName
321-
+ " failed. Error : "
322-
+ deleteResponse.body().string());
323-
throw new IOException(
324-
"Attachment was not created in section : "
325-
+ facetName
326-
+ " and its container was not deleted : ");
327-
}
328-
List<String> createResponse = new ArrayList<>();
329-
createResponse.add(error);
330-
return createResponse;
331-
} catch (IOException e) {
332-
System.out.println(
333-
"Attachment was not created in section : "
334-
+ facetName
335-
+ " and its container was not deleted : "
336-
+ e);
337-
}
338-
}
339-
long endTime = System.nanoTime(); // Record end time
340-
double duration = (endTime - startTime) / 1_000_000_000.0;
341-
System.out.println("Time taken to create(s) : " + duration);
342-
List<String> createResponse = new ArrayList<>();
343-
createResponse.add("Attachment created");
344-
createResponse.add(ID);
345-
return createResponse;
346-
} catch (IOException e) {
347-
System.out.println("Attachment was not created in section: " + facetName + " : " + e);
348-
}
349-
} catch (IOException e) {
350-
System.out.println("Attachment was not created in section: " + facetName + " : " + e);
351-
}
352-
List<String> createResponse = new ArrayList<>();
353-
createResponse.add("Attachment was not created in section: " + facetName);
354-
return createResponse;
355-
}
356-
357-
public List<String> createAttachment_RestrictedCharacter(
358-
String appUrl,
359-
String serviceName,
360-
String entityName,
361-
String facetName,
362-
String entityID,
363-
String srvpath,
364-
Map<String, Object> postData,
365-
File file)
366-
throws IOException {
367-
String ID;
368-
String error = "";
369-
370-
// Creating empty attachments
371-
String fileName = "sam/../ple.pdf";
228+
File renamedFile = new File(file.getParent(), "samp..//..pdf");
372229

373230
MediaType mediaType = MediaType.parse("application/json");
374231
RequestBody body =

sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_MultipleFacet.java

Lines changed: 4 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ void testDeleteMultipleAttachmentsReferencesFootnotes() throws IOException {
602602
@Test
603603
@Order(13)
604604
void testUploadMultipleAttachment_RestrictedCharacters() throws IOException {
605-
System.out.println("Test (13) : Restricted characters");
605+
System.out.println("Test (13) : Creating attachment with Restricted characters");
606606

607607
ClassLoader classLoader = getClass().getClassLoader();
608608
File file = new File(classLoader.getResource("abc.pdf").getFile());
@@ -624,9 +624,10 @@ void testUploadMultipleAttachment_RestrictedCharacters() throws IOException {
624624
int renameCount = 0;
625625

626626
for (int i = 0; i < facet.length; i++) {
627+
File renamedFile = new File(file.getParent(), "Sam.//..pdf");
627628
List<String> createResponse =
628-
api.createAttachment_RestrictedCharacter(
629-
appUrl, serviceName, entityName, facet[i], entityID2, srvpath, postData, file);
629+
api.createAttachment(
630+
appUrl, serviceName, entityName, facet[i], entityID2, srvpath, postData, renamedFile);
630631

631632
System.out.println("Create response for facet[" + i + "]: " + createResponse);
632633

@@ -2013,104 +2014,4 @@ void testUploadNAttachments() throws IOException {
20132014
}
20142015
}
20152016
}
2016-
2017-
// @Test
2018-
// @Order(23)
2019-
// void testUploadNAttachmentsNoSDMRoles() throws IOException {
2020-
// System.out.println("Upload attachment without SDM Roles and with assigned role collection");
2021-
2022-
// Properties credentialsProperties = Credentials.getCredentials();
2023-
// String clientId = credentialsProperties.getProperty("clientID");
2024-
// String clientSecret = credentialsProperties.getProperty("clientSecret");
2025-
// appUrl = credentialsProperties.getProperty("appUrl");
2026-
// authUrl = credentialsProperties.getProperty("authUrl");
2027-
// String username2 = credentialsProperties.getProperty("username2");
2028-
// String password2 = credentialsProperties.getProperty("password2");
2029-
2030-
// String credentials = clientId + ":" + clientSecret;
2031-
// String basicAuth =
2032-
// "Basic " +
2033-
// Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8));
2034-
2035-
// OkHttpClient client = new OkHttpClient().newBuilder().build();
2036-
// MediaType mediaType = MediaType.parse("text/plain");
2037-
// RequestBody body = RequestBody.create(mediaType, "");
2038-
// Request request =
2039-
// new Request.Builder()
2040-
// .url(
2041-
// authUrl
2042-
// + "/oauth/token?grant_type=password&username="
2043-
// + username2
2044-
// + "&password="
2045-
// + password2)
2046-
// .method("POST", body)
2047-
// .addHeader("Authorization", basicAuth)
2048-
// .build();
2049-
// Response response = client.newCall(request).execute();
2050-
// if (response.code() != 200) {
2051-
// System.out.println("Token generation failed. Response code: " + response.code());
2052-
// String errorBody = response.body().string();
2053-
// System.out.println("Error body: " + errorBody);
2054-
// }
2055-
// token = new ObjectMapper().readTree(response.body().string()).get("access_token").asText();
2056-
// response.close();
2057-
// Map<String, String> config = new HashMap<>();
2058-
// config.put("Authorization", "Bearer " + token);
2059-
// api = new Api(config);
2060-
2061-
// ClassLoader classLoader = getClass().getClassLoader();
2062-
// File originalFile = new File(classLoader.getResource("sample.exe").getFile());
2063-
2064-
// boolean testStatus = false;
2065-
// String createResponse =
2066-
// api.editEntityDraft(appUrl, serviceName, entityName, srvpath, entityID4);
2067-
// System.out.println("response: " + response);
2068-
2069-
// if ("Entity in draft mode".equals(response)) {
2070-
// for (int i = 1; i <= 5; i++) {
2071-
// File tempFile = File.createTempFile("sample_" + i + "_", ".exe");
2072-
// Files.copy(originalFile.toPath(), tempFile.toPath(),
2073-
// StandardCopyOption.REPLACE_EXISTING);
2074-
2075-
// Map<String, Object> postData = new HashMap<>();
2076-
// postData.put("up__ID", entityID4);
2077-
// postData.put("mimeType", "application/exe");
2078-
// postData.put("createdAt", new Date().toString());
2079-
// postData.put("createdBy", "test@test.com");
2080-
// postData.put("modifiedBy", "test@test.com");
2081-
2082-
// List<String> createResponse2 =
2083-
// api.createAttachment(
2084-
// appUrl, serviceName, entityName, facet[0], entityID4, srvpath, postData,
2085-
// tempFile);
2086-
2087-
// String resultMessage = createResponse2.get(0);
2088-
// System.out.println("Result message for attachment " + i + ": " + resultMessage);
2089-
2090-
// if (resultMessage.contains("Only 4 attachments allowed")) {
2091-
// String expectedJson =
2092-
// "{\"error\":{\"code\":\"500\",\"message\":\"Only 4 attachments allowed.\"}}";
2093-
// ObjectMapper objectMapper = new ObjectMapper();
2094-
// JsonNode actualJsonNode = objectMapper.readTree(resultMessage);
2095-
// JsonNode expectedJsonNode = objectMapper.readTree(expectedJson);
2096-
// if (expectedJsonNode.equals(actualJsonNode)) {
2097-
// testStatus = true;
2098-
// }
2099-
// } else {
2100-
// testStatus = false;
2101-
// }
2102-
// tempFile.delete();
2103-
// }
2104-
// if (!testStatus) {
2105-
// fail("5th attachment did not trigger the expected error.");
2106-
// }
2107-
// String deleteEntityResponse = api.deleteEntity(appUrl, serviceName, entityName, entityID4);
2108-
// if (deleteEntityResponse != "Entity Deleted") {
2109-
// fail("Could not delete entity");
2110-
// } else {
2111-
// System.out.println("Successfully deleted the test entity4");
2112-
// }
2113-
// }
2114-
// }
2115-
21162017
}

0 commit comments

Comments
 (0)