Skip to content

Commit

Permalink
Merge pull request #425 from deutschebank/spring-bot-master-db
Browse files Browse the repository at this point in the history
Test case for upload file feature
  • Loading branch information
vaibhav-db authored May 21, 2024
2 parents 61916e7 + 386754f commit 845b15f
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -73,48 +74,59 @@ private CompletableFuture<ResultPair<String>> upload(FileConsentCardResponse fil
LOG.info("File upload endpoint : {}", fileConsentCardResponse.getUploadInfo().getUploadUrl());
File filePath = new File(context.get("filepath"));

HttpURLConnection connection = null;
URLConnection connection = null;
try {
URL url = new URL(fileConsentCardResponse.getUploadInfo().getUploadUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Length", Long.toString(filePath.length()));
connection.setRequestProperty("Content-Range",
String.format("bytes 0-%d/%d", filePath.length() - 1, filePath.length()));

try (FileInputStream fileStream = new FileInputStream(filePath);
OutputStream uploadStream = connection.getOutputStream()) {
byte[] buffer = new byte[4096];
int bytes_read;
while ((bytes_read = fileStream.read(buffer)) != -1) {
uploadStream.write(buffer, 0, bytes_read);
if (url.openConnection() instanceof HttpURLConnection) {
connection = (HttpURLConnection) url.openConnection();
((HttpURLConnection) connection).setRequestMethod("PUT");

connection.setDoOutput(true);
connection.setRequestProperty("Content-Length", Long.toString(filePath.length()));
connection.setRequestProperty("Content-Range",
String.format("bytes 0-%d/%d", filePath.length() - 1, filePath.length()));

try (FileInputStream fileStream = new FileInputStream(filePath);
OutputStream uploadStream = connection.getOutputStream()) {
byte[] buffer = new byte[4096];
int bytes_read;
while ((bytes_read = fileStream.read(buffer)) != -1) {
uploadStream.write(buffer, 0, bytes_read);
}

uploadStream.flush();
}

uploadStream.flush();
}

try {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
LOG.info(inputLine);
in.close();
} catch (Exception e) {
LOG.error("Exception occured while reading steam.. ignore this error " + e);
try {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
LOG.info(inputLine);
in.close();
} catch (Exception e) {
LOG.error("Exception occured while reading steam.. ignore this error " + e);
}
}
result.set(new ResultPair<String>(true, null));

} catch (Throwable t) {
t.printStackTrace();
result.set(new ResultPair<String>(false, t.getLocalizedMessage()));
} finally {
if (connection != null) {
connection.disconnect();

if (connection instanceof HttpURLConnection) {
((HttpURLConnection) connection).disconnect();
}
}
}
}).thenApply(aVoid -> result.get());
}

private CompletableFuture<Void> fileUploadFailed(TurnContext turnContext, String error) {

LOG.info("fileUploadFailed called with error {}" , error);

Activity reply = MessageFactory.text("<b>File upload failed.</b> Error: <pre>" + error + "</pre>");
reply.setTextFormat(TextFormatTypes.XML);
return turnContext.sendActivityBlind(reply);
Expand All @@ -129,6 +141,9 @@ private CompletableFuture<Void> fileDownloadCompleted(TurnContext turnContext, A

private CompletableFuture<Void> fileUploadCompleted(TurnContext turnContext,
FileConsentCardResponse fileConsentCardResponse) {

LOG.info("file Upload Completed with unique id {} ", fileConsentCardResponse.getUploadInfo().getUniqueId());

FileInfoCard downloadCard = new FileInfoCard();
downloadCard.setUniqueId(fileConsentCardResponse.getUploadInfo().getUniqueId());
downloadCard.setFileType(fileConsentCardResponse.getUploadInfo().getFileType());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
package org.finos.springbot.teams.state;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.finos.springbot.teams.TeamsException;

public class FileStateStorageUtility {

public static Set<File> getAllDataFiles(String filePath) {
Set<File> fileList = new HashSet<>();
getAllAddressableFiles(new File(filePath), fileList);
return fileList;
}

private static void getAllAddressableFiles(File node, Set<File> fileList) {
if (node.isDirectory()) {
String[] subNote = node.list();
for (String fileName : subNote) {
File dir = new File(node, fileName);
if (dir.isDirectory()) {
getAllAddressableFiles(dir, fileList);
} else {
if (dir.getParentFile().getName().equals(FileStateStorage.DATA_FOLDER)
&& dir.getName().equals(TeamsStateStorage.ADDRESSABLE_KEY + FileStateStorage.FILE_EXT)) {
fileList.add(dir);
}
}
}
}
}

public static Optional<String> readFile(String filePath) {
try {
Path path = Paths.get(filePath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.finos.springbot.teams.controller;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import org.finos.springbot.teams.MockTeamsConfiguration;
import org.finos.springbot.teams.bot.BotController;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.microsoft.bot.builder.Bot;
import com.microsoft.bot.builder.InvokeResponse;
import com.microsoft.bot.integration.BotFrameworkHttpAdapter;
import com.microsoft.bot.schema.Activity;

@SpringBootTest(classes = { MockTeamsConfiguration.class})
@ActiveProfiles("teams")
@ExtendWith(SpringExtension.class)
public class BotControllerTest {

@Mock
BotFrameworkHttpAdapter adapter;

@Spy
Bot bot;

@InjectMocks
BotController controller;


@SuppressWarnings({ "deprecation" })
@Test
public void testIncoming() throws InterruptedException, ExecutionException {
Activity a = Mockito.mock(Activity.class);
InvokeResponse ir = new InvokeResponse(HttpStatus.OK.value(), "Success");
Mockito.when(adapter.processIncomingActivity("any text", a, bot)).thenReturn(CompletableFuture.completedFuture(ir));
CompletableFuture<ResponseEntity<Object>> future = controller.incoming(a, "any text");

ResponseEntity<Object> entity = future.get();

Assertions.assertEquals(200, entity.getStatusCodeValue());
Assertions.assertEquals("Success", entity.getBody());

}

}
Loading

0 comments on commit 845b15f

Please sign in to comment.