Skip to content
Permalink
Browse files
Update test parsing to ensure ID is present (#39)
  • Loading branch information
davidjumani committed Jun 13, 2022
1 parent f8fbd00 commit 396c6240941b3fd1f556f8585cec99f7ded2e04d
Show file tree
Hide file tree
Showing 57 changed files with 949 additions and 225 deletions.
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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))
}
@@ -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("}")
@@ -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("")
@@ -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(" }")
@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -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)

0 comments on commit 396c624

Please sign in to comment.