|
| 1 | +package edu.kit.datamanager.ro_crate.entities; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 5 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | +import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.NullAndEmptySource; |
| 10 | +import org.junit.jupiter.params.provider.ValueSource; |
| 11 | + |
| 12 | +import java.util.Optional; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | + |
| 16 | +class AbstractEntityTest { |
| 17 | + |
| 18 | + @ParameterizedTest |
| 19 | + @NullAndEmptySource |
| 20 | + @ValueSource(strings = {" ", "\t", "\n"}) |
| 21 | + void mergeIdIntoValue_withInvalidId_returnsEmpty(String invalidId) { |
| 22 | + Optional<JsonNode> result = AbstractEntity.mergeIdIntoValue(invalidId, null); |
| 23 | + assertTrue(result.isEmpty(), "Should return empty Optional for invalid ID"); |
| 24 | + |
| 25 | + Optional<JsonNode> result2 = AbstractEntity.mergeIdIntoValue( |
| 26 | + invalidId, |
| 27 | + MyObjectMapper.getMapper().createObjectNode() |
| 28 | + ); |
| 29 | + assertTrue(result2.isEmpty(), "Should return empty Optional for invalid ID"); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void mergeIdIntoValue_withNullCurrentValue_returnsIdObject() { |
| 34 | + String id = "test-id"; |
| 35 | + Optional<JsonNode> result = AbstractEntity.mergeIdIntoValue(id, null); |
| 36 | + |
| 37 | + // Should return a value for valid ID |
| 38 | + JsonNode node = result.orElseThrow(); |
| 39 | + assertTrue(node.isObject(), "Should return an object"); |
| 40 | + assertEquals(id, node.get("@id").asText(), "Should contain the ID"); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void mergeIdIntoValue_withExistingIdAsString_returnsEmpty() { |
| 45 | + String id = "test-id"; |
| 46 | + JsonNode currentValue = MyObjectMapper.getMapper().valueToTree(id); |
| 47 | + |
| 48 | + Optional<JsonNode> result = AbstractEntity.mergeIdIntoValue(id, currentValue); |
| 49 | + assertTrue(result.isEmpty(), "Should return empty when ID already exists as string"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void mergeIdIntoValue_withExistingIdObject_returnsEmpty() { |
| 54 | + String id = "test-id"; |
| 55 | + ObjectNode currentValue = MyObjectMapper.getMapper().createObjectNode(); |
| 56 | + currentValue.put("@id", id); |
| 57 | + |
| 58 | + Optional<JsonNode> result = AbstractEntity.mergeIdIntoValue(id, currentValue); |
| 59 | + assertTrue(result.isEmpty(), "Should return empty when ID already exists as object"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void mergeIdIntoValue_withNonArrayValue_createsArray() { |
| 64 | + String id = "new-id"; |
| 65 | + String existingId = "existing-id"; |
| 66 | + ObjectNode existingValue = MyObjectMapper.getMapper().createObjectNode(); |
| 67 | + existingValue.put("@id", existingId); |
| 68 | + |
| 69 | + Optional<JsonNode> result = AbstractEntity.mergeIdIntoValue(id, existingValue); |
| 70 | + |
| 71 | + assertTrue(result.isPresent(), "Should return a value"); |
| 72 | + JsonNode node = result.get(); |
| 73 | + assertTrue(node.isArray(), "Should be converted to array"); |
| 74 | + assertEquals(2, node.size(), "Should contain both values"); |
| 75 | + assertEquals(existingId, node.get(0).get("@id").asText(), "Should contain existing ID"); |
| 76 | + assertEquals(id, node.get(1).get("@id").asText(), "Should contain new ID"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void mergeIdIntoValue_withExistingArray_addsToArray() { |
| 81 | + String id = "new-id"; |
| 82 | + String existingId = "existing-id"; |
| 83 | + |
| 84 | + ArrayNode currentValue = MyObjectMapper.getMapper().createArrayNode(); |
| 85 | + ObjectNode existingIdObj = MyObjectMapper.getMapper().createObjectNode(); |
| 86 | + existingIdObj.put("@id", existingId); |
| 87 | + currentValue.add(existingIdObj); |
| 88 | + |
| 89 | + Optional<JsonNode> result = AbstractEntity.mergeIdIntoValue(id, currentValue); |
| 90 | + |
| 91 | + assertTrue(result.isPresent(), "Should return a value"); |
| 92 | + JsonNode node = result.get(); |
| 93 | + assertTrue(node.isArray(), "Should remain an array"); |
| 94 | + assertEquals(2, node.size(), "Should contain both values"); |
| 95 | + assertEquals(existingId, node.get(0).get("@id").asText(), "Should contain existing ID"); |
| 96 | + assertEquals(id, node.get(1).get("@id").asText(), "Should contain new ID"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void mergeIdIntoValue_withExistingArrayContainingId_returnsEmpty() { |
| 101 | + String id = "test-id"; |
| 102 | + ArrayNode currentValue = MyObjectMapper.getMapper().createArrayNode(); |
| 103 | + ObjectNode existingIdObj = MyObjectMapper.getMapper().createObjectNode(); |
| 104 | + existingIdObj.put("@id", id); |
| 105 | + currentValue.add(existingIdObj); |
| 106 | + |
| 107 | + Optional<JsonNode> result = AbstractEntity.mergeIdIntoValue(id, currentValue); |
| 108 | + assertTrue(result.isEmpty(), "Should return empty when ID already exists in array"); |
| 109 | + } |
| 110 | +} |
| 111 | + |
0 commit comments