Skip to content

Commit f9b8ab0

Browse files
committed
Update tests
1 parent dcad8a1 commit f9b8ab0

File tree

1 file changed

+47
-66
lines changed

1 file changed

+47
-66
lines changed

sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/GroundingTest.java

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -53,40 +53,6 @@ void getRepositoriesGetAll() {
5353
}
5454
}
5555

56-
@Test
57-
void getCollectionsGetAll() {
58-
final var controller = new GroundingController();
59-
60-
final var result = controller.getAllCollections(JSON_FORMAT);
61-
assertThat(result).isInstanceOf(CollectionsListResponse.class);
62-
final var collectionsList = ((CollectionsListResponse) result).getResources();
63-
final var collectionsCount = ((CollectionsListResponse) result).getCount();
64-
65-
assertThat(collectionsCount).isGreaterThan(0);
66-
for (var collection : collectionsList) {
67-
assertThat(collection.getId()).isNotNull();
68-
assertThat(collection.getTitle()).isNotEmpty();
69-
assertThat(collection.getEmbeddingConfig()).isNotNull();
70-
assertThat(collection.getEmbeddingConfig().getModelName()).isNotNull();
71-
}
72-
}
73-
74-
@Test
75-
void testGetDocuments() {
76-
final var controller = new GroundingController();
77-
78-
UUID collectionId = UUID.fromString("6ebdb977-f9f8-4200-8849-6559b4cff311");
79-
final var result = controller.getDocumentsByCollectionId(collectionId, JSON_FORMAT);
80-
assertThat(result).isInstanceOf(Documents.class);
81-
final var documentsList = ((Documents) result).getResources();
82-
final var documentsCount = ((Documents) result).getCount();
83-
84-
assertThat(documentsCount).isGreaterThan(0);
85-
for (var document : documentsList) {
86-
assertThat(document.getId()).isNotNull();
87-
}
88-
}
89-
9056
@Test
9157
void testCreateDeleteCollection() {
9258
final var controller = new GroundingController();
@@ -96,45 +62,29 @@ void testCreateDeleteCollection() {
9662
final var collectionUuid = UUID.fromString(collectionId);
9763
assertThat(collectionId).isNotNull();
9864

65+
// (1.1) TEST COLLECTION LOOKUP
66+
this.testCollectionsGetAll();
67+
9968
// (2) SANITY CHECK: NO DOCUMENTS
10069
final var documentsEmpty = controller.getDocumentsByCollectionId(collectionUuid, JSON_FORMAT);
10170
assertThat(documentsEmpty).isInstanceOf(Documents.class);
10271
assertThat(((Documents) documentsEmpty).getCount()).isEqualTo(0);
10372
assertThat(((Documents) documentsEmpty).getResources()).isNotNull().isEmpty();
10473

10574
// (3) UPLOAD A DOCUMENT
106-
final var documentCreated = controller.createDocument(collectionUuid, JSON_FORMAT);
107-
assertThat(documentCreated).isInstanceOf(DocumentsListResponse.class);
108-
assertThat(((DocumentsListResponse) documentCreated).getDocuments()).isNotNull().hasSize(1);
75+
final var documentsCreated = controller.createDocument(collectionUuid, JSON_FORMAT);
76+
assertThat(documentsCreated).isInstanceOf(DocumentsListResponse.class);
77+
final var document = ((DocumentsListResponse) documentsCreated).getDocuments();
78+
assertThat(document).isNotNull().hasSize(1);
79+
80+
// (3.1) TEST DOCUMENT LOOKUP
81+
this.testGetDocumentById(collectionUuid, document.get(0).getId());
10982

11083
// (4) SEARCH FOR DOCUMENTS
11184
Object search = controller.searchInDocuments(JSON_FORMAT);
112-
final var dayOfWeek = now().getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
113-
11485
assertThat(search).isInstanceOf(RetievalSearchResults.class);
115-
assertThat(((RetievalSearchResults) search).getResults())
116-
.isNotNull()
117-
.isNotEmpty()
118-
.allSatisfy(
119-
r ->
120-
assertThat(r.getResults())
121-
.isNotNull()
122-
.isNotEmpty()
123-
.allSatisfy(
124-
doc ->
125-
assertThat(doc.getDataRepository().getDocuments())
126-
.isNotNull()
127-
.isNotEmpty()
128-
.allSatisfy(
129-
m ->
130-
assertThat(m.getChunks())
131-
.isNotNull()
132-
.isNotEmpty()
133-
.allSatisfy(
134-
chunk ->
135-
assertThat(chunk.getContent())
136-
.isNotNull()
137-
.contains(dayOfWeek)))));
86+
final var dayOfWeek = now().getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
87+
this.assertDocumentSearchResult((RetievalSearchResults) search, dayOfWeek);
13888

13989
// (5) DELETE COLLECTION
14090
Object deletion = controller.deleteDocuments(collectionUuid, JSON_FORMAT);
@@ -146,12 +96,43 @@ void testCreateDeleteCollection() {
14696
.hasMessageContaining("404 Not Found");
14797
}
14898

149-
@Test
150-
void testGetDocumentById() {
99+
private void assertDocumentSearchResult(RetievalSearchResults search, String dayOfWeek) {
100+
assertThat(search.getResults()).isNotEmpty();
101+
for (var resultsByFilter : search.getResults()) {
102+
assertThat(resultsByFilter.getFilterId()).isEqualTo("question");
103+
assertThat(resultsByFilter.getResults()).isNotEmpty();
104+
for (var result : resultsByFilter.getResults()) {
105+
assertThat(result.getDataRepository().getDocuments()).isNotEmpty();
106+
for (var document : result.getDataRepository().getDocuments()) {
107+
assertThat(document.getChunks()).isNotEmpty();
108+
for (var chunk : document.getChunks()) {
109+
assertThat(chunk.getContent()).contains(dayOfWeek);
110+
}
111+
}
112+
}
113+
}
114+
}
115+
116+
void testCollectionsGetAll() {
117+
final var controller = new GroundingController();
118+
119+
final var result = controller.getAllCollections(JSON_FORMAT);
120+
assertThat(result).isInstanceOf(CollectionsListResponse.class);
121+
final var collectionsList = ((CollectionsListResponse) result).getResources();
122+
final var collectionsCount = ((CollectionsListResponse) result).getCount();
123+
124+
assertThat(collectionsCount).isGreaterThan(0);
125+
for (var collection : collectionsList) {
126+
assertThat(collection.getId()).isNotNull();
127+
assertThat(collection.getTitle()).isNotEmpty();
128+
assertThat(collection.getEmbeddingConfig()).isNotNull();
129+
assertThat(collection.getEmbeddingConfig().getModelName()).isNotNull();
130+
}
131+
}
132+
133+
void testGetDocumentById(UUID collectionId, UUID documentId) {
151134
final var controller = new GroundingController();
152135

153-
UUID collectionId = UUID.fromString("6ebdb977-f9f8-4200-8849-6559b4cff311");
154-
UUID documentId = UUID.fromString("500d1e93-270e-479e-abbd-83a2291e4f6d");
155136
final var result = controller.getDocumentChunksById(collectionId, documentId, JSON_FORMAT);
156137
assertThat(result).isInstanceOf(DocumentResponse.class);
157138
final var chunks = ((DocumentResponse) result).getChunks();

0 commit comments

Comments
 (0)