|
| 1 | +package com.box.sdk; |
| 2 | + |
| 3 | +import static com.box.sdk.BoxApiProvider.jwtApiForServiceAccount; |
| 4 | +import static com.box.sdk.CleanupTools.deleteFile; |
| 5 | +import static com.box.sdk.Retry.retry; |
| 6 | +import static com.box.sdk.UniqueTestFolder.removeUniqueFolder; |
| 7 | +import static com.box.sdk.UniqueTestFolder.setupUniqeFolder; |
| 8 | +import static com.box.sdk.UniqueTestFolder.uploadFileToUniqueFolder; |
| 9 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 10 | +import static org.hamcrest.Matchers.containsString; |
| 11 | +import static org.hamcrest.Matchers.equalTo; |
| 12 | +import static org.hamcrest.Matchers.is; |
| 13 | + |
| 14 | +import java.text.ParseException; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.Date; |
| 18 | +import java.util.List; |
| 19 | +import org.junit.AfterClass; |
| 20 | +import org.junit.BeforeClass; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * {@link BoxGroup} related integration tests. |
| 26 | + */ |
| 27 | +public class BoxAIIT { |
| 28 | + |
| 29 | + @BeforeClass |
| 30 | + public static void setup() { |
| 31 | + setupUniqeFolder(); |
| 32 | + } |
| 33 | + |
| 34 | + @AfterClass |
| 35 | + public static void afterClass() { |
| 36 | + removeUniqueFolder(); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + @Test |
| 41 | + public void askAISingleItem() throws InterruptedException { |
| 42 | + BoxAPIConnection api = jwtApiForServiceAccount(); |
| 43 | + String fileName = "[askAISingleItem] Test File.txt"; |
| 44 | + BoxFile uploadedFile = uploadFileToUniqueFolder(api, fileName, "Test file"); |
| 45 | + |
| 46 | + try { |
| 47 | + BoxFile.Info uploadedFileInfo = uploadedFile.getInfo(); |
| 48 | + // When a file has been just uploaded, AI service may not be ready to return text response |
| 49 | + // and 412 is returned |
| 50 | + retry(() -> { |
| 51 | + BoxAIResponse response = BoxAI.sendAIRequest( |
| 52 | + api, |
| 53 | + "What is the name of the file?", |
| 54 | + Collections.singletonList(new BoxAIItem(uploadedFileInfo.getID(), BoxAIItem.Type.FILE)), |
| 55 | + BoxAI.Mode.SINGLE_ITEM_QA |
| 56 | + ); |
| 57 | + assertThat(response.getAnswer(), containsString("Test file")); |
| 58 | + assert response.getCreatedAt().before(new Date(System.currentTimeMillis())); |
| 59 | + assertThat(response.getCompletionReason(), equalTo("done")); |
| 60 | + }, 2, 2000); |
| 61 | + |
| 62 | + } finally { |
| 63 | + deleteFile(uploadedFile); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void askAIMultipleItems() throws InterruptedException { |
| 69 | + BoxAPIConnection api = jwtApiForServiceAccount(); |
| 70 | + String fileName1 = "[askAIMultipleItems] Test File.txt"; |
| 71 | + BoxFile uploadedFile1 = uploadFileToUniqueFolder(api, fileName1, "Test file"); |
| 72 | + |
| 73 | + try { |
| 74 | + String fileName2 = "[askAIMultipleItems] Weather forecast.txt"; |
| 75 | + BoxFile uploadedFile2 = uploadFileToUniqueFolder(api, fileName2, "Test file"); |
| 76 | + |
| 77 | + try { |
| 78 | + BoxFile.Info uploadedFileInfo1 = uploadedFile1.getInfo(); |
| 79 | + BoxFile.Info uploadedFileInfo2 = uploadedFile2.getInfo(); |
| 80 | + |
| 81 | + List<BoxAIItem> items = new ArrayList<>(); |
| 82 | + items.add(new BoxAIItem(uploadedFileInfo1.getID(), BoxAIItem.Type.FILE)); |
| 83 | + items.add(new BoxAIItem(uploadedFileInfo2.getID(), BoxAIItem.Type.FILE)); |
| 84 | + |
| 85 | + // When a file has been just uploaded, AI service may not be ready to return text response |
| 86 | + // and 412 is returned |
| 87 | + retry(() -> { |
| 88 | + BoxAIResponse response = BoxAI.sendAIRequest( |
| 89 | + api, |
| 90 | + "What is the content of these files?", |
| 91 | + items, |
| 92 | + BoxAI.Mode.MULTIPLE_ITEM_QA |
| 93 | + ); |
| 94 | + assertThat(response.getAnswer(), containsString("Test file")); |
| 95 | + assert response.getCreatedAt().before(new Date(System.currentTimeMillis())); |
| 96 | + assertThat(response.getCompletionReason(), equalTo("done")); |
| 97 | + }, 2, 2000); |
| 98 | + } finally { |
| 99 | + deleteFile(uploadedFile2); |
| 100 | + } |
| 101 | + |
| 102 | + } finally { |
| 103 | + deleteFile(uploadedFile1); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void askAITextGenItemWithDialogueHistory() throws ParseException, InterruptedException { |
| 109 | + BoxAPIConnection api = jwtApiForServiceAccount(); |
| 110 | + String fileName = "[askAITextGenItemWithDialogueHistory] Test File.txt"; |
| 111 | + Date date1 = BoxDateFormat.parse("2013-05-16T15:27:57-07:00"); |
| 112 | + Date date2 = BoxDateFormat.parse("2013-05-16T15:26:57-07:00"); |
| 113 | + |
| 114 | + BoxFile uploadedFile = uploadFileToUniqueFolder(api, fileName, "Test file"); |
| 115 | + try { |
| 116 | + // When a file has been just uploaded, AI service may not be ready to return text response |
| 117 | + // and 412 is returned |
| 118 | + retry(() -> { |
| 119 | + BoxFile.Info uploadedFileInfo = uploadedFile.getInfo(); |
| 120 | + assertThat(uploadedFileInfo.getName(), is(equalTo(fileName))); |
| 121 | + |
| 122 | + List<BoxAIDialogueEntry> dialogueHistory = new ArrayList<>(); |
| 123 | + dialogueHistory.add( |
| 124 | + new BoxAIDialogueEntry("What is the name of the file?", "Test file", date1) |
| 125 | + ); |
| 126 | + dialogueHistory.add( |
| 127 | + new BoxAIDialogueEntry("What is the size of the file?", "10kb", date2) |
| 128 | + ); |
| 129 | + BoxAIResponse response = BoxAI.sendAITextGenRequest( |
| 130 | + api, |
| 131 | + "What is the name of the file?", |
| 132 | + Collections.singletonList(new BoxAIItem(uploadedFileInfo.getID(), BoxAIItem.Type.FILE)), |
| 133 | + dialogueHistory |
| 134 | + ); |
| 135 | + assertThat(response.getAnswer(), containsString("name")); |
| 136 | + assert response.getCreatedAt().before(new Date(System.currentTimeMillis())); |
| 137 | + assertThat(response.getCompletionReason(), equalTo("done")); |
| 138 | + }, 2, 2000); |
| 139 | + |
| 140 | + } finally { |
| 141 | + deleteFile(uploadedFile); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments