Skip to content

Commit

Permalink
Add testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
GomathiselviS committed Feb 9, 2024
1 parent b0a1bfd commit 6de1f93
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 28 deletions.
22 changes: 10 additions & 12 deletions internal/provider/inventory_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,19 @@ func (d *InventoryDataSource) Schema(_ context.Context, _ datasource.SchemaReque
Required: true,
},
"organization": schema.Int64Attribute{
Computed: true,
Description: "Identifier for the organization the inventory should be created in. " +
"If not provided, the inventory will be created in the default organization.",
Computed: true,
Description: "Identifier for the organization to which the inventory belongs",
},
"url": schema.StringAttribute{
Computed: true,
},
"name": schema.StringAttribute{
Computed: true,
Optional: true,
},
"description": schema.StringAttribute{
Computed: true,
Optional: true,
},
"variables": schema.StringAttribute{
Optional: true,
Expand Down Expand Up @@ -87,8 +88,12 @@ func (d *InventoryDataSourceModel) ParseHttpResponse(body []byte) diag.Diagnosti
d.Id = types.Int64Value(apiInventory.Id)
d.Organization = types.Int64Value(apiInventory.Organization)
d.Url = types.StringValue(apiInventory.Url)
d.Name = types.StringValue(apiInventory.Name)

if apiInventory.Name != "" {
d.Name = types.StringValue(apiInventory.Name)
} else {
d.Name = types.StringNull()
}
if apiInventory.Description != "" {
d.Description = types.StringValue(apiInventory.Description)
} else {
Expand All @@ -113,13 +118,7 @@ func (d *InventoryDataSource) Read(ctx context.Context, req datasource.ReadReque
return
}

url, diags := getURL(state.Url.ValueString(), state.Id.String())
diags.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

readResponseBody, diags := d.client.Get(url)
readResponseBody, diags := d.client.Get("api/v2/inventories/" + state.Id.String())
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -136,7 +135,6 @@ func (d *InventoryDataSource) Read(ctx context.Context, req datasource.ReadReque
if resp.Diagnostics.HasError() {
return
}

}

// Configure adds the provider configured client to the data source.
Expand Down
87 changes: 71 additions & 16 deletions internal/provider/inventory_data_source_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,92 @@
package provider

import (
"reflect"
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
fwresource "github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

func TestAddHost(t *testing.T) {
testTable := []struct {
func TestInventoryDataSourceSchema(t *testing.T) {
t.Parallel()

ctx := context.Background()
schemaRequest := fwresource.SchemaRequest{}
schemaResponse := &fwresource.SchemaResponse{}

// Instantiate the InventoryDataSource and call its Schema method
NewInventoryDataSource().Schema(ctx, schemaRequest, schemaResponse)

if schemaResponse.Diagnostics.HasError() {
t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics)
}

// Validate the schema
diagnostics := schemaResponse.Schema.ValidateImplementation(ctx)

if diagnostics.HasError() {
t.Fatalf("Schema validation diagnostics: %+v", diagnostics)
}
}

func TestInventoryDataSourceParseHttpResponse(t *testing.T) {
jsonError := diag.Diagnostics{}
jsonError.AddError("Error parsing JSON response from AAP", "invalid character 'N' looking for beginning of value")

var testTable = []struct {
name string
state InventoryDataSourceModel
input []byte
expected InventoryDataSourceModel
errors diag.Diagnostics
}{
{
name: "add new host",
state: InventoryDataSourceModel{
Id: basetypes.NewInt64Value(1),
Name: types.StringValue("test inventory"),
name: "JSON error",
input: []byte("Not valid JSON"),
expected: InventoryDataSourceModel{},
errors: jsonError,
},
{
name: "missing values",
input: []byte(`{"id":1,"organization":2,"url":"/inventories/1/"}`),
expected: InventoryDataSourceModel{
Id: types.Int64Value(1),
Organization: types.Int64Value(2),
Url: types.StringValue("/inventories/1/"),
Name: types.StringNull(),
Description: types.StringNull(),
Variables: jsontypes.NewNormalizedNull(),
},
errors: diag.Diagnostics{},
},
{
name: "all values",
input: []byte(
`{"id":1,"organization":2,"url":"/inventories/1/","name":"my inventory","description":"My Test Inventory","variables":"{\"foo\":\"bar\"}"}`,
),
expected: InventoryDataSourceModel{
Id: basetypes.NewInt64Value(1),
Name: types.StringValue("test inventory"),
Id: types.Int64Value(1),
Organization: types.Int64Value(2),
Url: types.StringValue("/inventories/1/"),
Name: types.StringValue("my inventory"),
Description: types.StringValue("My Test Inventory"),
Variables: jsontypes.NewNormalizedValue("{\"foo\":\"bar\"}"),
},
errors: diag.Diagnostics{},
},
}

for _, tc := range testTable {
t.Run(tc.name, func(t *testing.T) {
if !reflect.DeepEqual(tc.state, tc.expected) {
t.Errorf("expected (%v)", tc.expected)
t.Errorf("result (%v)", tc.state)
for _, test := range testTable {
t.Run(test.name, func(t *testing.T) {
resource := InventoryDataSourceModel{}
diags := resource.ParseHttpResponse(test.input)
if !test.errors.Equal(diags) {
t.Errorf("Expected error diagnostics (%s), Received (%s)", test.errors, diags)
}
if test.expected != resource {
t.Errorf("Expected (%s) not equal to actual (%s)", test.expected, resource)
}
})
}
Expand Down

0 comments on commit 6de1f93

Please sign in to comment.