Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik authored and tommysitu committed Mar 9, 2024
1 parent ec75263 commit 3b7f9c8
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 1 deletion.
73 changes: 73 additions & 0 deletions core/handlers/v2/hoverfly_journalindex_handler_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
35 changes: 35 additions & 0 deletions core/hoverfly_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion core/journal/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
124 changes: 124 additions & 0 deletions core/journal/journal_index_test.go
Original file line number Diff line number Diff line change
@@ -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: "<document><id>1234</id><name>Test</name></document>",
},
}
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"))

}
101 changes: 101 additions & 0 deletions core/journal/journal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 3b7f9c8

Please sign in to comment.