|
1 | 1 | package edu.harvard.iq.dataverse.api;
|
2 | 2 |
|
| 3 | +import edu.harvard.iq.dataverse.ControlledVocabularyValueServiceBean; |
| 4 | +import edu.harvard.iq.dataverse.DatasetFieldServiceBean; |
| 5 | +import edu.harvard.iq.dataverse.DataverseServiceBean; |
| 6 | +import edu.harvard.iq.dataverse.MetadataBlockServiceBean; |
| 7 | +import edu.harvard.iq.dataverse.actionlogging.ActionLogServiceBean; |
3 | 8 | import edu.harvard.iq.dataverse.util.BundleUtil;
|
| 9 | +import jakarta.json.Json; |
| 10 | +import jakarta.json.JsonObject; |
| 11 | +import jakarta.json.JsonReader; |
| 12 | +import jakarta.ws.rs.core.Response; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
4 | 14 | import org.junit.jupiter.api.Test;
|
| 15 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.junit.jupiter.MockitoExtension; |
5 | 18 |
|
| 19 | +import java.io.File; |
| 20 | +import java.io.StringReader; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.nio.file.Paths; |
6 | 23 | import java.util.ArrayList;
|
7 | 24 | import java.util.List;
|
8 | 25 |
|
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
9 | 27 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
10 | 28 |
|
| 29 | +@ExtendWith(MockitoExtension.class) |
11 | 30 | public class DatasetFieldServiceApiTest {
|
12 | 31 |
|
| 32 | + @Mock |
| 33 | + private ActionLogServiceBean actionLogSvc; |
| 34 | + |
| 35 | + @Mock |
| 36 | + private MetadataBlockServiceBean metadataBlockService; |
| 37 | + |
| 38 | + @Mock |
| 39 | + private DataverseServiceBean dataverseService; |
| 40 | + |
| 41 | + @Mock |
| 42 | + private DatasetFieldServiceBean datasetFieldService; |
| 43 | + |
| 44 | + @Mock |
| 45 | + private ControlledVocabularyValueServiceBean controlledVocabularyValueService; |
| 46 | + |
| 47 | + private DatasetFieldServiceApi api; |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + public void setup(){ |
| 51 | + api = new DatasetFieldServiceApi(); |
| 52 | + api.actionLogSvc = actionLogSvc; |
| 53 | + api.metadataBlockService = metadataBlockService; |
| 54 | + api.dataverseService = dataverseService; |
| 55 | + api.datasetFieldService = datasetFieldService; |
| 56 | + api.controlledVocabularyValueService = controlledVocabularyValueService; |
| 57 | + } |
| 58 | + |
13 | 59 | @Test
|
14 | 60 | public void testArrayIndexOutOfBoundMessageBundle() {
|
15 | 61 | List<String> arguments = new ArrayList<>();
|
@@ -59,4 +105,41 @@ public void testGetGeneralErrorMessage() {
|
59 | 105 | message
|
60 | 106 | );
|
61 | 107 | }
|
| 108 | + |
| 109 | + @Test |
| 110 | + public void testGetGeneralErrorMessageEmptyHeader() { |
| 111 | + DatasetFieldServiceApi api = new DatasetFieldServiceApi(); |
| 112 | + String message = api.getGeneralErrorMessage(null, 5, "some error"); |
| 113 | + assertEquals( |
| 114 | + "Error parsing metadata block in unknown part, line #5: some error", |
| 115 | + message |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testLoadDatasetFieldsWhitespaceTrimming() { |
| 121 | + |
| 122 | + Path resourceDirectory = Paths.get("src/test/resources/tsv/whitespace-test.tsv"); |
| 123 | + File testfile = new File(resourceDirectory.toFile().getAbsolutePath()); |
| 124 | + JsonReader jsonReader; |
| 125 | + try (Response response = api.loadDatasetFields(testfile)) { |
| 126 | + assertEquals(200, response.getStatus()); |
| 127 | + jsonReader = Json.createReader(new StringReader(response.getEntity().toString())); |
| 128 | + } |
| 129 | + JsonObject jsonObject = jsonReader.readObject(); |
| 130 | + |
| 131 | + final List<String> metadataNames = jsonObject.getJsonObject("data").getJsonArray("added") |
| 132 | + .getValuesAs(e -> e.asJsonObject().getString("name")); |
| 133 | + assertThat(metadataNames).contains("whitespaceDemo") |
| 134 | + .contains("whitespaceDemoOne") |
| 135 | + .contains("whitespaceDemoTwo") |
| 136 | + .contains("whitespaceDemoThree") |
| 137 | + .contains("CV1") |
| 138 | + .contains("CV2") |
| 139 | + .contains("CV3"); |
| 140 | + assertThat(metadataNames).doesNotContain(" whitespaceDemo") |
| 141 | + .doesNotContain("whitespaceDemoOne ") |
| 142 | + .doesNotContain("CV1 ") |
| 143 | + .doesNotContain(" CV2"); |
| 144 | + } |
62 | 145 | }
|
0 commit comments