From 6de1f93893bd0cde0faaf8fe4ca380ab85cc3a08 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Fri, 9 Feb 2024 15:44:18 -0500 Subject: [PATCH] Add testcases --- internal/provider/inventory_data_source.go | 22 +++-- .../provider/inventory_data_source_test.go | 87 +++++++++++++++---- 2 files changed, 81 insertions(+), 28 deletions(-) diff --git a/internal/provider/inventory_data_source.go b/internal/provider/inventory_data_source.go index 31b5f30..947ef7c 100644 --- a/internal/provider/inventory_data_source.go +++ b/internal/provider/inventory_data_source.go @@ -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, @@ -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 { @@ -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 @@ -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. diff --git a/internal/provider/inventory_data_source_test.go b/internal/provider/inventory_data_source_test.go index 34007ec..6fd09b1 100644 --- a/internal/provider/inventory_data_source_test.go +++ b/internal/provider/inventory_data_source_test.go @@ -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) } }) }