|
| 1 | +package manifest |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAltIdentifierUnmarshalString(t *testing.T) { |
| 11 | + ai, err := AltIdentifierFromJSON("https://example.com/alt-id") |
| 12 | + require.NoError(t, err) |
| 13 | + require.Equal(t, &AltIdentifier{ |
| 14 | + Value: "https://example.com/alt-id", |
| 15 | + }, ai, "parsed JSON string should be equal to string") |
| 16 | +} |
| 17 | + |
| 18 | +func TestAltIdentifierUnmarshalMinimalJSON(t *testing.T) { |
| 19 | + var ai AltIdentifier |
| 20 | + err := ai.UnmarshalJSON([]byte(`{"value":"https://example.com/alt-id"}`)) |
| 21 | + require.NoError(t, err) |
| 22 | + |
| 23 | + require.Equal(t, &AltIdentifier{ |
| 24 | + Value: "https://example.com/alt-id", |
| 25 | + }, &ai, "parsed JSON object should be equal to AltIdentifier object") |
| 26 | +} |
| 27 | + |
| 28 | +func TestAltIdentifierUnmarshalFullJSON(t *testing.T) { |
| 29 | + var ai AltIdentifier |
| 30 | + err := ai.UnmarshalJSON([]byte(`{ |
| 31 | + "value": "https://example.com/alt-id", |
| 32 | + "scheme": "http://example.com/scheme" |
| 33 | + }`)) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + require.Equal(t, &AltIdentifier{ |
| 37 | + Value: "https://example.com/alt-id", |
| 38 | + Scheme: "http://example.com/scheme", |
| 39 | + }, &ai, "parsed JSON object should be equal to AltIdentifier object") |
| 40 | +} |
| 41 | + |
| 42 | +func TestAltIdentifierUnmarshalNilJSON(t *testing.T) { |
| 43 | + ai, err := AltIdentifierFromJSON(nil) |
| 44 | + require.NoError(t, err) |
| 45 | + require.Nil(t, ai, "should return nil for nil JSON input") |
| 46 | +} |
| 47 | + |
| 48 | +func TestAltIdentifierRequiresValue(t *testing.T) { |
| 49 | + var ai AltIdentifier |
| 50 | + require.Error(t, json.Unmarshal([]byte(`{"scheme":"http://example.com/scheme"}`), &ai), "value is required for AltIdentifier objects") |
| 51 | +} |
| 52 | + |
| 53 | +func TestAltIdentifierFromJSONArray(t *testing.T) { |
| 54 | + var ais []AltIdentifier |
| 55 | + err := json.Unmarshal([]byte(`["id1", {"value":"id2", "scheme":"http://example.com/scheme"}]`), &ais) |
| 56 | + require.NoError(t, err) |
| 57 | + require.Len(t, ais, 2) |
| 58 | + require.Equal(t, []AltIdentifier{ |
| 59 | + {Value: "id1"}, |
| 60 | + {Value: "id2", Scheme: "http://example.com/scheme"}, |
| 61 | + }, ais, "parsed JSON array should match expected AltIdentifier objects") |
| 62 | +} |
| 63 | + |
| 64 | +func TestAltIdentifierUnmarshalNilJSONArray(t *testing.T) { |
| 65 | + ais, err := AltIdentifierFromJSONArray(nil) |
| 66 | + require.NoError(t, err) |
| 67 | + require.Empty(t, ais) |
| 68 | +} |
| 69 | + |
| 70 | +func TestAltIdentifierUnmarshalJSONArrayString(t *testing.T) { |
| 71 | + ais, err := AltIdentifierFromJSONArray("https://example.com/alt-id") |
| 72 | + require.NoError(t, err) |
| 73 | + require.Len(t, ais, 1) |
| 74 | + require.Equal(t, []AltIdentifier{{Value: "https://example.com/alt-id"}}, ais, "parsed JSON string should be converted to single AltIdentifier") |
| 75 | +} |
| 76 | + |
| 77 | +func TestAltIdentifierMinimalJSON(t *testing.T) { |
| 78 | + bin, err := json.Marshal(AltIdentifier{Value: "https://example.com/alt-id"}) |
| 79 | + require.NoError(t, err) |
| 80 | + require.JSONEq(t, string(bin), `"https://example.com/alt-id"`) |
| 81 | +} |
| 82 | + |
| 83 | +func TestAltIdentifierFullJSON(t *testing.T) { |
| 84 | + bin, err := json.Marshal(AltIdentifier{ |
| 85 | + Value: "https://example.com/alt-id", |
| 86 | + Scheme: "http://example.com/scheme", |
| 87 | + }) |
| 88 | + require.NoError(t, err) |
| 89 | + require.JSONEq(t, string(bin), `{"value":"https://example.com/alt-id", "scheme":"http://example.com/scheme"}`) |
| 90 | +} |
| 91 | + |
| 92 | +func TestAltIdentifierJSONArray(t *testing.T) { |
| 93 | + bin, err := json.Marshal([]AltIdentifier{ |
| 94 | + {Value: "https://example.com/alt-id1"}, |
| 95 | + {Value: "https://example.com/alt-id2", Scheme: "http://example.com/scheme"}, |
| 96 | + }) |
| 97 | + require.NoError(t, err) |
| 98 | + require.JSONEq(t, string(bin), `[ |
| 99 | + "https://example.com/alt-id1", |
| 100 | + {"value":"https://example.com/alt-id2", "scheme":"http://example.com/scheme"} |
| 101 | + ]`) |
| 102 | +} |
0 commit comments