From 3b7f9c81b1a72ecc7a0f3c68c2c0b2dd537818f2 Mon Sep 17 00:00:00 2001 From: kapishmalik Date: Sun, 29 Oct 2023 15:34:34 +0530 Subject: [PATCH] unit tests --- .../v2/hoverfly_journalindex_handler_test.go | 73 +++++++++++ core/hoverfly_service_test.go | 35 +++++ core/journal/journal.go | 2 +- core/journal/journal_index_test.go | 124 ++++++++++++++++++ core/journal/journal_test.go | 101 ++++++++++++++ 5 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 core/handlers/v2/hoverfly_journalindex_handler_test.go create mode 100644 core/journal/journal_index_test.go diff --git a/core/handlers/v2/hoverfly_journalindex_handler_test.go b/core/handlers/v2/hoverfly_journalindex_handler_test.go new file mode 100644 index 000000000..6339435a2 --- /dev/null +++ b/core/handlers/v2/hoverfly_journalindex_handler_test.go @@ -0,0 +1,73 @@ +package v2 + +import ( + "bytes" + "encoding/json" + . "github.com/onsi/gomega" + "io/ioutil" + "net/http" + "testing" +) + +type HoverflyJournalIndexStub struct{} + +func (HoverflyJournalIndexStub) GetAllIndexes() []JournalIndexView { + + return getJournalIndexes() +} + +func getJournalIndexes() []JournalIndexView { + journalIndexViews := []JournalIndexView{} + journalIndexViews = append(journalIndexViews, JournalIndexView{Name: "Request.QueryParam.id"}) + return journalIndexViews +} +func (HoverflyJournalIndexStub) AddJournalIndex(string) error { + return nil +} +func (HoverflyJournalIndexStub) DeleteJournalIndex(string) { + +} + +func Test_JournalIndexHandler_GetAllJournalIndexes(t *testing.T) { + RegisterTestingT(t) + stubHoverfly := HoverflyJournalIndexStub{} + unit := HoverflyJournalIndexHandler{Hoverfly: stubHoverfly} + + request, err := http.NewRequest("GET", "/api/v2/journal/index", nil) + Expect(err).To(BeNil()) + response := makeRequestOnHandler(unit.GetAll, request) + Expect(response.Code).To(Equal(http.StatusOK)) + + responseBody := response.Body.String() + expectedResponseBodyBytes, _ := json.Marshal(getJournalIndexes()) + Expect(responseBody).To(Equal(string(expectedResponseBodyBytes))) + +} + +func Test_JournalIndexHandler_SetJournalIndex(t *testing.T) { + RegisterTestingT(t) + stubHoverfly := HoverflyJournalIndexStub{} + unit := HoverflyJournalIndexHandler{Hoverfly: stubHoverfly} + + journalIndexRequest := JournalIndexRequestView{ + Name: "Request.QueryParams.id", + } + bodyBytes, err := json.Marshal(journalIndexRequest) + Expect(err).To(BeNil()) + request, err := http.NewRequest("POST", "/api/v2/journal/index", ioutil.NopCloser(bytes.NewBuffer(bodyBytes))) + + Expect(err).To(BeNil()) + response := makeRequestOnHandler(unit.Post, request) + Expect(response.Code).To(Equal(http.StatusOK)) +} + +func Test_JournalIndexHandler_DeleteJournalIndex(t *testing.T) { + RegisterTestingT(t) + stubHoverfly := HoverflyJournalIndexStub{} + unit := HoverflyJournalIndexHandler{Hoverfly: stubHoverfly} + request, err := http.NewRequest("DELETE", "/api/v2/journal/index/Request.QueryParams.id", nil) + + Expect(err).To(BeNil()) + response := makeRequestOnHandler(unit.Delete, request) + Expect(response.Code).To(Equal(http.StatusOK)) +} diff --git a/core/hoverfly_service_test.go b/core/hoverfly_service_test.go index a30a36fcc..0b4789e15 100644 --- a/core/hoverfly_service_test.go +++ b/core/hoverfly_service_test.go @@ -1481,3 +1481,38 @@ func TestHoverfly_GetTemplateDataSources(t *testing.T) { Expect(templateDataSourceView.DataSources[0].Name).To(Equal("test-csv1")) Expect(templateDataSourceView.DataSources[0].Data).To(Equal(content)) } + +func TestHoverfly_AddAndGetJournalIndex(t *testing.T) { + RegisterTestingT(t) + + unit := NewHoverflyWithConfiguration(&Configuration{}) + indexName1 := "Request.QueryParam.id" + indexName2 := "Request.Body 'jsonpath' '$.id'" + addIndexes(unit, indexName1, indexName2) + journalIndexes := unit.Journal.GetAllIndexes() + Expect(journalIndexes).ToNot(BeNil()) + Expect(journalIndexes).To(HaveLen(2)) + Expect(journalIndexes[0].Name).To(Equal(indexName1)) + Expect(journalIndexes[1].Name).To(Equal(indexName2)) +} + +func TestHoverfly_DeleteJournalIndex(t *testing.T) { + RegisterTestingT(t) + unit := NewHoverflyWithConfiguration(&Configuration{}) + indexName1 := "Request.QueryParam.id" + indexName2 := "Request.Body 'jsonpath' '$.id'" + addIndexes(unit, indexName1, indexName2) + unit.Journal.DeleteIndex(indexName1) + journalIndexes := unit.Journal.GetAllIndexes() + Expect(journalIndexes).ToNot(BeNil()) + Expect(journalIndexes).To(HaveLen(1)) + Expect(journalIndexes[0].Name).To(Equal(indexName2)) +} + +func addIndexes(unit *Hoverfly, indexName1, indexName2 string) (string, string) { + err1 := unit.Journal.AddIndex(indexName1) + err2 := unit.Journal.AddIndex(indexName2) + Expect(err1).To(BeNil()) + Expect(err2).To(BeNil()) + return indexName1, indexName2 +} diff --git a/core/journal/journal.go b/core/journal/journal.go index 2d0d94e5a..1eacf2c50 100644 --- a/core/journal/journal.go +++ b/core/journal/journal.go @@ -68,7 +68,7 @@ func (this *Journal) AddIndex(indexKey string) error { func (this *Journal) DeleteIndex(indexKey string) { - var indexes []Index + indexes := []Index{} for _, index := range this.Indexes { if index.Name != indexKey { indexes = append(indexes, index) diff --git a/core/journal/journal_index_test.go b/core/journal/journal_index_test.go new file mode 100644 index 000000000..f09c4db84 --- /dev/null +++ b/core/journal/journal_index_test.go @@ -0,0 +1,124 @@ +package journal + +import ( + "github.com/SpectoLabs/hoverfly/core/models" + "github.com/SpectoLabs/hoverfly/core/util" + . "github.com/onsi/gomega" + "testing" +) + +func TestIndex_AddJournalEntry(t *testing.T) { + RegisterTestingT(t) + + index := Index{ + Name: "Request.QueryParam.id", + Entries: make(map[string]*JournalEntry), + } + + queryParams := make(map[string][]string) + queryParams["id"] = []string{"0100"} + journalEntry := JournalEntry{ + Id: "12345", + Request: &models.RequestDetails{ + Query: queryParams, + }, + } + index.AddJournalEntry(&journalEntry) + Expect(index.Entries).ToNot(BeNil()) + Expect(index.Entries).To(HaveLen(1)) + Expect(index.Entries["0100"]).To(Equal(&journalEntry)) +} + +func TestIndex_AddJournalEntryWithJsonRequestBody(t *testing.T) { + RegisterTestingT(t) + + index := Index{ + Name: "Request.Body 'jsonpath' '$.name'", + Entries: make(map[string]*JournalEntry), + } + + journalEntry := JournalEntry{ + Id: "12345", + Request: &models.RequestDetails{ + Body: "{\"name\":\"Application Testing\"}", + }, + } + index.AddJournalEntry(&journalEntry) + Expect(index.Entries).ToNot(BeNil()) + Expect(index.Entries).To(HaveLen(1)) + Expect(index.Entries["Application Testing"]).To(Equal(&journalEntry)) +} + +func TestIndex_AddJournalEntryWithXMLRequestBody(t *testing.T) { + RegisterTestingT(t) + + index := Index{ + Name: "Request.Body 'xpath' '/document/id'", + Entries: make(map[string]*JournalEntry), + } + + journalEntry := JournalEntry{ + Id: "12345", + Request: &models.RequestDetails{ + Body: "1234Test", + }, + } + index.AddJournalEntry(&journalEntry) + Expect(index.Entries).ToNot(BeNil()) + Expect(index.Entries).To(HaveLen(1)) + Expect(index.Entries["1234"]).To(Equal(&journalEntry)) +} + +func TestIndex_ConvertToIndexView(t *testing.T) { + RegisterTestingT(t) + + journalEntry1 := JournalEntry{ + Id: "1", + } + journalEntry2 := JournalEntry{ + Id: "2", + } + + entries := make(map[string]*JournalEntry) + entries["a"] = &journalEntry1 + entries["b"] = &journalEntry2 + index := Index{ + Name: "Request.QueryParam.id", + Entries: entries, + } + indexView := index.getIndexView() + Expect(indexView).ToNot(BeNil()) + Expect(indexView.Name).To(Equal("Request.QueryParam.id")) + Expect(indexView.Entries).To(HaveLen(2)) + Expect(indexView.Entries[0].Key).To(Equal("a")) + Expect(indexView.Entries[0].JournalEntryId).To(Equal("1")) + Expect(indexView.Entries[1].Key).To(Equal("b")) + Expect(indexView.Entries[1].JournalEntryId).To(Equal("2")) +} + +func TestIndex_FilteredConversionIndexView(t *testing.T) { + RegisterTestingT(t) + journalEntry1 := JournalEntry{ + Id: "1", + } + journalEntry2 := JournalEntry{ + Id: "2", + } + + entries := make(map[string]*JournalEntry) + entries["a"] = &journalEntry1 + entries["b"] = &journalEntry2 + index := Index{ + Name: "Request.QueryParam.id", + Entries: entries, + } + selectedJournalEntryIds := util.NewHashSet() + selectedJournalEntryIds.Add("2") + indexView := index.convertIndex(selectedJournalEntryIds) + Expect(indexView).ToNot(BeNil()) + Expect(indexView.Name).To(Equal("Request.QueryParam.id")) + Expect(indexView.Entries).To(HaveLen(1)) + Expect(indexView.Entries[0].Key).To(Equal("b")) + Expect(indexView.Entries[0].JournalEntryId).To(Equal("2")) + +} diff --git a/core/journal/journal_test.go b/core/journal/journal_test.go index c9907ce0a..f8e357838 100644 --- a/core/journal/journal_test.go +++ b/core/journal/journal_test.go @@ -24,10 +24,13 @@ func Test_NewJournal_ProducesAJournalWithAnEmptyArray(t *testing.T) { journalView, err := unit.GetEntries(0, 25, nil, nil, "") entries := journalView.Journal + indexes := journalView.Index Expect(err).To(BeNil()) Expect(entries).ToNot(BeNil()) Expect(entries).To(HaveLen(0)) + Expect(indexes).ToNot(BeNil()) + Expect(indexes).To(HaveLen(0)) Expect(unit.EntryLimit).To(Equal(1000)) } @@ -73,6 +76,104 @@ func Test_Journal_NewEntry_AddsJournalEntryToEntries(t *testing.T) { Expect(entries[0].Latency).To(BeNumerically("<", 1)) } +func Test_JournalIndex_NewEntryAfterAddingIndex_AddsJournalIndexEntryToIndexes(t *testing.T) { + RegisterTestingT(t) + + unit := journal.NewJournal() + + request, _ := http.NewRequest("GET", "http://hoverfly.io?id=1234", nil) + + nowTime := time.Now() + + indexName := "Request.QueryParam.id" + indexErr := unit.AddIndex(indexName) + Expect(indexErr).To(BeNil()) + + err := unit.NewEntry(request, &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString("test body")), + Header: http.Header{ + "test-header": []string{ + "one", "two", + }, + }, + }, "test-mode", nowTime) + + Expect(err).To(BeNil()) + indexes := unit.Indexes + Expect(indexes).ToNot(BeNil()) + Expect(indexes).To(HaveLen(1)) + Expect(indexes[0].Name).To(Equal(indexName)) + Expect(indexes[0].Entries).To(HaveKey("1234")) +} + +func Test_JournalIndex_NewEntryBeforeAddingIndex_AddsJournalIndexEntryToIndexes(t *testing.T) { + RegisterTestingT(t) + + unit := journal.NewJournal() + + request, _ := http.NewRequest("GET", "http://hoverfly.io?id=1234", nil) + + nowTime := time.Now() + + err := unit.NewEntry(request, &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString("test body")), + Header: http.Header{ + "test-header": []string{ + "one", "two", + }, + }, + }, "test-mode", nowTime) + + Expect(err).To(BeNil()) + + indexName := "Request.QueryParam.id" + indexErr := unit.AddIndex(indexName) + Expect(indexErr).To(BeNil()) + + indexes := unit.Indexes + Expect(indexes).ToNot(BeNil()) + Expect(indexes).To(HaveLen(1)) + Expect(indexes[0].Name).To(Equal(indexName)) + Expect(indexes[0].Entries).To(HaveKey("1234")) +} + +func Test_DeleteJournalIndex(t *testing.T) { + RegisterTestingT(t) + + unit := journal.NewJournal() + + indexName := "Request.QueryParam.id" + indexErr := unit.AddIndex(indexName) + Expect(indexErr).To(BeNil()) + + unit.DeleteIndex(indexName) + + indexes := unit.Indexes + Expect(indexes).ToNot(BeNil()) + Expect(indexes).To(HaveLen(0)) +} + +func Test_DeleteAndAddIndexBack(t *testing.T) { + RegisterTestingT(t) + + unit := journal.NewJournal() + + indexName := "Request.QueryParam.id" + indexErr := unit.AddIndex(indexName) + Expect(indexErr).To(BeNil()) + + unit.DeleteIndex(indexName) + indexErr = unit.AddIndex(indexName) + Expect(indexErr).To(BeNil()) + + indexes := unit.Indexes + Expect(indexes).ToNot(BeNil()) + Expect(indexes).To(HaveLen(1)) + Expect(indexes[0].Name).To(Equal(indexName)) +} + func Test_Journal_NewEntry_RespectsEntryLimit(t *testing.T) { RegisterTestingT(t)