Skip to content

Commit 396c624

Browse files
authored
Update test parsing to ensure ID is present (#39)
1 parent f8fbd00 commit 396c624

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+949
-225
lines changed

cloudstack/DiskOfferingService.go

+4
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,10 @@ func (s *DiskOfferingService) CreateDiskOffering(p *CreateDiskOfferingParams) (*
616616
return nil, err
617617
}
618618

619+
if resp, err = getRawValue(resp); err != nil {
620+
return nil, err
621+
}
622+
619623
var r CreateDiskOfferingResponse
620624
if err := json.Unmarshal(resp, &r); err != nil {
621625
return nil, err

cloudstack/HostService.go

+4
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,10 @@ func (s *HostService) AddHost(p *AddHostParams) (*AddHostResponse, error) {
744744
return nil, err
745745
}
746746

747+
if resp, err = getRawValue(resp); err != nil {
748+
return nil, err
749+
}
750+
747751
var r AddHostResponse
748752
if err := json.Unmarshal(resp, &r); err != nil {
749753
return nil, err

cloudstack/ISOService.go

+4
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,10 @@ func (s *ISOService) RegisterIso(p *RegisterIsoParams) (*RegisterIsoResponse, er
20332033
return nil, err
20342034
}
20352035

2036+
if resp, err = getRawValue(resp); err != nil {
2037+
return nil, err
2038+
}
2039+
20362040
var r RegisterIsoResponse
20372041
if err := json.Unmarshal(resp, &r); err != nil {
20382042
return nil, err

cloudstack/KubernetesService.go

+4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ func (s *KubernetesService) AddKubernetesSupportedVersion(p *AddKubernetesSuppor
218218
return nil, err
219219
}
220220

221+
if resp, err = getRawValue(resp); err != nil {
222+
return nil, err
223+
}
224+
221225
var r AddKubernetesSupportedVersionResponse
222226
if err := json.Unmarshal(resp, &r); err != nil {
223227
return nil, err

cloudstack/cloudstack.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
"strings"
4040
"time"
4141

42-
"github.com/golang/mock/gomock"
42+
gomock "github.com/golang/mock/gomock"
4343
)
4444

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

536-
// Generic function to get the first raw value from a response as json.RawMessage
536+
// Generic function to get the first non-count raw value from a response as json.RawMessage
537537
func getRawValue(b json.RawMessage) (json.RawMessage, error) {
538538
var m map[string]json.RawMessage
539539
if err := json.Unmarshal(b, &m); err != nil {
540540
return nil, err
541541
}
542-
for _, v := range m {
543-
return v, nil
542+
getArrayResponse := false
543+
for k := range m {
544+
if k == "count" {
545+
getArrayResponse = true
546+
}
547+
}
548+
if getArrayResponse {
549+
var resp []json.RawMessage
550+
for k, v := range m {
551+
if k != "count" {
552+
if err := json.Unmarshal(v, &resp); err != nil {
553+
return nil, err
554+
}
555+
return resp[0], nil
556+
}
557+
}
558+
559+
} else {
560+
for _, v := range m {
561+
return v, nil
562+
}
544563
}
545564
return nil, fmt.Errorf("Unable to extract the raw value from:\n\n%s\n\n", string(b))
546565
}

generate/generate.go

+45-5
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,33 @@ func (as *allServices) GeneralCode() ([]byte, error) {
584584
pn(" return buf.String()")
585585
pn("}")
586586
pn("")
587-
pn("// Generic function to get the first raw value from a response as json.RawMessage")
587+
pn("// Generic function to get the first non-count raw value from a response as json.RawMessage")
588588
pn("func getRawValue(b json.RawMessage) (json.RawMessage, error) {")
589589
pn(" var m map[string]json.RawMessage")
590590
pn(" if err := json.Unmarshal(b, &m); err != nil {")
591591
pn(" return nil, err")
592592
pn(" }")
593-
pn(" for _, v := range m {")
594-
pn(" return v, nil")
593+
pn(" getArrayResponse := false")
594+
pn(" for k := range m {")
595+
pn(" if k == \"count\" {")
596+
pn(" getArrayResponse = true")
597+
pn(" }")
598+
pn(" }")
599+
pn(" if getArrayResponse {")
600+
pn(" var resp []json.RawMessage")
601+
pn(" for k, v := range m {")
602+
pn(" if k != \"count\" {")
603+
pn(" if err := json.Unmarshal(v, &resp); err != nil {")
604+
pn(" return nil, err")
605+
pn(" }")
606+
pn(" return resp[0], nil")
607+
pn(" }")
608+
pn(" }")
609+
pn("")
610+
pn(" } else {")
611+
pn(" for _, v := range m {")
612+
pn(" return v, nil")
613+
pn(" }")
595614
pn(" }")
596615
pn(" return nil, fmt.Errorf(\"Unable to extract the raw value from:\\n\\n%%s\\n\\n\", string(b))")
597616
pn("}")
@@ -1068,10 +1087,27 @@ func (s *service) generateAPITest(a *API) {
10681087
}
10691088
}
10701089
pn(")")
1071-
pn(" _, err := client.%s.%s(p)", strings.TrimSuffix(s.name, "Service"), capitalize(a.Name))
1090+
idPresent := false
1091+
if !(strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate") {
1092+
for _, ap := range a.Response {
1093+
if ap.Name == "id" && ap.Type == "string" {
1094+
pn(" r, err := client.%s.%s(p)", strings.TrimSuffix(s.name, "Service"), capitalize(a.Name))
1095+
idPresent = true
1096+
break
1097+
}
1098+
}
1099+
}
1100+
if !idPresent {
1101+
pn(" _, err := client.%s.%s(p)", strings.TrimSuffix(s.name, "Service"), capitalize(a.Name))
1102+
}
10721103
pn(" if err != nil {")
10731104
pn(" t.Errorf(err.Error())")
10741105
pn(" }")
1106+
if idPresent {
1107+
pn(" if r.Id == \"\" {")
1108+
pn(" t.Errorf(\"Failed to parse response. ID not found\")")
1109+
pn(" }")
1110+
}
10751111
pn(" }")
10761112
pn(" t.Run(\"%s\", test%s)", capitalize(a.Name), a.Name)
10771113
pn("")
@@ -1624,7 +1660,11 @@ func (s *service) generateNewAPICallFunc(a *API) {
16241660
"RegisterUserKeys",
16251661
"GetUserKeys",
16261662
"AddAnnotation",
1627-
"RemoveAnnotation":
1663+
"RemoveAnnotation",
1664+
"AddKubernetesSupportedVersion",
1665+
"CreateDiskOffering",
1666+
"AddHost",
1667+
"RegisterIso":
16281668
pn(" if resp, err = getRawValue(resp); err != nil {")
16291669
pn(" return nil, err")
16301670
pn(" }")

test/AccountService_test.go

+24-6
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ func TestAccountService(t *testing.T) {
4040
t.Skipf("Skipping as no json response is provided in testdata")
4141
}
4242
p := client.Account.NewCreateAccountParams("email", "firstname", "lastname", "password", "username")
43-
_, err := client.Account.CreateAccount(p)
43+
r, err := client.Account.CreateAccount(p)
4444
if err != nil {
4545
t.Errorf(err.Error())
4646
}
47+
if r.Id == "" {
48+
t.Errorf("Failed to parse response. ID not found")
49+
}
4750
}
4851
t.Run("CreateAccount", testcreateAccount)
4952

@@ -64,10 +67,13 @@ func TestAccountService(t *testing.T) {
6467
t.Skipf("Skipping as no json response is provided in testdata")
6568
}
6669
p := client.Account.NewDisableAccountParams(true)
67-
_, err := client.Account.DisableAccount(p)
70+
r, err := client.Account.DisableAccount(p)
6871
if err != nil {
6972
t.Errorf(err.Error())
7073
}
74+
if r.Id == "" {
75+
t.Errorf("Failed to parse response. ID not found")
76+
}
7177
}
7278
t.Run("DisableAccount", testdisableAccount)
7379

@@ -76,10 +82,13 @@ func TestAccountService(t *testing.T) {
7682
t.Skipf("Skipping as no json response is provided in testdata")
7783
}
7884
p := client.Account.NewEnableAccountParams()
79-
_, err := client.Account.EnableAccount(p)
85+
r, err := client.Account.EnableAccount(p)
8086
if err != nil {
8187
t.Errorf(err.Error())
8288
}
89+
if r.Id == "" {
90+
t.Errorf("Failed to parse response. ID not found")
91+
}
8392
}
8493
t.Run("EnableAccount", testenableAccount)
8594

@@ -124,10 +133,13 @@ func TestAccountService(t *testing.T) {
124133
t.Skipf("Skipping as no json response is provided in testdata")
125134
}
126135
p := client.Account.NewLockAccountParams("account", "domainid")
127-
_, err := client.Account.LockAccount(p)
136+
r, err := client.Account.LockAccount(p)
128137
if err != nil {
129138
t.Errorf(err.Error())
130139
}
140+
if r.Id == "" {
141+
t.Errorf("Failed to parse response. ID not found")
142+
}
131143
}
132144
t.Run("LockAccount", testlockAccount)
133145

@@ -136,10 +148,13 @@ func TestAccountService(t *testing.T) {
136148
t.Skipf("Skipping as no json response is provided in testdata")
137149
}
138150
p := client.Account.NewMarkDefaultZoneForAccountParams("account", "domainid", "zoneid")
139-
_, err := client.Account.MarkDefaultZoneForAccount(p)
151+
r, err := client.Account.MarkDefaultZoneForAccount(p)
140152
if err != nil {
141153
t.Errorf(err.Error())
142154
}
155+
if r.Id == "" {
156+
t.Errorf("Failed to parse response. ID not found")
157+
}
143158
}
144159
t.Run("MarkDefaultZoneForAccount", testmarkDefaultZoneForAccount)
145160

@@ -148,10 +163,13 @@ func TestAccountService(t *testing.T) {
148163
t.Skipf("Skipping as no json response is provided in testdata")
149164
}
150165
p := client.Account.NewUpdateAccountParams()
151-
_, err := client.Account.UpdateAccount(p)
166+
r, err := client.Account.UpdateAccount(p)
152167
if err != nil {
153168
t.Errorf(err.Error())
154169
}
170+
if r.Id == "" {
171+
t.Errorf("Failed to parse response. ID not found")
172+
}
155173
}
156174
t.Run("UpdateAccount", testupdateAccount)
157175

