github.com/aiven/aiven-go-client@v1.36.0/kafka_connector_test.go (about)

     1  package aiven
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func setupKafkaConnectorsTestCase(t *testing.T) (*Client, func(t *testing.T)) {
    12  	t.Log("setup Kafka Connectors test case")
    13  
    14  	const (
    15  		UserName     = "test@aiven.io"
    16  		UserPassword = "testabcd"
    17  		AccessToken  = "some-random-token"
    18  	)
    19  
    20  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    21  		if r.URL.Path == "/userauth" {
    22  			w.Header().Set("Content-Type", "application/json")
    23  			w.WriteHeader(http.StatusOK)
    24  			err := json.NewEncoder(w).Encode(authResponse{
    25  				Token: AccessToken,
    26  				State: "active",
    27  			})
    28  
    29  			if err != nil {
    30  				t.Error(err)
    31  			}
    32  			return
    33  		}
    34  
    35  		if r.URL.Path == "/project/test-pr/service/test-sr/connectors" {
    36  			w.Header().Set("Content-Type", "application/json")
    37  			w.WriteHeader(http.StatusOK)
    38  			err := json.NewEncoder(w).Encode(KafkaConnectorsResponse{
    39  				APIResponse: APIResponse{},
    40  				Connectors: []KafkaConnector{
    41  					{
    42  						Name: "es-connector",
    43  						Config: KafkaConnectorConfig{
    44  							"topics":              "TestT1",
    45  							"connection.username": "testUser1",
    46  							"name":                "es-connector",
    47  							"connection.password": "pass",
    48  							"connector.class":     "io.aiven.connect.elasticsearch.ElasticsearchSinkConnector",
    49  							"type.name":           "es-connector",
    50  							"connection.url":      " https://elasticsearchUrl.aive.io:28038",
    51  						},
    52  						Plugin: KafkaConnectorPlugin{},
    53  						Tasks:  []KafkaConnectorTask{},
    54  					},
    55  				},
    56  			})
    57  
    58  			if err != nil {
    59  				t.Error(err)
    60  			}
    61  			return
    62  		}
    63  
    64  		if r.URL.Path == "/project/test-pr/service/test-sr/connectors/test-kafka-con" {
    65  			w.Header().Set("Content-Type", "application/json")
    66  			w.WriteHeader(http.StatusOK)
    67  
    68  			err := json.NewEncoder(w).Encode(&KafkaConnectorResponse{
    69  				APIResponse: APIResponse{},
    70  				Connector: KafkaConnector{
    71  					Name: "test-kafka-con",
    72  					Config: KafkaConnectorConfig{
    73  						"topics":              "TestT1",
    74  						"connection.username": "testUser1",
    75  						"name":                "es-connector",
    76  						"connection.password": "pass",
    77  						"connector.class":     "io.aiven.connect.elasticsearch.ElasticsearchSinkConnector",
    78  						"type.name":           "test-kafka-con",
    79  						"connection.url":      " https://elasticsearchUrl.aive.io:28038",
    80  					},
    81  					Plugin: KafkaConnectorPlugin{},
    82  					Tasks:  []KafkaConnectorTask{},
    83  				},
    84  			})
    85  
    86  			if err != nil {
    87  				t.Error(err)
    88  			}
    89  		}
    90  
    91  		if r.URL.Path == "/project/test-pr/service/test-sr/connectors/test-kafka-con/status" {
    92  			w.Header().Set("Content-Type", "application/json")
    93  			w.WriteHeader(http.StatusOK)
    94  
    95  			err := json.NewEncoder(w).Encode(KafkaConnectorStatusResponse{
    96  				APIResponse: APIResponse{},
    97  				Status: KafkaConnectorStatus{
    98  					State: "RUNNING",
    99  					Tasks: []KafkaConnectorTaskStatus{},
   100  				},
   101  			})
   102  
   103  			if err != nil {
   104  				t.Error(err)
   105  			}
   106  		}
   107  
   108  	}))
   109  
   110  	apiUrl = ts.URL
   111  
   112  	c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version())
   113  	if err != nil {
   114  		t.Fatalf("user authentication error: %s", err)
   115  	}
   116  
   117  	return c, func(t *testing.T) {
   118  		t.Log("teardown Kafka Connectors test case")
   119  		ts.Close()
   120  	}
   121  }
   122  
   123  func TestKafkaConnectorsHandler_Create(t *testing.T) {
   124  	c, tearDown := setupKafkaConnectorsTestCase(t)
   125  	defer tearDown(t)
   126  
   127  	type fields struct {
   128  		client *Client
   129  	}
   130  	type args struct {
   131  		project string
   132  		service string
   133  		c       KafkaConnectorConfig
   134  	}
   135  	tests := []struct {
   136  		name    string
   137  		fields  fields
   138  		args    args
   139  		wantErr bool
   140  	}{
   141  		{
   142  			"",
   143  			fields{client: c},
   144  			args{
   145  				project: "test-pr",
   146  				service: "test-sr",
   147  				c: KafkaConnectorConfig{
   148  					"topics":              "TestT1",
   149  					"connection.username": "testUser1",
   150  					"name":                "es-connector",
   151  					"connection.password": "pass",
   152  					"connector.class":     "io.aiven.connect.elasticsearch.ElasticsearchSinkConnector",
   153  					"type.name":           "es-connector",
   154  					"connection.url":      " https://elasticsearchUrl.aive.io:28038",
   155  				},
   156  			},
   157  			false,
   158  		},
   159  	}
   160  	for _, tt := range tests {
   161  		t.Run(tt.name, func(t *testing.T) {
   162  			h := &KafkaConnectorsHandler{
   163  				client: tt.fields.client,
   164  			}
   165  			if err := h.Create(tt.args.project, tt.args.service, tt.args.c); (err != nil) != tt.wantErr {
   166  				t.Errorf("Create() error = %v, wantErr %v", err, tt.wantErr)
   167  			}
   168  		})
   169  	}
   170  }
   171  
   172  func TestKafkaConnectorsHandler_Delete(t *testing.T) {
   173  	c, tearDown := setupKafkaConnectorsTestCase(t)
   174  	defer tearDown(t)
   175  
   176  	type fields struct {
   177  		client *Client
   178  	}
   179  	type args struct {
   180  		project string
   181  		service string
   182  		name    string
   183  	}
   184  	tests := []struct {
   185  		name    string
   186  		fields  fields
   187  		args    args
   188  		wantErr bool
   189  	}{
   190  		{
   191  			"",
   192  			fields{client: c},
   193  			args{
   194  				project: "test-pr",
   195  				service: "test-sr",
   196  				name:    "test-kafka-con",
   197  			},
   198  			false,
   199  		},
   200  	}
   201  	for _, tt := range tests {
   202  		t.Run(tt.name, func(t *testing.T) {
   203  			h := &KafkaConnectorsHandler{
   204  				client: tt.fields.client,
   205  			}
   206  			if err := h.Delete(tt.args.project, tt.args.service, tt.args.name); (err != nil) != tt.wantErr {
   207  				t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
   208  			}
   209  		})
   210  	}
   211  }
   212  
   213  func TestKafkaConnectorsHandler_List(t *testing.T) {
   214  	c, tearDown := setupKafkaConnectorsTestCase(t)
   215  	defer tearDown(t)
   216  
   217  	type fields struct {
   218  		client *Client
   219  	}
   220  	type args struct {
   221  		project string
   222  		service string
   223  	}
   224  	tests := []struct {
   225  		name    string
   226  		fields  fields
   227  		args    args
   228  		want    *KafkaConnectorsResponse
   229  		wantErr bool
   230  	}{
   231  		{
   232  			"",
   233  			fields{client: c},
   234  			args{
   235  				project: "test-pr",
   236  				service: "test-sr",
   237  			},
   238  			&KafkaConnectorsResponse{
   239  				APIResponse: APIResponse{},
   240  				Connectors: []KafkaConnector{
   241  					{
   242  						Name: "es-connector",
   243  						Config: KafkaConnectorConfig{
   244  							"topics":              "TestT1",
   245  							"connection.username": "testUser1",
   246  							"name":                "es-connector",
   247  							"connection.password": "pass",
   248  							"connector.class":     "io.aiven.connect.elasticsearch.ElasticsearchSinkConnector",
   249  							"type.name":           "es-connector",
   250  							"connection.url":      " https://elasticsearchUrl.aive.io:28038",
   251  						},
   252  						Plugin: KafkaConnectorPlugin{},
   253  						Tasks:  []KafkaConnectorTask{},
   254  					}},
   255  			},
   256  			false,
   257  		},
   258  	}
   259  	for _, tt := range tests {
   260  		t.Run(tt.name, func(t *testing.T) {
   261  			h := &KafkaConnectorsHandler{
   262  				client: tt.fields.client,
   263  			}
   264  			got, err := h.List(tt.args.project, tt.args.service)
   265  			if (err != nil) != tt.wantErr {
   266  				t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr)
   267  				return
   268  			}
   269  			if !reflect.DeepEqual(got, tt.want) {
   270  				t.Errorf("List() got = %v, want %v", got, tt.want)
   271  			}
   272  		})
   273  	}
   274  }
   275  
   276  func TestKafkaConnectorsHandler_GetByName(t *testing.T) {
   277  	c, tearDown := setupKafkaConnectorsTestCase(t)
   278  	defer tearDown(t)
   279  
   280  	type fields struct {
   281  		client *Client
   282  	}
   283  	type args struct {
   284  		project string
   285  		service string
   286  		name    string
   287  	}
   288  	tests := []struct {
   289  		name    string
   290  		fields  fields
   291  		args    args
   292  		want    *KafkaConnector
   293  		wantErr bool
   294  	}{
   295  		{
   296  			"connector found",
   297  			fields{client: c},
   298  			args{
   299  				project: "test-pr",
   300  				service: "test-sr",
   301  				name:    "es-connector",
   302  			},
   303  			&KafkaConnector{
   304  				Name: "es-connector",
   305  				Config: KafkaConnectorConfig{
   306  					"topics":              "TestT1",
   307  					"connection.username": "testUser1",
   308  					"name":                "es-connector",
   309  					"connection.password": "pass",
   310  					"connector.class":     "io.aiven.connect.elasticsearch.ElasticsearchSinkConnector",
   311  					"type.name":           "es-connector",
   312  					"connection.url":      " https://elasticsearchUrl.aive.io:28038",
   313  				},
   314  				Plugin: KafkaConnectorPlugin{},
   315  				Tasks:  []KafkaConnectorTask{},
   316  			},
   317  			false,
   318  		},
   319  		{
   320  			"connector not found",
   321  			fields{client: c},
   322  			args{
   323  				project: "test-pr",
   324  				service: "test-sr",
   325  				name:    "does-not-exist",
   326  			},
   327  			nil,
   328  			true,
   329  		},
   330  	}
   331  	for _, tt := range tests {
   332  		t.Run(tt.name, func(t *testing.T) {
   333  			h := &KafkaConnectorsHandler{
   334  				client: tt.fields.client,
   335  			}
   336  			got, err := h.GetByName(tt.args.project, tt.args.service, tt.args.name)
   337  			if (err != nil) != tt.wantErr {
   338  				t.Errorf("GetByName() error = %v, wantErr %v", err, tt.wantErr)
   339  				return
   340  			}
   341  			if !reflect.DeepEqual(got, tt.want) {
   342  				t.Errorf("GetByName() got = %v, want %v", got, tt.want)
   343  			}
   344  		})
   345  	}
   346  }
   347  
   348  func TestKafkaConnectorsHandler_Status(t *testing.T) {
   349  	c, tearDown := setupKafkaConnectorsTestCase(t)
   350  	defer tearDown(t)
   351  
   352  	type fields struct {
   353  		client *Client
   354  	}
   355  	type args struct {
   356  		project string
   357  		service string
   358  		name    string
   359  	}
   360  	tests := []struct {
   361  		name    string
   362  		fields  fields
   363  		args    args
   364  		want    *KafkaConnectorStatusResponse
   365  		wantErr bool
   366  	}{
   367  		{
   368  			"",
   369  			fields{client: c},
   370  			args{
   371  				project: "test-pr",
   372  				service: "test-sr",
   373  				name:    "test-kafka-con",
   374  			},
   375  			&KafkaConnectorStatusResponse{
   376  				APIResponse: APIResponse{},
   377  				Status: KafkaConnectorStatus{
   378  					State: "RUNNING",
   379  					Tasks: []KafkaConnectorTaskStatus{},
   380  				},
   381  			},
   382  			false,
   383  		},
   384  	}
   385  	for _, tt := range tests {
   386  		t.Run(tt.name, func(t *testing.T) {
   387  			h := &KafkaConnectorsHandler{
   388  				client: tt.fields.client,
   389  			}
   390  			got, err := h.Status(tt.args.project, tt.args.service, tt.args.name)
   391  			if (err != nil) != tt.wantErr {
   392  				t.Errorf("Status() error = %v, wantErr %v", err, tt.wantErr)
   393  				return
   394  			}
   395  			if !reflect.DeepEqual(got, tt.want) {
   396  				t.Errorf("Status() got = %v, want %v", got, tt.want)
   397  			}
   398  		})
   399  	}
   400  }
   401  
   402  func TestKafkaConnectorsHandler_Update(t *testing.T) {
   403  	c, tearDown := setupKafkaConnectorsTestCase(t)
   404  	defer tearDown(t)
   405  
   406  	type fields struct {
   407  		client *Client
   408  	}
   409  	type args struct {
   410  		project string
   411  		service string
   412  		name    string
   413  		c       KafkaConnectorConfig
   414  	}
   415  	tests := []struct {
   416  		name    string
   417  		fields  fields
   418  		args    args
   419  		want    *KafkaConnectorResponse
   420  		wantErr bool
   421  	}{
   422  		{
   423  			"",
   424  			fields{client: c},
   425  			args{
   426  				project: "test-pr",
   427  				service: "test-sr",
   428  				name:    "test-kafka-con",
   429  				c: KafkaConnectorConfig{
   430  					"topics":              "TestT1",
   431  					"connection.username": "testUser1",
   432  					"name":                "es-connector",
   433  					"connection.password": "pass",
   434  					"connector.class":     "io.aiven.connect.elasticsearch.ElasticsearchSinkConnector",
   435  					"type.name":           "test-kafka-con",
   436  					"connection.url":      " https://elasticsearchUrl.aive.io:28038",
   437  				},
   438  			},
   439  			&KafkaConnectorResponse{
   440  				APIResponse: APIResponse{},
   441  				Connector: KafkaConnector{
   442  					Name: "test-kafka-con",
   443  					Config: KafkaConnectorConfig{
   444  						"topics":              "TestT1",
   445  						"connection.username": "testUser1",
   446  						"name":                "es-connector",
   447  						"connection.password": "pass",
   448  						"connector.class":     "io.aiven.connect.elasticsearch.ElasticsearchSinkConnector",
   449  						"type.name":           "test-kafka-con",
   450  						"connection.url":      " https://elasticsearchUrl.aive.io:28038",
   451  					},
   452  					Plugin: KafkaConnectorPlugin{},
   453  					Tasks:  []KafkaConnectorTask{},
   454  				},
   455  			},
   456  			false,
   457  		},
   458  	}
   459  	for _, tt := range tests {
   460  		t.Run(tt.name, func(t *testing.T) {
   461  			h := &KafkaConnectorsHandler{
   462  				client: tt.fields.client,
   463  			}
   464  			got, err := h.Update(tt.args.project, tt.args.service, tt.args.name, tt.args.c)
   465  			if (err != nil) != tt.wantErr {
   466  				t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr)
   467  				return
   468  			}
   469  			if !reflect.DeepEqual(got, tt.want) {
   470  				t.Errorf("Update() got = %v, want %v", got, tt.want)
   471  			}
   472  		})
   473  	}
   474  }