Skip to content

Commit

Permalink
Update test parsing to ensure ID is present (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjumani authored Jun 13, 2022
1 parent f8fbd00 commit 396c624
Show file tree
Hide file tree
Showing 57 changed files with 949 additions and 225 deletions.
4 changes: 4 additions & 0 deletions cloudstack/DiskOfferingService.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@ func (s *DiskOfferingService) CreateDiskOffering(p *CreateDiskOfferingParams) (*
return nil, err
}

if resp, err = getRawValue(resp); err != nil {
return nil, err
}

var r CreateDiskOfferingResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions cloudstack/HostService.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,10 @@ func (s *HostService) AddHost(p *AddHostParams) (*AddHostResponse, error) {
return nil, err
}

if resp, err = getRawValue(resp); err != nil {
return nil, err
}

var r AddHostResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions cloudstack/ISOService.go
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,10 @@ func (s *ISOService) RegisterIso(p *RegisterIsoParams) (*RegisterIsoResponse, er
return nil, err
}

if resp, err = getRawValue(resp); err != nil {
return nil, err
}

var r RegisterIsoResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions cloudstack/KubernetesService.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ func (s *KubernetesService) AddKubernetesSupportedVersion(p *AddKubernetesSuppor
return nil, err
}

if resp, err = getRawValue(resp); err != nil {
return nil, err
}

var r AddKubernetesSupportedVersionResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
Expand Down
27 changes: 23 additions & 4 deletions cloudstack/cloudstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"strings"
"time"

"github.com/golang/mock/gomock"
gomock "github.com/golang/mock/gomock"
)

// UnlimitedResourceID is a special ID to define an unlimited resource
Expand Down Expand Up @@ -533,14 +533,33 @@ func encodeValues(v url.Values) string {
return buf.String()
}

