github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/topic/delete_test.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package topic
    22  
    23  import (
    24  	"fmt"
    25  	"net/http"
    26  	"net/http/httptest"
    27  	"testing"
    28  
    29  	"github.com/m3db/m3/src/cluster/kv"
    30  	"github.com/m3db/m3/src/cmd/services/m3query/config"
    31  	"github.com/m3db/m3/src/x/instrument"
    32  
    33  	"github.com/golang/mock/gomock"
    34  	"github.com/stretchr/testify/assert"
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func TestTopicDeleteHandler(t *testing.T) {
    39  	ctrl := gomock.NewController(t)
    40  	defer ctrl.Finish()
    41  
    42  	mockService := setupTest(t, ctrl)
    43  	handler := newDeleteHandler(nil, config.Configuration{}, instrument.NewOptions())
    44  	handler.(*DeleteHandler).serviceFn = testServiceFn(mockService)
    45  
    46  	// Test successful get
    47  	w := httptest.NewRecorder()
    48  	req := httptest.NewRequest("DELETE", "/topic", nil)
    49  	require.NotNil(t, req)
    50  
    51  	mockService.
    52  		EXPECT().
    53  		Delete(DefaultTopicName).
    54  		Return(nil)
    55  	handler.ServeHTTP(w, req)
    56  
    57  	resp := w.Result()
    58  	assert.Equal(t, http.StatusOK, resp.StatusCode)
    59  
    60  	w = httptest.NewRecorder()
    61  	req = httptest.NewRequest("DELETE", "/topic", nil)
    62  	req.Header.Add(HeaderTopicName, "foobar")
    63  	require.NotNil(t, req)
    64  
    65  	mockService.
    66  		EXPECT().
    67  		Delete("foobar").
    68  		Return(nil)
    69  	handler.ServeHTTP(w, req)
    70  
    71  	resp = w.Result()
    72  	assert.Equal(t, http.StatusOK, resp.StatusCode)
    73  
    74  	w = httptest.NewRecorder()
    75  	req = httptest.NewRequest("DELETE", "/topic", nil)
    76  	require.NotNil(t, req)
    77  
    78  	mockService.
    79  		EXPECT().
    80  		Delete(DefaultTopicName).
    81  		Return(kv.ErrNotFound)
    82  	handler.ServeHTTP(w, req)
    83  
    84  	resp = w.Result()
    85  	assert.Equal(t, http.StatusBadRequest, resp.StatusCode,
    86  		fmt.Sprintf("response: %s", w.Body.String()))
    87  }