test/AddressService_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ func TestAddressService(t *testing.T) {
4040
t.Skipf("Skipping as no json response is provided in testdata")
4141
}
4242
p := client.Address.NewAssociateIpAddressParams()
43-
_, err := client.Address.AssociateIpAddress(p)
43+
r, err := client.Address.AssociateIpAddress(p)
4444
if err != nil {
4545
t.Errorf(err.Error())
4646
}
47+
if r.Id == "" {
48+
t.Errorf("Failed to parse response. ID not found")
49+
}
4750
}
4851
t.Run("AssociateIpAddress", testassociateIpAddress)
4952

@@ -76,10 +79,13 @@ func TestAddressService(t *testing.T) {
7679
t.Skipf("Skipping as no json response is provided in testdata")
7780
}
7881
p := client.Address.NewUpdateIpAddressParams("id")
79-
_, err := client.Address.UpdateIpAddress(p)
82+
r, err := client.Address.UpdateIpAddress(p)
8083
if err != nil {
8184
t.Errorf(err.Error())
8285
}
86+
if r.Id == "" {
87+
t.Errorf("Failed to parse response. ID not found")
88+
}
8389
}
8490
t.Run("UpdateIpAddress", testupdateIpAddress)
8591

test/AffinityGroupService_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ func TestAffinityGroupService(t *testing.T) {
4040
t.Skipf("Skipping as no json response is provided in testdata")
4141
}
4242
p := client.AffinityGroup.NewCreateAffinityGroupParams("name", "type")
43-
_, err := client.AffinityGroup.CreateAffinityGroup(p)
43+
r, err := client.AffinityGroup.CreateAffinityGroup(p)
4444
if err != nil {
4545
t.Errorf(err.Error())
4646
}
47+
if r.Id == "" {
48+
t.Errorf("Failed to parse response. ID not found")
49+
}
4750
}
4851
t.Run("CreateAffinityGroup", testcreateAffinityGroup)
4952

