Skip to content

Commit

Permalink
fix the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Nov 14, 2024
1 parent 0e9f154 commit 84165ea
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 26 deletions.
15 changes: 11 additions & 4 deletions internal/provider/resources/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,20 @@ func (r *agentResource) Update(
return
}

errorSummary := util.GenerateErrorSummary("update", "uxi_agent")
patchRequest := config_api_client.NewAgentsPatchRequest()
patchRequest.Name = plan.Name.ValueStringPointer()
if !plan.Notes.IsUnknown() {
patchRequest.Notes = plan.Notes.ValueStringPointer()
}
if !plan.PCapMode.IsUnknown() {
patchRequest.PcapMode = plan.PCapMode.ValueStringPointer()
plannedPcapMode := plan.PCapMode.ValueStringPointer()
if !plan.PCapMode.IsUnknown() && plannedPcapMode != nil {
pcapMode, err := config_api_client.NewPcapModeFromValue(*plannedPcapMode)
if err != nil {
resp.Diagnostics.AddError(errorSummary, err.Error())
return
}
patchRequest.PcapMode = pcapMode
}
request := r.client.ConfigurationAPI.
AgentsPatch(ctx, plan.ID.ValueString()).
Expand All @@ -182,7 +189,7 @@ func (r *agentResource) Update(
errorPresent, errorDetail := util.RaiseForStatus(response, err)

if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("update", "uxi_agent"), errorDetail)
resp.Diagnostics.AddError(errorSummary, errorDetail)
return
}

Expand All @@ -193,7 +200,7 @@ func (r *agentResource) Update(
plan.Notes = types.StringValue(*agent.Notes.Get())
}
if agent.PcapMode.Get() != nil {
plan.PCapMode = types.StringValue(*agent.PcapMode.Get())
plan.PCapMode = types.StringValue(string(*agent.PcapMode.Get()))
}

// Set state to fully populated data
Expand Down
17 changes: 14 additions & 3 deletions internal/provider/resources/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,20 @@ func (r *sensorResource) Update(
return
}

errorSummary := util.GenerateErrorSummary("update", "uxi_sensor")
patchRequest := config_api_client.NewSensorsPatchRequest()
patchRequest.Name = plan.Name.ValueStringPointer()
patchRequest.AddressNote = plan.AddressNote.ValueStringPointer()
patchRequest.Notes = plan.Notes.ValueStringPointer()
patchRequest.PcapMode = plan.PCapMode.ValueStringPointer()
plannedPcapMode := plan.PCapMode.ValueStringPointer()
if !plan.PCapMode.IsUnknown() && plannedPcapMode != nil {
pcapMode, err := config_api_client.NewPcapModeFromValue(*plannedPcapMode)
if err != nil {
resp.Diagnostics.AddError(errorSummary, err.Error())
return
}
patchRequest.PcapMode = pcapMode
}

request := r.client.ConfigurationAPI.
SensorsPatch(ctx, plan.ID.ValueString()).
Expand All @@ -183,15 +192,17 @@ func (r *sensorResource) Update(
errorPresent, errorDetail := util.RaiseForStatus(response, err)

if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("update", "uxi_sensor"), errorDetail)
resp.Diagnostics.AddError(errorSummary, errorDetail)
return
}

