Skip to content

Commit 81aa56b

Browse files
committed
test per review
1 parent d8ba1f6 commit 81aa56b

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/test/java/edu/harvard/iq/dataverse/DatasetFieldServiceBeanTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package edu.harvard.iq.dataverse;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
47

58
import java.io.File;
69
import java.io.IOException;
@@ -17,7 +20,10 @@
1720

1821
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
1922
import jakarta.json.Json;
23+
import jakarta.json.JsonArray;
24+
import jakarta.json.JsonArrayBuilder;
2025
import jakarta.json.JsonObject;
26+
import jakarta.json.JsonObjectBuilder;
2127

2228
public class DatasetFieldServiceBeanTest {
2329

@@ -152,6 +158,57 @@ void getIndexableStringsByTermUriOrcid() throws IOException {
152158
assertEquals(Collections.emptySet(), result);
153159
}
154160

161+
@Test
162+
public void testProcessPathSegmentWithArrayStringMatching() {
163+
// Create a DatasetFieldServiceBean instance
164+
DatasetFieldServiceBean bean = new DatasetFieldServiceBean();
165+
166+
String termUri = "http://example.org/term/123";
167+
168+
// Create a JSON structure with an array containing string values
169+
JsonArrayBuilder tagsArrayBuilder = Json.createArrayBuilder();
170+
JsonObject testObject = Json.createObjectBuilder().add("tag", Json.createArrayBuilder().add("research").add("science")).add("name", "one").build();
171+
tagsArrayBuilder.add(testObject);
172+
JsonObject testObject2 = Json.createObjectBuilder().add("tag", "art").add("name", "two").build();
173+
tagsArrayBuilder.add(testObject2);
174+
JsonObject testObject3 = Json.createObjectBuilder().add("tag", termUri).add("name", "three").build();
175+
tagsArrayBuilder.add(testObject3);
176+
JsonArray tags = tagsArrayBuilder.build();
177+
178+
// Test case 1: Match a string in an array of strings
179+
String[] pathParts = { "tag=research", "name" };
180+
Object result = bean.processPathSegment(0, pathParts, tags, termUri);
181+
182+
// The method should return the test object when a match is found
183+
// Result should not be null when a match is found
184+
assertNotNull(result);
185+
assertEquals("one", result, "Result should be the original test object");
186+
187+
// Test case 2: Match a string
188+
pathParts[0] = "tag=art";
189+
result = bean.processPathSegment(0, pathParts, tags, termUri);
190+
191+
// The method should return the test object when a match is found
192+
// Result should not be null when a match is found
193+
assertNotNull(result);
194+
assertEquals("two", result, "Result should be the original test object");
195+
196+
// Test case 3: No match in the array
197+
String[] noMatchPathParts = { "tags=nonexistent" };
198+
Object noMatchResult = bean.processPathSegment(0, noMatchPathParts, testObject, termUri);
199+
200+
// The method should return null when no match is found
201+
assertNull(noMatchResult, "Result should be null when no match is found");
202+
203+
// Test case 4: Match with @id substitution
204+
205+
String[] idPathParts = { "tag=@id", "name" };
206+
Object idResult = bean.processPathSegment(0, idPathParts, tags, termUri);
207+
208+
assertNotNull(idResult, "Result should not be null when matching with @id");
209+
assertEquals("three", idResult, "Result should be the original test object");
210+
}
211+
155212
/**
156213
* Prepare unit tests with mock methods.
157214
*

0 commit comments

Comments
 (0)