|
1 | 1 | package edu.harvard.iq.dataverse; |
2 | 2 |
|
3 | 3 | 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; |
4 | 7 |
|
5 | 8 | import java.io.File; |
6 | 9 | import java.io.IOException; |
|
17 | 20 |
|
18 | 21 | import edu.harvard.iq.dataverse.settings.SettingsServiceBean; |
19 | 22 | import jakarta.json.Json; |
| 23 | +import jakarta.json.JsonArray; |
| 24 | +import jakarta.json.JsonArrayBuilder; |
20 | 25 | import jakarta.json.JsonObject; |
| 26 | +import jakarta.json.JsonObjectBuilder; |
21 | 27 |
|
22 | 28 | public class DatasetFieldServiceBeanTest { |
23 | 29 |
|
@@ -152,6 +158,57 @@ void getIndexableStringsByTermUriOrcid() throws IOException { |
152 | 158 | assertEquals(Collections.emptySet(), result); |
153 | 159 | } |
154 | 160 |
|
| 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 | + |
155 | 212 | /** |
156 | 213 | * Prepare unit tests with mock methods. |
157 | 214 | * |
|
0 commit comments