From c986c62fc00cdfe8f808d7d8d1cbc0b61d0d6e9f Mon Sep 17 00:00:00 2001 From: Alberto Pinon Formoso Date: Tue, 28 Jan 2025 12:48:44 -0600 Subject: [PATCH] added test function --- partner_interconnect_attachments.go | 5 +- partner_interconnect_attachments_test.go | 74 ++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/partner_interconnect_attachments.go b/partner_interconnect_attachments.go index dfdf84e..df21c92 100644 --- a/partner_interconnect_attachments.go +++ b/partner_interconnect_attachments.go @@ -46,7 +46,6 @@ type PartnerInterconnectAttachmentCreateRequest struct { BGP BGP `json:"bgp,omitempty"` } -// PartnerInterconnectAttachmentCreateRequest represents a request to create a Partner Interconnect Attachment. type partnerInterconnectAttachmentRequestBody struct { // Name is the name of the Partner Interconnect Attachment Name string `json:"name,omitempty"` @@ -62,8 +61,8 @@ type partnerInterconnectAttachmentRequestBody struct { BGP *BGP `json:"bgp,omitempty"` } -func (req *PartnerInterconnectAttachmentCreateRequest) buildReq() *PartnerInterconnectAttachmentRequestBody { - request := &PartnerInterconnectAttachmentRequestBody{ +func (req *PartnerInterconnectAttachmentCreateRequest) buildReq() *partnerInterconnectAttachmentRequestBody { + request := &partnerInterconnectAttachmentRequestBody{ Name: req.Name, ConnectionBandwidthInMbps: req.ConnectionBandwidthInMbps, Region: req.Region, diff --git a/partner_interconnect_attachments_test.go b/partner_interconnect_attachments_test.go index 2d74e3c..9ca04d6 100644 --- a/partner_interconnect_attachments_test.go +++ b/partner_interconnect_attachments_test.go @@ -2,6 +2,7 @@ package godo import ( "encoding/json" + "io" "net/http" "testing" "time" @@ -27,6 +28,17 @@ var vInterconnectTestObj = &PartnerInterconnectAttachment{ CreatedAt: time.Date(2024, 12, 26, 21, 48, 40, 995304079, time.UTC), } +var vInterconnectNoBGPTestObj = &PartnerInterconnectAttachment{ + ID: "880b7f98-f062-404d-b33c-458d545696f6", + Name: "my-new-partner-interconnect", + State: "ACTIVE", + ConnectionBandwidthInMbps: 50, + Region: "NYC", + NaaSProvider: "MEGAPORT", + VPCIDs: []string{"f5a0c5e4-7537-47de-bb8d-46c766f89ffb"}, + CreatedAt: time.Date(2024, 12, 26, 21, 48, 40, 995304079, time.UTC), +} + var vInterconnectTestJSON = ` { "id":"880b7f98-f062-404d-b33c-458d545696f6", @@ -46,6 +58,22 @@ var vInterconnectTestJSON = ` } ` +var vInterconnectNoBGPTestJSON = ` + { + "id":"880b7f98-f062-404d-b33c-458d545696f6", + "name":"my-new-partner-interconnect", + "state":"ACTIVE", + "connection_bandwidth_in_mbps":50, + "region":"NYC", + "naas_provider":"MEGAPORT", + "vpc_ids":["f5a0c5e4-7537-47de-bb8d-46c766f89ffb"], + "created_at":"2024-12-26T21:48:40.995304079Z" + } +` + +const expectedCreateBodyNoBGP = `{"name":"my-new-partner-interconnect","connection_bandwidth_in_mbps":50,"region":"NYC","naas_provider":"MEGAPORT","vpc_ids":["f5a0c5e4-7537-47de-bb8d-46c766f89ffb"]} +` + func TestPartnerInterconnectAttachments_List(t *testing.T) { setup() defer teardown() @@ -134,6 +162,52 @@ func TestPartnerInterconnectAttachments_Create(t *testing.T) { require.Equal(t, want, got) } +func TestPartnerInterconnectAttachments_CreateNoBGP(t *testing.T) { + setup() + defer teardown() + + svc := client.PartnerInterconnectAttachments + path := "/v2/partner_interconnect/attachments" + want := vInterconnectNoBGPTestObj + req := &PartnerInterconnectAttachmentCreateRequest{ + Name: "my-new-partner-interconnect", + ConnectionBandwidthInMbps: 50, + Region: "NYC", + NaaSProvider: "MEGAPORT", + VPCIDs: []string{"f5a0c5e4-7537-47de-bb8d-46c766f89ffb"}, + } + jsonBlob := ` +{ + "partner_interconnect_attachment": +` + vInterconnectNoBGPTestJSON + ` +} +` + + mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + body, err := io.ReadAll(r.Body) + if err != nil { + t.Fatal(err) + } + defer r.Body.Close() + + require.Equal(t, expectedCreateBodyNoBGP, string(body)) + + c := new(PartnerInterconnectAttachmentCreateRequest) + err = json.Unmarshal(body, c) + if err != nil { + t.Fatal(err) + } + + testMethod(t, r, http.MethodPost) + require.Equal(t, c, req) + w.Write([]byte(jsonBlob)) + }) + + got, _, err := svc.Create(ctx, req) + require.NoError(t, err) + require.Equal(t, want, got) +} + func TestPartnerInterconnectAttachments_Get(t *testing.T) { setup() defer teardown()