@@ -88,10 +91,13 @@ func TestAffinityGroupService(t *testing.T) {
8891
t.Skipf("Skipping as no json response is provided in testdata")
8992
}
9093
p := client.AffinityGroup.NewUpdateVMAffinityGroupParams("id")
91-
_, err := client.AffinityGroup.UpdateVMAffinityGroup(p)
94+
r, err := client.AffinityGroup.UpdateVMAffinityGroup(p)
9295
if err != nil {
9396
t.Errorf(err.Error())
9497
}
98+
if r.Id == "" {
99+
t.Errorf("Failed to parse response. ID not found")
100+
}
95101
}
96102
t.Run("UpdateVMAffinityGroup", testupdateVMAffinityGroup)
97103

test/AnnotationService_test.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ func TestAnnotationService(t *testing.T) {
4040
t.Skipf("Skipping as no json response is provided in testdata")
4141
}
4242
p := client.Annotation.NewAddAnnotationParams()
43-
_, err := client.Annotation.AddAnnotation(p)
43+
r, err := client.Annotation.AddAnnotation(p)
4444
if err != nil {
4545
t.Errorf(err.Error())
4646
}
47+
if r.Id == "" {
48+
t.Errorf("Failed to parse response. ID not found")
49+
}
4750
}
4851
t.Run("AddAnnotation", testaddAnnotation)
4952

@@ -64,10 +67,13 @@ func TestAnnotationService(t *testing.T) {
6467
t.Skipf("Skipping as no json response is provided in testdata")
6568
}
6669
p := client.Annotation.NewRemoveAnnotationParams("id")
67-
_, err := client.Annotation.RemoveAnnotation(p)
70+
r, err := client.Annotation.RemoveAnnotation(p)
6871
if err != nil {
6972
t.Errorf(err.Error())
7073
}
74+
if r.Id == "" {
75+
t.Errorf("Failed to parse response. ID not found")
76+
}
7177
}
7278
t.Run("RemoveAnnotation", testremoveAnnotation)
7379

@@ -76,10 +82,13 @@ func TestAnnotationService(t *testing.T) {
7682
t.Skipf("Skipping as no json response is provided in testdata")
7783
}
7884
p := client.Annotation.NewUpdateAnnotationVisibilityParams(true, "id")
79-
_, err := client.Annotation.UpdateAnnotationVisibility(p)
85+
r, err := client.Annotation.UpdateAnnotationVisibility(p)
8086
if err != nil {
8187
t.Errorf(err.Error())
8288
}
89+
if r.Id == "" {
90+
t.Errorf("Failed to parse response. ID not found")
91+
}
8392
}
8493
t.Run("UpdateAnnotationVisibility", testupdateAnnotationVisibility)
8594

0 commit comments

Comments
 (0)