plan.ID = types.StringValue(sensor.Id)
plan.Name = types.StringValue(sensor.Name)
plan.AddressNote = types.StringPointerValue(sensor.AddressNote.Get())
plan.Notes = types.StringPointerValue(sensor.Notes.Get())
plan.PCapMode = types.StringPointerValue(sensor.PcapMode.Get())
if sensor.PcapMode.Get() != nil {
plan.PCapMode = types.StringValue(string(*sensor.PcapMode.Get()))
}

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
Expand Down
10 changes: 5 additions & 5 deletions pkg/config-api-client/test/api_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestConfigurationAPI(t *testing.T) {
WifiMacAddress: *config_api_client.NewNullableString(&wifiMacAddress),
EthernetMacAddress: *config_api_client.NewNullableString(&ethernetMacAddress),
Notes: *config_api_client.NewNullableString(&notes),
PcapMode: *config_api_client.NewNullableString(&pcapMode),
PcapMode: *config_api_client.NewNullablePcapMode(&pcapMode),
Type: "networking-uxi/agent",
})
})
Expand Down Expand Up @@ -296,7 +296,7 @@ func TestConfigurationAPI(t *testing.T) {
var Longitude float32 = 0.0
var Latitude float32 = 0.0
Notes := "notes"
pcapMode := config_api_client.LIGHT
pcapMode := "light"

require.Nil(t, err)
assert.Equal(t, http.StatusOK, httpRes.StatusCode)
Expand All @@ -313,7 +313,7 @@ func TestConfigurationAPI(t *testing.T) {
Longitude: *config_api_client.NewNullableFloat32(&Longitude),
Latitude: *config_api_client.NewNullableFloat32(&Latitude),
Notes: *config_api_client.NewNullableString(&Notes),
PcapMode: *config_api_client.NewNullablePcapMode(&pcapMode),
PcapMode: *config_api_client.NewNullableString(&pcapMode),
Type: "networking-uxi/sensor",
},
},
Expand Down Expand Up @@ -346,7 +346,7 @@ func TestConfigurationAPI(t *testing.T) {
name := "new_name"
addressNote := "new_address_note"
notes := "new_notes"
pcapMode := "off"
pcapMode := config_api_client.OFF
sensorsPatchRequest := config_api_client.SensorsPatchRequest{
Name: &name,
AddressNote: &addressNote,
Expand Down Expand Up @@ -376,7 +376,7 @@ func TestConfigurationAPI(t *testing.T) {
Longitude: *config_api_client.NewNullableFloat32(&longitude),
Latitude: *config_api_client.NewNullableFloat32(&latitude),
Notes: *config_api_client.NewNullableString(&notes),
PcapMode: *config_api_client.NewNullableString(&pcapMode),
PcapMode: *config_api_client.NewNullablePcapMode(&pcapMode),
Type: "networking-uxi/sensor",
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/mocked/resources/agent_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
resource "uxi_agent" "my_agent_2" {
name = "name_2"
notes = "notes_2"
pcap_mode = "light_2"
pcap_mode = "light"
}
import {
Expand Down
8 changes: 4 additions & 4 deletions test/mocked/resources/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ func TestAgentResource(t *testing.T) {
resource "uxi_agent" "my_agent" {
name = "name_2"
notes = "notes_2"
pcap_mode = "light_2"
pcap_mode = "light"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_agent.my_agent", "name", "name_2"),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "notes", "notes_2"),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "light_2"),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "light"),
),
},
// Delete testing
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestAgentResourceTooManyRequestsHandling(t *testing.T) {
resource "uxi_agent" "my_agent" {
name = "name_2"
notes = "notes_2"
pcap_mode = "light_2"
pcap_mode = "light"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_agent.my_agent", "name", "name_2"),
Expand Down Expand Up @@ -366,7 +366,7 @@ func TestAgentResourceHttpErrorHandling(t *testing.T) {
resource "uxi_agent" "my_agent" {
name = "name_2"
notes = "notes_2"
pcap_mode = "light_2"
pcap_mode = "light"
}`,
ExpectError: regexp.MustCompile(
`(?s)Unable to update agent - pcap_mode must be one the following \['light',\s*'full', 'off'\].\s*DebugID: 12312-123123-123123-1231212`,
Expand Down
2 changes: 1 addition & 1 deletion test/mocked/resources/sensor_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func TestSensorGroupAssignmentResource(t *testing.T) {
name = "name_2"
address_note = "address_note_2"
notes = "notes_2"
pcap_mode = "light_2"
pcap_mode = "light"
}
import {
Expand Down
8 changes: 4 additions & 4 deletions test/mocked/resources/sensor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestSensorResource(t *testing.T) {
name = "name_2"
address_note = "address_note_2"
notes = "notes_2"
pcap_mode = "light_2"
pcap_mode = "light"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "name", "name_2"),
Expand All @@ -120,7 +120,7 @@ func TestSensorResource(t *testing.T) {
"address_note_2",
),
resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "notes", "notes_2"),
resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "pcap_mode", "light_2"),
resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "pcap_mode", "light"),
resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "id", "id"),
),
},
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestSensorResourceTooManyRequestsHandling(t *testing.T) {
name = "name_2"
address_note = "address_note_2"
notes = "notes_2"
pcap_mode = "light_2"
pcap_mode = "light"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "name", "name_2"),
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestSensorResourceHttpErrorHandling(t *testing.T) {
name = "name_2"
address_note = "address_note_2"
notes = "notes_2"
pcap_mode = "light_2"
pcap_mode = "light"
}`,
ExpectError: regexp.MustCompile(
`(?s)Unable to update sensor - pcap_mode must be one the following \['light',\s*'full', 'off'\].\s*DebugID: 12312-123123-123123-1231212`,
Expand Down
8 changes: 4 additions & 4 deletions test/mocked/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func GenerateSensorResponseModel(id string, postfix string) map[string]interface
"longitude": 0.0,
"latitude": 0.0,
"notes": "notes" + postfix,
"pcapMode": "light" + postfix,
"pcapMode": "light",
"type": "networking-uxi/sensor",
}
}
Expand All @@ -29,15 +29,15 @@ func GenerateSensorRequestUpdateModel(postfix string) map[string]interface{} {
"name": "name" + postfix,
"addressNote": "address_note" + postfix,
"notes": "notes" + postfix,
"pcapMode": "light" + postfix,
"pcapMode": "light",
}
}

func GenerateAgentRequestUpdateModel(postfix string) map[string]interface{} {
return map[string]interface{}{
"name": "name" + postfix,
"notes": "notes" + postfix,
"pcapMode": "light" + postfix,
"pcapMode": "light",
}
}

Expand All @@ -50,7 +50,7 @@ func GenerateAgentResponseModel(id string, postfix string) map[string]interface{
"wifiMacAddress": "wifi_mac_address" + postfix,
"ethernetMacAddress": "ethernet_mac_address" + postfix,
"notes": "notes" + postfix,
"pcapMode": "light" + postfix,
"pcapMode": "light",
"type": "networking-uxi/sensor",
}
}
Expand Down

0 comments on commit 84165ea

Please sign in to comment.