github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/configgtm/property_test.go (about)

     1  package gtm
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"errors"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/session"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestGtm_NewTrafficTarget(t *testing.T) {
    18  	client := Client(session.Must(session.New()))
    19  
    20  	tgt := client.NewTrafficTarget(context.Background())
    21  
    22  	assert.NotNil(t, tgt)
    23  }
    24  
    25  func TestGtm_NewStaticRRSet(t *testing.T) {
    26  	client := Client(session.Must(session.New()))
    27  
    28  	set := client.NewStaticRRSet(context.Background())
    29  
    30  	assert.NotNil(t, set)
    31  }
    32  
    33  func TestGtm_NewLivenessTest(t *testing.T) {
    34  	client := Client(session.Must(session.New()))
    35  
    36  	test := client.NewLivenessTest(context.Background(), "foo", "bar", 1, 1000)
    37  
    38  	assert.NotNil(t, test)
    39  	assert.Equal(t, "foo", test.Name)
    40  	assert.Equal(t, "bar", test.TestObjectProtocol)
    41  	assert.Equal(t, 1, test.TestInterval)
    42  	assert.Equal(t, float32(1000), test.TestTimeout)
    43  }
    44  
    45  func TestGtm_NewProperty(t *testing.T) {
    46  	client := Client(session.Must(session.New()))
    47  
    48  	prop := client.NewProperty(context.Background(), "foo")
    49  
    50  	assert.NotNil(t, prop)
    51  	assert.Equal(t, prop.Name, "foo")
    52  }
    53  
    54  func TestGtm_ListProperties(t *testing.T) {
    55  	var result PropertyList
    56  
    57  	respData, err := loadTestData("TestGtm_ListProperties.resp.json")
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	if err := json.NewDecoder(bytes.NewBuffer(respData)).Decode(&result); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	tests := map[string]struct {
    67  		domain           string
    68  		responseStatus   int
    69  		responseBody     string
    70  		expectedPath     string
    71  		expectedResponse []*Property
    72  		withError        error
    73  		headers          http.Header
    74  	}{
    75  		"200 OK": {
    76  			domain: "example.akadns.net",
    77  			headers: http.Header{
    78  				"Content-Type": []string{"application/vnd.config-gtm.v1.4+json;charset=UTF-8"},
    79  			},
    80  			responseStatus:   http.StatusOK,
    81  			responseBody:     string(respData),
    82  			expectedPath:     "/config-gtm/v1/domains/example.akadns.net/properties",
    83  			expectedResponse: result.PropertyItems,
    84  		},
    85  		"500 internal server error": {
    86  			domain:         "example.akadns.net",
    87  			headers:        http.Header{},
    88  			responseStatus: http.StatusInternalServerError,
    89  			responseBody: `
    90  {
    91      "type": "internal_error",
    92      "title": "Internal Server Error",
    93      "detail": "Error fetching propertys",
    94      "status": 500
    95  }`,
    96  			expectedPath: "/config-gtm/v1/domains/example.akadns.net/properties",
    97  			withError: &Error{
    98  				Type:       "internal_error",
    99  				Title:      "Internal Server Error",
   100  				Detail:     "Error fetching propertys",
   101  				StatusCode: http.StatusInternalServerError,
   102  			},
   103  		},
   104  	}
   105  
   106  	for name, test := range tests {
   107  		t.Run(name, func(t *testing.T) {
   108  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   109  				assert.Equal(t, test.expectedPath, r.URL.String())
   110  				assert.Equal(t, http.MethodGet, r.Method)
   111  				w.WriteHeader(test.responseStatus)
   112  				_, err := w.Write([]byte(test.responseBody))
   113  				assert.NoError(t, err)
   114  			}))
   115  			client := mockAPIClient(t, mockServer)
   116  			result, err := client.ListProperties(
   117  				session.ContextWithOptions(
   118  					context.Background(),
   119  					session.WithContextHeaders(test.headers)), test.domain)
   120  			if test.withError != nil {
   121  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   122  				return
   123  			}
   124  			require.NoError(t, err)
   125  			assert.Equal(t, test.expectedResponse, result)
   126  		})
   127  	}
   128  }
   129  
   130  // Test GetProperty
   131  // GetProperty(context.Context, string) (*Property, error)
   132  func TestGtm_GetProperty(t *testing.T) {
   133  	var result Property
   134  
   135  	respData, err := loadTestData("TestGtm_GetProperty.resp.json")
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  
   140  	if err := json.NewDecoder(bytes.NewBuffer(respData)).Decode(&result); err != nil {
   141  		t.Fatal(err)
   142  	}
   143  
   144  	tests := map[string]struct {
   145  		name             string
   146  		domain           string
   147  		responseStatus   int
   148  		responseBody     []byte
   149  		expectedPath     string
   150  		expectedResponse *Property
   151  		withError        error
   152  	}{
   153  		"200 OK": {
   154  			name:             "www",
   155  			domain:           "example.akadns.net",
   156  			responseStatus:   http.StatusOK,
   157  			responseBody:     respData,
   158  			expectedPath:     "/config-gtm/v1/domains/example.akadns.net/properties/www",
   159  			expectedResponse: &result,
   160  		},
   161  		"500 internal server error": {
   162  			name:           "www",
   163  			domain:         "example.akadns.net",
   164  			responseStatus: http.StatusInternalServerError,
   165  			responseBody: []byte(`
   166  {
   167      "type": "internal_error",
   168      "title": "Internal Server Error",
   169      "detail": "Error fetching property"
   170  }`),
   171  			expectedPath: "/config-gtm/v1/domains/example.akadns.net/properties/www",
   172  			withError: &Error{
   173  				Type:       "internal_error",
   174  				Title:      "Internal Server Error",
   175  				Detail:     "Error fetching property",
   176  				StatusCode: http.StatusInternalServerError,
   177  			},
   178  		},
   179  	}
   180  
   181  	for name, test := range tests {
   182  		t.Run(name, func(t *testing.T) {
   183  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   184  				assert.Equal(t, test.expectedPath, r.URL.String())
   185  				assert.Equal(t, http.MethodGet, r.Method)
   186  				w.WriteHeader(test.responseStatus)
   187  				_, err := w.Write(test.responseBody)
   188  				assert.NoError(t, err)
   189  			}))
   190  			client := mockAPIClient(t, mockServer)
   191  			result, err := client.GetProperty(context.Background(), test.name, test.domain)
   192  			if test.withError != nil {
   193  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   194  				return
   195  			}
   196  			require.NoError(t, err)
   197  			assert.Equal(t, test.expectedResponse, result)
   198  		})
   199  	}
   200  }
   201  
   202  // Test Create domain.
   203  // CreateProperty(context.Context, *Property, map[string]string) (*PropertyResponse, error)
   204  func TestGtm_CreateProperty(t *testing.T) {
   205  	var result PropertyResponse
   206  	var req Property
   207  
   208  	respData, err := loadTestData("TestGtm_CreateProperty.resp.json")
   209  	if err != nil {
   210  		t.Fatal(err)
   211  	}
   212  
   213  	if err := json.NewDecoder(bytes.NewBuffer(respData)).Decode(&result); err != nil {
   214  		t.Fatal(err)
   215  	}
   216  
   217  	reqData, err := loadTestData("TestGtm_CreateProperty.req.json")
   218  	if err != nil {
   219  		t.Fatal(err)
   220  	}
   221  
   222  	if err := json.NewDecoder(bytes.NewBuffer(reqData)).Decode(&req); err != nil {
   223  		t.Fatal(err)
   224  	}
   225  
   226  	tests := map[string]struct {
   227  		domain           string
   228  		prop             *Property
   229  		responseStatus   int
   230  		responseBody     []byte
   231  		expectedPath     string
   232  		expectedResponse *PropertyResponse
   233  		withError        error
   234  		headers          http.Header
   235  	}{
   236  		"201 Created": {
   237  			prop:   &req,
   238  			domain: "example.akadns.net",
   239  			headers: http.Header{
   240  				"Content-Type": []string{"application/vnd.config-gtm.v1.4+json;charset=UTF-8"},
   241  			},
   242  			responseStatus:   http.StatusCreated,
   243  			responseBody:     respData,
   244  			expectedResponse: &result,
   245  			expectedPath:     "/config-gtm/v1/domains/example.akadns.net?contractId=1-2ABCDE",
   246  		},
   247  		"500 internal server error": {
   248  			prop:           &req,
   249  			domain:         "example.akadns.net",
   250  			responseStatus: http.StatusInternalServerError,
   251  			responseBody: []byte(`
   252  {
   253      "type": "internal_error",
   254      "title": "Internal Server Error",
   255      "detail": "Error creating domain"
   256  }`),
   257  			expectedPath: "/config-gtm/v1/domains/example.akadns.net?contractId=1-2ABCDE",
   258  			withError: &Error{
   259  				Type:       "internal_error",
   260  				Title:      "Internal Server Error",
   261  				Detail:     "Error creating domain",
   262  				StatusCode: http.StatusInternalServerError,
   263  			},
   264  		},
   265  	}
   266  
   267  	for name, test := range tests {
   268  		t.Run(name, func(t *testing.T) {
   269  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   270  				assert.Equal(t, http.MethodPut, r.Method)
   271  				w.WriteHeader(test.responseStatus)
   272  				if len(test.responseBody) > 0 {
   273  					_, err := w.Write(test.responseBody)
   274  					assert.NoError(t, err)
   275  				}
   276  			}))
   277  			client := mockAPIClient(t, mockServer)
   278  			result, err := client.CreateProperty(
   279  				session.ContextWithOptions(
   280  					context.Background(),
   281  					session.WithContextHeaders(test.headers)), test.prop, test.domain)
   282  			if test.withError != nil {
   283  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   284  				return
   285  			}
   286  			require.NoError(t, err)
   287  			assert.Equal(t, test.expectedResponse, result)
   288  		})
   289  	}
   290  }
   291  
   292  // Test Update domain.
   293  // UpdateProperty(context.Context, *Property, map[string]string) (*PropertyResponse, error)
   294  func TestGtm_UpdateProperty(t *testing.T) {
   295  	var result PropertyResponse
   296  	var req Property
   297  
   298  	respData, err := loadTestData("TestGtm_CreateProperty.resp.json")
   299  	if err != nil {
   300  		t.Fatal(err)
   301  	}
   302  
   303  	if err := json.NewDecoder(bytes.NewBuffer(respData)).Decode(&result); err != nil {
   304  		t.Fatal(err)
   305  	}
   306  
   307  	reqData, err := loadTestData("TestGtm_CreateProperty.req.json")
   308  	if err != nil {
   309  		t.Fatal(err)
   310  	}
   311  
   312  	if err := json.NewDecoder(bytes.NewBuffer(reqData)).Decode(&req); err != nil {
   313  		t.Fatal(err)
   314  	}
   315  
   316  	tests := map[string]struct {
   317  		prop             *Property
   318  		domain           string
   319  		responseStatus   int
   320  		responseBody     []byte
   321  		expectedPath     string
   322  		expectedResponse *ResponseStatus
   323  		withError        error
   324  		headers          http.Header
   325  	}{
   326  		"200 Success": {
   327  			prop:   &req,
   328  			domain: "example.akadns.net",
   329  			headers: http.Header{
   330  				"Content-Type": []string{"application/vnd.config-gtm.v1.4+json;charset=UTF-8"},
   331  			},
   332  			responseStatus:   http.StatusCreated,
   333  			responseBody:     respData,
   334  			expectedResponse: result.Status,
   335  			expectedPath:     "/config-gtm/v1/domains/example.akadns.net?contractId=1-2ABCDE",
   336  		},
   337  		"500 internal server error": {
   338  			prop:           &req,
   339  			domain:         "example.akadns.net",
   340  			responseStatus: http.StatusInternalServerError,
   341  			responseBody: []byte(`
   342  {
   343      "type": "internal_error",
   344      "title": "Internal Server Error",
   345      "detail": "Error creating zone"
   346  }`),
   347  			expectedPath: "/config-gtm/v1/domains/example.akadns.net?contractId=1-2ABCDE",
   348  			withError: &Error{
   349  				Type:       "internal_error",
   350  				Title:      "Internal Server Error",
   351  				Detail:     "Error creating zone",
   352  				StatusCode: http.StatusInternalServerError,
   353  			},
   354  		},
   355  	}
   356  
   357  	for name, test := range tests {
   358  		t.Run(name, func(t *testing.T) {
   359  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   360  				assert.Equal(t, http.MethodPut, r.Method)
   361  				w.WriteHeader(test.responseStatus)
   362  				if len(test.responseBody) > 0 {
   363  					_, err := w.Write(test.responseBody)
   364  					assert.NoError(t, err)
   365  				}
   366  			}))
   367  			client := mockAPIClient(t, mockServer)
   368  			result, err := client.UpdateProperty(
   369  				session.ContextWithOptions(
   370  					context.Background(),
   371  					session.WithContextHeaders(test.headers)), test.prop, test.domain)
   372  			if test.withError != nil {
   373  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   374  				return
   375  			}
   376  			require.NoError(t, err)
   377  			assert.Equal(t, test.expectedResponse, result)
   378  		})
   379  	}
   380  }
   381  
   382  func TestGtm_DeleteProperty(t *testing.T) {
   383  	var result PropertyResponse
   384  	var req Property
   385  
   386  	respData, err := loadTestData("TestGtm_CreateProperty.resp.json")
   387  	if err != nil {
   388  		t.Fatal(err)
   389  	}
   390  
   391  	if err := json.NewDecoder(bytes.NewBuffer(respData)).Decode(&result); err != nil {
   392  		t.Fatal(err)
   393  	}
   394  
   395  	reqData, err := loadTestData("TestGtm_CreateProperty.req.json")
   396  	if err != nil {
   397  		t.Fatal(err)
   398  	}
   399  
   400  	if err := json.NewDecoder(bytes.NewBuffer(reqData)).Decode(&req); err != nil {
   401  		t.Fatal(err)
   402  	}
   403  
   404  	tests := map[string]struct {
   405  		prop             *Property
   406  		domain           string
   407  		responseStatus   int
   408  		responseBody     []byte
   409  		expectedPath     string
   410  		expectedResponse *ResponseStatus
   411  		withError        error
   412  		headers          http.Header
   413  	}{
   414  		"200 Success": {
   415  			prop:   &req,
   416  			domain: "example.akadns.net",
   417  			headers: http.Header{
   418  				"Content-Type": []string{"application/vnd.config-gtm.v1.4+json;charset=UTF-8"},
   419  			},
   420  			responseStatus:   http.StatusOK,
   421  			responseBody:     respData,
   422  			expectedResponse: result.Status,
   423  			expectedPath:     "/config-gtm/v1/domains/example.akadns.net?contractId=1-2ABCDE",
   424  		},
   425  		"500 internal server error": {
   426  			prop:           &req,
   427  			domain:         "example.akadns.net",
   428  			responseStatus: http.StatusInternalServerError,
   429  			responseBody: []byte(`
   430  {
   431      "type": "internal_error",
   432      "title": "Internal Server Error",
   433      "detail": "Error creating zone"
   434  }`),
   435  			expectedPath: "/config-gtm/v1/domains/example.akadns.net?contractId=1-2ABCDE",
   436  			withError: &Error{
   437  				Type:       "internal_error",
   438  				Title:      "Internal Server Error",
   439  				Detail:     "Error creating zone",
   440  				StatusCode: http.StatusInternalServerError,
   441  			},
   442  		},
   443  	}
   444  
   445  	for name, test := range tests {
   446  		t.Run(name, func(t *testing.T) {
   447  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   448  				assert.Equal(t, http.MethodDelete, r.Method)
   449  				w.WriteHeader(test.responseStatus)
   450  				if len(test.responseBody) > 0 {
   451  					_, err := w.Write(test.responseBody)
   452  					assert.NoError(t, err)
   453  				}
   454  			}))
   455  			client := mockAPIClient(t, mockServer)
   456  			result, err := client.DeleteProperty(
   457  				session.ContextWithOptions(
   458  					context.Background(),
   459  					session.WithContextHeaders(test.headers)), test.prop, test.domain)
   460  			if test.withError != nil {
   461  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   462  				return
   463  			}
   464  			require.NoError(t, err)
   465  			assert.Equal(t, test.expectedResponse, result)
   466  		})
   467  	}
   468  }