// Generic function to get the first raw value from a response as json.RawMessage
// Generic function to get the first non-count raw value from a response as json.RawMessage
func getRawValue(b json.RawMessage) (json.RawMessage, error) {
var m map[string]json.RawMessage
if err := json.Unmarshal(b, &m); err != nil {
return nil, err
}
for _, v := range m {
return v, nil
getArrayResponse := false
for k := range m {
if k == "count" {
getArrayResponse = true
}
}
if getArrayResponse {
var resp []json.RawMessage
for k, v := range m {
if k != "count" {
if err := json.Unmarshal(v, &resp); err != nil {
return nil, err
}
return resp[0], nil
}
}

} else {
for _, v := range m {
return v, nil
}
}
return nil, fmt.Errorf("Unable to extract the raw value from:\n\n%s\n\n", string(b))
}
Expand Down
50 changes: 45 additions & 5 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,14 +584,33 @@ func (as *allServices) GeneralCode() ([]byte, error) {
pn(" return buf.String()")
pn("}")
pn("")
pn("// Generic function to get the first raw value from a response as json.RawMessage")
pn("// Generic function to get the first non-count raw value from a response as json.RawMessage")
pn("func getRawValue(b json.RawMessage) (json.RawMessage, error) {")
pn(" var m map[string]json.RawMessage")
pn(" if err := json.Unmarshal(b, &m); err != nil {")
pn(" return nil, err")
pn(" }")
pn(" for _, v := range m {")
pn(" return v, nil")
pn(" getArrayResponse := false")
pn(" for k := range m {")
pn(" if k == \"count\" {")
pn(" getArrayResponse = true")
pn(" }")
pn(" }")
pn(" if getArrayResponse {")
pn(" var resp []json.RawMessage")
pn(" for k, v := range m {")
pn(" if k != \"count\" {")
pn(" if err := json.Unmarshal(v, &resp); err != nil {")
pn(" return nil, err")
pn(" }")
pn(" return resp[0], nil")
pn(" }")
pn(" }")
pn("")
pn(" } else {")
pn(" for _, v := range m {")
pn(" return v, nil")
pn(" }")
pn(" }")
pn(" return nil, fmt.Errorf(\"Unable to extract the raw value from:\\n\\n%%s\\n\\n\", string(b))")
pn("}")
Expand Down Expand Up @@ -1068,10 +1087,27 @@ func (s *service) generateAPITest(a *API) {
}
}
pn(")")
pn(" _, err := client.%s.%s(p)", strings.TrimSuffix(s.name, "Service"), capitalize(a.Name))
idPresent := false
if !(strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate") {
for _, ap := range a.Response {
if ap.Name == "id" && ap.Type == "string" {
pn(" r, err := client.%s.%s(p)", strings.TrimSuffix(s.name, "Service"), capitalize(a.Name))
idPresent = true
break
}
}
}
if !idPresent {
pn(" _, err := client.%s.%s(p)", strings.TrimSuffix(s.name, "Service"), capitalize(a.Name))
}
pn(" if err != nil {")
pn(" t.Errorf(err.Error())")
pn(" }")
if idPresent {
pn(" if r.Id == \"\" {")
pn(" t.Errorf(\"Failed to parse response. ID not found\")")
pn(" }")
}
pn(" }")
pn(" t.Run(\"%s\", test%s)", capitalize(a.Name), a.Name)
pn("")
Expand Down Expand Up @@ -1624,7 +1660,11 @@ func (s *service) generateNewAPICallFunc(a *API) {
"RegisterUserKeys",
"GetUserKeys",
"AddAnnotation",
"RemoveAnnotation":
"RemoveAnnotation",
"AddKubernetesSupportedVersion",
"CreateDiskOffering",
"AddHost",
"RegisterIso":
pn(" if resp, err = getRawValue(resp); err != nil {")
pn(" return nil, err")
pn(" }")
Expand Down
30 changes: 24 additions & 6 deletions test/AccountService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ func TestAccountService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Account.NewCreateAccountParams("email", "firstname", "lastname", "password", "username")
_, err := client.Account.CreateAccount(p)
r, err := client.Account.CreateAccount(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("CreateAccount", testcreateAccount)

Expand All @@ -64,10 +67,13 @@ func TestAccountService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Account.NewDisableAccountParams(true)
_, err := client.Account.DisableAccount(p)
r, err := client.Account.DisableAccount(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("DisableAccount", testdisableAccount)

Expand All @@ -76,10 +82,13 @@ func TestAccountService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Account.NewEnableAccountParams()
_, err := client.Account.EnableAccount(p)
r, err := client.Account.EnableAccount(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("EnableAccount", testenableAccount)

Expand Down Expand Up @@ -124,10 +133,13 @@ func TestAccountService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Account.NewLockAccountParams("account", "domainid")
_, err := client.Account.LockAccount(p)
r, err := client.Account.LockAccount(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("LockAccount", testlockAccount)

Expand All @@ -136,10 +148,13 @@ func TestAccountService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Account.NewMarkDefaultZoneForAccountParams("account", "domainid", "zoneid")
_, err := client.Account.MarkDefaultZoneForAccount(p)
r, err := client.Account.MarkDefaultZoneForAccount(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("MarkDefaultZoneForAccount", testmarkDefaultZoneForAccount)

Expand All @@ -148,10 +163,13 @@ func TestAccountService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Account.NewUpdateAccountParams()
_, err := client.Account.UpdateAccount(p)
r, err := client.Account.UpdateAccount(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("UpdateAccount", testupdateAccount)

Expand Down
10 changes: 8 additions & 2 deletions test/AddressService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ func TestAddressService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Address.NewAssociateIpAddressParams()
_, err := client.Address.AssociateIpAddress(p)
r, err := client.Address.AssociateIpAddress(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("AssociateIpAddress", testassociateIpAddress)

Expand Down Expand Up @@ -76,10 +79,13 @@ func TestAddressService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Address.NewUpdateIpAddressParams("id")
_, err := client.Address.UpdateIpAddress(p)
r, err := client.Address.UpdateIpAddress(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("UpdateIpAddress", testupdateIpAddress)

Expand Down
10 changes: 8 additions & 2 deletions test/AffinityGroupService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ func TestAffinityGroupService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.AffinityGroup.NewCreateAffinityGroupParams("name", "type")
_, err := client.AffinityGroup.CreateAffinityGroup(p)
r, err := client.AffinityGroup.CreateAffinityGroup(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("CreateAffinityGroup", testcreateAffinityGroup)

Expand Down Expand Up @@ -88,10 +91,13 @@ func TestAffinityGroupService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.AffinityGroup.NewUpdateVMAffinityGroupParams("id")
_, err := client.AffinityGroup.UpdateVMAffinityGroup(p)
r, err := client.AffinityGroup.UpdateVMAffinityGroup(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("UpdateVMAffinityGroup", testupdateVMAffinityGroup)

Expand Down
15 changes: 12 additions & 3 deletions test/AnnotationService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ func TestAnnotationService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Annotation.NewAddAnnotationParams()
_, err := client.Annotation.AddAnnotation(p)
r, err := client.Annotation.AddAnnotation(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("AddAnnotation", testaddAnnotation)

Expand All @@ -64,10 +67,13 @@ func TestAnnotationService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Annotation.NewRemoveAnnotationParams("id")
_, err := client.Annotation.RemoveAnnotation(p)
r, err := client.Annotation.RemoveAnnotation(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("RemoveAnnotation", testremoveAnnotation)

Expand All @@ -76,10 +82,13 @@ func TestAnnotationService(t *testing.T) {
t.Skipf("Skipping as no json response is provided in testdata")
}
p := client.Annotation.NewUpdateAnnotationVisibilityParams(true, "id")
_, err := client.Annotation.UpdateAnnotationVisibility(p)
r, err := client.Annotation.UpdateAnnotationVisibility(p)
if err != nil {
t.Errorf(err.Error())
}
if r.Id == "" {
t.Errorf("Failed to parse response. ID not found")
}
}
t.Run("UpdateAnnotationVisibility", testupdateAnnotationVisibility)

Expand Down
Loading

0 comments on commit 396c624

Please sign in to comment.