2023-05-02 11:33:06 -04:00
|
|
|
// Copyright IBM Corp. 2014, 2026
|
2023-08-10 18:43:27 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-02 11:33:06 -04:00
|
|
|
|
2019-02-25 16:32:47 -05:00
|
|
|
package jsonprovider
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
2021-05-17 15:17:09 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2019-02-25 16:32:47 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestMarshalAttribute(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
Input *configschema.Attribute
|
2023-01-09 04:48:23 -05:00
|
|
|
Want *Attribute
|
2019-02-25 16:32:47 -05:00
|
|
|
}{
|
|
|
|
|
{
|
2026-02-20 11:05:53 -05:00
|
|
|
&configschema.Attribute{
|
|
|
|
|
Type: cty.String,
|
|
|
|
|
Optional: true,
|
|
|
|
|
Computed: true,
|
|
|
|
|
Deprecated: true,
|
|
|
|
|
DeprecationMessage: "This is a deprecated attribute",
|
|
|
|
|
},
|
2023-01-09 04:48:23 -05:00
|
|
|
&Attribute{
|
2026-02-20 11:05:53 -05:00
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
|
Optional: true,
|
|
|
|
|
Computed: true,
|
|
|
|
|
DescriptionKind: "plain",
|
|
|
|
|
Deprecated: true,
|
|
|
|
|
DeprecationMessage: "This is a deprecated attribute",
|
2019-02-25 16:32:47 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ // collection types look a little odd.
|
2024-11-12 08:06:16 -05:00
|
|
|
&configschema.Attribute{Type: cty.Map(cty.String), Optional: true, Computed: true, WriteOnly: true},
|
2023-01-09 04:48:23 -05:00
|
|
|
&Attribute{
|
2020-03-05 19:53:24 -05:00
|
|
|
AttributeType: json.RawMessage(`["map","string"]`),
|
|
|
|
|
Optional: true,
|
|
|
|
|
Computed: true,
|
2024-11-12 08:06:16 -05:00
|
|
|
WriteOnly: true,
|
2020-03-05 19:53:24 -05:00
|
|
|
DescriptionKind: "plain",
|
2019-02-25 16:32:47 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
got := marshalAttribute(test.Input)
|
|
|
|
|
if !cmp.Equal(got, test.Want) {
|
|
|
|
|
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, test.Want))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-12 04:53:03 -04:00
|
|
|
|
|
|
|
|
func TestMarshalIdentityAttribute(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
Input *configschema.Attribute
|
|
|
|
|
Want *IdentityAttribute
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
&configschema.Attribute{Type: cty.String, Optional: true},
|
|
|
|
|
&IdentityAttribute{
|
|
|
|
|
IdentityType: json.RawMessage(`"string"`),
|
|
|
|
|
OptionalForImport: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ // collection types look a little odd.
|
|
|
|
|
&configschema.Attribute{Type: cty.List(cty.String), Required: true},
|
|
|
|
|
&IdentityAttribute{
|
|
|
|
|
IdentityType: json.RawMessage(`["list","string"]`),
|
|
|
|
|
RequiredForImport: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
got := marshalIdentityAttribute(test.Input)
|
|
|
|
|
if !cmp.Equal(got, test.Want) {
|
|
|
|
|
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, test.Want))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|